From 667dc9e09d1b5571c0fb4392fe5f7ba21790ed8d Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Mon, 25 Nov 2024 18:16:50 +0700 Subject: [PATCH] implement some cron task --- package-lock.json | 48 +++++++++++++++++++++- package.json | 1 + src/Cron/cron.module.ts | 8 ++++ src/Cron/cron.service.ts | 89 ++++++++++++++++++++++++++++++++++++++++ src/app.module.ts | 2 + 5 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 src/Cron/cron.module.ts create mode 100644 src/Cron/cron.service.ts diff --git a/package-lock.json b/package-lock.json index 0763bed..71786fa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,6 +26,7 @@ "@nestjs/graphql": "^12.2.0", "@nestjs/jwt": "^10.2.0", "@nestjs/platform-express": "^10.0.0", + "@nestjs/schedule": "^4.1.1", "@nestjs/swagger": "^7.4.2", "@payos/node": "^1.0.10", "@pothos/core": "^4.3.0", @@ -5062,6 +5063,33 @@ "integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA==", "license": "0BSD" }, + "node_modules/@nestjs/schedule": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/@nestjs/schedule/-/schedule-4.1.1.tgz", + "integrity": "sha512-VxAnCiU4HP0wWw8IdWAVfsGC/FGjyToNjjUtXDEQL6oj+w/N5QDd2VT9k6d7Jbr8PlZuBZNdWtDKSkH5bZ+RXQ==", + "license": "MIT", + "dependencies": { + "cron": "3.1.7", + "uuid": "10.0.0" + }, + "peerDependencies": { + "@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0", + "@nestjs/core": "^8.0.0 || ^9.0.0 || ^10.0.0" + } + }, + "node_modules/@nestjs/schedule/node_modules/uuid": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-10.0.0.tgz", + "integrity": "sha512-8XkAphELsDnEGrDxUOHB3RGvXz6TeuYSGEZBOjtTtPm2lwhGBjLgOzLHB63IUWfBpNucQjND6d3AOudO+H3RWQ==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], + "license": "MIT", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/@nestjs/schematics": { "version": "10.2.3", "resolved": "https://registry.npmjs.org/@nestjs/schematics/-/schematics-10.2.3.tgz", @@ -6025,7 +6053,6 @@ "version": "3.4.2", "resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-3.4.2.tgz", "integrity": "sha512-TifLZlFudklWlMBfhubvgqTXRzLDI5pCbGa4P8a3wPyUQSW+1xQ5eDsreP9DWHX3tjq1ke96uYG/nwundroWcA==", - "dev": true, "license": "MIT" }, "node_modules/@types/methods": { @@ -9014,6 +9041,25 @@ "dev": true, "license": "MIT" }, + "node_modules/cron": { + "version": "3.1.7", + "resolved": "https://registry.npmjs.org/cron/-/cron-3.1.7.tgz", + "integrity": "sha512-tlBg7ARsAMQLzgwqVxy8AZl/qlTc5nibqYwtNGoCrd+cV+ugI+tvZC1oT/8dFH8W455YrywGykx/KMmAqOr7Jw==", + "license": "MIT", + "dependencies": { + "@types/luxon": "~3.4.0", + "luxon": "~3.4.0" + } + }, + "node_modules/cron/node_modules/luxon": { + "version": "3.4.4", + "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.4.4.tgz", + "integrity": "sha512-zobTr7akeGHnv7eBOXcRgMeCP6+uyYsczwmeRCauvpvaAltgNyTbLH/+VaEAPUeWBT+1GuNmz4wC/6jtQzbbVA==", + "license": "MIT", + "engines": { + "node": ">=12" + } + }, "node_modules/cross-fetch": { "version": "3.1.8", "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.8.tgz", diff --git a/package.json b/package.json index 8b43f38..213a27e 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "@nestjs/graphql": "^12.2.0", "@nestjs/jwt": "^10.2.0", "@nestjs/platform-express": "^10.0.0", + "@nestjs/schedule": "^4.1.1", "@nestjs/swagger": "^7.4.2", "@payos/node": "^1.0.10", "@pothos/core": "^4.3.0", diff --git a/src/Cron/cron.module.ts b/src/Cron/cron.module.ts new file mode 100644 index 0000000..0fddf6b --- /dev/null +++ b/src/Cron/cron.module.ts @@ -0,0 +1,8 @@ +import { Module } from '@nestjs/common' +import { ScheduleModule } from '@nestjs/schedule' +import { CronService } from './cron.service' +@Module({ + imports: [ScheduleModule.forRoot()], + providers: [CronService], +}) +export class CronModule {} diff --git a/src/Cron/cron.service.ts b/src/Cron/cron.service.ts new file mode 100644 index 0000000..ee8bb13 --- /dev/null +++ b/src/Cron/cron.service.ts @@ -0,0 +1,89 @@ +import { Injectable, Logger } from '@nestjs/common' +import { Cron } from '@nestjs/schedule' +import { CronExpression } from '@nestjs/schedule' +import { PaymentStatus, ScheduleDateStatus } from '@prisma/client' +import { PrismaService } from 'src/Prisma/prisma.service' + +@Injectable() +export class CronService { + constructor(private readonly prisma: PrismaService) {} + + // cron every 1 minute to check schedule date status + @Cron(CronExpression.EVERY_MINUTE) + async checkScheduleDateStatus() { + Logger.log('Checking schedule date status') + const schedules = await this.prisma.scheduleDate.findMany({ + where: { + end: { + lt: new Date(), + }, + status: { + notIn: [ + ScheduleDateStatus.COMPLETED, + ScheduleDateStatus.MISSING_MENTOR, + ScheduleDateStatus.MISSING_CUSTOMER, + ], + }, + }, + }) + Logger.log( + `Found ${schedules.length} schedules to update`, + 'checkScheduleDateStatus', + ) + + const updates = schedules + .map((schedule) => { + if (schedule.status === ScheduleDateStatus.NOT_STARTED) { + return { + id: schedule.id, + status: ScheduleDateStatus.MISSING_MENTOR, + } + } + if ( + schedule.status === ScheduleDateStatus.IN_PROGRESS && + schedule.participantIds.length === 1 + ) { + return { + id: schedule.id, + status: ScheduleDateStatus.MISSING_CUSTOMER, + } + } + if ( + schedule.status === ScheduleDateStatus.IN_PROGRESS && + schedule.participantIds.length === 2 + ) { + return { + id: schedule.id, + status: ScheduleDateStatus.COMPLETED, + } + } + return null + }) + .filter((update) => update !== null) + + for (const update of updates) { + await this.prisma.scheduleDate.update({ + where: { id: update.id }, + data: { status: update.status }, + }) + } + } + + // cron every 1 minute to check payment status where created_at is more than 15 minutes + @Cron(CronExpression.EVERY_MINUTE) + async checkPaymentStatus() { + Logger.log('Checking payment status') + const payments = await this.prisma.payment.findMany({ + where: { + status: PaymentStatus.PENDING, + createdAt: { + lt: new Date(Date.now() - 15 * 60 * 1000), + }, + }, + }) + Logger.log( + `Found ${payments.length} payments to update`, + 'checkPaymentStatus', + ) + } +} diff --git a/src/app.module.ts b/src/app.module.ts index e25a0d4..44b6f69 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -4,6 +4,7 @@ import { GraphqlModule } from './Graphql/graphql.module' import { MailModule } from './Mail/mail.module' import { Module } from '@nestjs/common' import { RestfulModule } from './Restful/restful.module' +import { CronModule } from './Cron/cron.module' // import { LiveKitModule } from './LiveKit/livekit.module' @Module({ @@ -15,6 +16,7 @@ import { RestfulModule } from './Restful/restful.module' MailModule, GraphqlModule, RestfulModule, + CronModule, // LiveKitModule, ], })