Spaces:
Sleeping
Sleeping
import { BadRequestException, Injectable } from '@nestjs/common'; | |
import { CreateOrderDto } from './dto/create-order.dto.js'; | |
import { UserEntity } from '../../entities/user.entity.js'; | |
import { OrderEntity } from '../../entities/order.entity.js'; | |
import { BranchService } from '../branch/branch.service.js'; | |
import { OrderType } from '../../common/enums/OrderType.enum.js'; | |
import { isUUID } from 'class-validator'; | |
import { OrderItemEntity } from '../../entities/order-item.entity.js'; | |
import { BranchMenuEntity } from '../../entities/branch-menu.entity.js'; | |
import { OrderStatus } from '../../common/enums/OrderStatus.enum.js'; | |
() | |
export class OrderService { | |
constructor(private readonly branchService: BranchService) {} | |
async createFromCustomer( | |
branchId: string, | |
userId: string, | |
createOrderDto: CreateOrderDto, | |
) { | |
console.log('??'); | |
if (createOrderDto.order_type != OrderType.ONLINE) { | |
throw new BadRequestException('customer cannot create offline order'); | |
} | |
const user = await UserEntity.findOneBy({ id: userId }); | |
if (!user) { | |
throw new BadRequestException('User not found'); | |
} | |
const branch = await this.branchService.getBranchOrError(branchId); | |
if (!branch) { | |
throw new BadRequestException('Branch not found'); | |
} | |
const order = OrderEntity.create(); | |
order.branch = branch; | |
order.customer = user; | |
order.order_type = createOrderDto.order_type; | |
order.order_status = OrderStatus.PENDING; | |
order.total_value = 0; | |
await order.save(); | |
let orderItems: OrderItemEntity[] = []; | |
let totalValue = 0; | |
for (const item of createOrderDto.order_items) { | |
let branchMenu: BranchMenuEntity; | |
if (!isUUID(item.menu_id)) { | |
branchMenu = await BranchMenuEntity.findOne({ | |
where: { branch_id: branchId, menu_id: item.menu_id }, | |
relations: ['menu_item'], | |
}); | |
} else { | |
branchMenu = await BranchMenuEntity.findOne({ | |
where: { branch_id: branchId, id: item.menu_id }, | |
relations: ['menu_item'], | |
}); | |
} | |
if (!branchMenu) { | |
throw new BadRequestException('Item not found in branch menu'); | |
} | |
const orderItem = OrderItemEntity.create(); | |
orderItem.branch_menu = branchMenu; | |
orderItem.price = branchMenu.menu_item.price; | |
orderItem.quantity = item.quantity; | |
orderItem.order_id = order.id; | |
totalValue += orderItem.price * orderItem.quantity; | |
orderItems.push(orderItem); | |
} | |
await order.save(); | |
order.total_value = totalValue; | |
await OrderItemEntity.save(orderItems); | |
return { ...order, order_items: orderItems }; | |
} | |
async createFromStaff( | |
branchId: string, | |
userId: string, | |
createOrderDto: CreateOrderDto, | |
) { | |
if (createOrderDto.order_type == OrderType.ONLINE) { | |
throw new BadRequestException('staff cannot create online order'); | |
} | |
// staff | |
const staff = await UserEntity.findOneBy({ id: userId }); | |
const branch = await this.branchService.getBranchOrError(branchId); | |
const order = OrderEntity.create(); | |
order.branch = branch; | |
order.staff = staff; | |
order.order_type = createOrderDto.order_type; | |
order.table_number = createOrderDto.table_number; | |
order.order_status = OrderStatus.PREPARING; | |
order.total_value = 0; | |
await order.save(); | |
let orderItems: OrderItemEntity[] = []; | |
let totalValue = 0; | |
for (const item of createOrderDto.order_items) { | |
let branchMenu: BranchMenuEntity; | |
if (!isUUID(item.menu_id)) { | |
branchMenu = await BranchMenuEntity.findOne({ | |
where: { branch_id: branchId, menu_id: item.menu_id }, | |
relations: ['menu_item'], | |
}); | |
} else { | |
branchMenu = await BranchMenuEntity.findOne({ | |
where: { branch_id: branchId, id: item.menu_id }, | |
relations: ['menu_item'], | |
}); | |
} | |
if (!branchMenu) { | |
throw new BadRequestException('Item not found in branch menu'); | |
} | |
const orderItem = OrderItemEntity.create(); | |
orderItem.branch_menu = branchMenu; | |
orderItem.price = branchMenu.menu_item.price; | |
orderItem.quantity = item.quantity; | |
orderItem.order_id = order.id; | |
totalValue += orderItem.price * orderItem.quantity; | |
orderItems.push(orderItem); | |
} | |
await order.save(); | |
order.total_value = totalValue; | |
await OrderItemEntity.save(orderItems); | |
return { ...order, order_items: orderItems }; | |
} | |
findAll() { | |
return `This action returns all order`; | |
} | |
findOne(id: number) { | |
return `This action returns a #${id} order`; | |
} | |
remove(id: number) { | |
return `This action removes a #${id} order`; | |
} | |
} | |