delete order
This commit is contained in:
@@ -30,6 +30,13 @@ export class OrderController {
|
||||
return this.orderService.updateOrderItemAsUser(orderId, itemId, body);
|
||||
}
|
||||
|
||||
@Delete('public/order/:id')
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiOperation({ summary: 'Delete order as user' })
|
||||
deleteOrder(@Param('id') orderId: string) {
|
||||
return this.orderService.removeOrderAsUser(orderId);
|
||||
}
|
||||
|
||||
@Patch('public/order/:id/item')
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiOperation({ summary: 'add item to order as user' })
|
||||
|
||||
@@ -96,6 +96,122 @@ export class OrderService {
|
||||
return order
|
||||
}
|
||||
|
||||
async addOrderItemAsUser(orderId: string, dto: CreateOrderItemDto) {
|
||||
const { attributesValues, description, productId, quantity, attachments } = dto
|
||||
|
||||
const order = await this.findOneOrFail(orderId)
|
||||
|
||||
if (!order) {
|
||||
throw new BadRequestException(`Order not found`)
|
||||
}
|
||||
|
||||
if (order.status !== OrderStatusEnum.CREATED) {
|
||||
throw new BadRequestException(`You can not update when status is ${order.status}`)
|
||||
}
|
||||
|
||||
const found = order.items.find(it => it.product.id == productId)
|
||||
if (found) {
|
||||
throw new BadRequestException(`Product already exists`)
|
||||
}
|
||||
|
||||
const product = await this.productRepository.findOne({ id: productId })
|
||||
if (!product) {
|
||||
throw new BadRequestException(`Product not found`)
|
||||
}
|
||||
const orderItem = this.orderItemRepository.create({
|
||||
order,
|
||||
attributesValues,
|
||||
description,
|
||||
quantity,
|
||||
attachments,
|
||||
subTotal: 0,
|
||||
discount: 0,
|
||||
total: 0,
|
||||
unitPrice: 0,
|
||||
product
|
||||
})
|
||||
|
||||
this.em.persistAndFlush(orderItem)
|
||||
|
||||
return orderItem
|
||||
}
|
||||
|
||||
async updateOrderItemAsUser(orderId: string, itemId: string, dto: UpdateOrderItemDto) {
|
||||
const { attributesValues, description, productId, quantity, attachments } = dto
|
||||
|
||||
const order = await this.findOneOrFail(orderId)
|
||||
|
||||
if (!order) {
|
||||
throw new BadRequestException(`Order not found`)
|
||||
}
|
||||
|
||||
if (order.status !== OrderStatusEnum.CREATED) {
|
||||
throw new BadRequestException(`You can not update when status is ${order.status}`)
|
||||
}
|
||||
|
||||
const orderItem = await this.orderItemRepository.findOne({
|
||||
id: itemId
|
||||
})
|
||||
|
||||
if (!orderItem) {
|
||||
throw new BadRequestException(`orderItem not found`)
|
||||
}
|
||||
|
||||
// product is changed
|
||||
if (productId && orderItem.product.id !== productId) {
|
||||
const product = await this.productRepository.findOne({ id: productId })
|
||||
|
||||
if (!product) {
|
||||
throw new BadRequestException(`product not found`)
|
||||
}
|
||||
|
||||
orderItem.product = product
|
||||
}
|
||||
|
||||
if (attributesValues) {
|
||||
orderItem.attributesValues = attributesValues
|
||||
}
|
||||
if (description) {
|
||||
orderItem.description = description
|
||||
}
|
||||
if (quantity) {
|
||||
orderItem.quantity = quantity
|
||||
}
|
||||
|
||||
if (attachments) {
|
||||
orderItem.attachments = attachments
|
||||
}
|
||||
|
||||
|
||||
return orderItem
|
||||
}
|
||||
|
||||
async removeOrderItem(orderId: string, itemId: string) {
|
||||
|
||||
const order = await this.findOneOrFail(orderId)
|
||||
|
||||
if (!order) {
|
||||
throw new BadRequestException(`Order not found`)
|
||||
}
|
||||
|
||||
if (order.status !== OrderStatusEnum.CREATED) {
|
||||
throw new BadRequestException(`You can not update when status is ${order.status}`)
|
||||
}
|
||||
|
||||
const orderItem = await this.orderItemRepository.findOne({
|
||||
id: itemId
|
||||
})
|
||||
|
||||
if (!orderItem) {
|
||||
throw new BadRequestException(`orderItem not found`)
|
||||
}
|
||||
|
||||
await this.em.removeAndFlush(orderItem)
|
||||
|
||||
|
||||
return orderItem
|
||||
}
|
||||
|
||||
async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) {
|
||||
const { items, userPhone } = dto
|
||||
|
||||
@@ -214,96 +330,6 @@ export class OrderService {
|
||||
return order
|
||||
}
|
||||
|
||||
async updateOrderItemAsUser(orderId: string, itemId: string, dto: UpdateOrderItemDto) {
|
||||
const { attributesValues, description, productId, quantity, attachments } = dto
|
||||
|
||||
const order = await this.findOneOrFail(orderId)
|
||||
|
||||
if (!order) {
|
||||
throw new BadRequestException(`Order not found`)
|
||||
}
|
||||
|
||||
if (order.status !== OrderStatusEnum.CREATED) {
|
||||
throw new BadRequestException(`You can not update when status is ${order.status}`)
|
||||
}
|
||||
|
||||
const orderItem = await this.orderItemRepository.findOne({
|
||||
id: itemId
|
||||
})
|
||||
|
||||
if (!orderItem) {
|
||||
throw new BadRequestException(`orderItem not found`)
|
||||
}
|
||||
|
||||
// product is changed
|
||||
if (productId && orderItem.product.id !== productId) {
|
||||
const product = await this.productRepository.findOne({ id: productId })
|
||||
|
||||
if (!product) {
|
||||
throw new BadRequestException(`product not found`)
|
||||
}
|
||||
|
||||
orderItem.product = product
|
||||
}
|
||||
|
||||
if (attributesValues) {
|
||||
orderItem.attributesValues = attributesValues
|
||||
}
|
||||
if (description) {
|
||||
orderItem.description = description
|
||||
}
|
||||
if (quantity) {
|
||||
orderItem.quantity = quantity
|
||||
}
|
||||
|
||||
if (attachments) {
|
||||
orderItem.attachments = attachments
|
||||
}
|
||||
|
||||
|
||||
return orderItem
|
||||
}
|
||||
|
||||
async addOrderItemAsUser(orderId: string, dto: CreateOrderItemDto) {
|
||||
const { attributesValues, description, productId, quantity, attachments } = dto
|
||||
|
||||
const order = await this.findOneOrFail(orderId)
|
||||
|
||||
if (!order) {
|
||||
throw new BadRequestException(`Order not found`)
|
||||
}
|
||||
|
||||
if (order.status !== OrderStatusEnum.CREATED) {
|
||||
throw new BadRequestException(`You can not update when status is ${order.status}`)
|
||||
}
|
||||
|
||||
const found = order.items.find(it => it.product.id == productId)
|
||||
if (found) {
|
||||
throw new BadRequestException(`Product already exists`)
|
||||
}
|
||||
|
||||
const product = await this.productRepository.findOne({ id: productId })
|
||||
if (!product) {
|
||||
throw new BadRequestException(`Product not found`)
|
||||
}
|
||||
const orderItem = this.orderItemRepository.create({
|
||||
order,
|
||||
attributesValues,
|
||||
description,
|
||||
quantity,
|
||||
attachments,
|
||||
subTotal: 0,
|
||||
discount: 0,
|
||||
total: 0,
|
||||
unitPrice: 0,
|
||||
product
|
||||
})
|
||||
|
||||
this.em.persistAndFlush(orderItem)
|
||||
|
||||
return orderItem
|
||||
}
|
||||
|
||||
// async updateOrderAsAdmin(orderId: string, dto: UpdateNewOrderDto) {
|
||||
// const { items, enableTax, attachments, newItems } = dto
|
||||
|
||||
@@ -452,6 +478,19 @@ export class OrderService {
|
||||
|
||||
}
|
||||
|
||||
async removeOrderAsUser(orderId:string){
|
||||
const order = await this.orderRepository.findOne({ id: orderId })
|
||||
if (!order) {
|
||||
throw new BadRequestException("Order not found")
|
||||
}
|
||||
|
||||
if (![OrderStatusEnum.CREATED].includes(order.status)) {
|
||||
throw new BadRequestException("Order can not be deleted")
|
||||
}
|
||||
|
||||
return this.hardDeleteOrder(orderId)
|
||||
}
|
||||
|
||||
async updateStatus(orderId: string, newStatus: OrderStatusEnum) {
|
||||
const order = await this.orderRepository.findOne({ id: orderId })
|
||||
if (!order) {
|
||||
|
||||
Reference in New Issue
Block a user