This commit is contained in:
2026-02-20 11:13:06 +03:30
parent 75a4154f90
commit 5d4154d135
4 changed files with 108 additions and 29 deletions
+76 -4
View File
@@ -24,6 +24,7 @@ import { PermissionEnum } from 'src/common/enums/permission.enum';
import { User } from 'src/modules/user/entities/user.entity';
import { OrderType } from '../entities/order-type.entity';
import { Product } from 'src/modules/product/entities/product.entity';
import { InvoiceItem } from 'src/modules/invoice/entities/invoice-item.entity';
@Injectable()
export class OrderService {
@@ -45,11 +46,34 @@ export class OrderService {
) { }
async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto): Promise<Order> {
const user = await this.userService.findById(dto.userId);
if (!user) {
throw new NotFoundException(`User with ID ${dto.userId} not found.`);
let invoiceItem: null | InvoiceItem = null
let user: User | null = null
if (dto.invoiceItemId) {
invoiceItem = await this.em.findOne(InvoiceItem, { id: dto.invoiceItemId },{populate:['invoice','invoice.user']});
if (!invoiceItem) {
throw new NotFoundException(`Invoice item with ID ${dto.invoiceItemId} not found.`);
}
user=invoiceItem.invoice.user
}
if (!invoiceItem) {
if (!dto.userId) {
throw new NotFoundException(`UserId is mandatory`);
}
const user = await this.userService.findById(dto.userId);
if (!user) {
throw new NotFoundException(`User with ID ${dto.userId} not found.`);
}
}
if(!user){
throw new NotFoundException(`User not found`);
}
const creator = await this.adminService.findOrFail(adminId);
const type = await this.em.findOne(OrderType, { id: dto.typeId });
@@ -66,16 +90,64 @@ export class OrderService {
}
const order = this.em.create(Order, {
user: user as User,
user,
creator,
type,
product: product ?? null,
title: dto.title,
status: OrderStatusEnum.CREATED,
estimatedDays: dto.estimatedDays,
invoiceItem
});
await this.em.persistAndFlush(order);
return order;
}
async findOrderOrFail(id: string): Promise<Order> {
const order = await this.em.findOne(Order, { id }, { populate: ['user', 'type', 'product', 'creator'] });
if (!order) {
throw new NotFoundException(`Order with ID ${id} not found.`);
}
return order;
}
// user should not be change able
async updateOrderAsAdmin(id: string, dto: UpdateOrderAsAdminDto): Promise<Order> {
const order = await this.findOrderOrFail(id);
if (dto.userId !== undefined) {
const user = await this.userService.findById(dto.userId);
if (!user) {
throw new NotFoundException(`User with ID ${dto.userId} not found.`);
}
order.user = user as User;
}
if (dto.typeId !== undefined) {
const type = await this.em.findOne(OrderType, { id: dto.typeId });
if (!type) {
throw new NotFoundException(`Order type with ID ${dto.typeId} not found.`);
}
order.type = type;
}
if (dto.estimatedDays !== undefined) {
order.estimatedDays = dto.estimatedDays;
}
if (dto.title !== undefined) {
order.title = dto.title;
}
if (dto.productId !== undefined) {
if (dto.productId == null || dto.productId === '') {
order.product = null;
} else {
const product = await this.em.findOne(Product, { id: dto.productId });
if (!product) {
throw new NotFoundException(`Product with ID ${dto.productId} not found.`);
}
order.product = product;
}
}
await this.em.flush();
return order;
}
}