From 2f534e79a03a9e56c4e8124b4fa0b042988573ed Mon Sep 17 00:00:00 2001 From: Cheryl Chen Date: Mon, 22 Apr 2024 14:01:08 -0700 Subject: [PATCH] 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);