add order item
This commit is contained in:
@@ -1,14 +1,9 @@
|
||||
import { Injectable, NotFoundException, BadRequestException, Logger } from '@nestjs/common';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { OrderItem } from '../entities/order-item.entity';
|
||||
import { PaymentService } from '../../payment/services/payments.service';
|
||||
import { OrderRepository } from '../repositories/order.repository';
|
||||
import { FindOrdersDto } from '../dto/find-orders.dto';
|
||||
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
||||
import { Payment } from 'src/modules/payment/entities/payment.entity';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { OrderCreatedEvent, OrderStatusChangedEvent } from '../events/order.events';
|
||||
import { OrderMessage } from 'src/common/enums/message.enum';
|
||||
import { CreateOrderDto } from '../dto/create-order.dto';
|
||||
import { UserService } from 'src/modules/user/providers/user.service';
|
||||
import { OrderItemStatus, OrderStatusEnum } from '../interface/order.interface';
|
||||
@@ -20,6 +15,7 @@ import { TicketRepository } from 'src/modules/ticket/repositories/tickets.reposi
|
||||
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 { AddOrderItemDto } from '../dto/add-order-item.dto';
|
||||
|
||||
|
||||
@Injectable()
|
||||
@@ -81,7 +77,9 @@ export class OrderService {
|
||||
quantity: item.quantity,
|
||||
status: OrderItemStatus.PENDING,
|
||||
unitPrice: 0,
|
||||
totalPrice: 0,
|
||||
total: 0,
|
||||
subTotal: 0,
|
||||
discount: 0
|
||||
})
|
||||
em.persist(orderItem)
|
||||
});
|
||||
@@ -159,7 +157,9 @@ export class OrderService {
|
||||
quantity: item.quantity,
|
||||
status: OrderItemStatus.CONFIRMED,
|
||||
unitPrice: item.unitPrice,
|
||||
totalPrice: item.unitPrice * item.quantity,
|
||||
total: item.unitPrice * item.quantity,
|
||||
discount: 0,
|
||||
subTotal: 0
|
||||
})
|
||||
|
||||
em.persist(orderItem)
|
||||
@@ -200,7 +200,7 @@ export class OrderService {
|
||||
}
|
||||
|
||||
async createInvoice(orderId: string, dto: CreateInvoiceDto) {
|
||||
const { items, discount } = dto
|
||||
const { items } = dto
|
||||
|
||||
const targetOrder = await this.findOneOrFail(orderId)
|
||||
|
||||
@@ -212,6 +212,7 @@ export class OrderService {
|
||||
const order = await this.em.transactional(async (em) => {
|
||||
|
||||
let subTotal = 0
|
||||
let totalDiscount = 0
|
||||
// Calculate order item finnicials
|
||||
|
||||
for (let orderItem of targetOrder.items) {
|
||||
@@ -222,9 +223,10 @@ export class OrderService {
|
||||
}
|
||||
// TODO use Deciaml js to calculation
|
||||
orderItem.unitPrice = found.unitPrice
|
||||
orderItem.totalPrice = (found.unitPrice) * orderItem.quantity
|
||||
orderItem.subTotal = (found.unitPrice) * orderItem.quantity
|
||||
|
||||
subTotal += orderItem.totalPrice
|
||||
|
||||
subTotal += orderItem.subTotal
|
||||
|
||||
em.persist(orderItem)
|
||||
|
||||
@@ -232,10 +234,10 @@ export class OrderService {
|
||||
|
||||
// calculate order financials
|
||||
targetOrder.subTotal = subTotal
|
||||
targetOrder.discount = discount
|
||||
targetOrder.discount = totalDiscount
|
||||
|
||||
targetOrder.total = subTotal - discount
|
||||
targetOrder.balance = subTotal - discount
|
||||
targetOrder.total = subTotal - totalDiscount
|
||||
targetOrder.balance = subTotal - totalDiscount
|
||||
|
||||
// change status
|
||||
targetOrder.status = OrderStatusEnum.INVOICED
|
||||
@@ -311,4 +313,41 @@ export class OrderService {
|
||||
return { message: "Order deleted successfully" }
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user