update order and fix approve center

This commit is contained in:
2024-10-27 18:00:26 +07:00
parent 71971d7ae4
commit a5e9ad5ac1
4 changed files with 51 additions and 15 deletions

View File

@@ -125,9 +125,32 @@ export class OrderSchema extends PothosSchema {
}),
},
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.order.create({
...query,
data: args.data,
return this.prisma.$transaction(async (prisma) => {
const order = await prisma.order.create({
...query,
data: args.data,
});
// check if service is valid
if (!args.data.service.connect) {
throw new Error('Service not found');
}
// check if service price is free
if (args.data.service.connect.price === 0) {
return order;
}
// generate payment code by prefix 'EPESS' + 6 hex digits
const paymentCode =
'EPESS' + Math.random().toString(16).slice(2, 8);
// create payment
await prisma.payment.create({
data: {
orderId: order.id,
amount: args.data.service.connect.price as number,
paymentCode: paymentCode,
expiredAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
},
});
return order;
});
},
}),