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:
@@ -165,7 +165,7 @@ export class CronService {
|
|||||||
Logger.log(`Found ${schedules.length} schedules to check`, 'taskCheckScheduleDateStart')
|
Logger.log(`Found ${schedules.length} schedules to check`, 'taskCheckScheduleDateStart')
|
||||||
for (const schedule of schedules) {
|
for (const schedule of schedules) {
|
||||||
await this.prisma.scheduleDate.updateMany({
|
await this.prisma.scheduleDate.updateMany({
|
||||||
where: { scheduleId: schedule.id, start: { lt: DateTimeUtils.now().plus({ minutes: 30 }).toJSDate() } },
|
where: { scheduleId: schedule.id },
|
||||||
data: { status: ScheduleDateStatus.EXPIRED },
|
data: { status: ScheduleDateStatus.EXPIRED },
|
||||||
})
|
})
|
||||||
// update schedule status to expired
|
// update schedule status to expired
|
||||||
@@ -183,7 +183,7 @@ export class CronService {
|
|||||||
'Lịch hướng dẫn của bạn đã hết hạn',
|
'Lịch hướng dẫn của bạn đã hết hạn',
|
||||||
`Lịch hướng dẫn với ngày bắt đầu: ${DateTimeUtils.format(
|
`Lịch hướng dẫn với ngày bắt đầu: ${DateTimeUtils.format(
|
||||||
DateTimeUtils.fromDate(schedule.scheduleStart),
|
DateTimeUtils.fromDate(schedule.scheduleStart),
|
||||||
'DD/MM/YYYY',
|
'D',
|
||||||
)}, slot: ${schedule.slots.map((s) => s).join(', ')} của bạn đã hết hạn do không có học viên đăng ký, vui lòng tạo lịch hướng dẫn mới`,
|
)}, slot: ${schedule.slots.map((s) => s).join(', ')} của bạn đã hết hạn do không có học viên đăng ký, vui lòng tạo lịch hướng dẫn mới`,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -162,8 +162,12 @@ export class DocumentSchema extends PothosSchema {
|
|||||||
if (!ctx.http?.me?.id) throw new Error('User not found')
|
if (!ctx.http?.me?.id) throw new Error('User not found')
|
||||||
if (!args.documentId) throw new Error('Document id not found')
|
if (!args.documentId) throw new Error('Document id not found')
|
||||||
if (args.pageIndex === undefined || args.pageIndex === null) throw new Error('Page index not found')
|
if (args.pageIndex === undefined || args.pageIndex === null) throw new Error('Page index not found')
|
||||||
const delta = await this.minio.getDocumentPage(args.documentId, args.pageIndex)
|
let delta
|
||||||
if (!delta) throw new Error('Delta not found')
|
try {
|
||||||
|
delta = await this.minio.getDocumentPage(args.documentId, args.pageIndex)
|
||||||
|
} catch (_error) {
|
||||||
|
delta = null
|
||||||
|
}
|
||||||
const totalPage = await this.minio.countDocumentPages(args.documentId)
|
const totalPage = await this.minio.countDocumentPages(args.documentId)
|
||||||
return {
|
return {
|
||||||
documentId: args.documentId,
|
documentId: args.documentId,
|
||||||
@@ -172,6 +176,7 @@ export class DocumentSchema extends PothosSchema {
|
|||||||
totalPage,
|
totalPage,
|
||||||
senderId: ctx.http?.me?.id,
|
senderId: ctx.http?.me?.id,
|
||||||
eventType: DocumentEvent.CLIENT_REQUEST_SYNC,
|
eventType: DocumentEvent.CLIENT_REQUEST_SYNC,
|
||||||
|
requestSync: false,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -220,10 +220,32 @@ export class ScheduleSchema extends PothosSchema {
|
|||||||
type: this.schedule(),
|
type: this.schedule(),
|
||||||
description: 'Retrieve a single schedule by its unique identifier.',
|
description: 'Retrieve a single schedule by its unique identifier.',
|
||||||
args: this.builder.generator.findUniqueArgs('Schedule'),
|
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({
|
return await this.prisma.schedule.findUnique({
|
||||||
...query,
|
...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')
|
throw new Error('Cannot create schedule in subscription')
|
||||||
}
|
}
|
||||||
Logger.log('args.schedule', args.schedule)
|
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
|
// generate preview and check if there is any overlapping with other schedules date in same service
|
||||||
const previewSchedule = await this.scheduleService.createSchedulePreviewForCenter({
|
const previewSchedule = await this.scheduleService.createSchedulePreviewForCenter({
|
||||||
startDate: args.schedule.scheduleStart as string,
|
startDate: args.schedule.scheduleStart as string,
|
||||||
|
|||||||
Reference in New Issue
Block a user