update sth :)
This commit is contained in:
@@ -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 { OrderStatus, PaymentStatus, RefundTicketStatus, Role } from '@prisma/client'
|
||||
import { DateTimeUtils } from 'src/common/utils/datetime.utils'
|
||||
|
||||
@Injectable()
|
||||
export class RefundTicketSchema extends PothosSchema {
|
||||
@@ -107,12 +108,20 @@ export class RefundTicketSchema extends PothosSchema {
|
||||
if (!order.total || order.total === 0) {
|
||||
throw new Error('Order total is null or free')
|
||||
}
|
||||
// calculate refund amount based on order time: if order is less than 24 hours, refund 100%, if more than 24 hours, less than 48 hours, refund 50%, if more than 72 hours, cannot refund
|
||||
const now = DateTimeUtils.now()
|
||||
const orderDate = DateTimeUtils.fromDate(order.createdAt)
|
||||
const diffTime = Math.abs(now.diff(orderDate).toMillis())
|
||||
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
|
||||
let refundAmount = 0
|
||||
if (diffDays < 24) refundAmount = order.total
|
||||
else if (diffDays < 48) refundAmount = order.total * 0.5
|
||||
// create refund ticket
|
||||
const refundTicket = await this.prisma.refundTicket.create({
|
||||
data: {
|
||||
orderId: order.id,
|
||||
status: RefundTicketStatus.PENDING,
|
||||
amount: order.total,
|
||||
amount: refundAmount,
|
||||
},
|
||||
})
|
||||
return refundTicket
|
||||
|
||||
Reference in New Issue
Block a user