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