feat(admin): wallet adjustment endpoint and atomic prisma wallet topup transaction [TASK-26.14] [TASK-26.15] [TASK-26.16]

This commit is contained in:
parsa aghaei committed 2026-08-05 14:40:35 +03:30
1 parent 3497a743b3
commit d94b33f96d
3 files changed
+59 -3

No files matched your search

+17
View File
@@ -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: 'لیست محصولات (مدیریت)' })
+39
View File
@@ -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 },
+3 -3
View File
@@ -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.