update manyyyyyyyyy

This commit is contained in:
2024-11-22 15:06:49 +07:00
parent 8eec1bed55
commit d8a3894aba
10 changed files with 272 additions and 101 deletions

View File

@@ -2,8 +2,20 @@ import { Inject, Injectable, Logger } from '@nestjs/common'
import { PrismaService } from '../Prisma/prisma.service'
import PayOS from '@payos/node'
import type { CheckoutRequestType, CheckoutResponseDataType, WebhookType, WebhookDataType, CancelPaymentLinkRequestType, DataType } from '@payos/node/lib/type'
import { ChatRoomType, OrderStatus, PaymentStatus, ScheduleStatus } from '@prisma/client'
import type {
CheckoutRequestType,
CheckoutResponseDataType,
WebhookType,
WebhookDataType,
CancelPaymentLinkRequestType,
DataType,
} from '@payos/node/lib/type'
import {
ChatRoomType,
OrderStatus,
PaymentStatus,
ScheduleStatus,
} from '@prisma/client'
export type CreatePaymentBody = CheckoutRequestType
export type CreatePaymentResponse = CheckoutResponseDataType
@Injectable()
@@ -33,7 +45,8 @@ export class PayosService {
Logger.error(`Invalid checksum: ${JSON.stringify(data)}`)
throw new Error('Invalid checksum')
}
const paymentStatus = paymentData.code === '00' ? PaymentStatus.PAID : PaymentStatus.CANCELLED
const paymentStatus =
paymentData.code === '00' ? PaymentStatus.PAID : PaymentStatus.CANCELLED
/* ---------------------------- begin transaction --------------------------- */
try {
await this.prisma.$transaction(async (tx) => {
@@ -44,7 +57,10 @@ export class PayosService {
status: paymentStatus,
},
})
const orderStatus = paymentStatus === PaymentStatus.PAID ? OrderStatus.PAID : OrderStatus.FAILED
const orderStatus =
paymentStatus === PaymentStatus.PAID
? OrderStatus.PAID
: OrderStatus.FAILED
// update order status
await tx.order.update({
where: { id: payment.orderId },
@@ -78,7 +94,7 @@ export class PayosService {
})
const centerId = orderService.centerId
// create chatroom for support
await tx.chatRoom.create({
const chatRoom = await tx.chatRoom.create({
data: {
type: ChatRoomType.SUPPORT,
customerId: order.userId,
@@ -86,6 +102,13 @@ export class PayosService {
mentorId: mentorId,
},
})
// update order chatRoomId
await tx.order.update({
where: { id: order.id },
data: {
chatRoomId: chatRoom.id,
},
})
// update orderId for schedule dates
await tx.scheduleDate.updateMany({
where: { scheduleId: schedule?.id },
@@ -115,7 +138,10 @@ export class PayosService {
return await this.payos.getPaymentLinkInformation(orderId)
}
async cancelPaymentURL(orderId: string | number, cancellationReason?: string) {
async cancelPaymentURL(
orderId: string | number,
cancellationReason?: string,
) {
return await this.payos.cancelPaymentLink(orderId, cancellationReason)
}