implement preview for center

This commit is contained in:
2024-11-01 21:10:54 +07:00
parent db9f3213fd
commit e86d979ddb
2 changed files with 97 additions and 39 deletions

View File

@@ -24,15 +24,12 @@ export type ScheduleConfigType =
| null | null
| undefined | undefined
export type ScheduleConfigTypeForCenter = export type ScheduleConfigTypeForCenter = {
| { startDate: string
startDate?: string | null | undefined endDate: string
endDate?: string | null | undefined slots: number[]
slots?: number[] | null | undefined days: number[]
days?: number[] | null | undefined }
}
| null
| undefined
export type ScheduleSlotType = { export type ScheduleSlotType = {
slot: string slot: string
@@ -185,10 +182,18 @@ export class ScheduleSchema extends PothosSchema {
scheduleConfigInputForCenter() { scheduleConfigInputForCenter() {
return this.builder.inputType('ScheduleConfigInputForCenter', { return this.builder.inputType('ScheduleConfigInputForCenter', {
fields: (t) => ({ fields: (t) => ({
startDate: t.string(), startDate: t.string({
endDate: t.string(), required: true,
slots: t.intList(), }),
days: t.intList(), 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({ centerPreviewSchedule: t.field({
// type: this.previewSchedule(), type: this.previewSchedule(),
// description: 'Preview a schedule for center mentor.', description: 'Preview a schedule for center mentor.',
// args: { args: {
// scheduleConfig: t.arg({ scheduleConfigInput: t.arg({
// type: this.scheduleConfigInputForCenter(), type: this.scheduleConfigInputForCenter(),
// }), required: true,
// }, }),
// resolve: async (_parent, args, _context, _info) => { },
// return await this.scheduleService.createSchedulePreviewForCenter( resolve: async (_parent, args, _context, _info) => {
// args.scheduleConfig, return await this.scheduleService.createSchedulePreviewForCenter(
// ) args.scheduleConfigInput,
// }, )
// }), },
}),
adminPreviewSchedule: t.field({ adminPreviewSchedule: t.field({
type: this.previewSchedule(), type: this.previewSchedule(),

View File

@@ -42,7 +42,6 @@ export class ScheduleService {
scheduleConfig: ScheduleConfigType, scheduleConfig: ScheduleConfigType,
): Promise<PreviewScheduleType> { ): Promise<PreviewScheduleType> {
const config: Config[] = await this.appConfigService.getVisibleConfigs() const config: Config[] = await this.appConfigService.getVisibleConfigs()
Logger.log(config)
// process scheduleConfig input by filling with default values from config // process scheduleConfig input by filling with default values from config
const scheduleConfigFilled = this.processScheduleConfig( const scheduleConfigFilled = this.processScheduleConfig(
scheduleConfig, scheduleConfig,
@@ -57,17 +56,17 @@ export class ScheduleService {
} }
} }
// async createSchedulePreviewForCenter( // 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] }
// scheduleConfig: ScheduleConfigTypeForCenter, async createSchedulePreviewForCenter(
// ): Promise<PreviewScheduleType> { scheduleConfig: ScheduleConfigTypeForCenter,
// const config: Config[] = await this.appConfigService.getVisibleConfigs() ): Promise<PreviewScheduleType> {
// Logger.log(config) const config: Config[] = await this.appConfigService.getVisibleConfigs()
// // process scheduleConfig input by filling with default values from config const slots = this.generateSlotsPreviewForCenter(scheduleConfig, config)
// const scheduleConfigFilled = this.processScheduleConfig( return {
// scheduleConfig, totalSlots: slots.length,
// config, slots: slots,
// ) }
// } }
async generateScheduleDates(schedule: Schedule): Promise<ScheduleDate[]> { async generateScheduleDates(schedule: Schedule): Promise<ScheduleDate[]> {
// generate schedule dates based on data and config // generate schedule dates based on data and config
@@ -118,6 +117,59 @@ export class ScheduleService {
return scheduleDatesCreated 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[] { generateSlots(scheduleConfigFilled: ScheduleConfigType): ScheduleSlotType[] {
const slots: ScheduleSlotType[] = [] const slots: ScheduleSlotType[] = []
const numberOfSlots = this.calculateNumberOfSlots( const numberOfSlots = this.calculateNumberOfSlots(