set delivery
This commit is contained in:
@@ -9,8 +9,10 @@ import { UpdateItemQuantityDto } from '../dto/update-item.dto';
|
||||
import { ApplyCouponDto } from '../dto/apply-coupon.dto';
|
||||
import { SetAddressDto } from '../dto/set-address.dto';
|
||||
import { SetPaymentMethodDto } from '../dto/set-payment-method.dto';
|
||||
import { SetDeliveryMethodDto } from '../dto/set-delivery-method.dto';
|
||||
import { UserAddress } from '../../users/entities/user-address.entity';
|
||||
import { PaymentMethod } from '../../payments/entities/payment-method.entity';
|
||||
import { Delivery } from '../../delivery/entities/delivery.entity';
|
||||
import { Cart, CartItem, CartCoupon } from '../interfaces/cart.interface';
|
||||
|
||||
@Injectable()
|
||||
@@ -452,6 +454,46 @@ export class CartService {
|
||||
return cart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set delivery method for cart
|
||||
*/
|
||||
async setDeliveryMethod(
|
||||
userId: string,
|
||||
restaurantId: string,
|
||||
setDeliveryMethodDto: SetDeliveryMethodDto,
|
||||
): Promise<Cart> {
|
||||
const cart = await this.findOne(userId, restaurantId);
|
||||
|
||||
// Find and validate delivery method belongs to restaurant
|
||||
const deliveryMethod = await this.em.findOne(
|
||||
Delivery,
|
||||
{
|
||||
id: setDeliveryMethodDto.deliveryMethodId,
|
||||
restaurant: { id: restaurantId },
|
||||
},
|
||||
{ populate: ['restaurant'] },
|
||||
);
|
||||
|
||||
if (!deliveryMethod) {
|
||||
throw new NotFoundException(
|
||||
`Delivery method with ID ${setDeliveryMethodDto.deliveryMethodId} not found for restaurant ${restaurantId}`,
|
||||
);
|
||||
}
|
||||
|
||||
// Verify delivery method is enabled
|
||||
if (!deliveryMethod.enabled) {
|
||||
throw new BadRequestException('Delivery method is not enabled for this restaurant');
|
||||
}
|
||||
|
||||
cart.deliveryMethodId = setDeliveryMethodDto.deliveryMethodId;
|
||||
|
||||
// Recalculate cart totals (this will update shipmentFee based on delivery method)
|
||||
await this.recalculateCartTotals(cart);
|
||||
await this.saveCart(cart);
|
||||
|
||||
return cart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculate cart totals (including coupon discount and tax)
|
||||
*/
|
||||
@@ -500,8 +542,14 @@ export class CartService {
|
||||
cart.tax = tax;
|
||||
|
||||
// Shipment fee is calculated separately via delivery method
|
||||
// For now, it's set to 0. It should be set when a delivery method is selected
|
||||
cart.shipmentFee = 0;
|
||||
let shipmentFee = 0;
|
||||
if (cart.deliveryMethodId) {
|
||||
const deliveryMethod = await this.em.findOne(Delivery, { id: cart.deliveryMethodId });
|
||||
if (deliveryMethod && deliveryMethod.enabled) {
|
||||
shipmentFee = Number(deliveryMethod.deliveryFee) || 0;
|
||||
}
|
||||
}
|
||||
cart.shipmentFee = shipmentFee;
|
||||
|
||||
// Calculate total: total = subtotal – totalDiscount + tax + shipmentFee
|
||||
cart.total = subTotal - cart.totalDiscount + tax + cart.shipmentFee;
|
||||
|
||||
Reference in New Issue
Block a user