orders
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
export enum DeliveryMethodEnum {
|
||||
DineIn = 'dineIn',
|
||||
Pickup = 'pickup',
|
||||
CustomerPickup = 'customerPickup',
|
||||
DeliveryCar = 'deliveryCar',
|
||||
DeliveryCourier = 'deliveryCourier',
|
||||
}
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
export enum OrderStatus {
|
||||
Pending = 'pending',
|
||||
Confirmed = 'confirmed',
|
||||
RejectedByRestaurant = 'rejectedByRestaurant',
|
||||
CancelledByUser = 'cancelledByUser',
|
||||
|
||||
ReadyForCustomerPickup = 'readyForCustomerPickup',
|
||||
ReadyForDineIn = 'readyForDineIn',
|
||||
ReadyForDeliveryCar = 'readyForDeliveryCar',
|
||||
ReadyForDeliveryCourier = 'readyForDeliveryCourier',
|
||||
|
||||
Delivered = 'delivered',
|
||||
Cancelled = 'cancelled',
|
||||
CancelledBySystem = 'cancelledBySystem',
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Controller, Get, Post, Param, UseGuards } from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { Controller, Get, Post, Param, UseGuards, Patch } from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
|
||||
import { OrdersService } from './orders.service';
|
||||
import { AuthGuard } from '../auth/guards/auth.guard';
|
||||
import { UserId } from '../../common/decorators/user-id.decorator';
|
||||
import { RestId } from 'src/common/decorators/rest-id.decorator';
|
||||
import { AdminAuthGuard } from '../auth/guards/adminAuth.guard';
|
||||
|
||||
|
||||
@ApiTags('orders')
|
||||
@Controller()
|
||||
export class OrdersController {
|
||||
@@ -27,6 +27,13 @@ export class OrdersController {
|
||||
return this.ordersService.findAll(restId);
|
||||
}
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Get('public/orders/:id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.ordersService.findOne(+id);
|
||||
}
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Get('admin/orders')
|
||||
@@ -35,17 +42,32 @@ export class OrdersController {
|
||||
return this.ordersService.findAll(restId);
|
||||
}
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Get('public/orders/:id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.ordersService.findOne(+id);
|
||||
@Patch('admin/orders/:orderId/confirm')
|
||||
@ApiOperation({ summary: 'Accept an order' })
|
||||
@ApiParam({ name: 'orderId', description: 'Order ID' })
|
||||
acceptOrder(@Param('orderId') orderId: string) {
|
||||
return this.ordersService.acceptOrder(orderId);
|
||||
}
|
||||
|
||||
// @Patch(':id')
|
||||
// update(@Param('id') id: string, @Body() updateOrderDto: UpdateOrderDto) {
|
||||
// return this.ordersService.update(+id, updateOrderDto);
|
||||
// }
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Patch('admin/orders/:orderId/reject')
|
||||
@ApiOperation({ summary: 'Reject an order' })
|
||||
@ApiParam({ name: 'orderId', description: 'Order ID' })
|
||||
rejectOrder(@Param('orderId') orderId: string) {
|
||||
return this.ordersService.acceptOrder(orderId);
|
||||
}
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Patch('admin/orders/:orderId/ready')
|
||||
@ApiOperation({ summary: 'Ready for pickup an order' })
|
||||
@ApiParam({ name: 'orderId', description: 'Order ID' })
|
||||
readyForPickup(@Param('orderId') orderId: string) {
|
||||
return this.ordersService.readyForDelivery(orderId);
|
||||
}
|
||||
|
||||
// @Delete(':id')
|
||||
// remove(@Param('id') id: string) {
|
||||
|
||||
@@ -250,9 +250,53 @@ export class OrdersService {
|
||||
return `This action returns a #${id} order`;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
update(id: number, _updateOrderDto: unknown) {
|
||||
return `This action updates a #${id} order`;
|
||||
async acceptOrder(orderId: string) {
|
||||
const order = await this.em.findOne(Order, { id: orderId });
|
||||
if (!order) {
|
||||
throw new NotFoundException('Order not found');
|
||||
}
|
||||
order.status = OrderStatus.Confirmed;
|
||||
await this.em.persistAndFlush(order);
|
||||
return order;
|
||||
}
|
||||
|
||||
async rejectOrder(orderId: string) {
|
||||
const order = await this.em.findOne(Order, { id: orderId });
|
||||
if (!order) {
|
||||
throw new NotFoundException('Order not found');
|
||||
}
|
||||
order.status = OrderStatus.RejectedByRestaurant;
|
||||
await this.em.persistAndFlush(order);
|
||||
return order;
|
||||
}
|
||||
|
||||
async readyForDelivery(orderId: string) {
|
||||
const order = await this.em.findOne(Order, { id: orderId });
|
||||
if (!order) {
|
||||
throw new NotFoundException('Order not found');
|
||||
}
|
||||
let orderStatus: OrderStatus | undefined;
|
||||
|
||||
switch (order.deliveryMethod.method) {
|
||||
case DeliveryMethodEnum.DeliveryCourier:
|
||||
orderStatus = OrderStatus.ReadyForDeliveryCourier;
|
||||
break;
|
||||
case DeliveryMethodEnum.DeliveryCar:
|
||||
orderStatus = OrderStatus.ReadyForDeliveryCar;
|
||||
break;
|
||||
case DeliveryMethodEnum.DineIn:
|
||||
orderStatus = OrderStatus.ReadyForDineIn;
|
||||
break;
|
||||
case DeliveryMethodEnum.CustomerPickup:
|
||||
orderStatus = OrderStatus.ReadyForCustomerPickup;
|
||||
break;
|
||||
default:
|
||||
throw new BadRequestException('Invalid delivery method');
|
||||
}
|
||||
|
||||
order.status = orderStatus;
|
||||
await this.em.persistAndFlush(order);
|
||||
return order;
|
||||
}
|
||||
|
||||
remove(id: number) {
|
||||
@@ -307,7 +351,7 @@ export class OrdersService {
|
||||
}
|
||||
|
||||
// Update order status to Cancelled
|
||||
order.status = OrderStatus.Cancelled;
|
||||
order.status = OrderStatus.CancelledBySystem;
|
||||
em.persist(order);
|
||||
|
||||
await em.flush();
|
||||
|
||||
@@ -21,9 +21,9 @@ export const deliveryMethodsData: DeliveryMethodData[] = [
|
||||
order: 1,
|
||||
},
|
||||
{
|
||||
method: DeliveryMethodEnum.Pickup,
|
||||
title: 'تحویل در رستوران',
|
||||
description: 'تحویل غذا در محل',
|
||||
method: DeliveryMethodEnum.CustomerPickup,
|
||||
title: 'برداشت از رستوران',
|
||||
description: 'برداشت غذا از رستوران',
|
||||
deliveryFee: 0,
|
||||
minOrderPrice: 0,
|
||||
enabled: true,
|
||||
|
||||
Reference in New Issue
Block a user