diff --git a/src/Schedule/schedule.schema.ts b/src/Schedule/schedule.schema.ts index 2841592..86d61fb 100644 --- a/src/Schedule/schedule.schema.ts +++ b/src/Schedule/schedule.schema.ts @@ -24,15 +24,12 @@ export type ScheduleConfigType = | null | undefined -export type ScheduleConfigTypeForCenter = - | { - startDate?: string | null | undefined - endDate?: string | null | undefined - slots?: number[] | null | undefined - days?: number[] | null | undefined - } - | null - | undefined +export type ScheduleConfigTypeForCenter = { + startDate: string + endDate: string + slots: number[] + days: number[] +} export type ScheduleSlotType = { slot: string @@ -185,10 +182,18 @@ export class ScheduleSchema extends PothosSchema { scheduleConfigInputForCenter() { return this.builder.inputType('ScheduleConfigInputForCenter', { fields: (t) => ({ - startDate: t.string(), - endDate: t.string(), - slots: t.intList(), - days: t.intList(), + startDate: t.string({ + required: true, + }), + endDate: t.string({ + required: true, + }), + slots: t.intList({ + required: true, + }), + days: t.intList({ + required: true, + }), }), }) } @@ -224,20 +229,21 @@ export class ScheduleSchema extends PothosSchema { }, }), - // centerPreviewSchedule: t.field({ - // type: this.previewSchedule(), - // description: 'Preview a schedule for center mentor.', - // args: { - // scheduleConfig: t.arg({ - // type: this.scheduleConfigInputForCenter(), - // }), - // }, - // resolve: async (_parent, args, _context, _info) => { - // return await this.scheduleService.createSchedulePreviewForCenter( - // args.scheduleConfig, - // ) - // }, - // }), + centerPreviewSchedule: t.field({ + type: this.previewSchedule(), + description: 'Preview a schedule for center mentor.', + args: { + scheduleConfigInput: t.arg({ + type: this.scheduleConfigInputForCenter(), + required: true, + }), + }, + resolve: async (_parent, args, _context, _info) => { + return await this.scheduleService.createSchedulePreviewForCenter( + args.scheduleConfigInput, + ) + }, + }), adminPreviewSchedule: t.field({ type: this.previewSchedule(), diff --git a/src/Schedule/schedule.service.ts b/src/Schedule/schedule.service.ts index b296620..ebff78a 100644 --- a/src/Schedule/schedule.service.ts +++ b/src/Schedule/schedule.service.ts @@ -42,7 +42,6 @@ export class ScheduleService { scheduleConfig: ScheduleConfigType, ): Promise { const config: Config[] = await this.appConfigService.getVisibleConfigs() - Logger.log(config) // process scheduleConfig input by filling with default values from config const scheduleConfigFilled = this.processScheduleConfig( scheduleConfig, @@ -57,17 +56,17 @@ export class ScheduleService { } } - // async createSchedulePreviewForCenter( - // scheduleConfig: ScheduleConfigTypeForCenter, - // ): Promise { - // const config: Config[] = await this.appConfigService.getVisibleConfigs() - // Logger.log(config) - // // process scheduleConfig input by filling with default values from config - // const scheduleConfigFilled = this.processScheduleConfig( - // scheduleConfig, - // config, - // ) - // } + // create preview for center require scheduleConfigInput: { startDate: "2024-11-02T00:00:00.000Z", endDate: "2024-11-22T00:00:00.000Z", slots: [1, 3], days: [2, 5] } + async createSchedulePreviewForCenter( + scheduleConfig: ScheduleConfigTypeForCenter, + ): Promise { + const config: Config[] = await this.appConfigService.getVisibleConfigs() + const slots = this.generateSlotsPreviewForCenter(scheduleConfig, config) + return { + totalSlots: slots.length, + slots: slots, + } + } async generateScheduleDates(schedule: Schedule): Promise { // generate schedule dates based on data and config @@ -118,6 +117,59 @@ export class ScheduleService { return scheduleDatesCreated } + generateSlotsPreviewForCenter( + scheduleConfig: ScheduleConfigTypeForCenter, + config: Config[], + ): ScheduleSlotType[] { + const startDate = DateTime.fromISO(scheduleConfig.startDate) + const endDate = DateTime.fromISO(scheduleConfig.endDate) + const daysOfWeeks = scheduleConfig.days + + // Retrieve slot configuration values once + const slotDuration = + config.find((c) => c.key === 'SLOT_DURATION')?.value ?? '' + const slotBreakDuration = + config.find((c) => c.key === 'SLOT_BREAK_DURATION')?.value ?? '' + const slotStartTime = + config.find((c) => c.key === 'SLOT_START_TIME')?.value ?? '' + const slotEndTime = + config.find((c) => c.key === 'SLOT_END_TIME')?.value ?? '' + + // Calculate the number of slots based on configuration + const numberOfSlots = this.calculateNumberOfSlots( + slotStartTime, + slotEndTime, + slotDuration, + slotBreakDuration, + ) + + const slots: ScheduleSlotType[] = [] + + // Loop through each day between start and end dates + for (let date = startDate; date <= endDate; date = date.plus({ days: 1 })) { + if (daysOfWeeks.includes(date.weekday)) { + Logger.log(`Generating slots for date: ${date.toISO()}`) + // For each slot number, calculate start and end times + for (let i = 1; i <= numberOfSlots; i++) { + const { startTime, endTime } = this.getSlotStartAndEndTime( + i, + slotDuration, + slotBreakDuration, + slotStartTime, + ) + + slots.push({ + slot: i.toString(), + start: startTime.toISO() ?? '', + end: endTime.toISO() ?? '', + }) + } + } + } + + return slots + } + generateSlots(scheduleConfigFilled: ScheduleConfigType): ScheduleSlotType[] { const slots: ScheduleSlotType[] = [] const numberOfSlots = this.calculateNumberOfSlots(