implement some cron task
This commit is contained in:
8
src/Cron/cron.module.ts
Normal file
8
src/Cron/cron.module.ts
Normal 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
89
src/Cron/cron.service.ts
Normal 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',
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
],
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user