Skip to content

Commit

Permalink
fixed userCount returning based on search
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheryl Chen authored and Cheryl Chen committed Apr 22, 2024
1 parent c8fe1a2 commit 2f534e7
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,26 @@ 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];
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,
);
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);
Expand Down

0 comments on commit 2f534e7

Please sign in to comment.