Files
UT-Registration-Plus/src/pages/background/lib/duplicateSchedule.ts
Kabir Ramzan 94744e01b9 feat: use "copy of" for duplicated schedules and place them under the original schedule #358 (#397)
* feat: use "copy of" for duplicated schedules and place them under the original schedule #358

* fix: code style and consistency
2024-10-28 17:56:02 -05:00

35 lines
1.2 KiB
TypeScript

import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
import { generateRandomId } from '@shared/util/random';
import handleDuplicate from './handleDuplicate';
/**
* Creates a new schedule with the given name
* @param scheduleName the name of the schedule to create
* @returns undefined if successful, otherwise an error message
*/
export default async function duplicateSchedule(scheduleId: string): Promise<string | undefined> {
const schedules = await UserScheduleStore.get('schedules');
const scheduleIndex = schedules.findIndex(schedule => schedule.id === scheduleId);
if (scheduleIndex === -1) {
throw new Error(`Schedule ${scheduleId} does not exist`);
}
const schedule = schedules[scheduleIndex]!;
const copyOfName = `Copy of ${schedule.name}`;
const updatedName = await handleDuplicate(copyOfName);
schedules.splice(scheduleIndex + 1, 0, {
id: generateRandomId(),
name: updatedName,
courses: JSON.parse(JSON.stringify(schedule.courses)),
hours: schedule.hours,
updatedAt: Date.now(),
} satisfies typeof schedule);
await UserScheduleStore.set('schedules', schedules);
return undefined;
}