implement cron mechanic for refund strageries and make workshop reliable
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { Injectable, Logger } from '@nestjs/common'
|
import { Injectable, Logger } from '@nestjs/common'
|
||||||
import { Cron } from '@nestjs/schedule'
|
import { Cron } from '@nestjs/schedule'
|
||||||
import { CronExpression } 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'
|
import { PrismaService } from 'src/Prisma/prisma.service'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -18,33 +18,23 @@ export class CronService {
|
|||||||
lt: new Date(),
|
lt: new Date(),
|
||||||
},
|
},
|
||||||
status: {
|
status: {
|
||||||
notIn: [
|
notIn: [ScheduleDateStatus.COMPLETED, ScheduleDateStatus.MISSING_MENTOR, ScheduleDateStatus.MISSING_CUSTOMER],
|
||||||
ScheduleDateStatus.COMPLETED,
|
|
||||||
ScheduleDateStatus.MISSING_MENTOR,
|
|
||||||
ScheduleDateStatus.MISSING_CUSTOMER,
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
Logger.log(
|
Logger.log(`Found ${schedules.length} schedules to update`, 'checkScheduleDateStatus')
|
||||||
`Found ${schedules.length} schedules to update`,
|
|
||||||
'checkScheduleDateStatus',
|
|
||||||
)
|
|
||||||
// get all collaboration sessions
|
// get all collaboration sessions
|
||||||
const collaborationSessions =
|
const collaborationSessions = await this.prisma.collaborationSession.findMany({
|
||||||
await this.prisma.collaborationSession.findMany({
|
where: {
|
||||||
where: {
|
collaboratorsIds: {
|
||||||
collaboratorsIds: {
|
hasEvery: schedules.map((s) => s.id),
|
||||||
hasEvery: schedules.map((s) => s.id),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
})
|
},
|
||||||
|
})
|
||||||
|
|
||||||
const updates = schedules
|
const updates = schedules
|
||||||
.map((schedule) => {
|
.map((schedule) => {
|
||||||
const collaborationSession = collaborationSessions.find(
|
const collaborationSession = collaborationSessions.find((session) => session.scheduleDateId === schedule.id)
|
||||||
(session) => session.scheduleDateId === schedule.id,
|
|
||||||
)
|
|
||||||
|
|
||||||
if (!collaborationSession) {
|
if (!collaborationSession) {
|
||||||
return {
|
return {
|
||||||
@@ -91,10 +81,7 @@ export class CronService {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
Logger.log(
|
Logger.log(`Found ${payments.length} payments to update`, 'checkPaymentStatus')
|
||||||
`Found ${payments.length} payments to update`,
|
|
||||||
'checkPaymentStatus',
|
|
||||||
)
|
|
||||||
for (const payment of payments) {
|
for (const payment of payments) {
|
||||||
await this.prisma.payment.update({
|
await this.prisma.payment.update({
|
||||||
where: { id: payment.id },
|
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) } },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-cor
|
|||||||
import { Builder } from '../Graphql/graphql.builder'
|
import { Builder } from '../Graphql/graphql.builder'
|
||||||
import { PrismaService } from '../Prisma/prisma.service'
|
import { PrismaService } from '../Prisma/prisma.service'
|
||||||
import { MinioService } from 'src/Minio/minio.service'
|
import { MinioService } from 'src/Minio/minio.service'
|
||||||
|
import { Role } from '@prisma/client'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class WorkshopSchema extends PothosSchema {
|
export class WorkshopSchema extends PothosSchema {
|
||||||
@@ -115,7 +116,7 @@ export class WorkshopSchema extends PothosSchema {
|
|||||||
type: this.workshop(),
|
type: this.workshop(),
|
||||||
args: {
|
args: {
|
||||||
input: t.arg({
|
input: t.arg({
|
||||||
type: this.builder.generator.getCreateInput('Workshop'),
|
type: this.builder.generator.getCreateInput('Workshop', ['workshopMeetingRoom']),
|
||||||
required: true,
|
required: true,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
@@ -123,11 +124,25 @@ export class WorkshopSchema extends PothosSchema {
|
|||||||
resolve: async (query, _root, args, ctx, _info) => {
|
resolve: async (query, _root, args, ctx, _info) => {
|
||||||
if (ctx.isSubscription) throw new Error('Workshops cannot be created in subscription context')
|
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) 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,
|
...query,
|
||||||
data: args.input,
|
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
Reference in New Issue
Block a user