day len server theo loi Khoi noi, toi xin tuyen bo mien tru trach nhiem
This commit is contained in:
@@ -11,6 +11,7 @@ import { ScheduleStatus } from '@prisma/client'
|
||||
import { ScheduleService } from './schedule.service'
|
||||
import { AppConfigService } from '../AppConfig/appconfig.service'
|
||||
import { ScheduleConfigType } from './schedule'
|
||||
import { DateTimeUtils } from 'src/common/utils/datetime.utils'
|
||||
|
||||
@Injectable()
|
||||
export class ScheduleSchema extends PothosSchema {
|
||||
@@ -290,36 +291,40 @@ d72a864e-2f41-45ab-9c9b-bf0512a31883,e9be51fd-2382-4e43-9988-74e76fde4b56,2024-1
|
||||
required: true,
|
||||
}),
|
||||
},
|
||||
resolve: async (query, _root, args, _ctx, _info) => {
|
||||
resolve: async (query, _root, args, ctx, _info) => {
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Cannot create schedule in subscription')
|
||||
}
|
||||
Logger.log('args.schedule', args.schedule)
|
||||
// check if there is any overlapping schedule
|
||||
const overlappingSchedules = await this.prisma.schedule.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
scheduleStart: {
|
||||
gte: args.schedule.scheduleStart,
|
||||
},
|
||||
scheduleEnd: {
|
||||
lte: args.schedule.scheduleEnd,
|
||||
},
|
||||
},
|
||||
],
|
||||
// generate preview and check if there is any overlapping with other schedules date in same service
|
||||
const previewSchedule =
|
||||
await this.scheduleService.createSchedulePreviewForCenter({
|
||||
startDate: args.schedule.scheduleStart as string,
|
||||
endDate: args.schedule.scheduleEnd as string,
|
||||
slots: args.schedule.slots as number[],
|
||||
days: args.schedule.daysOfWeek as number[],
|
||||
})
|
||||
const existingScheduleDates = await this.prisma.scheduleDate.findMany(
|
||||
{
|
||||
where: {
|
||||
serviceId: args.schedule.managedService.connect?.id,
|
||||
},
|
||||
},
|
||||
})
|
||||
// check if is same managedServiceId
|
||||
if (
|
||||
overlappingSchedules.some(
|
||||
(schedule) =>
|
||||
schedule.managedServiceId ===
|
||||
args.schedule.managedService.connect?.id,
|
||||
)
|
||||
) {
|
||||
throw new Error(
|
||||
`Overlapping schedule with ${JSON.stringify(
|
||||
overlappingSchedules.map((schedule) => schedule.id),
|
||||
)}`,
|
||||
)
|
||||
)
|
||||
// check if there is any overlapping with existing schedule dates in same service using DateTimeUtils
|
||||
const isOverlapping = DateTimeUtils.isOverlaps(
|
||||
previewSchedule.slots.map((slot) => ({
|
||||
start: DateTimeUtils.fromIsoString(slot.start),
|
||||
end: DateTimeUtils.fromIsoString(slot.end),
|
||||
})),
|
||||
existingScheduleDates.map((date) => ({
|
||||
start: DateTimeUtils.fromDate(date.start),
|
||||
end: DateTimeUtils.fromDate(date.end),
|
||||
})),
|
||||
)
|
||||
if (isOverlapping) {
|
||||
Logger.error('Overlapping schedule', 'ScheduleSchema')
|
||||
throw new Error('Overlapping schedule')
|
||||
}
|
||||
const schedule = await this.prisma.schedule.create({
|
||||
...query,
|
||||
|
||||
Reference in New Issue
Block a user