This commit is contained in:
2024-11-01 20:02:43 +07:00
parent ec77f07de1
commit db9f3213fd
5 changed files with 1039 additions and 975 deletions

View File

@@ -10,18 +10,27 @@ import {
ScheduleConfigTypeForCenter,
ScheduleSlotType,
} from './schedule.schema'
import { Config } from '@prisma/client'
import { Config, Schedule, ScheduleDate } from '@prisma/client'
import { DateTime, Settings, Zone } from 'luxon'
import * as _ from 'lodash'
Settings.defaultLocale = 'en-US'
Settings.defaultZone = 'utc'
// Settings.defaultWeekSettings = {
// firstDay: 2,
// minimalDays: 1,
// weekend: [6, 7],
// }
Settings.defaultWeekSettings = {
firstDay: 2,
minimalDays: 1,
weekend: [6, 7],
}
interface ScheduleDateInput {
scheduleId: string
start: string
end: string
dayOfWeek: number
slot: number
serviceId: string
orderId: string | null
}
@Injectable()
export class ScheduleService {
constructor(
@@ -60,8 +69,56 @@ export class ScheduleService {
// )
// }
async generateScheduleDates(schedule: Schedule): Promise<ScheduleDate[]> {
// generate schedule dates based on data and config
const config: Config[] = await this.appConfigService.getVisibleConfigs()
const daysOfWeeks = schedule.daysOfWeek
const slots = schedule.slots
const scheduleStart = schedule.scheduleStart
const scheduleEnd = schedule.scheduleEnd
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 scheduleDates: ScheduleDateInput[] = []
// loop each day from scheduleStart to scheduleEnd
let date = DateTime.fromJSDate(scheduleStart)
while (date <= DateTime.fromJSDate(scheduleEnd)) {
// Check if the current date matches one of the specified days of the week
if (daysOfWeeks.includes(date.weekday)) {
// loop through slots
for (const slot of slots) {
const { startTime, endTime } = this.getSlotStartAndEndTime(
slot,
slotDuration ?? '',
slotBreakDuration ?? '',
slotStartTime ?? '',
)
scheduleDates.push({
scheduleId: schedule.id,
start: startTime.toISO() ?? '',
end: endTime.toISO() ?? '',
dayOfWeek: date.weekday,
slot: slot,
serviceId: schedule.managedServiceId,
orderId: schedule.orderId,
})
}
}
// Move to the next day
date = date.plus({ days: 1 })
}
const scheduleDatesCreated =
await this.prisma.scheduleDate.createManyAndReturn({
data: scheduleDates,
})
return scheduleDatesCreated
}
generateSlots(scheduleConfigFilled: ScheduleConfigType): ScheduleSlotType[] {
Logger.log(`Generating slots with config: ${scheduleConfigFilled}`)
const slots: ScheduleSlotType[] = []
const numberOfSlots = this.calculateNumberOfSlots(
// @ts-ignore
@@ -143,15 +200,18 @@ export class ScheduleService {
slotBreakDuration: string,
slotStartTime: string,
) {
const durationInMinutes = parseInt(slotDuration);
const breakDurationInMinutes = parseInt(slotBreakDuration);
const durationInMinutes = parseInt(slotDuration)
const breakDurationInMinutes = parseInt(slotBreakDuration)
const startTime = DateTime.fromISO(slotStartTime).plus({
minutes: (slotNumber - 1) * (durationInMinutes + breakDurationInMinutes),
});
})
const endTime = startTime.plus({ minutes: durationInMinutes })
return { startTime, endTime }
return {
startTime: startTime,
endTime: endTime,
}
}
processScheduleConfig(