order entity modification
This commit is contained in:
@@ -42,14 +42,16 @@ export class OrderService {
|
||||
|
||||
async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) {
|
||||
const order = await this.createOrder({ ...dto, adminId })
|
||||
await this.calculateOrder(order)
|
||||
this.em.flush()
|
||||
// await this.calculateOrder(order)
|
||||
// this.em.flush()
|
||||
this.logger.log("Order created as admin")
|
||||
return order
|
||||
|
||||
}
|
||||
|
||||
async createOrderAsUser(userId: string, dto: CreateOrderDto) {
|
||||
const order = await this.createOrder({ ...dto, userId, status: OrderStatusEnum.CREATED })
|
||||
this.logger.log("Order created as admin")
|
||||
return order
|
||||
|
||||
}
|
||||
@@ -101,13 +103,13 @@ export class OrderService {
|
||||
throw new BadRequestException("some products not found")
|
||||
}
|
||||
|
||||
items.forEach(item => {
|
||||
for (const item of items) {
|
||||
this.persistOrderItem(order, item)
|
||||
});
|
||||
}
|
||||
|
||||
// TODO : calculation must be done after create order
|
||||
|
||||
await this.calculateOrder(order)
|
||||
// await this.calculateOrder(order)
|
||||
|
||||
// await em.flush()
|
||||
|
||||
@@ -131,7 +133,7 @@ export class OrderService {
|
||||
}
|
||||
|
||||
|
||||
if (adminId) {
|
||||
if (adminId != undefined) {
|
||||
const admin = await this.adminRepository.findOne({ id: adminId })
|
||||
if (!admin) {
|
||||
throw new BadRequestException("Admin not found")
|
||||
@@ -140,7 +142,7 @@ export class OrderService {
|
||||
}
|
||||
|
||||
|
||||
if (designerId) {
|
||||
if (designerId != undefined) {
|
||||
const designer = await this.adminRepository.findOne({ id: designerId })
|
||||
if (!designer) {
|
||||
throw new BadRequestException("designer not found")
|
||||
@@ -148,31 +150,27 @@ export class OrderService {
|
||||
order.designer = designer
|
||||
}
|
||||
|
||||
if (attachments) {
|
||||
if (attachments != undefined) {
|
||||
order.attachments = attachments
|
||||
}
|
||||
|
||||
if (typeof enableTax !== 'undefined') {
|
||||
if (enableTax != undefined) {
|
||||
order.enableTax = enableTax
|
||||
}
|
||||
|
||||
if (estimatedDays) {
|
||||
if (estimatedDays != undefined) {
|
||||
order.estimatedDays = estimatedDays
|
||||
}
|
||||
|
||||
if (paymentMethod) {
|
||||
if (paymentMethod != undefined) {
|
||||
order.paymentMethod = paymentMethod
|
||||
}
|
||||
|
||||
if (status) {
|
||||
if (status != undefined) {
|
||||
order.status = status
|
||||
}
|
||||
|
||||
this.em.persist(order)
|
||||
|
||||
await this.calculateOrder(order)
|
||||
|
||||
await this.em.flush()
|
||||
await this.em.persistAndFlush(order)
|
||||
|
||||
return order
|
||||
}
|
||||
@@ -180,11 +178,13 @@ export class OrderService {
|
||||
async updateOrderAsAdmin(orderId: string, dto: IUpdateOrder) {
|
||||
const order = await this.findOneOrFail(orderId)
|
||||
|
||||
await this.updateOrder(order, dto)
|
||||
const updateOrder= await this.updateOrder(order, dto)
|
||||
|
||||
const updateOrder = await this.calculateOrder(order)
|
||||
// const updateOrder = await this.calculateOrder(order)
|
||||
|
||||
await this.em.flush()
|
||||
// await this.em.flush()
|
||||
|
||||
this.logger.log("Order updated as admin")
|
||||
|
||||
return updateOrder
|
||||
}
|
||||
@@ -216,9 +216,9 @@ export class OrderService {
|
||||
|
||||
await this.em.flush()
|
||||
|
||||
await this.calculateOrder(undefined, orderId)
|
||||
// await this.calculateOrder(undefined, orderId)
|
||||
|
||||
await this.em.flush()
|
||||
// await this.em.flush()
|
||||
|
||||
return orderItem
|
||||
}
|
||||
@@ -276,9 +276,9 @@ export class OrderService {
|
||||
|
||||
const orderItem = await this.updateOrderItem(itemId, dto)
|
||||
|
||||
await this.calculateOrder(undefined, orderId)
|
||||
// await this.calculateOrder(undefined, orderId)
|
||||
|
||||
await this.em.flush()
|
||||
// await this.em.flush()
|
||||
|
||||
return orderItem
|
||||
}
|
||||
@@ -337,9 +337,9 @@ export class OrderService {
|
||||
|
||||
await this.removeOrderItem(itemId)
|
||||
|
||||
await this.calculateOrder(undefined, orderId)
|
||||
// await this.calculateOrder(undefined, orderId)
|
||||
|
||||
await this.em.flush()
|
||||
// await this.em.flush()
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -391,44 +391,44 @@ export class OrderService {
|
||||
return order
|
||||
}
|
||||
|
||||
async calculateOrder(inputOrder?: Order, orderId?: string) {
|
||||
let order: undefined | Order = inputOrder
|
||||
// async calculateOrder(inputOrder?: Order, orderId?: string) {
|
||||
// let order: undefined | Order = inputOrder
|
||||
|
||||
if (!order && orderId) {
|
||||
order = await this.findOneOrFail(orderId)
|
||||
}
|
||||
// if (!order && orderId) {
|
||||
// order = await this.findOneOrFail(orderId)
|
||||
// }
|
||||
|
||||
if (!order) {
|
||||
throw new BadRequestException("Order not found")
|
||||
}
|
||||
// calculate order financials
|
||||
let subTotal = 0
|
||||
let totalDiscount = 0
|
||||
// if (!order) {
|
||||
// throw new BadRequestException("Order not found")
|
||||
// }
|
||||
// // calculate order financials
|
||||
// const subTotal = order.items.reduce((sum, item) => {
|
||||
// return sum + item.subTotal
|
||||
// }, 0)
|
||||
|
||||
// TODO : use reduce
|
||||
for (let orderItem of order.items) {
|
||||
subTotal += orderItem.total
|
||||
totalDiscount += Number(orderItem.discount)
|
||||
}
|
||||
// const totalDiscount = order.items.reduce((sum, item) => {
|
||||
// return sum + Number(item.discount)
|
||||
// }, 0)
|
||||
|
||||
const totalBeforeTax = subTotal - totalDiscount
|
||||
let tax = 0
|
||||
|
||||
if (order.enableTax) {
|
||||
tax = 0.1 * totalBeforeTax
|
||||
}
|
||||
// const totalBeforeTax = subTotal - totalDiscount
|
||||
// let tax = 0
|
||||
|
||||
const total = totalBeforeTax + tax
|
||||
const paidAmount = await this.paymentRepository.getActualPaidAmount(order.id)
|
||||
// Update Order financial values
|
||||
order.subTotal = subTotal
|
||||
order.discount = totalDiscount
|
||||
order.taxAmount = tax
|
||||
order.total = total
|
||||
order.balance = total - paidAmount
|
||||
// if (order.enableTax) {
|
||||
// tax = 0.1 * totalBeforeTax
|
||||
// }
|
||||
|
||||
return order
|
||||
}
|
||||
// const total = totalBeforeTax + tax
|
||||
|
||||
// // Update Order financial values
|
||||
// // order.subTotal = subTotal
|
||||
// // order.discount = totalDiscount
|
||||
// // order.taxAmount = tax
|
||||
// // order.total = total
|
||||
// // order.balance = total - paidAmount
|
||||
|
||||
// return order
|
||||
// }
|
||||
|
||||
async confirmOrderItem(userId: string, orderId: string, orderItemId: number) {
|
||||
|
||||
@@ -477,7 +477,7 @@ export class OrderService {
|
||||
throw new BadRequestException("Order not found")
|
||||
}
|
||||
if (![OrderStatusEnum.CREATED, OrderStatusEnum.INVOICED].includes(order.status)) {
|
||||
throw new BadRequestException("Order status must be of of Drfat or Invoiced")
|
||||
throw new BadRequestException("Order status must be of of Draft or Invoiced")
|
||||
}
|
||||
if (order.payments.length > 0) {
|
||||
throw new BadRequestException("order with payments can not be deleted!")
|
||||
@@ -522,7 +522,7 @@ export class OrderService {
|
||||
async createInvoice(orderId: string) {
|
||||
const order = await this.findOneOrFail(orderId)
|
||||
await this.updateStatus(orderId, OrderStatusEnum.INVOICED)
|
||||
await this.calculateOrder(order)
|
||||
// await this.calculateOrder(order)
|
||||
await this.em.flush()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user