update order print
This commit is contained in:
@@ -7,7 +7,7 @@ import {
|
||||
CreateOrderAsAdminDto,
|
||||
} from '../dto/create-order.dto';
|
||||
import { UserService } from 'src/modules/user/providers/user.service';
|
||||
import { OrderStatusEnum } from '../interface/order.interface';
|
||||
import { OrderStatusEnum, IOrderPrintResponse } from '../interface/order.interface';
|
||||
import { Order } from '../entities/order.entity';
|
||||
import { AdminService } from 'src/modules/admin/providers/admin.service';
|
||||
import { UpdateOrderAsAdminDto } from '../dto/update-order.dto';
|
||||
@@ -19,8 +19,11 @@ import { InvoiceItem } from 'src/modules/invoice/entities/invoice-item.entity';
|
||||
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
||||
import { UpdateStatusDto } from '../dto/update-status.dto';
|
||||
import { CreateOrderPrintDto } from '../dto/create-order-print.dto';
|
||||
import { OrderPrintValueDto } from '../dto/order-print-value.dto';
|
||||
import { OrderPrint } from '../entities/order-print.entity';
|
||||
import { OrderPrintValue } from '../entities/order-print-value.entity';
|
||||
import { OrderPrintRepository } from '../repositories/order-print.repository';
|
||||
import { Field } from 'src/modules/form-builder/entities/field.entity';
|
||||
import { Request } from 'src/modules/request/entities/request.entity';
|
||||
import { Invoice } from 'src/modules/invoice/entities/invoice.entity';
|
||||
import { OrderCreatedEvent } from '../events/order.events';
|
||||
@@ -44,7 +47,7 @@ export class OrderService {
|
||||
let user: User | null = null
|
||||
|
||||
if (dto.invoiceItemId) {
|
||||
invoiceItem = await this.em.findOne(InvoiceItem, { id: dto.invoiceItemId }, { populate: ['invoice', 'invoice.user'] });
|
||||
invoiceItem = await this.em.findOne(InvoiceItem, { id: dto.invoiceItemId }, { populate: ['invoice', 'invoice.user', 'product'] });
|
||||
if (!invoiceItem) {
|
||||
throw new NotFoundException(`Invoice item with ID ${dto.invoiceItemId} not found.`);
|
||||
}
|
||||
@@ -86,6 +89,8 @@ export class OrderService {
|
||||
if (!product) {
|
||||
throw new NotFoundException(`Product with ID ${dto.productId} not found.`);
|
||||
}
|
||||
} else if (invoiceItem?.product) {
|
||||
product = invoiceItem.product;
|
||||
}
|
||||
|
||||
let designer = null;
|
||||
@@ -233,29 +238,74 @@ export class OrderService {
|
||||
return order
|
||||
}
|
||||
|
||||
async createPrint(orderId: string, dto: CreateOrderPrintDto): Promise<OrderPrint> {
|
||||
const order = await this.findOrderOrFail(orderId);
|
||||
|
||||
if (order.print) {
|
||||
order.print.fileds = dto.fields;
|
||||
await this.em.flush();
|
||||
return order.print;
|
||||
async createPrint(orderId: string, dto: CreateOrderPrintDto): Promise<IOrderPrintResponse> {
|
||||
const order = await this.em.findOne(Order, { id: orderId }, { populate: ['print'] });
|
||||
if (!order) {
|
||||
throw new NotFoundException(`Order with ID ${orderId} not found.`);
|
||||
}
|
||||
|
||||
const print = this.em.create(OrderPrint, {
|
||||
order,
|
||||
fileds: dto.fields,
|
||||
});
|
||||
const fieldMap = await this.resolvePrintFields(dto.fields);
|
||||
|
||||
let print = order.print;
|
||||
if (print) {
|
||||
await this.em.populate(print, ['values']);
|
||||
print.values.removeAll();
|
||||
} else {
|
||||
print = this.em.create(OrderPrint, { order });
|
||||
}
|
||||
|
||||
this.assignPrintValues(print, dto.fields, fieldMap);
|
||||
await this.em.persistAndFlush(print);
|
||||
return print;
|
||||
await this.em.populate(print, ['values', 'values.field']);
|
||||
return this.serializeOrderPrint(print);
|
||||
}
|
||||
|
||||
async getPrint(orderId: string) {
|
||||
const orderPrint = await this.orderPrintRepository.findOne({ order: { id: orderId } });
|
||||
async getPrint(orderId: string): Promise<IOrderPrintResponse> {
|
||||
const orderPrint = await this.orderPrintRepository.findOne(
|
||||
{ order: { id: orderId } },
|
||||
{ populate: ['values', 'values.field'] },
|
||||
);
|
||||
if (!orderPrint) {
|
||||
throw new NotFoundException(`Print with ID ${orderId} not found.`);
|
||||
}
|
||||
return orderPrint;
|
||||
return this.serializeOrderPrint(orderPrint);
|
||||
}
|
||||
|
||||
private serializeOrderPrint(print: OrderPrint): IOrderPrintResponse {
|
||||
return {
|
||||
id: print.id,
|
||||
createdAt: print.createdAt,
|
||||
deletedAt: print.deletedAt,
|
||||
fileds: print.values.getItems().map((item) => ({
|
||||
fieldId: item.field.id,
|
||||
value: item.value,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
private async resolvePrintFields(dtoFields: OrderPrintValueDto[]): Promise<Map<string, Field>> {
|
||||
const fieldIds = [...new Set(dtoFields.map((field) => field.fieldId))];
|
||||
const fields = await this.em.find(Field, { id: { $in: fieldIds } });
|
||||
if (fields.length !== fieldIds.length) {
|
||||
throw new NotFoundException('One or more fields not found.');
|
||||
}
|
||||
return new Map(fields.map((field) => [field.id, field]));
|
||||
}
|
||||
|
||||
private assignPrintValues(
|
||||
print: OrderPrint,
|
||||
dtoFields: OrderPrintValueDto[],
|
||||
fieldMap: Map<string, Field>,
|
||||
): void {
|
||||
for (const dtoField of dtoFields) {
|
||||
print.values.add(
|
||||
this.em.create(OrderPrintValue, {
|
||||
orderPrint: print,
|
||||
field: fieldMap.get(dtoField.fieldId)!,
|
||||
value: dtoField.value,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// -----------Hrlper Methods -----------
|
||||
|
||||
Reference in New Issue
Block a user