Skip to content

Commit

Permalink
Added calculations of grad year number given junior/senior standing, …
Browse files Browse the repository at this point in the history
…relative to current day
  • Loading branch information
h0ethan04 committed Feb 12, 2024
1 parent 54fb562 commit 84ce271
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
29 changes: 28 additions & 1 deletion common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -57,4 +84,4 @@ const keysToCamel = (data) => {
return data;
};

module.exports = { keysToCamel, isInteger };
module.exports = { keysToCamel, isInteger, calculateYear };
16 changes: 13 additions & 3 deletions routes/publishedSchedule.js
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 84ce271

Please sign in to comment.