implement some cron task

This commit is contained in:
2024-11-25 18:16:50 +07:00
parent dae2b0849f
commit 667dc9e09d
5 changed files with 147 additions and 1 deletions

48
package-lock.json generated
View File

@@ -26,6 +26,7 @@
"@nestjs/graphql": "^12.2.0", "@nestjs/graphql": "^12.2.0",
"@nestjs/jwt": "^10.2.0", "@nestjs/jwt": "^10.2.0",
"@nestjs/platform-express": "^10.0.0", "@nestjs/platform-express": "^10.0.0",
"@nestjs/schedule": "^4.1.1",
"@nestjs/swagger": "^7.4.2", "@nestjs/swagger": "^7.4.2",
"@payos/node": "^1.0.10", "@payos/node": "^1.0.10",
"@pothos/core": "^4.3.0", "@pothos/core": "^4.3.0",
@@ -5062,6 +5063,33 @@
"integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA==", "integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA==",
"license": "0BSD" "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": { "node_modules/@nestjs/schematics": {
"version": "10.2.3", "version": "10.2.3",
"resolved": "https://registry.npmjs.org/@nestjs/schematics/-/schematics-10.2.3.tgz", "resolved": "https://registry.npmjs.org/@nestjs/schematics/-/schematics-10.2.3.tgz",
@@ -6025,7 +6053,6 @@
"version": "3.4.2", "version": "3.4.2",
"resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-3.4.2.tgz", "resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-3.4.2.tgz",
"integrity": "sha512-TifLZlFudklWlMBfhubvgqTXRzLDI5pCbGa4P8a3wPyUQSW+1xQ5eDsreP9DWHX3tjq1ke96uYG/nwundroWcA==", "integrity": "sha512-TifLZlFudklWlMBfhubvgqTXRzLDI5pCbGa4P8a3wPyUQSW+1xQ5eDsreP9DWHX3tjq1ke96uYG/nwundroWcA==",
"dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/@types/methods": { "node_modules/@types/methods": {
@@ -9014,6 +9041,25 @@
"dev": true, "dev": true,
"license": "MIT" "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": { "node_modules/cross-fetch": {
"version": "3.1.8", "version": "3.1.8",
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.8.tgz", "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.8.tgz",

View File

@@ -48,6 +48,7 @@
"@nestjs/graphql": "^12.2.0", "@nestjs/graphql": "^12.2.0",
"@nestjs/jwt": "^10.2.0", "@nestjs/jwt": "^10.2.0",
"@nestjs/platform-express": "^10.0.0", "@nestjs/platform-express": "^10.0.0",
"@nestjs/schedule": "^4.1.1",
"@nestjs/swagger": "^7.4.2", "@nestjs/swagger": "^7.4.2",
"@payos/node": "^1.0.10", "@payos/node": "^1.0.10",
"@pothos/core": "^4.3.0", "@pothos/core": "^4.3.0",

8
src/Cron/cron.module.ts Normal file
View File

@@ -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 {}

89
src/Cron/cron.service.ts Normal file
View File

@@ -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',
)
}
}

View File

@@ -4,6 +4,7 @@ import { GraphqlModule } from './Graphql/graphql.module'
import { MailModule } from './Mail/mail.module' import { MailModule } from './Mail/mail.module'
import { Module } from '@nestjs/common' import { Module } from '@nestjs/common'
import { RestfulModule } from './Restful/restful.module' import { RestfulModule } from './Restful/restful.module'
import { CronModule } from './Cron/cron.module'
// import { LiveKitModule } from './LiveKit/livekit.module' // import { LiveKitModule } from './LiveKit/livekit.module'
@Module({ @Module({
@@ -15,6 +16,7 @@ import { RestfulModule } from './Restful/restful.module'
MailModule, MailModule,
GraphqlModule, GraphqlModule,
RestfulModule, RestfulModule,
CronModule,
// LiveKitModule, // LiveKitModule,
], ],
}) })