set delivery
This commit is contained in:
@@ -5,6 +5,7 @@ import { UpdateItemQuantityDto } from '../dto/update-item.dto';
|
|||||||
import { ApplyCouponDto } from '../dto/apply-coupon.dto';
|
import { ApplyCouponDto } from '../dto/apply-coupon.dto';
|
||||||
import { SetAddressDto } from '../dto/set-address.dto';
|
import { SetAddressDto } from '../dto/set-address.dto';
|
||||||
import { SetPaymentMethodDto } from '../dto/set-payment-method.dto';
|
import { SetPaymentMethodDto } from '../dto/set-payment-method.dto';
|
||||||
|
import { SetDeliveryMethodDto } from '../dto/set-delivery-method.dto';
|
||||||
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiBody, ApiParam } from '@nestjs/swagger';
|
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiBody, ApiParam } from '@nestjs/swagger';
|
||||||
import { AuthGuard } from '../../auth/guards/auth.guard';
|
import { AuthGuard } from '../../auth/guards/auth.guard';
|
||||||
import { UserId } from 'src/common/decorators/user-id.decorator';
|
import { UserId } from 'src/common/decorators/user-id.decorator';
|
||||||
@@ -129,4 +130,15 @@ export class CartController {
|
|||||||
) {
|
) {
|
||||||
return this.cartService.setPaymentMethod(userId, restaurantId, setPaymentMethodDto);
|
return this.cartService.setPaymentMethod(userId, restaurantId, setPaymentMethodDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Patch('delivery-method')
|
||||||
|
@ApiOperation({ summary: 'Set delivery method for cart' })
|
||||||
|
@ApiBody({ type: SetDeliveryMethodDto })
|
||||||
|
setDeliveryMethod(
|
||||||
|
@UserId() userId: string,
|
||||||
|
@RestId() restaurantId: string,
|
||||||
|
@Body() setDeliveryMethodDto: SetDeliveryMethodDto,
|
||||||
|
) {
|
||||||
|
return this.cartService.setDeliveryMethod(userId, restaurantId, setDeliveryMethodDto);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
|
export class SetDeliveryMethodDto {
|
||||||
|
@ApiProperty({ description: 'Delivery method ID', example: '01ARZ3NDEKTSV4RRFFQ69G5FAV' })
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsString()
|
||||||
|
deliveryMethodId!: string;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -21,6 +21,7 @@ export interface Cart {
|
|||||||
coupon?: CartCoupon;
|
coupon?: CartCoupon;
|
||||||
addressId?: string;
|
addressId?: string;
|
||||||
paymentMethodId?: string;
|
paymentMethodId?: string;
|
||||||
|
deliveryMethodId?: string;
|
||||||
couponDiscount: number;
|
couponDiscount: number;
|
||||||
itemsDiscount: number;
|
itemsDiscount: number;
|
||||||
totalDiscount: number;
|
totalDiscount: number;
|
||||||
|
|||||||
@@ -9,8 +9,10 @@ import { UpdateItemQuantityDto } from '../dto/update-item.dto';
|
|||||||
import { ApplyCouponDto } from '../dto/apply-coupon.dto';
|
import { ApplyCouponDto } from '../dto/apply-coupon.dto';
|
||||||
import { SetAddressDto } from '../dto/set-address.dto';
|
import { SetAddressDto } from '../dto/set-address.dto';
|
||||||
import { SetPaymentMethodDto } from '../dto/set-payment-method.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 { UserAddress } from '../../users/entities/user-address.entity';
|
||||||
import { PaymentMethod } from '../../payments/entities/payment-method.entity';
|
import { PaymentMethod } from '../../payments/entities/payment-method.entity';
|
||||||
|
import { Delivery } from '../../delivery/entities/delivery.entity';
|
||||||
import { Cart, CartItem, CartCoupon } from '../interfaces/cart.interface';
|
import { Cart, CartItem, CartCoupon } from '../interfaces/cart.interface';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -452,6 +454,46 @@ export class CartService {
|
|||||||
return cart;
|
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)
|
* Recalculate cart totals (including coupon discount and tax)
|
||||||
*/
|
*/
|
||||||
@@ -500,8 +542,14 @@ export class CartService {
|
|||||||
cart.tax = tax;
|
cart.tax = tax;
|
||||||
|
|
||||||
// Shipment fee is calculated separately via delivery method
|
// 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
|
let shipmentFee = 0;
|
||||||
cart.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
|
// Calculate total: total = subtotal – totalDiscount + tax + shipmentFee
|
||||||
cart.total = subTotal - cart.totalDiscount + tax + cart.shipmentFee;
|
cart.total = subTotal - cart.totalDiscount + tax + cart.shipmentFee;
|
||||||
|
|||||||
Reference in New Issue
Block a user