update order
This commit is contained in:
@@ -4,20 +4,21 @@ 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, CreateOrderItemAsUserDto, CreateOrderItemDtoAsAdmin } from '../dto/create-order.dto';
|
||||
import {
|
||||
CreateOrderDto, CreateOrderItemAsUserDto,
|
||||
CreateOrderItemDtoAsAdmin, CreateOrderAsAdminDto
|
||||
} from '../dto/create-order.dto';
|
||||
import { UserService } from 'src/modules/user/providers/user.service';
|
||||
import { IAddOrderItem, ICreateOrder, OrderStatusEnum, IUpdateOrderItem } from '../interface/order.interface';
|
||||
import { IAddOrderItem, ICreateOrder, OrderStatusEnum, IUpdateOrderItem, IUpdateOrder } 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';
|
||||
import { TicketService } from 'src/modules/ticket/providers/tickets.service';
|
||||
import { TicketRepository } from 'src/modules/ticket/repositories/tickets.repository';
|
||||
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 { UpdateOrderItemDtoAsAdmin, UpdateOrderItemDtoAsUser } from '../dto/update-order-item.dto';
|
||||
import { UpdateOrderDtoAsUser } from '../dto/update-order-as-user.dto';
|
||||
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
||||
|
||||
|
||||
@@ -117,6 +118,66 @@ export class OrderService {
|
||||
|
||||
}
|
||||
|
||||
async updateOrder(orderId: string, dto: IUpdateOrder) {
|
||||
const { attachments, adminId, designerId, enableTax, estimatedDays, paymentMethod, status, userId } = dto
|
||||
|
||||
const order = await this.findOneOrFail(orderId)
|
||||
|
||||
if (userId) {
|
||||
const user = await this.userService.findById(userId)
|
||||
if (!user) {
|
||||
throw new BadRequestException("User not found!")
|
||||
}
|
||||
order.user = user
|
||||
}
|
||||
|
||||
|
||||
if (adminId) {
|
||||
const admin = await this.adminRepository.findOne({ id: adminId })
|
||||
if (!admin) {
|
||||
throw new BadRequestException("Admin not found")
|
||||
}
|
||||
order.creator = admin
|
||||
}
|
||||
|
||||
|
||||
if (designerId) {
|
||||
const designer = await this.adminRepository.findOne({ id: designerId })
|
||||
if (!designer) {
|
||||
throw new BadRequestException("designer not found")
|
||||
}
|
||||
order.designer = designer
|
||||
}
|
||||
|
||||
if (attachments) {
|
||||
order.attachments = attachments
|
||||
}
|
||||
|
||||
if (typeof enableTax !== 'undefined') {
|
||||
order.enableTax = enableTax
|
||||
}
|
||||
|
||||
if (estimatedDays) {
|
||||
order.estimatedDays = estimatedDays
|
||||
}
|
||||
|
||||
if (paymentMethod) {
|
||||
order.paymentMethod = paymentMethod
|
||||
}
|
||||
|
||||
if (status) {
|
||||
order.status = status
|
||||
}
|
||||
|
||||
this.em.persist(order)
|
||||
|
||||
await this.calculateOrder(order)
|
||||
|
||||
await this.em.flush()
|
||||
|
||||
return order
|
||||
}
|
||||
|
||||
async addOrderItemAsUser(userId: string, orderId: string, dto: CreateOrderItemAsUserDto) {
|
||||
|
||||
const order = await this.findOneOrFail(orderId)
|
||||
@@ -152,7 +213,7 @@ export class OrderService {
|
||||
}
|
||||
|
||||
private async persistOrderItem(order: Order, dto: IAddOrderItem) {
|
||||
const { attributesValues, description, productId, quantity, attachments, discount, unitPrice } = dto
|
||||
const { attributes, description, productId, quantity, attachments, discount, unitPrice } = dto
|
||||
|
||||
const found = order.items.find(it => it.product.id == productId)
|
||||
if (found) {
|
||||
@@ -165,7 +226,7 @@ export class OrderService {
|
||||
}
|
||||
const orderItem = this.orderItemRepository.create({
|
||||
order,
|
||||
attributesValues,
|
||||
attributes,
|
||||
description,
|
||||
quantity,
|
||||
attachments,
|
||||
@@ -212,7 +273,7 @@ export class OrderService {
|
||||
}
|
||||
|
||||
async updateOrderItem(itemId: string, dto: IUpdateOrderItem) {
|
||||
const { attributesValues, description, productId, quantity, attachments, discount, unitPrice } = dto
|
||||
const { attributes, description, productId, quantity, attachments, discount, unitPrice } = dto
|
||||
|
||||
const orderItem = await this.orderItemRepository.findOne({
|
||||
id: itemId
|
||||
@@ -233,8 +294,8 @@ export class OrderService {
|
||||
orderItem.product = product
|
||||
}
|
||||
|
||||
if (attributesValues) {
|
||||
orderItem.attributesValues = attributesValues
|
||||
if (attributes) {
|
||||
orderItem.attributes = attributes
|
||||
}
|
||||
if (description) {
|
||||
orderItem.description = description
|
||||
|
||||
Reference in New Issue
Block a user