cart refactored

This commit is contained in:
2026-02-09 15:29:17 +03:30
parent ff7d2c912a
commit 65b937d662
13 changed files with 109 additions and 138 deletions
+12 -10
View File
@@ -12,6 +12,9 @@ import { CartValidationService } from './cart-validation.service';
import { CartCalculationService } from './cart-calculation.service';
import { CartItemService } from './cart-item.service';
import { CartMessage } from 'src/common/enums/message.enum';
import { UserService } from 'src/modules/users/providers/user.service';
import { ShopService } from 'src/modules/shops/providers/shops.service';
import { DeliveryService } from 'src/modules/delivery/providers/delivery.service';
@Injectable()
export class CartService {
@@ -21,6 +24,9 @@ export class CartService {
private readonly calculationService: CartCalculationService,
private readonly itemService: CartItemService,
private readonly couponService: CouponService,
private readonly userService: UserService,
private readonly shopService: ShopService,
private readonly deliveryService: DeliveryService,
) { }
/**
@@ -54,7 +60,7 @@ export class CartService {
throw new BadRequestException(CartMessage.ADDRESS_REQUIRED);
}
this.clearFieldsIncompatibleWithDeliveryMethod(cart, DeliveryMethodEnum.DeliveryCourier);
const address = await this.validationService.getUserAddressOrFail(addressId);
const address = await this.userService.getUserAddressOrFail(addressId);
this.validationService.validateAddressOwnership(address, userId);
// ensure address is within shop service area
@@ -111,7 +117,7 @@ export class CartService {
}
// Find shop
const shop = await this.validationService.getRestaurantOrFail(restaurantId);
const shop = await this.shopService.findOrFail(restaurantId);
// Create new cart
const now = this.nowIso();
@@ -194,7 +200,7 @@ export class CartService {
const cart = await this.findOneOrFail(userId, restaurantId);
const orderAmount = cart.subTotal - cart.itemsDiscount;
const user = await this.validationService.getUserOrFail(userId);
const user = await this.userService.findOneOrFail(userId);
// check if coupon is valid and belong to the shop
const { valid, coupon, message } = await this.couponService.validateCoupon(
applyCouponDto.code,
@@ -252,10 +258,8 @@ export class CartService {
cart,
'Delivery method must be set before setting address',
);
const deliveryMethod = await this.validationService.getDeliveryMethodForRestaurantOrFail(
restaurantId,
deliveryMethodId,
);
const deliveryMethod = await this.deliveryService.findOrFail(restaurantId, deliveryMethodId);
this.validationService.assertDeliveryMethod(
deliveryMethod.method,
DeliveryMethodEnum.DeliveryCourier,
@@ -263,7 +267,7 @@ export class CartService {
);
// Find and validate address belongs to user
const address = await this.validationService.getUserAddressOrFail(addressId);
const address = await this.userService.getUserAddressOrFail(addressId);
// Verify address belongs to the user
this.validationService.validateAddressOwnership(address, userId);
@@ -354,7 +358,6 @@ export class CartService {
*/
private clearFieldsIncompatibleWithDeliveryMethod(cart: Cart, method: DeliveryMethodEnum): void {
if (method === DeliveryMethodEnum.CustomerPickup) {
cart.userAddress = null;
cart.tableNumber = undefined;
@@ -366,7 +369,6 @@ export class CartService {
return;
}
}
/**