This commit is contained in:
2026-01-25 16:26:47 +03:30
parent 55856a40f1
commit b85ae3604c
7 changed files with 287 additions and 225 deletions
+169 -122
View File
@@ -4,7 +4,7 @@ import { OrderItem } from '../entities/order-item.entity';
import { OrderRepository } from '../repositories/order.repository';
import { FindOrdersDto } from '../dto/find-orders.dto';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { CreateOrderDto } from '../dto/create-order.dto';
import { CreateOrderDto, CreateOrderItemDto } from '../dto/create-order.dto';
import { UserService } from 'src/modules/user/providers/user.service';
import { OrderStatusEnum } from '../interface/order.interface';
import { OrderItemRepository } from '../repositories/order-item.repository';
@@ -12,11 +12,11 @@ import { ProductService } from 'src/modules/product/providers/product.service';
import { ProductRepository } from 'src/modules/product/repositories/product.repository';
import { TicketService } from 'src/modules/ticket/providers/tickets.service';
import { TicketRepository } from 'src/modules/ticket/repositories/tickets.repository';
import { CreateInvoiceDto } from '../dto/create-invoice.dto';
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository';
import { CreateOrderAsAdminDto } from '../dto/create-order-as-admin.dto';
import { Order } from '../entities/order.entity';
import { PaymentRepository } from 'src/modules/payment/repositories/payment.repository';
import { UpdateOrderItemDto } from '../dto/update-order-item.dto';
@Injectable()
@@ -27,7 +27,6 @@ export class OrderService {
private readonly em: EntityManager,
private readonly orderRepository: OrderRepository,
private readonly orderItemRepository: OrderItemRepository,
// private readonly paymentsService: PaymentService,
private readonly userService: UserService,
private readonly productService: ProductService,
private readonly productRepository: ProductRepository,
@@ -215,98 +214,181 @@ export class OrderService {
return order
}
async updateOrder(orderId: string, dto: CreateInvoiceDto) {
const { items, enableTax, attachments, newItems } = dto
async updateOrderItemAsUser(orderId: string, itemId: string, dto: UpdateOrderItemDto) {
const { attributesValues, description, productId, quantity, attachments } = dto
const targetOrder = await this.findOneOrFail(orderId)
const order = await this.findOneOrFail(orderId)
if (items.length !== targetOrder.items.length) {
throw new BadRequestException("One or more order items price is not set")
if (!order) {
throw new BadRequestException(`Order not found`)
}
//TODO : calc is not correct
const order = await this.em.transactional(async (em) => {
if (order.status !== OrderStatusEnum.CREATED) {
throw new BadRequestException(`You can not update when status is ${order.status}`)
}
let subTotal = 0
let totalDiscount = 0
// Calculate order item finnicials
for (let orderItem of targetOrder.items) {
const found = items.find(it => it.orderItemId == Number(orderItem.id))
if (!found) {
throw new BadRequestException(`order item ${orderItem} not found`)
}
// TODO use Deciaml js to calculation
orderItem.unitPrice = found.unitPrice
// orderItem.subTotal = (found.unitPrice) * orderItem.quantity
subTotal += orderItem.subTotal
em.persist(orderItem)
}
newItems.forEach(async item => {
const found = order.items.find((it => Number(it.product.id) === item.productId))
if (found) {
throw new BadRequestException("Product already exist!")
}
const product = await this.productRepository.findOne({ id: item.productId })
if (!product) {
throw new BadRequestException("Product not found!")
}
const newOrderItem = this.orderItemRepository.create({
discount: item.discount,
product,
unitPrice: item.unitPrice,
total: (item.unitPrice - item.discount) * item.quantity,
subTotal: item.unitPrice * item.quantity,
quantity: item.quantity,
confirmedAt: new Date(),
attributesValues: item.attributesValues,
order
})
subTotal += item.unitPrice
this.em.persist(newOrderItem)
})
// calculate order financials
targetOrder.subTotal = subTotal
targetOrder.discount = totalDiscount
const total = subTotal - totalDiscount
let tax = 0
if (enableTax) {
tax = 0.1 * total
}
targetOrder.taxAmount = tax
targetOrder.total = total + tax
targetOrder.balance = total + tax
if (attachments.length) {
order.attachments = attachments
}
// change status
targetOrder.status = OrderStatusEnum.INVOICED
targetOrder.invoicedAt = new Date()
em.persist(targetOrder)
return targetOrder
const orderItem = await this.orderItemRepository.findOne({
id: itemId
})
return order
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
// const order = await this.findOneOrFail(orderId)
// if (order.status !== OrderStatusEnum.CREATED) {
// throw new BadRequestException(`You can not update when status is ${order.status}`)
// }
// const updatedOrder = await this.em.transactional(async (em) => {
// for (let orderItem of order.items) {
// const found = items.find(it => it.orderItemId == Number(orderItem.id))
// if (!found) {
// throw new BadRequestException(`order item ${orderItem} not found`)
// }
// // TODO use Deciaml js to calculation
// orderItem.unitPrice = found.unitPrice
// subTotal += orderItem.subTotal
// em.persist(orderItem)
// }
// newItems.forEach(async item => {
// const found = updatedOrder.items.find((it => Number(it.product.id) === item.productId))
// if (found) {
// throw new BadRequestException("Product already exist!")
// }
// const product = await this.productRepository.findOne({ id: item.productId })
// if (!product) {
// throw new BadRequestException("Product not found!")
// }
// const newOrderItem = this.orderItemRepository.create({
// discount: item.discount,
// product,
// unitPrice: item.unitPrice,
// total: (item.unitPrice - item.discount) * item.quantity,
// subTotal: item.unitPrice * item.quantity,
// quantity: item.quantity,
// confirmedAt: new Date(),
// attributesValues: item.attributesValues,
// order: updatedOrder
// })
// subTotal += item.unitPrice
// this.em.persist(newOrderItem)
// })
// // calculate order financials
// order.subTotal = subTotal
// order.discount = totalDiscount
// const total = subTotal - totalDiscount
// let tax = 0
// if (enableTax) {
// tax = 0.1 * total
// }
// order.taxAmount = tax
// order.total = total + tax
// order.balance = total + tax
// if (attachments.length) {
// updatedOrder.attachments = attachments
// }
// // change status
// order.status = OrderStatusEnum.INVOICED
// order.invoicedAt = new Date()
// em.persist(order)
// return order
// })
// return updatedOrder
// }
async confirmOrderItem(userId: string, orderId: string, orderItemId: number) {
const orderItem = await this.orderItemRepository.findOne({ id: orderItemId, order: { id: orderId } },
@@ -327,7 +409,7 @@ export class OrderService {
return orderItem
}
async asignDesigner(orderId: string, designerId: string) {
async assignDesigner(orderId: string, designerId: string) {
const order = await this.orderRepository.findOne({ id: orderId })
if (!order) {
throw new BadRequestException("Order not found")
@@ -382,40 +464,5 @@ export class OrderService {
return order
}
// async addOrderItem(orderId: string, dto: AddOrderItemDto) {
// const order = await this.orderRepository.findOne({ id: orderId })
// if (!order) {
// throw new BadRequestException("Order not Found!")
// }
// const { items } = dto
// items.forEach(async item => {
// const found = order.items.find((it => Number(it.product.id) === item.productId))
// if (found) {
// throw new BadRequestException("Product already exist!")
// }
// const product = await this.productRepository.findOne({ id: item.productId })
// if (!product) {
// throw new BadRequestException("Product not found!")
// }
// const newOrderItem = this.orderItemRepository.create({
// discount: item.discount,
// product,
// unitPrice: item.unitPrice,
// total: (item.unitPrice - item.discount) * item.quantity,
// subTotal: item.unitPrice * item.quantity,
// quantity: item.quantity,
// status: OrderItemStatus.CONFIRMED,
// attributesValues: item.attributesValues,
// order
// })
// this.em.persist(newOrderItem)
// })
// // need calculate order finacial
// await this.em.flush()
// return order
// }
}