diff --git a/src/modules/order/dto/create-order.dto.ts b/src/modules/order/dto/create-order.dto.ts index 8126ccb..65cfa86 100644 --- a/src/modules/order/dto/create-order.dto.ts +++ b/src/modules/order/dto/create-order.dto.ts @@ -10,14 +10,15 @@ import { IAttachment } from '../interface/order.interface'; export class CreateOrderAsAdminDto { - // @ApiPropertyOptional({ example: '' }) - // @IsString() - // @IsNotEmpty() - // userId?: string; - - @ApiProperty({ example: '' }) + @ApiPropertyOptional({ example: '' }) @IsString() - invoiceItemId: string; + @IsNotEmpty() + userId?: string; + + @ApiPropertyOptional({ example: '' }) + @IsString() + @IsOptional() + invoiceItemId?: string; @ApiProperty() @IsString() @@ -28,10 +29,10 @@ export class CreateOrderAsAdminDto { @IsInt() estimatedDays: number; - // @ApiPropertyOptional() - // @IsOptional() - // @IsString() - // productId?: string; + @ApiPropertyOptional() + @IsOptional() + @IsString() + productId?: string; @ApiPropertyOptional() @IsOptional() diff --git a/src/modules/order/entities/order.entity.ts b/src/modules/order/entities/order.entity.ts index 29f3ae0..589bd2a 100644 --- a/src/modules/order/entities/order.entity.ts +++ b/src/modules/order/entities/order.entity.ts @@ -64,10 +64,5 @@ export class Order extends BaseEntity { @Property({ type: 'json' }) attachments: IAttachment[] = [] - @Property({ type: 'string', nullable: true }) - printery?: string - - @Property({ type: 'string', nullable: true }) - Lithography?: string } diff --git a/src/modules/order/providers/order.service.ts b/src/modules/order/providers/order.service.ts index 4aecdaf..3a4e79b 100644 --- a/src/modules/order/providers/order.service.ts +++ b/src/modules/order/providers/order.service.ts @@ -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 { 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; } -} +} \ No newline at end of file