implement preview for center
This commit is contained in:
@@ -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
|
||||
export type ScheduleConfigTypeForCenter = {
|
||||
startDate: string
|
||||
endDate: string
|
||||
slots: number[]
|
||||
days: number[]
|
||||
}
|
||||
| null
|
||||
| undefined
|
||||
|
||||
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(),
|
||||
|
||||
@@ -42,7 +42,6 @@ export class ScheduleService {
|
||||
scheduleConfig: ScheduleConfigType,
|
||||
): Promise<PreviewScheduleType> {
|
||||
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<PreviewScheduleType> {
|
||||
// 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<PreviewScheduleType> {
|
||||
const config: Config[] = await this.appConfigService.getVisibleConfigs()
|
||||
const slots = this.generateSlotsPreviewForCenter(scheduleConfig, config)
|
||||
return {
|
||||
totalSlots: slots.length,
|
||||
slots: slots,
|
||||
}
|
||||
}
|
||||
|
||||
async generateScheduleDates(schedule: Schedule): Promise<ScheduleDate[]> {
|
||||
// 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(
|
||||
|
||||
Reference in New Issue
Block a user