121 lines
4.5 KiB
TypeScript
121 lines
4.5 KiB
TypeScript
import { Controller, Get, Post, Param, UseGuards, Patch, Query, Body } from '@nestjs/common';
|
|
import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam, ApiHeader, ApiBody } from '@nestjs/swagger';
|
|
import { OrdersService } from '../providers/orders.service';
|
|
import { AuthGuard } from '../../auth/guards/auth.guard';
|
|
import { UserId } from '../../../common/decorators/user-id.decorator';
|
|
import { ShopId } from 'src/common/decorators/shop-id.decorator';
|
|
import { AdminAuthGuard } from '../../auth/guards/adminAuth.guard';
|
|
import { FindOrdersDto } from '../dto/find-orders.dto';
|
|
import { OrderStatus } from '../interface/order.interface';
|
|
import { API_HEADER_SLUG } from 'src/common/constants/index';
|
|
import { UpdateOrderStatusDto } from '../dto/update-order-status.dto';
|
|
import { Permissions } from 'src/common/decorators/permissions.decorator';
|
|
import { Permission } from 'src/common/enums/permission.enum';
|
|
|
|
@ApiTags('orders')
|
|
@ApiBearerAuth()
|
|
@Controller()
|
|
export class OrdersController {
|
|
constructor(private readonly ordersService: OrdersService) { }
|
|
|
|
@UseGuards(AuthGuard)
|
|
@Post('public/checkout')
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiOperation({ summary: 'Checkout : create order and payment record' })
|
|
checkout(@UserId() userId: string, @ShopId() shopId: string) {
|
|
return this.ordersService.checkout(userId, shopId);
|
|
}
|
|
|
|
@UseGuards(AuthGuard)
|
|
@Get('public/orders')
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiOperation({ summary: 'Get all orders with pagination and filters' })
|
|
findAll(@ShopId() shopId: string, @Query() dto: FindOrdersDto, @UserId() userId: string) {
|
|
return this.ordersService.findAllForUser(shopId, dto, userId);
|
|
}
|
|
|
|
@UseGuards(AuthGuard)
|
|
@ApiOperation({ summary: 'Get an order By id for User' })
|
|
@ApiParam({ name: 'orderId', description: 'Order ID' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@Get('public/orders/:orderId')
|
|
findOne(@Param('orderId') orderId: string, @ShopId() shopId: string) {
|
|
return this.ordersService.findOne(orderId, shopId);
|
|
}
|
|
|
|
@UseGuards(AuthGuard)
|
|
@Patch('public/orders/:id/:status')
|
|
@ApiParam({
|
|
name: 'status',
|
|
description: 'Order status',
|
|
enum: OrderStatus,
|
|
})
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiBody({ type: UpdateOrderStatusDto })
|
|
@ApiOperation({ summary: 'Update status of an order By User' })
|
|
@ApiParam({ name: 'id', description: 'Order ID' })
|
|
cancelOrder(
|
|
@Body() dto: UpdateOrderStatusDto,
|
|
@Param('id') orderId: string,
|
|
@Param('status') status: OrderStatus,
|
|
@ShopId() shopId: string,
|
|
) {
|
|
return this.ordersService.changeOrderStatus(orderId, shopId, status, 'user', dto?.desc);
|
|
}
|
|
|
|
/******************** Admin Routes **********************/
|
|
@UseGuards(AdminAuthGuard)
|
|
@Permissions(Permission.MANAGE_ORDERS)
|
|
@Get('admin/orders')
|
|
@ApiOperation({ summary: 'Get all orders with pagination and filters' })
|
|
findAllAdmin(@ShopId() shopId: string, @Query() dto: FindOrdersDto) {
|
|
return this.ordersService.findAllForAdmin(shopId, dto);
|
|
}
|
|
|
|
@UseGuards(AdminAuthGuard)
|
|
@Permissions(Permission.MANAGE_ORDERS)
|
|
@ApiOperation({ summary: 'Get an order By id for User' })
|
|
@ApiParam({ name: 'orderId', description: 'Order ID' })
|
|
@Get('admin/orders/:orderId')
|
|
findOneAsAdmin(@Param('orderId') orderId: string, @ShopId() shopId: string) {
|
|
return this.ordersService.findOne(orderId, shopId);
|
|
}
|
|
|
|
@UseGuards(AdminAuthGuard)
|
|
@Permissions(Permission.MANAGE_ORDERS)
|
|
@Patch('admin/orders/:orderId/:status')
|
|
@ApiOperation({ summary: 'Update an order status' })
|
|
@ApiParam({ name: 'orderId', description: 'Order ID' })
|
|
@ApiBody({ type: UpdateOrderStatusDto })
|
|
@ApiParam({
|
|
name: 'status',
|
|
description: 'Order status',
|
|
enum: OrderStatus,
|
|
})
|
|
updateStatus(
|
|
@Param('orderId') orderId: string,
|
|
@Body() dto: UpdateOrderStatusDto,
|
|
@Param('status') status: OrderStatus,
|
|
@ShopId() shopId: string,
|
|
) {
|
|
return this.ordersService.changeOrderStatus(orderId, shopId, status, 'admin', dto?.desc);
|
|
}
|
|
|
|
@UseGuards(AdminAuthGuard)
|
|
@Permissions(Permission.VIEW_REPORTS)
|
|
@ApiOperation({ summary: 'Get Stats for report page' })
|
|
@Get('admin/orders/stats')
|
|
findStats(@ShopId() shopId: string) {
|
|
return this.ordersService.getStats(shopId);
|
|
}
|
|
|
|
@UseGuards(AdminAuthGuard)
|
|
@Permissions(Permission.VIEW_REPORTS)
|
|
@ApiOperation({ summary: 'Get product sales pie chart data for last month' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@Get('admin/orders/product-sales-pie-chart')
|
|
getFoodSalesPieChart(@ShopId() shopId: string) {
|
|
return this.ordersService.getFoodSalesPieChart(shopId);
|
|
}
|
|
}
|