Skip to content

Commit

Permalink
69 account page pagination approve decline multiple (#67)
Browse files Browse the repository at this point in the history
* added keyword search for approved-accounts route

* changed approved-acounts route for pagination

* added query in pending accounts for admin and student

* fixed userCount returning based on search

---------

Co-authored-by: Cheryl Chen <[email protected]>
Co-authored-by: chloecheng8 <[email protected]>
  • Loading branch information
3 people authored Apr 28, 2024
1 parent f22fbe1 commit 539084b
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions routes/users.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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);
Expand All @@ -28,10 +33,39 @@ 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, 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 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);
}
res.status(200).json(keysToCamel({ accounts: approvedAccounts, count: userCount }));
} catch (err) {
res.status(500).send(err.message);
}
Expand Down

0 comments on commit 539084b

Please sign in to comment.