From 41092c3e03f8dce890bef11c4344ca39e26e9874 Mon Sep 17 00:00:00 2001 From: Cheryl Chen Date: Mon, 15 Apr 2024 23:30:15 -0700 Subject: [PATCH 1/4] added keyword search for approved-accounts route --- routes/users.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/routes/users.js b/routes/users.js index 707a026..7c6429c 100644 --- a/routes/users.js +++ b/routes/users.js @@ -1,5 +1,5 @@ const express = require('express'); -const { keysToCamel } = require('../common/utils'); +const { keysToCamel, isInteger } = require('../common/utils'); const { db } = require('../server/db'); const userRouter = express.Router(); @@ -28,10 +28,23 @@ userRouter.get('/pending-accounts', async (req, res) => { userRouter.get('/approved-accounts', async (req, res) => { try { - const pendingAccounts = await db.query( - `SELECT * FROM users WHERE approved = TRUE ORDER BY first_name ASC;`, - ); - res.status(200).json(keysToCamel(pendingAccounts)); + const { keyword } = req.query; + let { page, limit } = req.query; + page = isInteger(page) ? parseInt(page, 10) : 1; + limit = isInteger(limit) ? parseInt(limit, 10) : 10; + const offset = (page - 1) * limit; + if (keyword) { + const userSearchResult = await db.query( + `SELECT * FROM users WHERE approved = TRUE AND (first_name ILIKE $1 OR last_name ILIKE $2 OR email ILIKE $3 OR CONCAT(first_name, ' ', last_name) ILIKE $4) ORDER BY first_name ASC LIMIT $5 OFFSET $6;`, + [`%${keyword}%`, `%${keyword}%`, `%${keyword}%`, `%${keyword}%`, limit, offset], + ); + res.status(200).json(keysToCamel(userSearchResult)); + } else { + const approvedAccounts = await db.query( + `SELECT * FROM users WHERE approved = TRUE ORDER BY first_name ASC;`, + ); + res.status(200).json(keysToCamel(approvedAccounts)); + } } catch (err) { res.status(500).send(err.message); } From d21f6338e438e6b054d88bc77702c0a0e000585d Mon Sep 17 00:00:00 2001 From: Cheryl Chen Date: Tue, 16 Apr 2024 23:41:51 -0700 Subject: [PATCH 2/4] changed approved-acounts route for pagination --- routes/users.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/routes/users.js b/routes/users.js index 7c6429c..6abf3dc 100644 --- a/routes/users.js +++ b/routes/users.js @@ -28,23 +28,34 @@ userRouter.get('/pending-accounts', async (req, res) => { userRouter.get('/approved-accounts', async (req, res) => { try { - const { keyword } = req.query; + const { keyword, accountType } = req.query; let { page, limit } = req.query; page = isInteger(page) ? parseInt(page, 10) : 1; limit = isInteger(limit) ? parseInt(limit, 10) : 10; const offset = (page - 1) * limit; + let queryString = 'FROM users WHERE approved = TRUE '; + if (accountType === 'admin') { + queryString += `AND type = 'admin'`; + } else if (accountType === 'student') { + queryString += `AND type = 'student'`; + } + let approvedAccounts; + let params = []; if (keyword) { - const userSearchResult = await db.query( - `SELECT * FROM users WHERE approved = TRUE AND (first_name ILIKE $1 OR last_name ILIKE $2 OR email ILIKE $3 OR CONCAT(first_name, ' ', last_name) ILIKE $4) ORDER BY first_name ASC LIMIT $5 OFFSET $6;`, - [`%${keyword}%`, `%${keyword}%`, `%${keyword}%`, `%${keyword}%`, limit, offset], + params = [`%${keyword}%`, `%${keyword}%`, `%${keyword}%`, `%${keyword}%`, limit, offset]; + approvedAccounts = await db.query( + `SELECT * ${queryString} AND (first_name ILIKE $1 OR last_name ILIKE $2 OR email ILIKE $3 OR CONCAT(first_name, ' ', last_name) ILIKE $4) ORDER BY first_name ASC LIMIT $5 OFFSET $6;`, + params, ); - res.status(200).json(keysToCamel(userSearchResult)); } else { - const approvedAccounts = await db.query( - `SELECT * FROM users WHERE approved = TRUE ORDER BY first_name ASC;`, + params = [limit, offset]; + approvedAccounts = await db.query( + `SELECT * ${queryString} ORDER BY first_name ASC LIMIT $1 OFFSET $2;`, + params, ); - res.status(200).json(keysToCamel(approvedAccounts)); } + const userCount = await db.query(`SELECT COUNT(*) ${queryString};`, params); + res.status(200).json(keysToCamel({ accounts: approvedAccounts, count: userCount })); } catch (err) { res.status(500).send(err.message); } From c8fe1a27cc6c344db2e8aa73273244aba0184755 Mon Sep 17 00:00:00 2001 From: chloecheng8 <30807247+chloecheng8@users.noreply.github.com> Date: Sat, 20 Apr 2024 17:39:56 -0700 Subject: [PATCH 3/4] added query in pending accounts for admin and student --- routes/users.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/routes/users.js b/routes/users.js index 6abf3dc..5abfc9c 100644 --- a/routes/users.js +++ b/routes/users.js @@ -17,9 +17,14 @@ userRouter.get('/', async (req, res) => { userRouter.get('/pending-accounts', async (req, res) => { try { - const pendingAccounts = await db.query( - `SELECT * FROM users WHERE approved = FALSE ORDER BY first_name ASC;`, - ); + const { accountType } = req.query; + let queryString = 'SELECT * FROM users WHERE approved = FALSE '; + if (accountType === 'admin') { + queryString += `AND type = 'admin'`; + } else if (accountType === 'student') { + queryString += `AND type = 'student'`; + } + const pendingAccounts = await db.query(`${queryString} ORDER BY first_name ASC;`); res.status(200).json(keysToCamel(pendingAccounts)); } catch (err) { res.status(500).send(err.message); From 2f534e79a03a9e56c4e8124b4fa0b042988573ed Mon Sep 17 00:00:00 2001 From: Cheryl Chen Date: Mon, 22 Apr 2024 14:01:08 -0700 Subject: [PATCH 4/4] fixed userCount returning based on search --- routes/users.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/routes/users.js b/routes/users.js index 5abfc9c..e72e1f6 100644 --- a/routes/users.js +++ b/routes/users.js @@ -45,6 +45,7 @@ userRouter.get('/approved-accounts', async (req, res) => { queryString += `AND type = 'student'`; } let approvedAccounts; + let userCount; let params = []; if (keyword) { params = [`%${keyword}%`, `%${keyword}%`, `%${keyword}%`, `%${keyword}%`, limit, offset]; @@ -52,14 +53,18 @@ userRouter.get('/approved-accounts', async (req, res) => { `SELECT * ${queryString} AND (first_name ILIKE $1 OR last_name ILIKE $2 OR email ILIKE $3 OR CONCAT(first_name, ' ', last_name) ILIKE $4) ORDER BY first_name ASC LIMIT $5 OFFSET $6;`, params, ); + userCount = await db.query( + `SELECT COUNT(*) ${queryString} AND (first_name ILIKE $1 OR last_name ILIKE $2 OR email ILIKE $3 OR CONCAT(first_name, ' ', last_name) ILIKE $4);`, + params, + ); } else { params = [limit, offset]; approvedAccounts = await db.query( `SELECT * ${queryString} ORDER BY first_name ASC LIMIT $1 OFFSET $2;`, params, ); + userCount = await db.query(`SELECT COUNT(*) ${queryString};`, params); } - const userCount = await db.query(`SELECT COUNT(*) ${queryString};`, params); res.status(200).json(keysToCamel({ accounts: approvedAccounts, count: userCount })); } catch (err) { res.status(500).send(err.message);