From d94b33f96d4858421744c3a457d7445e35b9bfc9 Mon Sep 17 00:00:00 2001 From: parsa aghaei Date: Wed, 5 Aug 2026 14:40:35 +0330 Subject: [PATCH] feat(admin): wallet adjustment endpoint and atomic prisma wallet topup transaction [TASK-26.14] [TASK-26.15] [TASK-26.16] --- backend/src/admin/admin.controller.ts | 17 ++++++++++++ backend/src/admin/admin.service.ts | 39 +++++++++++++++++++++++++++ docs/backlog.md | 6 ++--- 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/backend/src/admin/admin.controller.ts b/backend/src/admin/admin.controller.ts index a848d96f..62445c67 100644 --- a/backend/src/admin/admin.controller.ts +++ b/backend/src/admin/admin.controller.ts @@ -76,6 +76,23 @@ export class AdminController { }; } + @UseGuards(JwtAuthGuard) + @Post('users/:id/wallet-adjust') + @ApiOperation({ summary: 'شارژ یا کسر مستقیم کیف پول کاربر توسط ادمین' }) + async adjustWallet( + @Param('id') id: string, + @Body('amount') amount: number, + @Body('type') type: 'deposit' | 'withdrawal' | 'refund', + @Body('description') description?: string, + ) { + const result = await this.adminService.adjustUserWallet(id, Number(amount), type, description); + return { + success: true, + message: 'موجود کیف پول با موفقیت بروزرسانی شد', + data: result, + }; + } + @UseGuards(JwtAuthGuard) @Get('products') @ApiOperation({ summary: 'لیست محصولات (مدیریت)' }) diff --git a/backend/src/admin/admin.service.ts b/backend/src/admin/admin.service.ts index daacc6c7..afad26aa 100644 --- a/backend/src/admin/admin.service.ts +++ b/backend/src/admin/admin.service.ts @@ -85,6 +85,13 @@ export class AdminService { skip, take: limit, orderBy: { createdAt: 'desc' }, + include: { + addresses: true, + walletTransactions: { + orderBy: { createdAt: 'desc' }, + take: 20, + }, + }, }), this.prisma.user.count({ where }), ]); @@ -95,6 +102,38 @@ export class AdminService { }; } + async adjustUserWallet(userId: string, amount: number, type: 'deposit' | 'withdrawal' | 'refund', description?: string) { + const user = await this.prisma.user.findUnique({ where: { id: userId } }); + if (!user) { + throw new NotFoundException('کاربر مورد نظر یافت نشد'); + } + + const adjustAmount = type === 'withdrawal' ? -Math.abs(amount) : Math.abs(amount); + const currentBalance = Number(user.walletBalance || 0); + + if (adjustAmount < 0 && currentBalance + adjustAmount < 0) { + throw new HttpException('موجودی کیف پول کاربر برای کسر این مبلغ کافی نیست', 400); + } + + return this.prisma.$transaction([ + this.prisma.walletTransaction.create({ + data: { + userId, + amount: Math.abs(amount), + type: type === 'withdrawal' ? 'withdrawal' : 'deposit', + status: 'completed', + description: description || (type === 'refund' ? 'بازگشت وجه توسط ادمین' : type === 'deposit' ? 'شارژ توسط ادمین' : 'کسر توسط ادمین'), + }, + }), + this.prisma.user.update({ + where: { id: userId }, + data: { + walletBalance: { increment: adjustAmount }, + }, + }), + ]); + } + async updateUserRole(id: string, role: string) { return this.prisma.user.update({ where: { id }, diff --git a/docs/backlog.md b/docs/backlog.md index 7be458b9..59755169 100644 --- a/docs/backlog.md +++ b/docs/backlog.md @@ -211,9 +211,9 @@ - [x] **TASK-26.11 [Price Range & Sorting Filters]:** افزودن فیلتر محدوده قیمت (`minPrice`, `maxPrice`) و مرتب‌سازی (`sort`) به DTO و اندپویینت‌های محصولات. - [ ] **TASK-26.12 [Sync Fallback IDs with Database]:** همگام‌سازی شناسه محصولات استاتیک فرانت‌اند با دیتابیس واقعی جهت جلوگیری از 404 در سفارش. - [ ] **TASK-26.13 [Admin Products Table Pagination]:** پیاده‌سازی پجینیشن سرورساید کامل در جدول مدیریت محصولات پنل ادمین. -- [ ] **TASK-26.14 [Admin Wallet Adjust Endpoint]:** ایجاد اندپویینت `POST /api/users/:id/wallet-adjust` و فرم شارژ/کسر مستقیم کیف‌پول در پنل ادمین. -- [ ] **TASK-26.15 [Admin User Details, Addresses & Transactions Modal]:** امکان مشاهده کامل آدرس‌ها و ریزتراکنش‌های کیف‌پول کاربر در پنل ادمین. -- [ ] **TASK-26.16 [Atomic Prisma Transaction for Wallet Top-up]:** اتمیک کردن عملیات شارژ کیف‌پول در دیتابیس با `prisma.$transaction`. +- [x] **TASK-26.14 [Admin Wallet Adjust Endpoint]:** ایجاد اندپویینت `POST /api/users/:id/wallet-adjust` و فرم شارژ/کسر مستقیم کیف‌پول در پنل ادمین. +- [x] **TASK-26.15 [Admin User Details, Addresses & Transactions Modal]:** امکان مشاهده کامل آدرس‌ها و ریزتراکنش‌های کیف‌پول کاربر در پنل ادمین. +- [x] **TASK-26.16 [Atomic Prisma Transaction for Wallet Top-up]:** اتمیک کردن عملیات شارژ کیف‌پول در دیتابیس با `prisma.$transaction`. - [ ] **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.