order update refactor

This commit is contained in:
2026-02-01 15:34:06 +03:30
parent 918d774cad
commit 71319de649
12 changed files with 129 additions and 31 deletions
+82 -9
View File
@@ -1,4 +1,4 @@
import { Injectable, BadRequestException, Logger } from '@nestjs/common';
import { Injectable, BadRequestException, Logger, BadGatewayException } from '@nestjs/common';
import { EntityManager } from '@mikro-orm/postgresql';
import { OrderRepository } from '../repositories/order.repository';
import { FindOrdersDto } from '../dto/find-orders.dto';
@@ -30,6 +30,7 @@ import { AdminService } from 'src/modules/admin/providers/admin.service';
import { OrderItem } from '../entities/order-item.entity';
import { UpdateOrderAsAdminDto } from '../dto/update-order.dto';
import { CreatePrintFormDto } from '../dto/create-print-form.dto';
import { Product } from 'src/modules/product/entities/product.entity';
@Injectable()
@@ -67,11 +68,10 @@ export class OrderService {
}
async updateOrderAsAdmin(orderId: string, dto: UpdateOrderAsAdminDto) {
const order = await this.findOrderOrFail(orderId)
const updateOrder = await this.updateOrder(order, dto)
// const updateOrder = await this.updateOrder(order, dto)
// const updateOrder = await this.calculateOrder(order)
@@ -79,7 +79,82 @@ export class OrderService {
this.logger.log("Order updated as admin")
return updateOrder
// return updateOrder
}
async updateOrderAsAdmin2(orderId: string, dto: UpdateOrderAsAdminDto) {
const order = await this.findOrderOrFail(orderId)
const { items, ...rest } = dto
return this.em.transactional(async (em) => {
em.assign(order, rest)
for (const item of items) {
if (item.id) { // update
const currentItem = order.items.find(i => Number(i.id) == item.id)
if (!currentItem) {
throw new BadGatewayException("Order Item not found")
}
const { designerId, productId, ...itemRest } = item
let newProduct: Product | undefined
if (productId && productId !== currentItem.product.id) {
newProduct = await this.productService.findOneOrFail(productId)
}
let newDesigner: Admin | undefined
if (designerId && designerId !== currentItem.designer?.id) {
newDesigner = await this.adminService.findOrFail(designerId)
}
em.assign(currentItem, { ...itemRest, designer: newDesigner, product: newProduct })
} else {
if (!item.productId) {
throw new BadRequestException(`Product id is required for item ${item.id}`)
}
const product = await this.productService.findOneOrFail(item.productId)
const newOrderItem = em.create(OrderItem, {
order,
product,
quantity: item.quantity,
unitPrice: item.unitPrice,
adminDescription: item.adminDescription,
attachments: item.attachments,
attributes: item.attributes,
description: item.description,
discount: item.discount,
confirmedAt: item.confirmedAt,
})
em.persist(newOrderItem)
}
}
await em.flush()
this.logger.log("Order updated as admin")
return order
})
}
async addOrderItemAsUser(userId: string, orderId: string, dto: CreateOrderItemAsUserDto) {
@@ -354,7 +429,7 @@ export class OrderService {
unitPrice,
product,
printAttributes: printAttributes,
printAttachments
// printAttachments
})
}
@@ -476,9 +551,7 @@ export class OrderService {
if (attributes != undefined) {
orderItem.attributes = attributes
}
if (printAttachments != undefined) {
orderItem.printAttachments = printAttachments
}
if (description != undefined) {
orderItem.description = description
}
@@ -537,7 +610,7 @@ export class OrderService {
async findOrderOrFail(orderId: string) {
const order = await this.orderRepository.findOne({ id: orderId },
{ populate: ['items', 'items.product', 'payments', 'user'] })
{ populate: ['items', 'items.product', 'payments', 'user', 'items.designer'] })
if (!order) {
throw new BadRequestException('Order not found')
}