update order

This commit is contained in:
2026-01-30 21:01:20 +03:30
parent fde120da3c
commit 5e5eeec5be
12 changed files with 189 additions and 256 deletions
+84 -68
View File
@@ -4,14 +4,19 @@ import { OrderRepository } from '../repositories/order.repository';
import { FindOrdersDto } from '../dto/find-orders.dto';
import { EventEmitter2 } from '@nestjs/event-emitter';
import {
CreateOrderDto, CreateOrderItemAsUserDto,
CreateOrderAsAdminDto,
CreateOrderAsUSerDto, CreateOrderItemAsUserDto,
CreateOrderItemDtoAsAdmin
} from '../dto/create-order.dto';
import { UserService } from 'src/modules/user/providers/user.service';
import {
CreateOrderItemParam,
CreateOrderParam, OrderStatusEnum,
UpdateOrderItem, UpdateOrderParam
CreateOrderItem,
CreateOrder, OrderStatusEnum,
// UpdateOrderItem,
CreateOrderWithItems,
CreateOrderItemPopulated,
UpdateOrder,
UpdateOrderItem
} from '../interface/order.interface';
import { OrderItemRepository } from '../repositories/order-item.repository';
import { ProductService } from 'src/modules/product/providers/product.service';
@@ -26,6 +31,7 @@ import { Admin } from 'src/modules/admin/entities/admin.entity';
import { Product } from 'src/modules/product/entities/product.entity';
import { AdminService } from 'src/modules/admin/providers/admin.service';
import { OrderItem } from '../entities/order-item.entity';
import { UpdateOrderDtoAsAdmin } from '../dto/update-order.dto';
@Injectable()
@@ -47,7 +53,7 @@ export class OrderService {
private readonly paymentRepository: PaymentRepository,
) { }
async createOrderAsAdmin(adminId: string, dto: CreateOrderParam) {
async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) {
const order = await this.createOrder({ ...dto, adminId })
// await this.calculateOrder(order)
// this.em.flush()
@@ -56,7 +62,7 @@ export class OrderService {
}
async createOrderAsUser(userId: string, dto: CreateOrderDto) {
async createOrderAsUser(userId: string, dto: CreateOrderAsUSerDto) {
const order = await this.createOrder({ ...dto, userId, status: OrderStatusEnum.CREATED })
this.logger.log("Order created as admin")
return order
@@ -64,10 +70,10 @@ export class OrderService {
}
async updateOrderAsAdmin(orderId: string, param: UpdateOrderParam) {
async updateOrderAsAdmin(orderId: string, dto: UpdateOrderDtoAsAdmin) {
const order = await this.findOrderOrFail(orderId)
const updateOrder = await this.updateOrder(order, param)
const updateOrder = await this.updateOrder(order, dto)
// const updateOrder = await this.calculateOrder(order)
@@ -92,7 +98,7 @@ export class OrderService {
const product = await this.productService.findOneOrFail(dto.productId)
const orderItem = this.createOrderItem(order, product, dto)
const orderItem = this.createOrderItemEntity(order, { ...dto, product })
await this.em.flush()
@@ -105,7 +111,12 @@ export class OrderService {
const product = await this.productService.findOneOrFail(dto.productId)
const orderItem = this.createOrderItem(order, product, dto)
let designer: Admin | undefined
if (dto.designerId) {
designer = await this.adminService.findOrFail(dto.designerId)
}
const orderItem = this.createOrderItemEntity(order, { ...dto, designer, product })
await this.em.flush()
@@ -118,32 +129,32 @@ export class OrderService {
async updateOrderItemAsUser(userId: string, orderId: string, itemId: number, dto: UpdateOrderItemDtoAsUser) {
const order = await this.findOrderOrFail(orderId)
const orderItem = await this.findOrderItemOrFail(orderId,itemId)
if (order.user.id !== userId) {
if (orderItem.order.user.id !== userId) {
throw new BadRequestException(`This order doesnt belongs to you`)
}
if (order.status !== OrderStatusEnum.CREATED) {
throw new BadRequestException(`You can not update when status is ${order.status}`)
if (orderItem.order.status !== OrderStatusEnum.CREATED) {
throw new BadRequestException(`You can not update when status is ${orderItem.order.status}`)
}
const orderItem = await this.updateOrderItem(itemId, dto)
const updated = await this.updateOrderItem(orderItem, dto)
return orderItem
return updated
}
async updateOrderItemAsAdmin(orderId: string, itemId: number, dto: UpdateOrderItemDtoAsAdmin) {
await this.findOrderItemOrFail(orderId, itemId)
const orderItem = await this.findOrderItemOrFail(orderId, itemId)
const orderItem = await this.updateOrderItem(itemId, dto)
const updated = await this.updateOrderItem(orderItem, dto)
// await this.calculateOrder(undefined, orderId)
// await this.em.flush()
return orderItem
return updated
}
async removeOrderItemAsAdmin(orderId: string, itemId: number) {
@@ -237,24 +248,21 @@ export class OrderService {
return orderItem
}
async assignDesigner(orderId: string, designerId: string) {
const order = await this.orderRepository.findOne({ id: orderId })
if (!order) {
throw new BadRequestException("Order not found")
}
async assignDesigner(orderId: string, orderItemId: number, designerId: string) {
const orderItem = await this.findOrderItemOrFail(orderId, orderItemId)
const designer = await this.adminRepository.findOne({ id: designerId })
if (!designer) {
throw new BadRequestException("designer not found")
}
order.designer = designer
// order.status = OrderStatusEnum.IN_DESIGN
orderItem.designer = designer
await this.em.persistAndFlush(orderItem)
await this.em.persistAndFlush(order)
return order
return orderItem
}
@@ -298,45 +306,51 @@ export class OrderService {
// ==================== Entity Methods ====================
private async createOrderItem(order: Order, product: Product, dto: CreateOrderItemParam, em?: EntityManager) {
const { attributes, description, quantity, attachments, discount, unitPrice } = dto
private async createOrderItemEntity(order: Order, dto: CreateOrderItemPopulated, em?: EntityManager) {
const { attributes, description, quantity, attachments, discount,
unitPrice, product, designer, print, printAttachments } = dto
const eM = em ?? this.em
return eM.create(
OrderItem,
{
designer,
order,
attributes,
description,
quantity,
attachments,
discount: discount ?? undefined,
unitPrice: unitPrice ?? undefined,
product
discount,
unitPrice,
product,
print,
printAttachments
})
}
// ==================== Core Logic ====================
async createOrder(dto: CreateOrderParam) {
const { items, userId, attachments, designerId, enableTax,
async createOrder(dto: CreateOrderWithItems) {
const { items, userId, enableTax,
estimatedDays, paymentMethod, status, adminId } = dto
// Validate and fetch all required entities
const user = await this.userService.findOrFail(userId)
const admin = adminId ? await this.adminService.findOrFail(adminId) : undefined
const designer = designerId ? await this.adminService.findOrFail(designerId) : undefined
const products = await this.productService.findProductsByIds(items.map(item => item.productId))
const desiners = await this.adminService.findByIds(items.map(item => item.designerId).filter(id => id != null))
const productMap = new Map(
products.map(p => [Number(p.id), p])
);
const designerMap = new Map(
desiners.map(d => [d.id, d])
)
return this.em.transactional(async (em) => {
const order = em.create(Order, {
creator: admin,
user,
attachments,
designer,
enableTax,
estimatedDays,
paymentMethod,
@@ -351,8 +365,16 @@ export class OrderService {
if (!product) {
throw new BadRequestException("Product not found!")
}
let designer: undefined | Admin = undefined
const orderItem = await this.createOrderItem(order, product, item, em)
if (item.designerId) {
designer = designerMap.get(item.designerId)
if (!designer) {
throw new BadRequestException("designer not found!")
}
}
const orderItem = await this.createOrderItemEntity(order, { ...item, designer, product }, em)
order.items.add(orderItem)
}
@@ -369,8 +391,8 @@ export class OrderService {
}
async updateOrder(order: Order, param: UpdateOrderParam) {
const { attachments, adminId, designerId, enableTax,
async updateOrder(order: Order, param: UpdateOrder) {
const { adminId, enableTax,
estimatedDays, paymentMethod, status, userId } = param
@@ -386,15 +408,6 @@ export class OrderService {
}
if (designerId != undefined && designerId != undefined) {
const designer = await this.adminService.findOrFail(designerId)
order.designer = designer
}
if (attachments != undefined) {
order.attachments = attachments
}
if (enableTax != undefined) {
order.enableTax = enableTax
}
@@ -411,36 +424,31 @@ export class OrderService {
order.status = status
}
await this.em.persistAndFlush(order)
return order
}
async updateOrderItem(itemId: number, dto: UpdateOrderItem) {
const { attributes, description, productId, quantity, attachments, discount, unitPrice } = dto
async updateOrderItem(orderItem: OrderItem, dto: UpdateOrderItem) {
const { attributes, description, product, quantity,
attachments, discount, unitPrice, designer, print, printAttachments } = dto
const orderItem = await this.orderItemRepository.findOne({
id: itemId
})
if (!orderItem) {
throw new BadRequestException(`orderItem not found`)
if (product && orderItem.product.id !== product.id) {
orderItem.product = product
}
// product is changed
if (productId && Number(orderItem.product.id) !== productId) {
const product = await this.productRepository.findOne({ id: productId })
if (!product) {
throw new BadRequestException(`product not found`)
}
orderItem.product = product
if (designer && orderItem.designer?.id !== designer.id) {
orderItem.designer = designer
}
if (attributes) {
orderItem.attributes = attributes
}
if (printAttachments) {
orderItem.printAttachments = printAttachments
}
if (description) {
orderItem.description = description
}
@@ -459,6 +467,14 @@ export class OrderService {
orderItem.unitPrice = unitPrice
}
if (print != undefined) {
orderItem.print = print
}
if (attributes != undefined) {
orderItem.attributes = attributes
}
await this.em.flush()
return orderItem