From 84ce271780be38cc8f07e2fd0c7582a46318818f Mon Sep 17 00:00:00 2001 From: Ethan Ho Date: Sun, 11 Feb 2024 21:41:57 -0800 Subject: [PATCH] Added calculations of grad year number given junior/senior standing, relative to current day --- common/utils.js | 29 ++++++++++++++++++++++++++++- routes/publishedSchedule.js | 16 +++++++++++++--- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/common/utils.js b/common/utils.js index d9f6238..b645885 100644 --- a/common/utils.js +++ b/common/utils.js @@ -25,6 +25,33 @@ const isInteger = (value) => { return value && /^\d+$/.test(value); }; +// dependency for publishedSchedule.js +const calculateYear = (gradeLevel) => { + if (gradeLevel) { + const currentDay = new Date(); + if (gradeLevel.toLowerCase() === 'junior') { + // if the current month is august or later + // then junior will be current year + 2 + // otherwise junior will be current year + 1 + // months are zero indexed + return [(currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 2 : 1)).toString(10)]; + } + if (gradeLevel.toLowerCase() === 'senior') { + // if the current month is august or later + // then senior will be current year + 1 + // otherwise senior will be current year + return [(currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 1 : 0)).toString(10)]; + } + if (gradeLevel.toLowerCase() === 'both') { + return [ + (currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 1 : 0)).toString(10), + (currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 2 : 1)).toString(10), + ]; + } + } + return []; +}; + const isObject = (o) => { return o === Object(o) && !isArray(o) && typeof o !== 'function' && !isISODate(o); }; @@ -57,4 +84,4 @@ const keysToCamel = (data) => { return data; }; -module.exports = { keysToCamel, isInteger }; +module.exports = { keysToCamel, isInteger, calculateYear }; diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index f255c16..ce3fc7b 100644 --- a/routes/publishedSchedule.js +++ b/routes/publishedSchedule.js @@ -1,6 +1,6 @@ const express = require('express'); const { db } = require('../server/db'); -const { keysToCamel } = require('../common/utils'); +const { keysToCamel, calculateYear } = require('../common/utils'); const publishedScheduleRouter = express.Router(); @@ -235,7 +235,7 @@ publishedScheduleRouter.post('/', async (req, res) => { ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id; `, - [eventId, dayId, confirmed, confirmedOn, startTime, endTime, cohort, notes], + [eventId, dayId, confirmed, confirmedOn, startTime, endTime, calculateYear(cohort), notes], ); res.status(201).json({ status: 'Success', @@ -267,7 +267,17 @@ publishedScheduleRouter.put('/:id', async (req, res) => { RETURNING *; `, - [eventId, dayId, confirmed, confirmedOn, startTime, endTime, cohort, notes, id], + [ + eventId, + dayId, + confirmed, + confirmedOn, + startTime, + endTime, + calculateYear(cohort), + notes, + id, + ], ); res.status(200).json(keysToCamel(updatedPublishedSchedule)); } catch (err) {