This commit is contained in:
2026-02-22 09:56:10 +03:30
parent 9bc025f1ae
commit 9175e83b1f
3 changed files with 60 additions and 60 deletions
+48 -44
View File
@@ -3,7 +3,10 @@ import { EntityManager } from '@mikro-orm/postgresql';
import { OrderRepository } from '../repositories/order.repository';
import { FindOrdersDto } from '../dto/find-orders.dto';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { CreateOrderAsAdminDto } from '../dto/create-order.dto';
import {
CreateOrderAsAdminDto,
CreateOrderItemDtoAsAdmin,
} from '../dto/create-order.dto';
import { UserService } from 'src/modules/user/providers/user.service';
import { OrderStatusEnum } from '../interface/order.interface';
import { ProductService } from 'src/modules/product/providers/product.service';
@@ -15,15 +18,16 @@ import { Order } from '../entities/order.entity';
import { PaymentRepository } from 'src/modules/payment/repositories/payment.repository';
import { AdminService } from 'src/modules/admin/providers/admin.service';
import { UpdateOrderAsAdminDto } from '../dto/update-order.dto';
import { FindOrderItemsDto } from '../dto/find-order-items.dto';
import { PermissionService } from 'src/modules/roles/providers/permissions.service';
import { PermissionEnum } from 'src/common/enums/permission.enum';
import { User } from 'src/modules/user/entities/user.entity';
import { Product } from 'src/modules/product/entities/product.entity';
import { Category } from 'src/modules/product/entities/category.entity';
import { InvoiceItem } from 'src/modules/invoice/entities/invoice-item.entity';
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
import { UpdateStatusAsDesignerDto } from '../dto/update-status-as-designer.dto';
import { UpdateStatusDto } from '../dto/update-status.dto';
import { Category } from 'src/modules/product/entities/category.entity';
@Injectable()
export class OrderService {
@@ -48,25 +52,26 @@ export class OrderService {
let invoiceItem: null | InvoiceItem = null
let user: User | null = null
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.`);
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) {
user = invoiceItem.invoice.user
if (!dto.userId) {
throw new NotFoundException(`UserId is mandatory`);
}
// if (!invoiceItem) {
const user = await this.userService.findById(dto.userId);
// 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 with ID ${dto.userId} not found.`);
}
}
if (!user) {
throw new NotFoundException(`User not found`);
@@ -78,15 +83,14 @@ export class OrderService {
if (!type) {
throw new NotFoundException(`Order type with ID ${dto.typeId} not found.`);
}
const product = invoiceItem.product
// let product: Product | null = null;
// product = await this.em.findOne(Product, { id: productId });
// if (!product) {
// throw new NotFoundException(`Product with ID ${dto.productId} not found.`);
// }
// if (dto.productId) {
// }
let product: Product | null = null;
if (dto.productId) {
product = await this.em.findOne(Product, { id: dto.productId });
if (!product) {
throw new NotFoundException(`Product with ID ${dto.productId} not found.`);
}
}
const order = this.em.create(Order, {
user,
@@ -145,13 +149,13 @@ export class OrderService {
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.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(Category, { id: dto.typeId });
if (!type) {
@@ -165,17 +169,17 @@ export class OrderService {
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;
// }
// }
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;
@@ -247,4 +251,4 @@ export class OrderService {
}
return order;
}
}
}