diff --git a/backend/src/orders/orders.service.ts b/backend/src/orders/orders.service.ts index a2fe2233..98ebf569 100644 --- a/backend/src/orders/orders.service.ts +++ b/backend/src/orders/orders.service.ts @@ -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(() => {}); + } + } + } } diff --git a/docs/backlog.md b/docs/backlog.md index 320ff8e1..b3eaa680 100644 --- a/docs/backlog.md +++ b/docs/backlog.md @@ -216,6 +216,6 @@ - [x] **TASK-26.16 [Atomic Prisma Transaction for Wallet Top-up]:** اتمیک کردن عملیات شارژ کیف‌پول در دیتابیس با `prisma.$transaction`. - [x] **TASK-26.17 [Admin Pet Health Records & Pagination]:** امکان مشاهده پرونده پزشکی، یادآورها و لاگ‌های سلامت پت در پنل ادمین همراه با پجینیشن کامل لیست پت‌ها. - [ ] **TASK-26.18 [Pet Image File Upload]:** امکان آپلود مستقیم فایل تصویر پت به سرور در فرم‌های فرانت‌اند. -- [ ] **TASK-26.19 [Atomic Order Creation Transaction & Refill Cron Job]:** اتمیک کردن ساخت سفارش با کیف‌پول در بک‌اند و ایجاد Cron Job روزانه برای پیامک خودکار یادآوری Refill. +- [x] **TASK-26.19 [Atomic Order Creation Transaction & Refill Cron Job]:** اتمیک کردن ساخت سفارش با کیف‌پول در بک‌اند و ایجاد Cron Job روزانه برای پیامک خودکار یادآوری Refill. - [ ] **TASK-26.20 [Reports Excel Export, Dashboard Live Chart & Video View Counter]:** افزودن قابلیت دانلود خروجی اکسل در صفحه گزارشات، اتصال نمودار داشبورد به دیتای واقعی و ارسال درخواست PATCH افزایش بازدید ویدیوها.