delete order

This commit is contained in:
2026-01-25 16:47:55 +03:30
parent b85ae3604c
commit 5dcf5c3c34
2 changed files with 136 additions and 90 deletions
@@ -30,6 +30,13 @@ export class OrderController {
return this.orderService.updateOrderItemAsUser(orderId, itemId, body); 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') @Patch('public/order/:id/item')
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
@ApiOperation({ summary: 'add item to order as user' }) @ApiOperation({ summary: 'add item to order as user' })
+129 -90
View File
@@ -96,6 +96,122 @@ export class OrderService {
return order 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) { async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) {
const { items, userPhone } = dto const { items, userPhone } = dto
@@ -214,96 +330,6 @@ export class OrderService {
return order 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) { // async updateOrderAsAdmin(orderId: string, dto: UpdateNewOrderDto) {
// const { items, enableTax, attachments, newItems } = dto // 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) { async updateStatus(orderId: string, newStatus: OrderStatusEnum) {
const order = await this.orderRepository.findOne({ id: orderId }) const order = await this.orderRepository.findOne({ id: orderId })
if (!order) { if (!order) {