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

@@ -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(