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:
1 parent
3497a743b3
commit
d94b33f96d
3 files changed
+59
-3
No files matched your search
@@ -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: 'لیست محصولات (مدیریت)' })
|
||||
|
||||
@@ -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 },
|
||||
|
||||
Reference in new issue
Block a user