From 2652c4e4a9a158460b921d773eafc059502621da Mon Sep 17 00:00:00 2001 From: Sean Fong Date: Tue, 20 Feb 2024 15:38:33 -0800 Subject: [PATCH] Complete Notification Page Functionality (Backend) (#53) * completed table, post, and put changes for users table * changed and added published sched routes * recently-added returns events with equivalent confirm and create dates * Fix swapped conditions for confirmed and added events * Revert previous condition change * minor fixes post-merge --------- Co-authored-by: Alyssia Tan Co-authored-by: ThatMegamind <92563733+ThatMegamind@users.noreply.github.com> Co-authored-by: ThatMegamind --- routes/publishedSchedule.js | 91 +++++++++++++++++++++++++--- routes/users.js | 18 +++--- server/schema/published_schedule.sql | 1 + server/schema/users.sql | 3 +- 4 files changed, 92 insertions(+), 21 deletions(-) diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index 06821be..0008790 100644 --- a/routes/publishedSchedule.js +++ b/routes/publishedSchedule.js @@ -19,7 +19,8 @@ publishedScheduleRouter.get('/', async (req, res) => { PS.start_time, PS.end_time, PS.cohort, - PS.notes + PS.notes, + PS.created_on FROM published_schedule PS LEFT JOIN catalog C ON PS.event_id = C.id; @@ -31,6 +32,64 @@ publishedScheduleRouter.get('/', async (req, res) => { } }); +// GET/published-schedule/recently-added - returns the rows that were added in the past week +publishedScheduleRouter.get('/recently-added', async (req, res) => { + try { + const recentAddResult = await db.query( + ` + SELECT + PS.id, + C.title, + C.event_type, + C.year, + PS.start_time, + PS.end_time, + PS.confirmed, + PS.confirmed_on, + PS.cohort, + PS.notes, + PS.created_on + FROM published_schedule PS + LEFT JOIN catalog C ON PS.event_id = C.id + WHERE PS.created_on = PS.confirmed_on AND PS.created_on > current_date - 7 AND confirmed = true + ORDER BY created_on DESC; + `, + ); + res.status(200).json(keysToCamel(recentAddResult)); + } catch (err) { + res.status(400).send(err.message); + } +}); + +// GET/published-schedule/recently-confirmed - returns the rows that were confirmed in the past week +publishedScheduleRouter.get('/recently-confirmed', async (req, res) => { + try { + const recentConfirm = await db.query( + ` + SELECT + PS.id, + C.title, + C.event_type, + C.year, + PS.start_time, + PS.end_time, + PS.confirmed, + PS.confirmed_on, + PS.cohort, + PS.notes, + PS.created_on + FROM published_schedule PS + LEFT JOIN catalog C ON PS.event_id = C.id + WHERE PS.confirmed_on > current_date - 7 + ORDER BY created_on DESC; + `, + ); + res.status(200).json(keysToCamel(recentConfirm)); + } catch (err) { + res.status(400).send(err.message); + } +}); + // GET /published-schedule/all-seasons - return all the seasons publishedScheduleRouter.get('/all-seasons', async (req, res) => { const getSeason = (date) => { @@ -66,10 +125,10 @@ publishedScheduleRouter.get('/all-seasons', async (req, res) => { try { const allDatesResult = await db.query( ` - SELECT D.event_date + SELECT D.event_date FROM published_schedule AS PS, day AS D - WHERE + WHERE D.id = PS.day_id `, ); @@ -131,7 +190,8 @@ publishedScheduleRouter.get('/season', async (req, res) => { PS.confirmed, PS.confirmed_on, PS.cohort, - PS.notes + PS.notes, + PS.created_on FROM published_schedule PS LEFT JOIN catalog C ON PS.event_id = C.id LEFT JOIN day D on PS.day_id = D.id @@ -235,7 +295,8 @@ publishedScheduleRouter.get('/:id', async (req, res) => { PS.start_time, PS.end_time, PS.cohort, - PS.notes + PS.notes, + PS.created_on FROM published_schedule PS LEFT JOIN catalog C ON PS.event_id = C.id @@ -253,6 +314,7 @@ publishedScheduleRouter.get('/:id', async (req, res) => { // NOTE: there is a requirement that the day already exist, // as that is how we are able to calculate the cohort from the event date publishedScheduleRouter.post('/', async (req, res) => { + const currDate = new Date(); const { eventId, dayId, confirmed, confirmedOn, startTime, endTime, cohort, notes } = req.body; try { const dayResult = await db.query( @@ -281,11 +343,12 @@ publishedScheduleRouter.post('/', async (req, res) => { start_time, end_time, cohort, - notes + notes, + created_on ) VALUES - ($1, $2, $3, $4, $5, $6, $7, $8) - RETURNING id; + ($1, $2, $3, $4, $5, $6, $7, $8, $9) + RETURNING id, created_on; `, [ eventId, @@ -296,6 +359,7 @@ publishedScheduleRouter.post('/', async (req, res) => { endTime, calculateYear(eventDate, cohort), notes, + currDate, ], ); res.status(201).json({ @@ -315,7 +379,10 @@ publishedScheduleRouter.post('/', async (req, res) => { publishedScheduleRouter.put('/:id', async (req, res) => { try { const { id } = req.params; - const { eventId, dayId, confirmed, confirmedOn, startTime, endTime, cohort, notes } = req.body; + + const { eventId, dayId, confirmed, confirmedOn, startTime, endTime, cohort, notes, createdOn } = + req.body; + // get the current day from the PS table const psDayIdResult = keysToCamel( await db.query(`SELECT day_id FROM published_schedule WHERE id = $1;`, id), @@ -329,6 +396,7 @@ publishedScheduleRouter.put('/:id', async (req, res) => { // grab the eventDate so that you can set the years const { eventDate } = dayResult; // update the PS + const updatedPublishedSchedule = await db.query( ` UPDATE published_schedule @@ -341,7 +409,9 @@ publishedScheduleRouter.put('/:id', async (req, res) => { end_time = COALESCE($6, end_time), cohort = COALESCE($7, cohort), notes = COALESCE($8, notes) - WHERE id = $9 + created_on = COALESCE($9, created_on) + + WHERE id = $10 RETURNING *; `, @@ -354,6 +424,7 @@ publishedScheduleRouter.put('/:id', async (req, res) => { endTime, cohort ? calculateYear(eventDate, cohort) : cohort, notes, + createdOn, id, ], ); diff --git a/routes/users.js b/routes/users.js index 6f26153..9f2b5cd 100644 --- a/routes/users.js +++ b/routes/users.js @@ -37,18 +37,15 @@ userRouter.get('/:uid', async (req, res) => { userRouter.post('/create', async (req, res) => { try { - const { id, email, type, approved } = req.body; - await db.query(`INSERT INTO users (id, email, "type", approved) VALUES ($1, $2, $3, $4);`, [ - id, - email, - type, - approved, - ]); + const { id, email, type, approved, approvedOn } = req.body; + await db.query( + `INSERT INTO users (id, email, "type", approved, approved_on) VALUES ($1, $2, $3, $4, $5);`, + [id, email, type, approved, approvedOn], + ); res.status(201).json({ id, }); } catch (err) { - console.log('err', err); res.status(500).json({ status: 'Failed', msg: err.message, @@ -59,9 +56,10 @@ userRouter.post('/create', async (req, res) => { userRouter.put('/approve/:uid', async (req, res) => { try { const { uid } = req.params; + const currDate = new Date(); const updatedApproval = await db.query( - `UPDATE users SET approved = TRUE WHERE id = $1 RETURNING *;`, - [uid], + `UPDATE users SET approved = TRUE, approved_on = $1 WHERE id = $2 RETURNING *;`, + [currDate, uid], ); return res.status(200).send(keysToCamel(updatedApproval)); } catch (err) { diff --git a/server/schema/published_schedule.sql b/server/schema/published_schedule.sql index 9d6de5c..90e6fe7 100644 --- a/server/schema/published_schedule.sql +++ b/server/schema/published_schedule.sql @@ -9,6 +9,7 @@ CREATE TABLE IF NOT EXISTS published_schedule ( end_time time NOT NULL, cohort varchar[] NOT NULL, notes varchar(100), + created_on date NOT NULL, FOREIGN KEY (event_id) REFERENCES catalog (id), FOREIGN KEY (day_id) diff --git a/server/schema/users.sql b/server/schema/users.sql index b652586..785246f 100644 --- a/server/schema/users.sql +++ b/server/schema/users.sql @@ -6,5 +6,6 @@ CREATE TABLE users ( id VARCHAR ( 256 ) PRIMARY KEY, email VARCHAR ( 50 ) NOT NULL, type account_type NOT NULL, - approved BOOLEAN NOT NULL + approved BOOLEAN NOT NULL, + approved_on DATE NOT NULL, ); \ No newline at end of file