Skip to content

Commit

Permalink
nusmoderator: Include preceding weekend in specific week types (#3870)
Browse files Browse the repository at this point in the history
* modify calculation of week type

* move tests into group, add false positive test

* adjust comments

---------

Co-authored-by: Kok Rui Wong <[email protected]>
  • Loading branch information
kaze-droid and kokrui authored Nov 25, 2024
1 parent d481855 commit 3c2418f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/nusmoderator/src/academicCalendar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
});

Expand Down
28 changes: 28 additions & 0 deletions packages/nusmoderator/src/academicCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 3c2418f

Please sign in to comment.