diff --git a/src/modules/cart/providers/cart.service.ts b/src/modules/cart/providers/cart.service.ts index 01e7cbe..52001cc 100644 --- a/src/modules/cart/providers/cart.service.ts +++ b/src/modules/cart/providers/cart.service.ts @@ -20,6 +20,7 @@ import { Cart, CartItem } from '../interfaces/cart.interface'; import { CouponService } from 'src/modules/coupons/providers/coupon.service'; import { OrderCouponDetail } from 'src/modules/orders/interface/order-status'; import { CouponType } from 'src/modules/coupons/interface/coupon'; +import { UserWallet } from 'src/modules/users/entities/user-wallet.entity'; @Injectable() export class CartService { @@ -221,11 +222,30 @@ export class CartService { const cart = await this.findOneOrFail(userId, restaurantId); await this.getEnabledPaymentMethodOrFail(restaurantId, setPaymentMethodDto.paymentMethodId); - + await this.assertWalletHasEnoughBalance(userId, restaurantId,cart.total); cart.paymentMethodId = setPaymentMethodDto.paymentMethodId; return this.recalculateAndSaveCart(cart); } + private async assertWalletHasEnoughBalance( + userId: string, + restaurantId: string, + amount: number, + ): Promise { + const wallet = await this.em.findOne(UserWallet, { + user: { id: userId }, + restaurant: { id: restaurantId }, + }); + + if (!wallet) { + throw new BadRequestException('User wallet not found'); + } + + if (wallet.wallet < amount) { + throw new BadRequestException('User wallet is not enough'); + } + } + /** * Set delivery method for cart */