order calc
This commit is contained in:
@@ -6,7 +6,7 @@ import { FindOrdersDto } from '../dto/find-orders.dto';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { CreateOrderDto, CreateOrderItemAsUserDto, CreateOrderItemDtoAsAdmin } from '../dto/create-order.dto';
|
||||
import { UserService } from 'src/modules/user/providers/user.service';
|
||||
import { AddOrderItem, OrderStatusEnum, UpdateOrderItem } from '../interface/order.interface';
|
||||
import { IAddOrderItem, ICreateOrder, OrderStatusEnum, IUpdateOrderItem } from '../interface/order.interface';
|
||||
import { OrderItemRepository } from '../repositories/order-item.repository';
|
||||
import { ProductService } from 'src/modules/product/providers/product.service';
|
||||
import { ProductRepository } from 'src/modules/product/repositories/product.repository';
|
||||
@@ -18,6 +18,7 @@ import { Order } from '../entities/order.entity';
|
||||
import { PaymentRepository } from 'src/modules/payment/repositories/payment.repository';
|
||||
import { UpdateOrderItemDtoAsAdmin, UpdateOrderItemDtoAsUser } from '../dto/update-order-item.dto';
|
||||
import { UpdateOrderDto } from '../dto/update-order.dto';
|
||||
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
||||
|
||||
|
||||
@Injectable()
|
||||
@@ -131,7 +132,7 @@ export class OrderService {
|
||||
return orderItem
|
||||
}
|
||||
|
||||
private async persistOrderItem(order: Order, dto: AddOrderItem) {
|
||||
private async persistOrderItem(order: Order, dto: IAddOrderItem) {
|
||||
const { attributesValues, description, productId, quantity, attachments, discount, unitPrice } = dto
|
||||
|
||||
const found = order.items.find(it => it.product.id == productId)
|
||||
@@ -187,7 +188,7 @@ export class OrderService {
|
||||
return orderItem
|
||||
}
|
||||
|
||||
async updateOrderItem(itemId: string, dto: UpdateOrderItem) {
|
||||
async updateOrderItem(itemId: string, dto: IUpdateOrderItem) {
|
||||
const { attributesValues, description, productId, quantity, attachments, discount, unitPrice } = dto
|
||||
|
||||
const orderItem = await this.orderItemRepository.findOne({
|
||||
@@ -277,16 +278,39 @@ export class OrderService {
|
||||
}
|
||||
|
||||
async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) {
|
||||
const { items, userPhone } = dto
|
||||
const order = await this.createOrder({ ...dto, adminId })
|
||||
return order
|
||||
|
||||
const user = await this.userService.findByPhone(userPhone)
|
||||
}
|
||||
|
||||
async createOrderAsUser(userId: string, dto: CreateOrderDto) {
|
||||
const order = await this.createOrder({ ...dto, userId, status: OrderStatusEnum.CREATED })
|
||||
return order
|
||||
|
||||
}
|
||||
|
||||
async createOrder(dto: ICreateOrder) {
|
||||
const { items, userId, attachments, designerId, enableTax, estimatedDays, paymentMethod, status, adminId } = dto
|
||||
|
||||
const user = await this.userService.findById(userId)
|
||||
if (!user) {
|
||||
throw new BadRequestException("User with this phone not found!")
|
||||
throw new BadRequestException("User not found!")
|
||||
}
|
||||
|
||||
const admin = await this.adminRepository.findOne({ id: adminId })
|
||||
if (!admin) {
|
||||
throw new BadRequestException("Admin not found")
|
||||
let admin: null | Admin = null
|
||||
if (adminId) {
|
||||
admin = await this.adminRepository.findOne({ id: adminId })
|
||||
if (!admin) {
|
||||
throw new BadRequestException("Admin not found")
|
||||
}
|
||||
}
|
||||
|
||||
let designer: null | Admin = null
|
||||
if (designerId) {
|
||||
designer = await this.adminRepository.findOne({ id: designerId })
|
||||
if (!designer) {
|
||||
throw new BadRequestException("designer not found")
|
||||
}
|
||||
}
|
||||
|
||||
const order = await this.em.transactional(async (em) => {
|
||||
@@ -294,13 +318,12 @@ export class OrderService {
|
||||
const order = this.orderRepository.create({
|
||||
creator: admin,
|
||||
user,
|
||||
discount: 0,
|
||||
subTotal: 0,
|
||||
total: 0,
|
||||
paidAmount: 0,
|
||||
balance: 0,
|
||||
status: OrderStatusEnum.INVOICED,
|
||||
taxAmount: 0
|
||||
attachments,
|
||||
designer,
|
||||
enableTax,
|
||||
estimatedDays,
|
||||
paymentMethod,
|
||||
status,
|
||||
})
|
||||
|
||||
em.persist(order)
|
||||
@@ -314,33 +337,14 @@ export class OrderService {
|
||||
}
|
||||
|
||||
items.forEach(item => {
|
||||
const product = products.find(p => p.id == item.productId)
|
||||
|
||||
if (!product) {
|
||||
throw new BadRequestException(`product ${product} not found`)
|
||||
}
|
||||
|
||||
const orderItem = this.orderItemRepository.create({
|
||||
order,
|
||||
attributesValues: item.attributesValues,
|
||||
product,
|
||||
quantity: item.quantity,
|
||||
unitPrice: item.unitPrice,
|
||||
total: item.unitPrice * item.quantity,
|
||||
discount: item.discount,
|
||||
subTotal: item.unitPrice * item.quantity - item.discount,
|
||||
attachments: item.attachments,
|
||||
description: item.description
|
||||
})
|
||||
|
||||
em.persist(orderItem)
|
||||
this.persistOrderItem(order, item)
|
||||
});
|
||||
|
||||
// TODO : calculation must be done after create order
|
||||
|
||||
await this.calculateOrder(order)
|
||||
|
||||
await em.flush()
|
||||
// await em.flush()
|
||||
|
||||
return order
|
||||
})
|
||||
@@ -381,7 +385,7 @@ export class OrderService {
|
||||
// TODO : use reduce
|
||||
for (let orderItem of order.items) {
|
||||
subTotal += orderItem.total
|
||||
totalDiscount += orderItem.discount
|
||||
totalDiscount += Number(orderItem.discount)
|
||||
}
|
||||
|
||||
const totalBeforeTax = subTotal - totalDiscount
|
||||
|
||||
Reference in New Issue
Block a user