lennnn
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Inject, Injectable } from '@nestjs/common'
|
||||
import { Inject, Injectable, Logger } from '@nestjs/common'
|
||||
import {
|
||||
Pothos,
|
||||
PothosRef,
|
||||
@@ -10,6 +10,7 @@ import { PrismaService } from '../Prisma/prisma.service'
|
||||
import { ScheduleStatus } from '@prisma/client'
|
||||
import { ScheduleService } from './schedule.service'
|
||||
import { AppConfigService } from '../AppConfig/appconfig.service'
|
||||
import { forEach } from 'lodash'
|
||||
|
||||
export type ScheduleConfigType =
|
||||
| {
|
||||
@@ -63,10 +64,18 @@ export class ScheduleSchema extends PothosSchema {
|
||||
id: t.exposeID('id', {
|
||||
description: 'The ID of the schedule.',
|
||||
}),
|
||||
customerId: t.exposeID('customerId', {
|
||||
description: 'The ID of the customer the schedule belongs to.',
|
||||
nullable: true,
|
||||
}),
|
||||
managedServiceId: t.exposeID('managedServiceId', {
|
||||
description: 'The ID of the managed service the schedule belongs to.',
|
||||
nullable: false,
|
||||
}),
|
||||
orderId: t.exposeID('orderId', {
|
||||
description: 'The ID of the order the schedule belongs to.',
|
||||
nullable: true,
|
||||
}),
|
||||
scheduleStart: t.expose('scheduleStart', {
|
||||
type: 'DateTime',
|
||||
nullable: false,
|
||||
@@ -75,6 +84,12 @@ export class ScheduleSchema extends PothosSchema {
|
||||
type: 'DateTime',
|
||||
nullable: false,
|
||||
}),
|
||||
slots: t.exposeIntList('slots', {
|
||||
nullable: false,
|
||||
}),
|
||||
daysOfWeek: t.exposeIntList('daysOfWeek', {
|
||||
nullable: false,
|
||||
}),
|
||||
dates: t.relation('dates', {
|
||||
description: 'The dates of the schedule.',
|
||||
}),
|
||||
@@ -132,6 +147,18 @@ export class ScheduleSchema extends PothosSchema {
|
||||
type: 'DateTime',
|
||||
nullable: false,
|
||||
}),
|
||||
dayOfWeek: t.exposeInt('dayOfWeek', {
|
||||
nullable: false,
|
||||
}),
|
||||
slot: t.exposeInt('slot', {
|
||||
nullable: false,
|
||||
}),
|
||||
serviceId: t.exposeID('serviceId', {
|
||||
nullable: false,
|
||||
}),
|
||||
orderId: t.exposeID('orderId', {
|
||||
nullable: true,
|
||||
}),
|
||||
schedule: t.relation('schedule', {
|
||||
description: 'The schedule the schedule date belongs to.',
|
||||
}),
|
||||
@@ -227,5 +254,78 @@ export class ScheduleSchema extends PothosSchema {
|
||||
},
|
||||
}),
|
||||
}))
|
||||
|
||||
/* overlapping case
|
||||
46836288-bb2c-4da6-892b-a559a480cbf8,e9be51fd-2382-4e43-9988-74e76fde4b56,2024-11-22 00:00:00.000,2024-11-02 00:00:00.000,UNPUBLISHED,,"{3,5}",,"{2,4}"
|
||||
d72a864e-2f41-45ab-9c9b-bf0512a31883,e9be51fd-2382-4e43-9988-74e76fde4b56,2024-11-22 00:00:00.000,2024-11-02 00:00:00.000,UNPUBLISHED,,"{3,5}",,"{2,4}"
|
||||
*/
|
||||
|
||||
this.builder.mutationFields((t) => ({
|
||||
// Mutations
|
||||
createSchedule: t.prismaField({
|
||||
type: this.schedule(),
|
||||
description: 'Create a new schedule.',
|
||||
args: {
|
||||
schedule: t.arg({
|
||||
type: this.builder.generator.getCreateInput('Schedule', [
|
||||
'id',
|
||||
'status',
|
||||
'customerId',
|
||||
'orderId',
|
||||
]),
|
||||
required: true,
|
||||
}),
|
||||
},
|
||||
resolve: async (query, _root, args, _ctx, _info) => {
|
||||
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,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
// 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),
|
||||
)}`,
|
||||
)
|
||||
}
|
||||
const schedule = await this.prisma.schedule.create({
|
||||
...query,
|
||||
data: args.schedule,
|
||||
})
|
||||
// generate schedule dates based on data and config
|
||||
const scheduleDates =
|
||||
await this.scheduleService.generateScheduleDates(schedule)
|
||||
// update schedule with schedule dates
|
||||
return await this.prisma.schedule.update({
|
||||
...query,
|
||||
where: { id: schedule.id },
|
||||
data: {
|
||||
dates: {
|
||||
connect: scheduleDates.map((date) => ({ id: date.id })),
|
||||
},
|
||||
},
|
||||
})
|
||||
},
|
||||
}),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user