Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Mobile Google Calendar Odd/Even Week Imports #3684

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions website/src/utils/ical.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ test('iCalEventForLesson generates correct output', () => {
description: 'Personal Development & Career Management\nSectional Teaching Group A1',
location: 'BIZ1-0303',
repeating: {
interval: 1,
freq: 'WEEKLY',
count: 14,
byDay: ['Mo'],
Expand Down Expand Up @@ -291,6 +292,7 @@ test('work for half hour lesson offsets', () => {
description: 'Personal Development & Career Management\nSectional Teaching Group A1',
location: 'BIZ1-0303',
repeating: {
interval: 1,
freq: 'WEEKLY',
count: 14,
byDay: ['Mo'],
Expand Down
22 changes: 21 additions & 1 deletion website/src/utils/ical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ function calculateStartEnd(date: Date, startTime: StartTime, endTime: EndTime) {
return { start, end };
}

function calculateLargestInterval(weeks: NumericWeeks) {
let largestInterval = 1;
for (let i = 0; i < weeks.length - 1; i++) {
const diff = weeks[i + 1] - weeks[i];
if (diff > largestInterval) {
largestInterval = diff;
}
}
return largestInterval - 1;
}

export function calculateNumericWeek(
lesson: RawLesson,
_semester: Semester,
Expand All @@ -91,12 +102,21 @@ export function calculateNumericWeek(
const { start, end } = calculateStartEnd(lessonDay, lesson.startTime, lesson.endTime);
const excludedWeeks = _.difference([RECESS_WEEK, ...ALL_WEEKS], weeks);

// Sets interval to 2 for odd and even weeks. Fix for mobile GCal imports.
const isAlternate =
weeks.every((week) => ODD_WEEKS.includes(week)) ||
weeks.every((week) => EVEN_WEEKS.includes(week));
const interval = isAlternate ? 2 : 1;
const largestInterval = calculateLargestInterval(weeks);
const adjCount = largestInterval === interval ? weeks.length : NUM_WEEKS_IN_A_SEM;

return {
start,
end,
repeating: {
interval,
freq: 'WEEKLY',
count: NUM_WEEKS_IN_A_SEM,
count: adjCount,
byDay: [lesson.day.slice(0, 2)],
exclude: [
...excludedWeeks.map((week) => datesForAcademicWeeks(start, week)),
Expand Down