update schedule
This commit is contained in:
@@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user