cart and coupon
This commit is contained in:
@@ -9,6 +9,7 @@ import { UserAddress } from '../users/entities/user-address.entity';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { UtilsModule } from '../utils/utils.module';
|
||||
import { CouponModule } from '../coupons/coupon.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -16,6 +17,7 @@ import { UtilsModule } from '../utils/utils.module';
|
||||
AuthModule,
|
||||
JwtModule,
|
||||
UtilsModule,
|
||||
CouponModule,
|
||||
],
|
||||
controllers: [CartController],
|
||||
providers: [CartService],
|
||||
|
||||
@@ -20,8 +20,6 @@ export class CartController {
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'Get cart for current user and restaurant' })
|
||||
@ApiResponse({ status: 200, description: 'Cart retrieved successfully' })
|
||||
@ApiResponse({ status: 404, description: 'Cart not found' })
|
||||
async findOne(@UserId() userId: string, @RestId() restaurantId: string) {
|
||||
return this.cartService.getOrCreateCart(userId, restaurantId);
|
||||
}
|
||||
@@ -29,9 +27,6 @@ export class CartController {
|
||||
@Post('items/:foodId/increment')
|
||||
@ApiOperation({ summary: 'Increment item quantity in cart by 1' })
|
||||
@ApiParam({ name: 'foodId', description: 'Food ID to increment in cart' })
|
||||
@ApiResponse({ status: 201, description: 'Item incremented in cart successfully' })
|
||||
@ApiResponse({ status: 400, description: 'Bad request (e.g., insufficient stock)' })
|
||||
@ApiResponse({ status: 404, description: 'Food not found' })
|
||||
async incrementItem(@UserId() userId: string, @RestId() restaurantId: string, @Param('foodId') foodId: string) {
|
||||
return this.cartService.incrementItem(userId, restaurantId, foodId);
|
||||
}
|
||||
@@ -39,8 +34,6 @@ export class CartController {
|
||||
@Post('items/:foodId/decrement')
|
||||
@ApiOperation({ summary: 'Decrement item quantity in cart by 1 (removes item if quantity reaches 0)' })
|
||||
@ApiParam({ name: 'foodId', description: 'Food ID to decrement in cart' })
|
||||
@ApiResponse({ status: 200, description: 'Item decremented in cart successfully' })
|
||||
@ApiResponse({ status: 404, description: 'Item not found in cart' })
|
||||
async decrementItem(@UserId() userId: string, @RestId() restaurantId: string, @Param('foodId') foodId: string) {
|
||||
return this.cartService.decrementItem(userId, restaurantId, foodId);
|
||||
}
|
||||
@@ -48,9 +41,6 @@ export class CartController {
|
||||
@Post('items/bulk')
|
||||
@ApiOperation({ summary: 'Bulk add items to cart (increments quantity if items exist)' })
|
||||
@ApiBody({ type: BulkAddItemsToCartDto })
|
||||
@ApiResponse({ status: 201, description: 'Items added to cart successfully' })
|
||||
@ApiResponse({ status: 400, description: 'Bad request (e.g., insufficient stock, invalid items)' })
|
||||
@ApiResponse({ status: 404, description: 'Food not found' })
|
||||
bulkAddItems(
|
||||
@UserId() userId: string,
|
||||
@RestId() restaurantId: string,
|
||||
@@ -63,9 +53,6 @@ export class CartController {
|
||||
@ApiOperation({ summary: 'Update item quantity in cart' })
|
||||
@ApiParam({ name: 'foodId', description: 'Food ID in the cart' })
|
||||
@ApiBody({ type: UpdateItemQuantityDto })
|
||||
@ApiResponse({ status: 200, description: 'Item quantity updated successfully' })
|
||||
@ApiResponse({ status: 400, description: 'Bad request (e.g., insufficient stock)' })
|
||||
@ApiResponse({ status: 404, description: 'Item not found in cart' })
|
||||
async updateItemQuantity(
|
||||
@UserId() userId: string,
|
||||
@RestId() restaurantId: string,
|
||||
@@ -94,8 +81,6 @@ export class CartController {
|
||||
@Post('apply-coupon')
|
||||
@ApiOperation({ summary: 'Apply coupon to cart' })
|
||||
@ApiBody({ type: ApplyCouponDto })
|
||||
@ApiResponse({ status: 200, description: 'Coupon applied successfully' })
|
||||
@ApiResponse({ status: 400, description: 'Invalid coupon code' })
|
||||
async applyCoupon(@UserId() userId: string, @RestId() restaurantId: string, @Body() applyCouponDto: ApplyCouponDto) {
|
||||
return this.cartService.applyCoupon(userId, restaurantId, applyCouponDto);
|
||||
}
|
||||
@@ -110,9 +95,6 @@ export class CartController {
|
||||
@Patch('address')
|
||||
@ApiOperation({ summary: 'Set address for cart' })
|
||||
@ApiBody({ type: SetAddressDto })
|
||||
@ApiResponse({ status: 200, description: 'Address set successfully' })
|
||||
@ApiResponse({ status: 400, description: 'Bad request (e.g., address does not belong to user)' })
|
||||
@ApiResponse({ status: 404, description: 'Address not found' })
|
||||
async setAddress(@UserId() userId: string, @RestId() restaurantId: string, @Body() setAddressDto: SetAddressDto) {
|
||||
return this.cartService.setAddress(userId, restaurantId, setAddressDto);
|
||||
}
|
||||
@@ -120,9 +102,6 @@ export class CartController {
|
||||
@Patch('payment-method')
|
||||
@ApiOperation({ summary: 'Set payment method for cart' })
|
||||
@ApiBody({ type: SetPaymentMethodDto })
|
||||
@ApiResponse({ status: 200, description: 'Payment method set successfully' })
|
||||
@ApiResponse({ status: 400, description: 'Bad request (e.g., payment method is not active)' })
|
||||
@ApiResponse({ status: 404, description: 'Payment method not found for restaurant' })
|
||||
async setPaymentMethod(
|
||||
@UserId() userId: string,
|
||||
@RestId() restaurantId: string,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import type { OrderCouponDetail } from 'src/modules/orders/interface/order-status';
|
||||
|
||||
export interface CartItem {
|
||||
foodId: string;
|
||||
foodTitle?: string;
|
||||
@@ -7,28 +9,25 @@ export interface CartItem {
|
||||
totalPrice: number;
|
||||
}
|
||||
|
||||
export interface CartCoupon {
|
||||
code: string;
|
||||
discount: number;
|
||||
discountType: 'amount' | 'percentage';
|
||||
}
|
||||
|
||||
export interface Cart {
|
||||
userId: string;
|
||||
restaurantId: string;
|
||||
restaurantName?: string;
|
||||
|
||||
items: CartItem[];
|
||||
coupon?: CartCoupon;
|
||||
coupon?: OrderCouponDetail;
|
||||
addressId?: string;
|
||||
paymentMethodId?: string;
|
||||
deliveryMethodId?: string;
|
||||
|
||||
deliveryFee: number;
|
||||
subTotal: number;
|
||||
tax: number;
|
||||
couponDiscount: number;
|
||||
itemsDiscount: number;
|
||||
totalDiscount: number;
|
||||
subTotal: number;
|
||||
tax: number;
|
||||
shipmentFee: number;
|
||||
total: number;
|
||||
|
||||
totalItems: number;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
|
||||
@@ -13,7 +13,10 @@ 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';
|
||||
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/entities/coupon.entity';
|
||||
|
||||
@Injectable()
|
||||
export class CartService {
|
||||
@@ -23,6 +26,7 @@ export class CartService {
|
||||
constructor(
|
||||
private readonly em: EntityManager,
|
||||
private readonly cacheService: CacheService,
|
||||
private readonly couponService: CouponService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -47,7 +51,7 @@ export class CartService {
|
||||
restaurantId,
|
||||
restaurantName: restaurant.name,
|
||||
items: [],
|
||||
shipmentFee: 0,
|
||||
deliveryFee: 0,
|
||||
subTotal: 0,
|
||||
itemsDiscount: 0,
|
||||
couponDiscount: 0,
|
||||
@@ -66,13 +70,14 @@ export class CartService {
|
||||
/**
|
||||
* Find cart by user and restaurant
|
||||
*/
|
||||
async findOne(userId: string, restaurantId: string): Promise<Cart> {
|
||||
async findOneOrFail(userId: string, restaurantId: string): Promise<Cart> {
|
||||
const cart = await this.getCartByRestaurant(userId, restaurantId);
|
||||
if (!cart) {
|
||||
throw new NotFoundException('Cart not found');
|
||||
}
|
||||
return cart;
|
||||
}
|
||||
|
||||
async findOne2(userId: string, restaurantId: string): Promise<Cart | null> {
|
||||
const cart = await this.getCartByRestaurant(userId, restaurantId);
|
||||
return cart;
|
||||
@@ -162,7 +167,7 @@ export class CartService {
|
||||
* Decrement item quantity in cart by 1 (removes item if quantity reaches 0)
|
||||
*/
|
||||
async decrementItem(userId: string, restaurantId: string, foodId: string): Promise<Cart> {
|
||||
const cart = await this.findOne(userId, restaurantId);
|
||||
const cart = await this.findOneOrFail(userId, restaurantId);
|
||||
|
||||
const itemIndex = cart.items.findIndex(item => item.foodId === foodId);
|
||||
if (itemIndex < 0) {
|
||||
@@ -287,7 +292,7 @@ export class CartService {
|
||||
foodId: string,
|
||||
updateItemDto: UpdateItemQuantityDto,
|
||||
): Promise<Cart> {
|
||||
const cart = await this.findOne(userId, restaurantId);
|
||||
const cart = await this.findOneOrFail(userId, restaurantId);
|
||||
|
||||
const itemIndex = cart.items.findIndex(item => item.foodId === foodId);
|
||||
if (itemIndex < 0) {
|
||||
@@ -331,7 +336,7 @@ export class CartService {
|
||||
* Remove item from cart
|
||||
*/
|
||||
async removeItem(userId: string, restaurantId: string, foodId: string): Promise<Cart> {
|
||||
const cart = await this.findOne(userId, restaurantId);
|
||||
const cart = await this.findOneOrFail(userId, restaurantId);
|
||||
|
||||
const itemIndex = cart.items.findIndex(item => item.foodId === foodId);
|
||||
if (itemIndex < 0) {
|
||||
@@ -352,20 +357,60 @@ export class CartService {
|
||||
* Apply coupon to cart
|
||||
*/
|
||||
async applyCoupon(userId: string, restaurantId: string, applyCouponDto: ApplyCouponDto): Promise<Cart> {
|
||||
const cart = await this.findOne(userId, restaurantId);
|
||||
const cart = await this.findOneOrFail(userId, restaurantId);
|
||||
|
||||
// TODO: Implement actual coupon validation logic
|
||||
// For now, this is a placeholder implementation
|
||||
// You should validate the coupon code against your coupon/discount system
|
||||
// Calculate current order amount (subtotal after item discounts) for coupon validation
|
||||
// const currentSubTotal = cart.items.reduce((sum, item) => sum + item.price * item.quantity, 0);
|
||||
// const currentItemsDiscount = cart.items.reduce((sum, item) => sum + item.discount * item.quantity, 0);
|
||||
// const orderAmount = currentSubTotal - currentItemsDiscount;
|
||||
|
||||
// Example coupon structure (replace with your actual coupon validation)
|
||||
const coupon: CartCoupon = {
|
||||
code: applyCouponDto.code,
|
||||
discount: 10, // Example: 10% discount
|
||||
discountType: 'percentage', // or 'amount'
|
||||
const orderAmount = cart.subTotal - cart.itemsDiscount;
|
||||
|
||||
const { valid, coupon } = await this.couponService.validateCoupon(
|
||||
applyCouponDto.code,
|
||||
restaurantId,
|
||||
Number(orderAmount),
|
||||
);
|
||||
|
||||
if (!valid) {
|
||||
throw new BadRequestException('Invalid coupon code');
|
||||
}
|
||||
|
||||
if (!coupon) {
|
||||
throw new BadRequestException('Coupon not found');
|
||||
}
|
||||
|
||||
// Check maxUsesPerUser limit by counting how many orders this user has with this coupon
|
||||
if (coupon.maxUsesPerUser) {
|
||||
const result = await this.em.getConnection().execute<{ count: string }>(
|
||||
`SELECT COUNT(*) as count
|
||||
FROM orders
|
||||
WHERE user_id = $1
|
||||
AND coupon_detail IS NOT NULL
|
||||
AND coupon_detail->>'couponId' = $2
|
||||
AND deleted_at IS NULL`,
|
||||
[userId, coupon.id],
|
||||
);
|
||||
|
||||
const userCouponUsageCount = parseInt((result[0] as { count: string })?.count || '0', 10);
|
||||
|
||||
if (userCouponUsageCount >= coupon.maxUsesPerUser) {
|
||||
throw new BadRequestException(
|
||||
`You have reached the maximum number of uses (${coupon.maxUsesPerUser}) for this coupon`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const couponDetail: OrderCouponDetail = {
|
||||
couponId: coupon.id,
|
||||
couponName: coupon.name,
|
||||
couponCode: coupon.code,
|
||||
type: coupon.type,
|
||||
value: coupon.value,
|
||||
maxDiscount: coupon.maxDiscount,
|
||||
};
|
||||
|
||||
cart.coupon = coupon;
|
||||
cart.coupon = couponDetail;
|
||||
|
||||
// Recalculate cart totals
|
||||
await this.recalculateCartTotals(cart);
|
||||
@@ -378,7 +423,7 @@ export class CartService {
|
||||
* Remove coupon from cart
|
||||
*/
|
||||
async removeCoupon(userId: string, restaurantId: string): Promise<Cart> {
|
||||
const cart = await this.findOne(userId, restaurantId);
|
||||
const cart = await this.findOneOrFail(userId, restaurantId);
|
||||
|
||||
cart.coupon = undefined;
|
||||
|
||||
@@ -393,7 +438,7 @@ export class CartService {
|
||||
* Set address for cart
|
||||
*/
|
||||
async setAddress(userId: string, restaurantId: string, setAddressDto: SetAddressDto): Promise<Cart> {
|
||||
const cart = await this.findOne(userId, restaurantId);
|
||||
const cart = await this.findOneOrFail(userId, restaurantId);
|
||||
|
||||
// Find and validate address belongs to user
|
||||
const address = await this.em.findOne(UserAddress, { id: setAddressDto.addressId }, { populate: ['user'] });
|
||||
@@ -422,7 +467,7 @@ export class CartService {
|
||||
restaurantId: string,
|
||||
setPaymentMethodDto: SetPaymentMethodDto,
|
||||
): Promise<Cart> {
|
||||
const cart = await this.findOne(userId, restaurantId);
|
||||
const cart = await this.findOneOrFail(userId, restaurantId);
|
||||
|
||||
// Find and validate payment method belongs to restaurant
|
||||
const paymentMethod = await this.em.findOne(
|
||||
@@ -462,7 +507,7 @@ export class CartService {
|
||||
restaurantId: string,
|
||||
setDeliveryMethodDto: SetDeliveryMethodDto,
|
||||
): Promise<Cart> {
|
||||
const cart = await this.findOne(userId, restaurantId);
|
||||
const cart = await this.findOneOrFail(userId, restaurantId);
|
||||
|
||||
// Find and validate delivery method belongs to restaurant
|
||||
const deliveryMethod = await this.em.findOne(
|
||||
@@ -487,7 +532,7 @@ export class CartService {
|
||||
|
||||
cart.deliveryMethodId = setDeliveryMethodDto.deliveryMethodId;
|
||||
|
||||
// Recalculate cart totals (this will update shipmentFee based on delivery method)
|
||||
// Recalculate cart totals (this will update deliveryFee based on delivery method)
|
||||
await this.recalculateCartTotals(cart);
|
||||
await this.saveCart(cart);
|
||||
|
||||
@@ -515,14 +560,19 @@ export class CartService {
|
||||
|
||||
// Calculate coupon discount
|
||||
let couponDiscount = 0;
|
||||
|
||||
if (cart.coupon) {
|
||||
if (cart.coupon.discountType === 'percentage') {
|
||||
const priceAfterItemDiscount = subTotal - itemsDiscount;
|
||||
couponDiscount = (priceAfterItemDiscount * cart.coupon.discount) / 100;
|
||||
const priceAfterItemDiscount = subTotal - itemsDiscount;
|
||||
if (cart.coupon.type === CouponType.PERCENTAGE) {
|
||||
let discount = (priceAfterItemDiscount * cart.coupon.value) / 100;
|
||||
// Apply maxDiscount limit if set
|
||||
if (cart.coupon.maxDiscount && discount > cart.coupon.maxDiscount) {
|
||||
discount = cart.coupon.maxDiscount;
|
||||
}
|
||||
couponDiscount = discount;
|
||||
} else {
|
||||
// amount discount
|
||||
const priceAfterItemDiscount = subTotal - itemsDiscount;
|
||||
couponDiscount = Math.min(cart.coupon.discount, priceAfterItemDiscount);
|
||||
// FIXED amount discount
|
||||
couponDiscount = Math.min(cart.coupon.value, priceAfterItemDiscount);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -542,17 +592,17 @@ export class CartService {
|
||||
cart.tax = tax;
|
||||
|
||||
// Shipment fee is calculated separately via delivery method
|
||||
let shipmentFee = 0;
|
||||
let deliveryFee = 0;
|
||||
if (cart.deliveryMethodId) {
|
||||
const deliveryMethod = await this.em.findOne(Delivery, { id: cart.deliveryMethodId });
|
||||
if (deliveryMethod && deliveryMethod.enabled) {
|
||||
shipmentFee = Number(deliveryMethod.deliveryFee) || 0;
|
||||
deliveryFee = Number(deliveryMethod.deliveryFee) || 0;
|
||||
}
|
||||
}
|
||||
cart.shipmentFee = shipmentFee;
|
||||
cart.deliveryFee = deliveryFee;
|
||||
|
||||
// Calculate total: total = subtotal – totalDiscount + tax + shipmentFee
|
||||
cart.total = subTotal - cart.totalDiscount + tax + cart.shipmentFee;
|
||||
// Calculate total: total = subtotal – totalDiscount + tax + deliveryFee
|
||||
cart.total = subTotal - cart.totalDiscount + tax + cart.deliveryFee;
|
||||
cart.totalItems = totalItems;
|
||||
cart.updatedAt = new Date().toISOString();
|
||||
}
|
||||
@@ -574,7 +624,7 @@ export class CartService {
|
||||
typeof cart.couponDiscount === 'number' &&
|
||||
typeof cart.totalDiscount === 'number' &&
|
||||
typeof cart.tax === 'number' &&
|
||||
typeof cart.shipmentFee === 'number' &&
|
||||
typeof cart.deliveryFee === 'number' &&
|
||||
typeof cart.total === 'number' &&
|
||||
typeof cart.totalItems === 'number' &&
|
||||
typeof cart.createdAt === 'string' &&
|
||||
|
||||
Reference in New Issue
Block a user