fix bugs
This commit is contained in:
@@ -714,6 +714,7 @@ export const enum CartMessage {
|
||||
DELIVERY_METHOD_NOT_FOUND = 'روش ارسال یافت نشد',
|
||||
DELIVERY_METHOD_NOT_ENABLED = 'روش ارسال برای این فروشگاه فعال نیست',
|
||||
PAYMENT_METHOD_NOT_FOUND = 'روش پرداخت یافت نشد',
|
||||
PAYMENT_METHOD_NOT_BELONGS_TO_SHOP = 'روش پرداخت به این فروشگاه تعلق ندارد',
|
||||
PAYMENT_METHOD_NOT_ENABLED = 'روش پرداخت برای این رستوران فعال نیست',
|
||||
WALLET_NOT_FOUND = 'کیف پول کاربر یافت نشد',
|
||||
WALLET_INSUFFICIENT = 'موجودی کیف پول کافی نیست',
|
||||
|
||||
@@ -51,8 +51,8 @@ export class CartItemService {
|
||||
/**
|
||||
* Get item index in cart
|
||||
*/
|
||||
getItemIndex(cart: Cart, shopId: string): number {
|
||||
return cart.items.findIndex(item => item.productId === shopId);
|
||||
getItemIndex(cart: Cart, productId: string): number {
|
||||
return cart.items.findIndex(item => item.productId === productId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,8 +77,8 @@ export class CartItemService {
|
||||
/**
|
||||
* Remove item from cart
|
||||
*/
|
||||
removeItemOrFail(cart: Cart, shopId: string): void {
|
||||
const itemIndex = this.getItemIndex(cart, shopId);
|
||||
removeItemOrFail(cart: Cart, productId: string): void {
|
||||
const itemIndex = this.getItemIndex(cart, productId);
|
||||
if (itemIndex < 0) {
|
||||
throw new NotFoundException(CartMessage.ITEM_NOT_FOUND);
|
||||
}
|
||||
@@ -88,8 +88,8 @@ export class CartItemService {
|
||||
/**
|
||||
* Decrement item quantity or remove if quantity reaches 0
|
||||
*/
|
||||
async decrementOrRemoveItem(cart: Cart, shopId: string): Promise<void> {
|
||||
const itemIndex = this.getItemIndex(cart, shopId);
|
||||
async decrementOrRemoveItem(cart: Cart, productId: string): Promise<void> {
|
||||
const itemIndex = this.getItemIndex(cart, productId);
|
||||
if (itemIndex < 0) {
|
||||
throw new NotFoundException(CartMessage.ITEM_NOT_FOUND);
|
||||
}
|
||||
@@ -102,7 +102,7 @@ export class CartItemService {
|
||||
return;
|
||||
}
|
||||
|
||||
const product = await this.productService.findOrFail(shopId);
|
||||
const product = await this.productService.findOrFail(productId);
|
||||
cart.items[itemIndex] = this.buildCartItemFromFood(product, newQuantity, existingItem);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common';
|
||||
import { Injectable, BadRequestException } from '@nestjs/common';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { Product } from 'src/modules/products/entities/product.entity';
|
||||
import { UserAddress } from 'src/modules/users/entities/user-address.entity';
|
||||
@@ -85,11 +85,6 @@ export class CartValidationService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get delivery method for shop or throw if not found
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Get enabled delivery method or throw if not found or disabled
|
||||
*/
|
||||
@@ -126,7 +121,7 @@ export class CartValidationService {
|
||||
async getEnabledPaymentMethodOrFail(shopId: string, paymentMethodId: string): Promise<PaymentMethod> {
|
||||
const paymentMethod = await this.paymentMethodService.findOneOrFail(paymentMethodId)
|
||||
if (paymentMethod.shop.id !== shopId) {
|
||||
throw new BadRequestException('CartMessage.PAYMENT_METHOD_NOT_BELONGS_TO_SHOP');
|
||||
throw new BadRequestException(CartMessage.PAYMENT_METHOD_NOT_BELONGS_TO_SHOP);
|
||||
}
|
||||
if (!paymentMethod.enabled) {
|
||||
throw new BadRequestException(CartMessage.PAYMENT_METHOD_NOT_ENABLED);
|
||||
|
||||
@@ -89,7 +89,7 @@ export class CartService {
|
||||
paymentMethodId,
|
||||
);
|
||||
|
||||
// Recalculate totals first so wallet check uses up-to-date total (delivery method may have changed above).
|
||||
// Recalculate totals first so wallet check uses up-to-date total (delivery method/address may have changed above).
|
||||
await this.calculationService.recalculateCartTotals(cart);
|
||||
|
||||
if (paymentMethod.method === PaymentMethodEnum.Wallet) {
|
||||
@@ -103,8 +103,8 @@ export class CartService {
|
||||
cart.description = description;
|
||||
}
|
||||
|
||||
// Final recalculation + save and return cart
|
||||
return this.recalculateAndSaveCart(cart);
|
||||
// Save and return cart (totals already recalculated above)
|
||||
return this.saveTouchedCart(cart);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -290,7 +290,7 @@ export class CartService {
|
||||
fullName: `${address.user.firstName || ''} ${address.user.lastName || ''}`.trim(),
|
||||
phone: address.user.phone,
|
||||
};
|
||||
return this.saveTouchedCart(cart);
|
||||
return this.recalculateAndSaveCart(cart);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -307,6 +307,10 @@ export class CartService {
|
||||
shopId,
|
||||
paymentMethodId,
|
||||
);
|
||||
|
||||
// Recalculate totals first so wallet check uses up-to-date total
|
||||
await this.calculationService.recalculateCartTotals(cart);
|
||||
|
||||
if (paymentMethod.method === PaymentMethodEnum.Wallet) {
|
||||
await this.validationService.assertWalletHasEnoughBalance(userId, shopId, cart.total);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user