Update dependencies in package.json and package-lock.json, refactor CronService to include notification handling for schedule expirations, and enhance DocumentSchema with OpenAI integration for document editing suggestions. Additionally, modify GraphqlModule to include PubSubModule for real-time notifications and improve datetime utility functions for better date formatting. Update epess-database subproject reference to indicate a dirty state.

This commit is contained in:
2024-11-30 16:41:47 +07:00
parent 7cff5069de
commit a619c95efc
17 changed files with 971 additions and 863 deletions

View File

@@ -3,11 +3,15 @@ import { Cron } from '@nestjs/schedule'
import { CronExpression } from '@nestjs/schedule'
import { OrderStatus, PaymentStatus, ScheduleDateStatus, ScheduleStatus } from '@prisma/client'
import { DateTimeUtils } from 'src/common/utils/datetime.utils'
import { NotificationService } from 'src/Notification/notification.service'
import { PrismaService } from 'src/Prisma/prisma.service'
@Injectable()
export class CronService {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly notificationService: NotificationService,
) {}
// cron every 1 minute to check schedule date status
@Cron(CronExpression.EVERY_MINUTE)
@@ -97,7 +101,7 @@ export class CronService {
// handle refund ticket by order, if order status is refunded, disable schedule and remove schedule date in future
@Cron(CronExpression.EVERY_MINUTE)
async handleRefundTicket() {
async taskRefundTicket() {
Logger.log('Handling refund ticket', 'handleRefundTicket')
const now = DateTimeUtils.now().toJSDate()
// get all orders where status is REFUNDED and has schedule.dates in future
@@ -139,4 +143,50 @@ 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() {
Logger.log('Checking schedule date start', 'taskCheckScheduleDateStart')
const schedules = await this.prisma.schedule.findMany({
where: {
AND: [
{
scheduleStart: {
lt: DateTimeUtils.now().plus({ minutes: 30 }).toJSDate(),
},
},
{
status: ScheduleStatus.PUBLISHED,
},
],
},
})
Logger.log(`Found ${schedules.length} schedules to check`, 'taskCheckScheduleDateStart')
for (const schedule of schedules) {
await this.prisma.scheduleDate.updateMany({
where: { scheduleId: schedule.id, start: { lt: DateTimeUtils.now().plus({ minutes: 30 }).toJSDate() } },
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),
'DD/MM/YYYY',
)}, 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`,
)
}
}
}
}