From 7911d7f0bd168ddaad8d60987ea5e6516e7c9c44 Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Thu, 28 Nov 2024 19:21:06 +0700 Subject: [PATCH] Refactor CronService to utilize DateTimeUtils for date handling. Update schedule and payment checks to use current time instead of new Date(), enhancing accuracy in scheduling logic. This change improves the reliability of cron job operations by ensuring consistent date management. --- src/Cron/cron.service.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Cron/cron.service.ts b/src/Cron/cron.service.ts index 0bd19f1..cf95f35 100644 --- a/src/Cron/cron.service.ts +++ b/src/Cron/cron.service.ts @@ -2,6 +2,7 @@ import { Injectable, Logger } from '@nestjs/common' 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 { PrismaService } from 'src/Prisma/prisma.service' @Injectable() @@ -15,7 +16,7 @@ export class CronService { const schedules = await this.prisma.scheduleDate.findMany({ where: { end: { - lt: new Date(), + lt: DateTimeUtils.now().toJSDate(), }, status: { notIn: [ScheduleDateStatus.COMPLETED, ScheduleDateStatus.MISSING_MENTOR, ScheduleDateStatus.MISSING_CUSTOMER], @@ -77,7 +78,7 @@ export class CronService { where: { status: PaymentStatus.PENDING, createdAt: { - lt: new Date(Date.now() - 15 * 60 * 1000), + lt: DateTimeUtils.now().minus({ minutes: 15 }).toJSDate(), }, }, }) @@ -98,7 +99,7 @@ export class CronService { @Cron(CronExpression.EVERY_MINUTE) async handleRefundTicket() { Logger.log('Handling refund ticket', 'handleRefundTicket') - const now = new Date() + const now = DateTimeUtils.now().toJSDate() // get all orders where status is REFUNDED and has schedule.dates in future const orders = await this.prisma.order.findMany({ where: {