implement cron mechanic for refund strageries and make workshop reliable

This commit is contained in:
2024-11-26 17:31:40 +07:00
parent 830c9ce3f6
commit 3ac8c3fd39
3 changed files with 70 additions and 27 deletions

View File

@@ -1,7 +1,7 @@
import { Injectable, Logger } from '@nestjs/common'
import { Cron } from '@nestjs/schedule'
import { CronExpression } from '@nestjs/schedule'
import { PaymentStatus, ScheduleDateStatus } from '@prisma/client'
import { OrderStatus, PaymentStatus, ScheduleDateStatus, ScheduleStatus } from '@prisma/client'
import { PrismaService } from 'src/Prisma/prisma.service'
@Injectable()
@@ -18,33 +18,23 @@ export class CronService {
lt: new Date(),
},
status: {
notIn: [
ScheduleDateStatus.COMPLETED,
ScheduleDateStatus.MISSING_MENTOR,
ScheduleDateStatus.MISSING_CUSTOMER,
],
notIn: [ScheduleDateStatus.COMPLETED, ScheduleDateStatus.MISSING_MENTOR, ScheduleDateStatus.MISSING_CUSTOMER],
},
},
})
Logger.log(
`Found ${schedules.length} schedules to update`,
'checkScheduleDateStatus',
)
Logger.log(`Found ${schedules.length} schedules to update`, 'checkScheduleDateStatus')
// get all collaboration sessions
const collaborationSessions =
await this.prisma.collaborationSession.findMany({
where: {
collaboratorsIds: {
hasEvery: schedules.map((s) => s.id),
},
const collaborationSessions = await this.prisma.collaborationSession.findMany({
where: {
collaboratorsIds: {
hasEvery: schedules.map((s) => s.id),
},
})
},
})
const updates = schedules
.map((schedule) => {
const collaborationSession = collaborationSessions.find(
(session) => session.scheduleDateId === schedule.id,
)
const collaborationSession = collaborationSessions.find((session) => session.scheduleDateId === schedule.id)
if (!collaborationSession) {
return {
@@ -91,10 +81,7 @@ export class CronService {
},
},
})
Logger.log(
`Found ${payments.length} payments to update`,
'checkPaymentStatus',
)
Logger.log(`Found ${payments.length} payments to update`, 'checkPaymentStatus')
for (const payment of payments) {
await this.prisma.payment.update({
where: { id: payment.id },
@@ -102,4 +89,45 @@ 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() {
Logger.log('Handling refund ticket', 'handleRefundTicket')
// get all orders where status is PENDING_REFUND or REFUNDED and has schedule.dates in future
const orders = await this.prisma.order.findMany({
where: {
status: { in: [OrderStatus.PENDING_REFUND, OrderStatus.REFUNDED] },
schedule: {
dates: {
some: {
end: {
gt: new Date(),
},
},
},
},
},
include: {
schedule: {
include: {
dates: true,
},
},
},
})
Logger.log(`Found ${orders.length} orders to handle`, 'handleRefundTicket')
for (const order of orders) {
await this.prisma.schedule.update({
where: { id: order.scheduleId },
data: { status: ScheduleStatus.REFUNDED },
})
}
// remove schedule date in future
for (const order of orders) {
await this.prisma.scheduleDate.deleteMany({
where: { id: { in: order.schedule.dates.map((d) => d.id) } },
})
}
}
}

View File

@@ -3,6 +3,7 @@ import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-cor
import { Builder } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service'
import { MinioService } from 'src/Minio/minio.service'
import { Role } from '@prisma/client'
@Injectable()
export class WorkshopSchema extends PothosSchema {
@@ -115,7 +116,7 @@ export class WorkshopSchema extends PothosSchema {
type: this.workshop(),
args: {
input: t.arg({
type: this.builder.generator.getCreateInput('Workshop'),
type: this.builder.generator.getCreateInput('Workshop', ['workshopMeetingRoom']),
required: true,
}),
},
@@ -123,11 +124,25 @@ export class WorkshopSchema extends PothosSchema {
resolve: async (query, _root, args, ctx, _info) => {
if (ctx.isSubscription) throw new Error('Workshops cannot be created in subscription context')
if (!ctx.http.me) throw new Error('User is not authenticated to create a workshop')
if (ctx.http.me.role !== Role.CENTER_OWNER) throw new Error('Only center owners can create workshops')
if (!args.input.service.connect) throw new Error('Service is required to create a workshop')
// check if service is active
const service = await this.prisma.service.findUnique({
where: { id: args.input.service.connect.id },
})
if (!service || !service.isActive) throw new Error('Service is not active')
return await this.prisma.workshop.create({
const workshop = await this.prisma.workshop.create({
...query,
data: args.input,
})
// create workshop meeting room
await this.prisma.workshopMeetingRoom.create({
data: {
workshopId: workshop.id,
},
})
return workshop
},
}),

File diff suppressed because one or more lines are too long