diff --git a/packages/nusmoderator/src/academicCalendar.test.ts b/packages/nusmoderator/src/academicCalendar.test.ts index 9d58af0c24..f22c89e255 100644 --- a/packages/nusmoderator/src/academicCalendar.test.ts +++ b/packages/nusmoderator/src/academicCalendar.test.ts @@ -247,6 +247,51 @@ describe('getAcadWeekInfo', () => { type: 'Orientation', num: null, }); + expect(getAcadWeekInfo(new Date('September 8, 2024'))).toEqual({ + year: '24/25', + sem: 'Semester 1', + type: 'Instructional', + num: 4, + }); + }); + + it('correctly handles week types whose days include the preceding weekend', () => { + expect(getAcadWeekInfo(new Date('September 21, 2024'))).toEqual({ + year: '24/25', + sem: 'Semester 1', + type: 'Recess', + num: null, + }); + expect(getAcadWeekInfo(new Date('November 16, 2024'))).toEqual({ + year: '24/25', + sem: 'Semester 1', + type: 'Reading', + num: null, + }); + expect(getAcadWeekInfo(new Date('November 23, 2024'))).toEqual({ + year: '24/25', + sem: 'Semester 1', + type: 'Examination', + num: 1, + }); + expect(getAcadWeekInfo(new Date('February 25, 2025'))).toEqual({ + year: '24/25', + sem: 'Semester 2', + type: 'Recess', + num: null, + }); + expect(getAcadWeekInfo(new Date('April 19, 2025'))).toEqual({ + year: '24/25', + sem: 'Semester 2', + type: 'Reading', + num: null, + }); + expect(getAcadWeekInfo(new Date('April 27, 2025'))).toEqual({ + year: '24/25', + sem: 'Semester 2', + type: 'Examination', + num: 1, + }); }); }); diff --git a/packages/nusmoderator/src/academicCalendar.ts b/packages/nusmoderator/src/academicCalendar.ts index ef77a27195..e3f5fbbd37 100644 --- a/packages/nusmoderator/src/academicCalendar.ts +++ b/packages/nusmoderator/src/academicCalendar.ts @@ -142,14 +142,22 @@ export function getAcadWeekInfo(date: Date): AcadWeekInfo { const acadYear = currentAcad.year; const acadYearStartDate = getAcadYearStartDate(acadYear); + // Computes week number of the academic year, assuming that each week + // starts on Monday and ends on Sunday. let acadWeekNumber = Math.ceil( (date.getTime() - acadYearStartDate.getTime() + 1) / oneWeekDuration, ); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const semester = getAcadSem(acadWeekNumber)!; + // Check if it is weekend + const dayOfWeek = date.getDay(); + const isWeekend = dayOfWeek === 0 || dayOfWeek === 6; + let weekType = null; let weekNumber = null; + switch (semester) { case sem2: // Semester 2 starts 22 weeks after Week 1 of semester 1 acadWeekNumber -= 22; @@ -164,6 +172,26 @@ export function getAcadWeekInfo(date: Date): AcadWeekInfo { break; } acadWeekNumber -= 1; + + // Handle special cases where some week types include the week's preceding weekend + if (isWeekend) { + if (acadWeekNumber === 6) { + weekType = 'Recess'; + weekNumber = null; + break; + } + if (acadWeekNumber === 14) { + weekType = 'Reading'; + weekNumber = null; + break; + } + if (acadWeekNumber === 15) { + weekType = 'Examination'; + weekNumber = 1; + break; + } + } + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const acadWeek = getAcadWeekName(acadWeekNumber)!; weekType = acadWeek.weekType;