refactor: enhance schedule and document handling in schemas

- Simplify schedule expiration logic in CronService by removing unnecessary date checks.
- Improve error handling in DocumentSchema for document page retrieval, ensuring graceful failure.
- Add validation in ScheduleSchema to restrict schedule retrieval to authorized centers and prevent subscription-based access.
- Implement checks to reject schedules with start dates in the past or today, enhancing data integrity.
This commit is contained in:
2024-11-30 20:35:34 +07:00
parent 7729d3ce63
commit c26bf36084
3 changed files with 37 additions and 6 deletions

View File

@@ -220,10 +220,32 @@ export class ScheduleSchema extends PothosSchema {
type: this.schedule(),
description: 'Retrieve a single schedule by its unique identifier.',
args: this.builder.generator.findUniqueArgs('Schedule'),
resolve: async (query, _root, args, _ctx, _info) => {
resolve: async (query, _root, args, ctx, _info) => {
if (ctx.isSubscription) {
throw new Error('Cannot retrieve schedule in subscription')
}
if (!ctx.http?.me?.id) {
throw new Error('User not found')
}
// only return schedule belong to center
const center = await this.prisma.center.findFirst({
where: {
centerMentors: {
some: {
mentorId: ctx.http.me.id,
},
},
},
})
if (!center) {
throw new Error('Center not found')
}
return await this.prisma.schedule.findUnique({
...query,
where: args.where,
where: {
id: args.where?.id,
managedService: { service: { centerId: center.id } },
},
})
},
}),
@@ -300,6 +322,10 @@ d72a864e-2f41-45ab-9c9b-bf0512a31883,e9be51fd-2382-4e43-9988-74e76fde4b56,2024-1
throw new Error('Cannot create schedule in subscription')
}
Logger.log('args.schedule', args.schedule)
// reject schedule if start date is today or in the past
if (DateTimeUtils.fromDate(args.schedule.scheduleStart as Date).day <= DateTimeUtils.now().day) {
throw new Error('Start date is in the past or today')
}
// 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,