update preview date
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
import * as DateTimeUtils from '../common/utils/datetime.utils'
|
||||
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { Injectable, Logger } from '@nestjs/common'
|
||||
import { PrismaService } from 'src/Prisma/prisma.service'
|
||||
import { Schedule } from '@prisma/client'
|
||||
|
||||
import { AppConfigService } from 'src/AppConfig/appconfig.service'
|
||||
import {
|
||||
PreviewScheduleType,
|
||||
ScheduleConfigType,
|
||||
ScheduleSlotType,
|
||||
} from './schedule.schema'
|
||||
import { Config } from '@prisma/client'
|
||||
import * as _ from 'lodash'
|
||||
|
||||
@Injectable()
|
||||
export class ScheduleService {
|
||||
@@ -13,10 +19,176 @@ export class ScheduleService {
|
||||
private readonly appConfigService: AppConfigService,
|
||||
) {}
|
||||
|
||||
async createSchedule(schedule: Schedule) {
|
||||
// get config
|
||||
const config = await this.appConfigService.getVisibleConfigs()
|
||||
console.log(config)
|
||||
async createSchedulePreview(
|
||||
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,
|
||||
config,
|
||||
)
|
||||
// generate Slot By config
|
||||
const slots = this.generateSlots(scheduleConfigFilled)
|
||||
|
||||
return {
|
||||
totalSlots: slots.length,
|
||||
slots: slots,
|
||||
}
|
||||
}
|
||||
|
||||
generateSlots(scheduleConfigFilled: ScheduleConfigType): ScheduleSlotType[] {
|
||||
Logger.log(`Generating slots with config: ${scheduleConfigFilled}`)
|
||||
const slots: ScheduleSlotType[] = []
|
||||
const numberOfSlots = this.calculateNumberOfSlots(
|
||||
// @ts-ignore
|
||||
scheduleConfigFilled?.slotStartTime,
|
||||
// @ts-ignore
|
||||
scheduleConfigFilled?.slotEndTime,
|
||||
// @ts-ignore
|
||||
scheduleConfigFilled?.slotDuration,
|
||||
// @ts-ignore
|
||||
scheduleConfigFilled?.slotBreakDuration,
|
||||
)
|
||||
for (let i = 1; i <= numberOfSlots; i++) {
|
||||
const { startTime, endTime } = this.getSlotStartAndEndTime(
|
||||
i,
|
||||
// @ts-ignore
|
||||
scheduleConfigFilled?.slotDuration,
|
||||
// @ts-ignore
|
||||
scheduleConfigFilled?.slotBreakDuration,
|
||||
// @ts-ignore
|
||||
scheduleConfigFilled?.slotStartTime,
|
||||
)
|
||||
// if the slot is not overlapping with mid day break time, add it to the slots
|
||||
if (
|
||||
!this.isOverLapping(
|
||||
startTime,
|
||||
endTime,
|
||||
this.getSpecificDateWithTime(
|
||||
// @ts-ignore
|
||||
scheduleConfigFilled?.midDayBreakTimeStart,
|
||||
),
|
||||
this.getSpecificDateWithTime(
|
||||
// @ts-ignore
|
||||
scheduleConfigFilled?.midDayBreakTimeEnd,
|
||||
),
|
||||
)
|
||||
) {
|
||||
slots.push({
|
||||
slot: i.toString(),
|
||||
start: startTime.toISOString(),
|
||||
end: endTime.toISOString(),
|
||||
})
|
||||
}
|
||||
}
|
||||
return slots
|
||||
}
|
||||
|
||||
isOverLapping(
|
||||
startTime1: Date,
|
||||
endTime1: Date,
|
||||
startTime2: Date,
|
||||
endTime2: Date,
|
||||
) {
|
||||
return (
|
||||
Math.max(startTime1.getTime(), startTime2.getTime()) <
|
||||
Math.min(endTime1.getTime(), endTime2.getTime())
|
||||
)
|
||||
}
|
||||
|
||||
calculateNumberOfSlots(
|
||||
startTime: string,
|
||||
endTime: string,
|
||||
slotDuration: string,
|
||||
slotBreakDuration: string,
|
||||
) {
|
||||
const startDate = new Date(startTime)
|
||||
const endDate = new Date(endTime)
|
||||
|
||||
const totalMinutes = (endDate.getTime() - startDate.getTime()) / (60 * 1000)
|
||||
const numberOfSlots = Math.floor(
|
||||
totalMinutes / (parseInt(slotDuration) + parseInt(slotBreakDuration)),
|
||||
)
|
||||
return numberOfSlots
|
||||
}
|
||||
|
||||
getSlotStartAndEndTime(
|
||||
slotNumber: number,
|
||||
slotDuration: string,
|
||||
slotBreakDuration: string,
|
||||
slotStartTime: string,
|
||||
) {
|
||||
const startTime = new Date(slotStartTime)
|
||||
startTime.setUTCMinutes(
|
||||
startTime.getUTCMinutes() +
|
||||
slotNumber * (parseInt(slotDuration) + parseInt(slotBreakDuration)),
|
||||
)
|
||||
const endTime = new Date(startTime)
|
||||
endTime.setUTCMinutes(endTime.getUTCMinutes() + parseInt(slotDuration))
|
||||
return { startTime, endTime }
|
||||
}
|
||||
|
||||
processScheduleConfig(
|
||||
scheduleConfig: ScheduleConfigType,
|
||||
config: Config[],
|
||||
): ScheduleConfigType {
|
||||
// if scheduleConfig is undefined, create a new object and seed all the values with default values from config
|
||||
if (scheduleConfig === undefined) {
|
||||
scheduleConfig = config.reduce((acc, curr) => {
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
;(acc as any)[this.sneakyCaseToCamelCase(curr.key)] = curr.value
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
// loop through scheduleConfig and fill with default values from config
|
||||
for (const key in scheduleConfig) {
|
||||
if (
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
(scheduleConfig as any)[key] === undefined ||
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
(scheduleConfig as any)[key] === null ||
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
(scheduleConfig as any)[key] === ''
|
||||
) {
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
;(scheduleConfig as any)[key] =
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
config[this.camelCaseToUpperSneakyCase(key) as any]
|
||||
}
|
||||
}
|
||||
return scheduleConfig
|
||||
}
|
||||
|
||||
camelCaseToUpperSneakyCase(str: string) {
|
||||
return _.snakeCase(str).toUpperCase()
|
||||
}
|
||||
|
||||
sneakyCaseToCamelCase(str: string) {
|
||||
return _.camelCase(str.toLowerCase())
|
||||
}
|
||||
|
||||
getTodayWithTime(date: Date) {
|
||||
const today = new Date()
|
||||
today.setUTCHours(
|
||||
date.getUTCHours(),
|
||||
date.getUTCMinutes(),
|
||||
date.getUTCSeconds(),
|
||||
0,
|
||||
)
|
||||
return today
|
||||
}
|
||||
|
||||
getSpecificDateWithTime(date: Date) {
|
||||
const specificDate = new Date(date)
|
||||
date = new Date(date)
|
||||
specificDate.setUTCHours(
|
||||
date.getUTCHours(),
|
||||
date.getUTCMinutes(),
|
||||
date.getUTCSeconds(),
|
||||
0,
|
||||
)
|
||||
return specificDate
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user