fix time geneate logic and replace default datetime by luxon
This commit is contained in:
@@ -23,6 +23,16 @@ export type ScheduleConfigType =
|
||||
| null
|
||||
| undefined
|
||||
|
||||
export type ScheduleConfigTypeForCenter =
|
||||
| {
|
||||
startDate?: string | null | undefined
|
||||
endDate?: string | null | undefined
|
||||
slots?: number[] | null | undefined
|
||||
days?: number[] | null | undefined
|
||||
}
|
||||
| null
|
||||
| undefined
|
||||
|
||||
export type ScheduleSlotType = {
|
||||
slot: string
|
||||
start: string
|
||||
@@ -144,6 +154,18 @@ export class ScheduleSchema extends PothosSchema {
|
||||
})
|
||||
}
|
||||
|
||||
@PothosRef()
|
||||
scheduleConfigInputForCenter() {
|
||||
return this.builder.inputType('ScheduleConfigInputForCenter', {
|
||||
fields: (t) => ({
|
||||
startDate: t.string(),
|
||||
endDate: t.string(),
|
||||
slots: t.intList(),
|
||||
days: t.intList(),
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
@Pothos()
|
||||
init(): void {
|
||||
this.builder.queryFields((t) => ({
|
||||
@@ -175,6 +197,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,
|
||||
// )
|
||||
// },
|
||||
// }),
|
||||
|
||||
adminPreviewSchedule: t.field({
|
||||
type: this.previewSchedule(),
|
||||
description: 'Preview a schedule for admin.',
|
||||
|
||||
@@ -7,11 +7,21 @@ import { AppConfigService } from 'src/AppConfig/appconfig.service'
|
||||
import {
|
||||
PreviewScheduleType,
|
||||
ScheduleConfigType,
|
||||
ScheduleConfigTypeForCenter,
|
||||
ScheduleSlotType,
|
||||
} from './schedule.schema'
|
||||
import { Config } 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],
|
||||
// }
|
||||
|
||||
@Injectable()
|
||||
export class ScheduleService {
|
||||
constructor(
|
||||
@@ -38,6 +48,18 @@ 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,
|
||||
// )
|
||||
// }
|
||||
|
||||
generateSlots(scheduleConfigFilled: ScheduleConfigType): ScheduleSlotType[] {
|
||||
Logger.log(`Generating slots with config: ${scheduleConfigFilled}`)
|
||||
const slots: ScheduleSlotType[] = []
|
||||
@@ -66,11 +88,11 @@ export class ScheduleService {
|
||||
!this.isOverLapping(
|
||||
startTime,
|
||||
endTime,
|
||||
this.getSpecificDateWithTime(
|
||||
DateTime.fromISO(
|
||||
// @ts-ignore
|
||||
scheduleConfigFilled?.midDayBreakTimeStart,
|
||||
),
|
||||
this.getSpecificDateWithTime(
|
||||
DateTime.fromISO(
|
||||
// @ts-ignore
|
||||
scheduleConfigFilled?.midDayBreakTimeEnd,
|
||||
),
|
||||
@@ -78,8 +100,8 @@ export class ScheduleService {
|
||||
) {
|
||||
slots.push({
|
||||
slot: i.toString(),
|
||||
start: startTime.toISOString(),
|
||||
end: endTime.toISOString(),
|
||||
start: startTime.toString(),
|
||||
end: endTime.toString(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -87,14 +109,14 @@ export class ScheduleService {
|
||||
}
|
||||
|
||||
isOverLapping(
|
||||
startTime1: Date,
|
||||
endTime1: Date,
|
||||
startTime2: Date,
|
||||
endTime2: Date,
|
||||
startTime1: DateTime,
|
||||
endTime1: DateTime,
|
||||
startTime2: DateTime,
|
||||
endTime2: DateTime,
|
||||
) {
|
||||
return (
|
||||
Math.max(startTime1.getTime(), startTime2.getTime()) <
|
||||
Math.min(endTime1.getTime(), endTime2.getTime())
|
||||
Math.max(startTime1.toMillis(), startTime2.toMillis()) <
|
||||
Math.min(endTime1.toMillis(), endTime2.toMillis())
|
||||
)
|
||||
}
|
||||
|
||||
@@ -104,10 +126,11 @@ export class ScheduleService {
|
||||
slotDuration: string,
|
||||
slotBreakDuration: string,
|
||||
) {
|
||||
const startDate = new Date(startTime)
|
||||
const endDate = new Date(endTime)
|
||||
const startDate = DateTime.fromISO(startTime)
|
||||
const endDate = DateTime.fromISO(endTime)
|
||||
|
||||
const totalMinutes = (endDate.getTime() - startDate.getTime()) / (60 * 1000)
|
||||
const totalMinutes =
|
||||
(endDate.toMillis() - startDate.toMillis()) / (60 * 1000)
|
||||
const numberOfSlots = Math.floor(
|
||||
totalMinutes / (parseInt(slotDuration) + parseInt(slotBreakDuration)),
|
||||
)
|
||||
@@ -120,13 +143,14 @@ export class ScheduleService {
|
||||
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))
|
||||
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 }
|
||||
}
|
||||
|
||||
@@ -169,26 +193,23 @@ export class ScheduleService {
|
||||
return _.camelCase(str.toLowerCase())
|
||||
}
|
||||
|
||||
getTodayWithTime(date: Date) {
|
||||
const today = new Date()
|
||||
today.setUTCHours(
|
||||
date.getUTCHours(),
|
||||
date.getUTCMinutes(),
|
||||
date.getUTCSeconds(),
|
||||
0,
|
||||
)
|
||||
getTodayWithTime(date: DateTime) {
|
||||
let today = DateTime.now()
|
||||
today = today.set({
|
||||
hour: date.hour,
|
||||
minute: date.minute,
|
||||
second: date.second,
|
||||
})
|
||||
return today
|
||||
}
|
||||
|
||||
getSpecificDateWithTime(date: Date) {
|
||||
const specificDate = new Date(date)
|
||||
date = new Date(date)
|
||||
specificDate.setUTCHours(
|
||||
date.getUTCHours(),
|
||||
date.getUTCMinutes(),
|
||||
date.getUTCSeconds(),
|
||||
0,
|
||||
)
|
||||
getSpecificDateWithTime(date: DateTime) {
|
||||
let specificDate = DateTime.now()
|
||||
specificDate = specificDate.set({
|
||||
hour: date.hour,
|
||||
minute: date.minute,
|
||||
second: date.second,
|
||||
})
|
||||
return specificDate
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user