Skip to content

Commit

Permalink
fix(KUI-1176): calculate initiallySelectedSemester based on spec
Browse files Browse the repository at this point in the history
  • Loading branch information
belanglos committed Jun 4, 2024
1 parent 0aa8c1f commit ea6e541
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 31 deletions.
46 changes: 37 additions & 9 deletions server/controllers/__tests__/courseCtrlHelpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ describe('courseCtrlHelpers', () => {
})

test.each([
['2022-09-04', 20231],
['2023-03-04', 20232],
['2022-10-20', 20231],
['2023-04-22', 20232],
['2023-12-30', 20241],
['2024-07-04', 20242],
])('should match the date %s to the active semester %s', (currentDate, expectedTerm) => {
Expand Down Expand Up @@ -129,26 +129,54 @@ describe('courseCtrlHelpers', () => {
})

describe('generateSemesterBasedOnDate', () => {
test('January through February result in period 1 of the same year', () => {
test('January through March result in period 1 of the same year', () => {
const expectedSemester = '20241'
expect(generateSelectedSemesterBasedOnDate(new Date('2024-01-01'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-02-29'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-03-01'))).toBe(expectedSemester)
})

test('March through August result in period 2 of the same year', () => {
test('April 1st through April 19th result in period 1 of the same year', () => {
const expectedSemester = '20241'
expect(generateSelectedSemesterBasedOnDate(new Date('2024-04-01'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-04-10'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-04-19'))).toBe(expectedSemester)
})

test('April 20th through April 30th result in period 2 of the same year', () => {
const expectedSemester = '20242'
expect(generateSelectedSemesterBasedOnDate(new Date('2024-03-01'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-04-29'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-04-20'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-04-25'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-04-30'))).toBe(expectedSemester)
})

test('May through September result in period 2 of the same year', () => {
const expectedSemester = '20242'
expect(generateSelectedSemesterBasedOnDate(new Date('2024-05-01'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-05-29'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-06-29'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-07-29'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-08-29'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-09-01'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-09-30'))).toBe(expectedSemester)
})

test('September through december result in period 1 of the next year', () => {
const expectedSemester = '20251'
expect(generateSelectedSemesterBasedOnDate(new Date('2024-09-01'))).toBe(expectedSemester)
test('October 1st through October 19th result in period 1 of the same year', () => {
const expectedSemester = '20242'
expect(generateSelectedSemesterBasedOnDate(new Date('2024-10-01'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-10-10'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-10-19'))).toBe(expectedSemester)
})

test('October 20th through October 30th result in period 1 of the next year', () => {
const expectedSemester = '20251'
expect(generateSelectedSemesterBasedOnDate(new Date('2024-10-20'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-10-25'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-10-30'))).toBe(expectedSemester)
})

test('November through December result in period 1 of the next year', () => {
const expectedSemester = '20251'
expect(generateSelectedSemesterBasedOnDate(new Date('2024-11-01'))).toBe(expectedSemester)
expect(generateSelectedSemesterBasedOnDate(new Date('2024-12-01'))).toBe(expectedSemester)
})
Expand Down
44 changes: 22 additions & 22 deletions server/controllers/courseCtrlHelpers.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
const { isBefore } = require('date-fns')
const { INFORM_IF_IMPORTANT_INFO_IS_MISSING } = require('../util/constants')
const { convertYearSemesterNumberIntoSemester } = require('../util/semesterUtils')
const { convertYearSemesterNumberIntoSemester, SEMESTER_NUMBER } = require('../util/semesterUtils')

const DATE_OF_MONTH_ON_WHICH_TO_SWITCH_TO_NEXT_SEMESTER = 20
const SPRING_BREAK_MONTH = 4
const AUTUMN_BREAK_MONTH = 10

const createSpringBreakDateForYear = year =>
new Date(`${year}-${SPRING_BREAK_MONTH}-${DATE_OF_MONTH_ON_WHICH_TO_SWITCH_TO_NEXT_SEMESTER}`)
const createAutumnBreakDateForYear = year =>
new Date(`${year}-${AUTUMN_BREAK_MONTH}-${DATE_OF_MONTH_ON_WHICH_TO_SWITCH_TO_NEXT_SEMESTER}`)

/**
* Generates a semester-string based on the current year and month.
* E.g. if its May 2024, the semester string would be `20242`
*
*/
function generateSelectedSemesterBasedOnDate(thisDate) {
const currentYear = thisDate.getFullYear()
switch (thisDate.getMonth()) {
case 0: // January
case 1: // February
return `${currentYear}1`
case 2: // March
case 3:
case 4:
case 5:
case 6:
case 7: // August
return `${currentYear}2`
case 8: // September
case 9:
case 10:
case 11: // December
return `${currentYear + 1}1`
default:
return ''
}
function generateSelectedSemesterBasedOnDate(date) {
const year = date.getFullYear()

const springBreakDate = createSpringBreakDateForYear(year)
const autumnBreakDate = createAutumnBreakDateForYear(year)

if (isBefore(date, springBreakDate)) return `${year}${SEMESTER_NUMBER.SPRING}`

if (isBefore(date, autumnBreakDate)) return `${year}${SEMESTER_NUMBER.AUTUMN}`

return `${year + 1}${SEMESTER_NUMBER.SPRING}`
}

const semesterExistsInArray = (needle, haystack) => haystack.some(({ semester }) => semester === needle)
Expand Down
6 changes: 6 additions & 0 deletions server/util/semesterUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
*
*/

const SEMESTER_NUMBER = {
SPRING: 1,
AUTUMN: 2,
}

/**
* Takes a yearSemesterNumber and returns a yearSemesterNumber representing the semester previous to the given semester
*
Expand Down Expand Up @@ -86,4 +91,5 @@ module.exports = {
parseSemesterIntoYearSemesterNumber,
convertYearSemesterNumberIntoSemester,
convertToYearSemesterNumberOrGetCurrent,
SEMESTER_NUMBER,
}

0 comments on commit ea6e541

Please sign in to comment.