feat: complete payment retry, reviews system, catalog mode enforcement, and UI text defaults
Deploy Canina / deploy (push) Successful in 1m48s
Deploy Canina / deploy (push) Successful in 1m48s
This commit is contained in:
1 parent
e1b035d6dd
commit
2fca778930
24 files changed
+2297
-242
No files matched your search
@@ -186,6 +186,19 @@ export class OrdersController {
|
||||
},
|
||||
},
|
||||
})
|
||||
@Post(':id/retry-payment')
|
||||
@ApiOperation({ summary: 'پرداخت مجدد و تکمیل سفارش با امکان انتخاب روش پرداخت' })
|
||||
@ApiOkResponse({
|
||||
description: 'درخواست پرداخت مجدد با موفقیت پردازش شد',
|
||||
})
|
||||
retryPayment(
|
||||
@Req() req: { user: { id: string } },
|
||||
@Param('id') id: string,
|
||||
@Body() body: { paymentMethod?: string },
|
||||
) {
|
||||
return this.ordersService.retryPayment(req.user.id, id, body?.paymentMethod);
|
||||
}
|
||||
|
||||
findOne(@Req() req: { user: { id: string } }, @Param('id') id: string) {
|
||||
return this.ordersService.findOne(id, req.user.id);
|
||||
}
|
||||
|
||||
@@ -20,10 +20,8 @@ export class OrdersService {
|
||||
) {}
|
||||
|
||||
private generateTrackingNumber(): string {
|
||||
const date = new Date();
|
||||
const dateStr = `${date.getFullYear()}${String(date.getMonth() + 1).padStart(2, '0')}${String(date.getDate()).padStart(2, '0')}`;
|
||||
const random = Math.floor(10000 + Math.random() * 90000);
|
||||
return `CN-${dateStr}-${random}`;
|
||||
return `CN-${random}`;
|
||||
}
|
||||
|
||||
async validateCoupon(
|
||||
@@ -101,6 +99,17 @@ export class OrdersService {
|
||||
}
|
||||
|
||||
async create(userId: string, createOrderDto: CreateOrderDto) {
|
||||
// Check catalog mode settings
|
||||
const catalogSetting = await this.prisma.setting.findFirst({
|
||||
where: { key: { in: ['catalog_mode', 'catalog_disable_checkout'] } },
|
||||
});
|
||||
const catalogUiText = await this.prisma.uiText.findFirst({
|
||||
where: { key: { in: ['catalog_mode', 'catalog_disable_checkout'] }, value: 'true' },
|
||||
});
|
||||
if ((catalogSetting && String(catalogSetting.value) === 'true') || catalogUiText) {
|
||||
throw new BadRequestException('امکان ثبت سفارش به دلیل فعال بودن حالت کاتالوگ در حال حاضر وجود ندارد.');
|
||||
}
|
||||
|
||||
if (!createOrderDto.items || createOrderDto.items.length === 0) {
|
||||
throw new BadRequestException('سبد خرید خالی است');
|
||||
}
|
||||
@@ -430,4 +439,120 @@ export class OrdersService {
|
||||
executedAt: now,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retry or complete payment for an existing pending order with selectable payment method
|
||||
*/
|
||||
async retryPayment(
|
||||
userId: string,
|
||||
orderId: string,
|
||||
paymentMethod?: string,
|
||||
) {
|
||||
const order = await this.prisma.order.findFirst({
|
||||
where: { id: orderId, userId },
|
||||
include: { user: true },
|
||||
});
|
||||
|
||||
if (!order) {
|
||||
throw new NotFoundException('سفارش مورد نظر یافت نشد');
|
||||
}
|
||||
|
||||
if (['shipped', 'delivered'].includes(order.status)) {
|
||||
throw new BadRequestException('این سفارش قبلاً پرداخت و تکمیل شده است');
|
||||
}
|
||||
|
||||
const selectedMethod = paymentMethod || order.paymentMethod || 'online';
|
||||
const finalAmount = Number(order.totalAmount);
|
||||
|
||||
if (selectedMethod === 'wallet') {
|
||||
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(
|
||||
`موجودی کیف پول شما (${userBalance.toLocaleString('fa-IR')} تومان) کمتر از مبلغ سفارش (${finalAmount.toLocaleString('fa-IR')} تومان) است.`,
|
||||
);
|
||||
}
|
||||
|
||||
await 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: `پرداخت مجدد سفارش ${order.trackingNumber || order.id}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (Number(order.charityDonation) > 0) {
|
||||
await tx.user.update({
|
||||
where: { id: userId },
|
||||
data: { charityDonationTotal: { increment: Number(order.charityDonation) } },
|
||||
});
|
||||
}
|
||||
|
||||
await tx.order.update({
|
||||
where: { id: order.id },
|
||||
data: {
|
||||
status: 'processing',
|
||||
paymentMethod: 'wallet',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
if (user.mobile) {
|
||||
const customerName = user.firstName || 'مشتری';
|
||||
await this.smsService
|
||||
.sendOrderConfirmation(
|
||||
user.mobile,
|
||||
customerName,
|
||||
order.trackingNumber || order.id.slice(0, 8),
|
||||
finalAmount.toLocaleString('fa-IR'),
|
||||
)
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'پرداخت با موفقیت از طریق کیف پول انجام شد.',
|
||||
paymentMethod: 'wallet',
|
||||
orderId: order.id,
|
||||
};
|
||||
}
|
||||
|
||||
if (selectedMethod === 'card') {
|
||||
await this.prisma.order.update({
|
||||
where: { id: order.id },
|
||||
data: { paymentMethod: 'card', status: 'pending_payment' },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'روش پرداخت به کارت به کارت تغییر یافت. لطفاً پس از واریز فیش را ثبت فرمایید.',
|
||||
paymentMethod: 'card',
|
||||
orderId: order.id,
|
||||
};
|
||||
}
|
||||
|
||||
// Default: Online Gateway (Zibal)
|
||||
await this.prisma.order.update({
|
||||
where: { id: order.id },
|
||||
data: { paymentMethod: 'online', status: 'pending_payment' },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'سفارش آماده پرداخت آنلاین است.',
|
||||
paymentMethod: 'online',
|
||||
orderId: order.id,
|
||||
paymentInitiateRequired: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user