From 6b0b95bb3238fc8e5f3cbea852f0613dac7b0c13 Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Mon, 16 Dec 2024 20:00:11 +0700 Subject: [PATCH] update schedule --- src/Cron/cron.service.ts | 116 ++++++++++++++++------------- src/Schedule/schedule.schema.ts | 14 +++- src/common/utils/datetime.utils.ts | 4 + 3 files changed, 80 insertions(+), 54 deletions(-) diff --git a/src/Cron/cron.service.ts b/src/Cron/cron.service.ts index d7dd2fe..8c811fe 100644 --- a/src/Cron/cron.service.ts +++ b/src/Cron/cron.service.ts @@ -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: { diff --git a/src/Schedule/schedule.schema.ts b/src/Schedule/schedule.schema.ts index 4ac7846..152ce02 100644 --- a/src/Schedule/schedule.schema.ts +++ b/src/Schedule/schedule.schema.ts @@ -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 diff --git a/src/common/utils/datetime.utils.ts b/src/common/utils/datetime.utils.ts index 2d3d013..839dd5a 100644 --- a/src/common/utils/datetime.utils.ts +++ b/src/common/utils/datetime.utils.ts @@ -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 }) + } }