177 lines
6.6 KiB
TypeScript
177 lines
6.6 KiB
TypeScript
import { Controller, Get, Post, Body, Patch, Delete, UseGuards, Param } from '@nestjs/common';
|
|
import { CartService } from '../providers/cart.service';
|
|
import { BulkAddItemsToCartDto } from '../dto/bulk-add-items.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 { SetDescriptionDto } from '../dto/set-description.dto';
|
|
import { SetTableNumberDto } from '../dto/set-table-number.dto';
|
|
import { SetCarDeliveryDto } from '../dto/set-car-delivery.dto';
|
|
import { ApiTags, ApiOperation, ApiBearerAuth, ApiBody, ApiParam, ApiHeader } from '@nestjs/swagger';
|
|
import { AuthGuard } from '../../auth/guards/auth.guard';
|
|
import { UserId } from 'src/common/decorators/user-id.decorator';
|
|
import { RestId } from 'src/common/decorators/rest-id.decorator';
|
|
import { SetAllCartParmsDto } from '../dto/set-all-cart-params.dto';
|
|
|
|
const API_HEADER_SLUG = {
|
|
name: 'X-Slug',
|
|
required: true,
|
|
schema: {
|
|
type: 'string',
|
|
default: 'zhivan',
|
|
},
|
|
};
|
|
|
|
@UseGuards(AuthGuard)
|
|
@ApiBearerAuth()
|
|
@ApiTags('cart')
|
|
@Controller('public/cart')
|
|
export class CartController {
|
|
constructor(private readonly cartService: CartService) {}
|
|
|
|
@Get()
|
|
@ApiOperation({ summary: 'Get cart for current user and restaurant' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
async findOne(@UserId() userId: string, @RestId() restaurantId: string) {
|
|
return this.cartService.getOrCreateCart(userId, restaurantId);
|
|
}
|
|
|
|
@Post('items/:foodId/increment')
|
|
@ApiOperation({ summary: 'Increment item quantity in cart by 1' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiParam({ name: 'foodId', description: 'Food ID to increment in cart' })
|
|
async incrementItem(@UserId() userId: string, @RestId() restaurantId: string, @Param('foodId') foodId: string) {
|
|
return this.cartService.incrementItem(userId, restaurantId, foodId, 1);
|
|
}
|
|
|
|
@Post('items/:foodId/decrement')
|
|
@ApiOperation({ summary: 'Decrement item quantity in cart by 1 (removes item if quantity reaches 0)' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiParam({ name: 'foodId', description: 'Food ID to decrement in cart' })
|
|
async decrementItem(@UserId() userId: string, @RestId() restaurantId: string, @Param('foodId') foodId: string) {
|
|
return this.cartService.decrementItem(userId, restaurantId, foodId);
|
|
}
|
|
|
|
@Post('items/bulk')
|
|
@ApiOperation({ summary: 'Bulk add items to cart (increments quantity if items exist)' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiBody({ type: BulkAddItemsToCartDto })
|
|
bulkAddItems(
|
|
@UserId() userId: string,
|
|
@RestId() restaurantId: string,
|
|
@Body() bulkAddItemsDto: BulkAddItemsToCartDto,
|
|
) {
|
|
return this.cartService.bulkAddItems(userId, restaurantId, bulkAddItemsDto);
|
|
}
|
|
|
|
@Delete('items/:foodId')
|
|
@ApiOperation({ summary: 'Remove item from cart' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiParam({ name: 'foodId', description: 'Food ID in the cart' })
|
|
async removeItem(@UserId() userId: string, @RestId() restaurantId: string, @Param('foodId') foodId: string) {
|
|
return this.cartService.removeItem(userId, restaurantId, foodId);
|
|
}
|
|
|
|
@Delete()
|
|
@ApiOperation({ summary: 'Clear entire cart' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
async clearCart(@UserId() userId: string, @RestId() restaurantId: string) {
|
|
return this.cartService.clearCart(userId, restaurantId);
|
|
}
|
|
|
|
@Post('apply-coupon')
|
|
@ApiOperation({ summary: 'Apply coupon to cart' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiBody({ type: ApplyCouponDto })
|
|
async applyCoupon(@UserId() userId: string, @RestId() restaurantId: string, @Body() applyCouponDto: ApplyCouponDto) {
|
|
return this.cartService.applyCoupon(userId, restaurantId, applyCouponDto);
|
|
}
|
|
|
|
@Delete('coupon')
|
|
@ApiOperation({ summary: 'Remove coupon from cart' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
async removeCoupon(@UserId() userId: string, @RestId() restaurantId: string) {
|
|
return this.cartService.removeCoupon(userId, restaurantId);
|
|
}
|
|
|
|
@Patch('address')
|
|
@ApiOperation({ summary: 'Set address for cart' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiBody({ type: SetAddressDto })
|
|
async setAddress(@UserId() userId: string, @RestId() restaurantId: string, @Body() setAddressDto: SetAddressDto) {
|
|
return this.cartService.setAddress(userId, restaurantId, setAddressDto);
|
|
}
|
|
|
|
@Patch('payment-method')
|
|
@ApiOperation({ summary: 'Set payment method for cart' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiBody({ type: SetPaymentMethodDto })
|
|
async setPaymentMethod(
|
|
@UserId() userId: string,
|
|
@RestId() restaurantId: string,
|
|
@Body() setPaymentMethodDto: SetPaymentMethodDto,
|
|
) {
|
|
return this.cartService.setPaymentMethod(userId, restaurantId, setPaymentMethodDto);
|
|
}
|
|
|
|
@Patch('delivery-method')
|
|
@ApiOperation({ summary: 'Set delivery method for cart' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiBody({ type: SetDeliveryMethodDto })
|
|
setDeliveryMethod(
|
|
@UserId() userId: string,
|
|
@RestId() restaurantId: string,
|
|
@Body() setDeliveryMethodDto: SetDeliveryMethodDto,
|
|
) {
|
|
return this.cartService.setDeliveryMethod(userId, restaurantId, setDeliveryMethodDto);
|
|
}
|
|
|
|
@Patch('description')
|
|
@ApiOperation({ summary: 'Set description for cart' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiBody({ type: SetDescriptionDto })
|
|
setDescription(
|
|
@UserId() userId: string,
|
|
@RestId() restaurantId: string,
|
|
@Body() setDescriptionDto: SetDescriptionDto,
|
|
) {
|
|
return this.cartService.setDescription(userId, restaurantId, setDescriptionDto);
|
|
}
|
|
|
|
@Patch('table-number')
|
|
@ApiOperation({ summary: 'Set table number for cart (required when delivery method is DineIn)' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiBody({ type: SetTableNumberDto })
|
|
setTableNumber(
|
|
@UserId() userId: string,
|
|
@RestId() restaurantId: string,
|
|
@Body() setTableNumberDto: SetTableNumberDto,
|
|
) {
|
|
return this.cartService.setTableNumber(userId, restaurantId, setTableNumberDto);
|
|
}
|
|
|
|
@Patch('car-delivery')
|
|
@ApiOperation({ summary: 'Set car delivery for cart' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiBody({ type: SetCarDeliveryDto })
|
|
setCarDelivery(
|
|
@UserId() userId: string,
|
|
@RestId() restaurantId: string,
|
|
@Body() setCarDeliveryDto: SetCarDeliveryDto,
|
|
) {
|
|
return this.cartService.setCarDelivery(userId, restaurantId, setCarDeliveryDto);
|
|
}
|
|
@Patch('all')
|
|
@ApiOperation({ summary: 'Set all cart params' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiBody({ type: SetAllCartParmsDto })
|
|
setAllCartParams(
|
|
@UserId() userId: string,
|
|
@RestId() restaurantId: string,
|
|
@Body() dto: SetAllCartParmsDto,
|
|
) {
|
|
return this.cartService.setAllCartParams(userId, restaurantId, dto);
|
|
}
|
|
}
|