update schedule

This commit is contained in:
2024-12-16 20:00:11 +07:00
parent b61e4dfc73
commit 6b0b95bb32
3 changed files with 80 additions and 54 deletions

View File

@@ -14,19 +14,19 @@ export class CronService {
) {}
// cron every 1 minute to check schedule date status
@Cron(CronExpression.EVERY_MINUTE)
@Cron(CronExpression.EVERY_10_SECONDS)
async checkScheduleDateStatus() {
const schedules = await this.prisma.scheduleDate.findMany({
where: {
end: {
lt: DateTimeUtils.now().toJSDate(), // past
},
// DEVELOPMENT ONLY
// end: {
// lt: DateTimeUtils.now().toJSDate(), // past
// },
status: {
notIn: [ScheduleDateStatus.COMPLETED, ScheduleDateStatus.MISSING_MENTOR, ScheduleDateStatus.MISSING_CUSTOMER],
},
},
})
Logger.log(`Found ${schedules.length} schedules to update`, 'CronService')
// get all collaboration sessions
const collaborationSessions = await this.prisma.collaborationSession.findMany({
where: {
@@ -40,11 +40,17 @@ export class CronService {
.map((schedule) => {
const collaborationSession = collaborationSessions.find((session) => session.scheduleDateId === schedule.id)
// if (!collaborationSession) {
// return {
// id: schedule.id,
// status: ScheduleDateStatus.MISSING_MENTOR,
// };
// }
// DEVELOPMENT ONLY
if (!collaborationSession) {
return {
id: schedule.id,
status: ScheduleDateStatus.MISSING_MENTOR,
}
return null
}
if (collaborationSession.collaboratorsIds.length === 2) {
@@ -113,51 +119,55 @@ export class CronService {
}
// cron every 1 minute to check if there is any schedule date start in less than 30 minutes
@Cron(CronExpression.EVERY_MINUTE)
async taskCheckScheduleDateStart() {
const schedules = await this.prisma.schedule.findMany({
where: {
AND: [
{
scheduleStart: {
lt: DateTimeUtils.now().plus({ days: 1 }).toJSDate(),
},
},
{
status: ScheduleStatus.PUBLISHED,
},
],
},
})
Logger.log(`Found ${schedules.length} schedules to check`, 'CronService')
for (const schedule of schedules) {
await this.prisma.scheduleDate.updateMany({
where: { scheduleId: schedule.id },
data: { status: ScheduleDateStatus.EXPIRED },
})
// update schedule status to expired
await this.prisma.schedule.update({
where: { id: schedule.id },
data: { status: ScheduleStatus.EXPIRED },
})
// send notification to mentor
const managedService = await this.prisma.managedService.findUnique({
where: { id: schedule.managedServiceId },
})
if (managedService) {
await this.notificationService.sendNotification(
managedService.mentorId,
'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(
DateTimeUtils.fromDate(schedule.scheduleStart),
'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`,
)
}
}
}
// @Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
// async taskCheckScheduleDateStart() {
// Logger.log(
// `DateTimeUtils.now().plus({ minutes: 1 }).toJSDate(): ${DateTimeUtils.now().plus({ minutes: 1 }).toJSDate().toISOString()}`,
// 'CronService',
// )
// const schedules = await this.prisma.schedule.findMany({
// where: {
// AND: [
// {
// scheduleStart: {
// lt: DateTimeUtils.withoutTime(DateTimeUtils.now()).toJSDate(),
// },
// },
// {
// status: ScheduleStatus.PUBLISHED,
// },
// ],
// },
// })
// Logger.log(`Found ${schedules.length} schedules to check`, 'CronService')
// for (const schedule of schedules) {
// await this.prisma.scheduleDate.updateMany({
// where: { scheduleId: schedule.id },
// data: { status: ScheduleDateStatus.EXPIRED },
// })
// // update schedule status to expired
// await this.prisma.schedule.update({
// where: { id: schedule.id },
// data: { status: ScheduleStatus.EXPIRED },
// })
// // send notification to mentor
// const managedService = await this.prisma.managedService.findUnique({
// where: { id: schedule.managedServiceId },
// })
// if (managedService) {
// await this.notificationService.sendNotification(
// managedService.mentorId,
// '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(
// DateTimeUtils.fromDate(schedule.scheduleStart),
// '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`,
// )
// }
// }
// }
// cron every day to disable service without any schedule in the past 30 days
@Cron(CronExpression.EVERY_DAY_AT_1AM)
@Cron(CronExpression.EVERY_10_SECONDS)
async taskDisableServiceWithoutSchedule() {
const services = await this.prisma.service.findMany({
where: {

View File

@@ -421,7 +421,19 @@ d72a864e-2f41-45ab-9c9b-bf0512a31883,e9be51fd-2382-4e43-9988-74e76fde4b56,2024-1
})
const existingScheduleDates = await this.prisma.scheduleDate.findMany({
where: {
serviceId: args.schedule.managedService.connect?.id,
AND: [
{ serviceId: args.schedule.managedService.connect?.id },
{
status: {
notIn: [
ScheduleDateStatus.COMPLETED,
ScheduleDateStatus.MISSING_MENTOR,
ScheduleDateStatus.MISSING_CUSTOMER,
ScheduleDateStatus.EXPIRED,
],
},
},
],
},
})
// check if there is any overlapping with existing schedule dates in same service using DateTimeUtils

View File

@@ -142,4 +142,8 @@ export class DateTimeUtils {
}
return DateTime.now().minus({ days })
}
static withoutTime(dateTime: DateTime): DateTime {
return dateTime.set({ hour: 0, minute: 0, second: 0, millisecond: 0 })
}
}