From 3dd913178c353da330d293e8367abe2761633606 Mon Sep 17 00:00:00 2001 From: Ethan Ho Date: Mon, 12 Feb 2024 10:42:57 -0800 Subject: [PATCH] Updated date calculations to be based on the event date and not current date --- common/utils.js | 5 +++-- routes/publishedSchedule.js | 25 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/common/utils.js b/common/utils.js index b645885..fb5f261 100644 --- a/common/utils.js +++ b/common/utils.js @@ -26,9 +26,10 @@ const isInteger = (value) => { }; // dependency for publishedSchedule.js -const calculateYear = (gradeLevel) => { +const calculateYear = (eventDate, gradeLevel) => { if (gradeLevel) { - const currentDay = new Date(); + const currentDay = new Date(eventDate); + // console.log('current day', currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 2 : 1)); if (gradeLevel.toLowerCase() === 'junior') { // if the current month is august or later // then junior will be current year + 2 diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index ce3fc7b..7f412f8 100644 --- a/routes/publishedSchedule.js +++ b/routes/publishedSchedule.js @@ -215,9 +215,13 @@ publishedScheduleRouter.get('/:id', async (req, res) => { }); // POST - Adds a new row to the published_schedule table +// 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 { eventId, dayId, confirmed, confirmedOn, startTime, endTime, cohort, notes } = req.body; try { + const dayResult = await db.query(`SELECT * FROM day WHERE id = $1;`, [dayId]); + const { eventDate } = dayResult ? keysToCamel(dayResult[0]) : null; const returnedData = await db.query( ` INSERT INTO @@ -235,7 +239,16 @@ publishedScheduleRouter.post('/', async (req, res) => { ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id; `, - [eventId, dayId, confirmed, confirmedOn, startTime, endTime, calculateYear(cohort), notes], + [ + eventId, + dayId, + confirmed, + confirmedOn, + startTime, + endTime, + calculateYear(eventDate, cohort), + notes, + ], ); res.status(201).json({ status: 'Success', @@ -247,10 +260,18 @@ publishedScheduleRouter.post('/', async (req, res) => { }); // PUT/:id - Updates an existing row given an id +// NOTE: there is a requirement that the selected DAY already exist; this is how +// we are able to grab the event day from the day table for use in the cohort publishedScheduleRouter.put('/:id', async (req, res) => { try { const { id } = req.params; const { eventId, dayId, confirmed, confirmedOn, startTime, endTime, cohort, notes } = req.body; + const psDayIdResult = await db.query(`SELECT day_id FROM published_schedule WHERE id = $1`, [ + id, + ]); + const psDayId = psDayIdResult ? keysToCamel(psDayIdResult[0]).dayId : null; + const dayResult = await db.query(`SELECT * FROM day WHERE id = $1;`, [dayId || psDayId]); + const { eventDate } = dayResult ? keysToCamel(dayResult[0]) : null; const updatedPublishedSchedule = await db.query( ` UPDATE published_schedule @@ -274,7 +295,7 @@ publishedScheduleRouter.put('/:id', async (req, res) => { confirmedOn, startTime, endTime, - calculateYear(cohort), + calculateYear(eventDate, cohort), notes, id, ],