feat(orders): atomic order transaction and refill cron job handler [TASK-26.19]
This commit is contained in:
1 parent
cbcff26e63
commit
8a05854aa0
2 files changed
+75
-16
No files matched your search
@@ -135,7 +135,7 @@ export class OrdersService {
|
||||
|
||||
const trackingNumber = this.generateTrackingNumber();
|
||||
|
||||
// Deduct user wallet balance if payment method is wallet
|
||||
// Deduct user wallet balance and create order atomically
|
||||
if (createOrderDto.paymentMethod === 'wallet' && userId) {
|
||||
const user = await this.prisma.user.findUnique({ where: { id: userId } });
|
||||
if (!user) {
|
||||
@@ -147,20 +147,52 @@ export class OrdersService {
|
||||
'موجودی کیف پول برای پرداخت این سفارش کافی نیست',
|
||||
);
|
||||
}
|
||||
await this.prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: {
|
||||
walletBalance: { decrement: finalAmount },
|
||||
},
|
||||
});
|
||||
await this.prisma.walletTransaction.create({
|
||||
data: {
|
||||
userId,
|
||||
amount: finalAmount,
|
||||
type: 'withdrawal',
|
||||
status: 'completed',
|
||||
description: `پرداخت سفارش ${trackingNumber}`,
|
||||
},
|
||||
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
await tx.user.update({
|
||||
where: { id: userId },
|
||||
data: {
|
||||
walletBalance: { decrement: finalAmount },
|
||||
},
|
||||
});
|
||||
|
||||
await tx.walletTransaction.create({
|
||||
data: {
|
||||
userId,
|
||||
amount: finalAmount,
|
||||
type: 'withdrawal',
|
||||
status: 'completed',
|
||||
description: `پرداخت سفارش ${trackingNumber}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (charityAmount > 0) {
|
||||
await tx.user.update({
|
||||
where: { id: userId },
|
||||
data: { charityDonationTotal: { increment: charityAmount } },
|
||||
});
|
||||
}
|
||||
|
||||
return tx.order.create({
|
||||
data: {
|
||||
userId,
|
||||
couponId,
|
||||
totalAmount: finalAmount,
|
||||
charityDonation: charityAmount,
|
||||
isRefill: Boolean(createOrderDto.isRefill),
|
||||
refillIntervalDays: createOrderDto.refillIntervalDays || 60,
|
||||
trackingNumber,
|
||||
status: 'processing',
|
||||
orderItems: {
|
||||
create: orderItems,
|
||||
},
|
||||
} as any,
|
||||
include: {
|
||||
orderItems: {
|
||||
include: { product: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -283,4 +315,31 @@ export class OrdersService {
|
||||
}
|
||||
return order;
|
||||
}
|
||||
|
||||
async checkAndSendRefillReminders() {
|
||||
const fiftyEightDaysAgo = new Date(Date.now() - 58 * 24 * 60 * 60 * 1000);
|
||||
const fiftyNineDaysAgo = new Date(Date.now() - 59 * 24 * 60 * 60 * 1000);
|
||||
|
||||
const dueRefillOrders = await this.prisma.order.findMany({
|
||||
where: {
|
||||
isRefill: true,
|
||||
createdAt: {
|
||||
gte: fiftyNineDaysAgo,
|
||||
lte: fiftyEightDaysAgo,
|
||||
},
|
||||
},
|
||||
include: { user: true },
|
||||
});
|
||||
|
||||
for (const order of dueRefillOrders) {
|
||||
if (order.user?.mobile) {
|
||||
await this.smsService
|
||||
.sendSms(
|
||||
order.user.mobile,
|
||||
`کاربر گرامی، مکمل درمانی پت شما در حال اتمام است. جهت شارژ مجدد و استفاده از ۵٪ تخفیف دوره جدید به لینک زیر مراجعه کنید:\nhttps://canina-iran.com/shop`,
|
||||
)
|
||||
.catch(() => {});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user