fix(orders,wallet): implement full-stack wallet deduction in PostgreSQL and sync charity donation total in user profile

This commit is contained in:
parsa aghaei committed 2026-07-26 20:40:01 +03:30
1 parent 51b2f811fa
commit 377ec7fb34
7 files changed
+76 -18

No files matched your search

@@ -39,6 +39,11 @@ export class CreateOrderDto {
@IsOptional()
isRefill?: boolean;
@ApiPropertyOptional({ description: 'روش پرداخت (online, wallet)' })
@IsOptional()
@IsString()
paymentMethod?: string;
@ApiPropertyOptional({ description: 'دوره زمانی تمدید خودکار (روز)' })
@IsOptional()
@IsNumber()
+28 -1
View File
@@ -116,8 +116,35 @@ export class OrdersService {
const finalAmount = Math.max(totalAmount - discountAmount - refillDiscount + charityAmount, 0);
const trackingNumber = this.generateTrackingNumber();
// Deduct user wallet balance if payment method is wallet
if (createOrderDto.paymentMethod === 'wallet' && userId) {
const user = await this.prisma.user.findUnique({ where: { id: userId } });
if (!user) {
throw new NotFoundException('کاربر یافت نشد');
}
const userBalance = Number(user.walletBalance || 0);
if (userBalance < finalAmount) {
throw new BadRequestException('موجودی کیف پول برای پرداخت این سفارش کافی نیست');
}
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}`
}
});
}
// Increment user charity total if charity donation was added
if (charityAmount > 0) {
if (charityAmount > 0 && userId) {
try {
await this.prisma.user.update({
where: { id: userId },