invoice
This commit is contained in:
@@ -17,7 +17,7 @@ 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 { UpdateOrderItemDtoAsAdmin, UpdateOrderItemDtoAsUser } from '../dto/update-order-item.dto';
|
||||
import { UpdateOrderDto } from '../dto/update-order.dto';
|
||||
import { UpdateOrderDtoAsUser } from '../dto/update-order-as-user.dto';
|
||||
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
||||
|
||||
|
||||
@@ -39,25 +39,55 @@ export class OrderService {
|
||||
private readonly paymentRepository: PaymentRepository,
|
||||
) { }
|
||||
|
||||
async createNewOrder(userId: string, dto: CreateOrderDto) {
|
||||
const { items } = dto
|
||||
async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) {
|
||||
const order = await this.createOrder({ ...dto, adminId })
|
||||
await this.calculateOrder(order)
|
||||
this.em.flush()
|
||||
return order
|
||||
|
||||
}
|
||||
|
||||
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 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) => {
|
||||
const user = await this.userService.findById(userId)
|
||||
if (!user) {
|
||||
throw new BadRequestException("user not found")
|
||||
}
|
||||
|
||||
const order = this.orderRepository.create({
|
||||
creator: admin,
|
||||
user,
|
||||
discount: 0,
|
||||
subTotal: 0,
|
||||
total: 0,
|
||||
paidAmount: 0,
|
||||
balance: 0,
|
||||
status: OrderStatusEnum.CREATED,
|
||||
taxAmount: 0,
|
||||
enableTax: false
|
||||
attachments,
|
||||
designer,
|
||||
enableTax,
|
||||
estimatedDays,
|
||||
paymentMethod,
|
||||
status,
|
||||
})
|
||||
|
||||
em.persist(order)
|
||||
@@ -69,33 +99,22 @@ export class OrderService {
|
||||
if (productIds.length !== products.length) {
|
||||
throw new BadRequestException("some products not found")
|
||||
}
|
||||
|
||||
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: 0,
|
||||
total: 0,
|
||||
subTotal: 0,
|
||||
discount: 0,
|
||||
attachments: item.attachments,
|
||||
description: item.description
|
||||
})
|
||||
em.persist(orderItem)
|
||||
this.persistOrderItem(order, item)
|
||||
});
|
||||
|
||||
// TODO : calculation must be done after create order
|
||||
|
||||
await em.flush()
|
||||
await this.calculateOrder(order)
|
||||
|
||||
// await em.flush()
|
||||
|
||||
return order
|
||||
})
|
||||
|
||||
return order
|
||||
|
||||
}
|
||||
|
||||
async addOrderItemAsUser(userId: string, orderId: string, dto: CreateOrderItemAsUserDto) {
|
||||
@@ -185,6 +204,10 @@ export class OrderService {
|
||||
|
||||
const orderItem = await this.updateOrderItem(itemId, dto)
|
||||
|
||||
await this.calculateOrder(undefined, orderId)
|
||||
|
||||
await this.em.flush()
|
||||
|
||||
return orderItem
|
||||
}
|
||||
|
||||
@@ -242,6 +265,10 @@ export class OrderService {
|
||||
|
||||
await this.removeOrderItem(itemId)
|
||||
|
||||
await this.calculateOrder(undefined, orderId)
|
||||
|
||||
await this.em.flush()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -277,81 +304,6 @@ export class OrderService {
|
||||
return true
|
||||
}
|
||||
|
||||
async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) {
|
||||
const order = await this.createOrder({ ...dto, adminId })
|
||||
return order
|
||||
|
||||
}
|
||||
|
||||
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 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) => {
|
||||
|
||||
const order = this.orderRepository.create({
|
||||
creator: admin,
|
||||
user,
|
||||
attachments,
|
||||
designer,
|
||||
enableTax,
|
||||
estimatedDays,
|
||||
paymentMethod,
|
||||
status,
|
||||
})
|
||||
|
||||
em.persist(order)
|
||||
|
||||
const productIds = items.map(item => item.productId)
|
||||
const products = await this.productRepository.find({
|
||||
id: { $in: productIds }
|
||||
})
|
||||
if (productIds.length !== products.length) {
|
||||
throw new BadRequestException("some products not found")
|
||||
}
|
||||
|
||||
items.forEach(item => {
|
||||
this.persistOrderItem(order, item)
|
||||
});
|
||||
|
||||
// TODO : calculation must be done after create order
|
||||
|
||||
await this.calculateOrder(order)
|
||||
|
||||
// await em.flush()
|
||||
|
||||
return order
|
||||
})
|
||||
|
||||
return order
|
||||
|
||||
}
|
||||
|
||||
async findUserOrders(userId: string, dto: FindOrdersDto) {
|
||||
const orders = await this.orderRepository.findAllPaginated({ userId, ...dto })
|
||||
@@ -496,4 +448,10 @@ export class OrderService {
|
||||
return order
|
||||
}
|
||||
|
||||
async createInvoice(orderId: string) {
|
||||
const order = await this.findOneOrFail(orderId)
|
||||
await this.updateStatus(orderId, OrderStatusEnum.INVOICED)
|
||||
await this.calculateOrder(order)
|
||||
await this.em.flush()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user