diff --git a/src/modules/order/controllers/order.controller.ts b/src/modules/order/controllers/order.controller.ts index 79b6eae..ef57dbd 100644 --- a/src/modules/order/controllers/order.controller.ts +++ b/src/modules/order/controllers/order.controller.ts @@ -20,14 +20,7 @@ export class OrderController { @UseGuards(AuthGuard) @ApiOperation({ summary: 'create order as user' }) createOrder(@UserId() userId: string, @Body() body: CreateOrderDto) { - return this.orderService.createNewOrder(userId, body); - } - - @Patch('public/order/:id/item/:itemId') - @UseGuards(AuthGuard) - @ApiOperation({ summary: 'Update order item as user' }) - updateOrder(@Param('id') orderId: string, @UserId() userId: string, @Param(':itemId') itemId: string, @Body() body: UpdateOrderItemDtoAsUser) { - return this.orderService.updateOrderItemAsUser(userId, orderId, itemId, body); + return this.orderService.createOrderAsUser(userId, body); } @Delete('public/order/:id') @@ -37,14 +30,6 @@ export class OrderController { return this.orderService.removeOrderAsUser(orderId); } - @Patch('public/order/:id/item') - @UseGuards(AuthGuard) - @ApiOperation({ summary: 'add item to order as user' }) - addOrderItem(@Param('id') orderId: string, @UserId() userId: string, @Body() body: CreateOrderItemAsUserDto) { - return this.orderService.addOrderItemAsUser(userId, orderId, body); - } - - @Get('public/orders') @UseGuards(AuthGuard) @ApiOperation({ summary: 'Get all orders with pagination and filters' }) @@ -53,7 +38,25 @@ export class OrderController { return orders } - @Post('public/orders/:orderId/items/:orderItemId') + + @Patch('public/order/:id/item') + @UseGuards(AuthGuard) + @ApiOperation({ summary: 'add item to order as user' }) + addOrderItem(@Param('id') orderId: string, @UserId() userId: string, @Body() body: CreateOrderItemAsUserDto) { + return this.orderService.addOrderItemAsUser(userId, orderId, body); + } + + + @Patch('public/order/:id/item/:itemId') + @UseGuards(AuthGuard) + @ApiOperation({ summary: 'Update order item as user' }) + updateOrder(@Param('id') orderId: string, @UserId() userId: string, @Param(':itemId') itemId: string, @Body() body: UpdateOrderItemDtoAsUser) { + return this.orderService.updateOrderItemAsUser(userId, orderId, itemId, body); + } + + + + @Post('public/orders/:orderId/items/:orderItemId/confirm') @UseGuards(AuthGuard) @ApiOperation({ summary: 'Confirm Invoice Item By User' }) confirmOrderItem( @@ -99,11 +102,11 @@ export class OrderController { return this.orderService.assignDesigner(orderId, body.designerId); } - @Patch('admin/order/:id/item') + @Post('admin/orders/:orderId/invoice') @UseGuards(AuthGuard) - @ApiOperation({ summary: 'add item to order By Admin' }) - addOrderItemAsAdmin(@Param('id') orderId: string, @Body() body: CreateOrderItemDtoAsAdmin) { - return this.orderService.addOrderItemAsAdmin(orderId, body); + @ApiOperation({ summary: 'Create Order invoice ' }) + createInvoice(@Param('orderId') orderId: string) { + return this.orderService.createInvoice(orderId); } @@ -117,6 +120,13 @@ export class OrderController { return this.orderService.removeOrderItemAsAdmin(orderId, orderItemId); } + @Patch('admin/order/:id/item') + @UseGuards(AuthGuard) + @ApiOperation({ summary: 'add item to order By Admin' }) + addOrderItemAsAdmin(@Param('id') orderId: string, @Body() body: CreateOrderItemDtoAsAdmin) { + return this.orderService.addOrderItemAsAdmin(orderId, body); + } + @Patch('admin/order/:id/item/:itemId') @UseGuards(AuthGuard) @ApiOperation({ summary: 'Update order item' }) diff --git a/src/modules/order/dto/update-invoiced-order.dto.ts b/src/modules/order/dto/update-invoiced-order.dto.ts deleted file mode 100644 index 8a755a0..0000000 --- a/src/modules/order/dto/update-invoiced-order.dto.ts +++ /dev/null @@ -1,91 +0,0 @@ -import { - IsArray, IsNumber, - IsNotEmpty, - ArrayMinSize, - IsBoolean, - IsString -} from 'class-validator'; -import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; -import { Type } from 'class-transformer'; - -export class InvoiceOrderItemDto { - @IsNumber() - @ApiProperty() - orderItemId: number; - - @ApiProperty() - @IsNumber() - unitPrice: number - - @ApiProperty() - @IsNumber() - discount: number - - @ApiProperty() - @IsNumber() - quantity: number - -} - -export class InvoiceOrderNewItemDto { - @IsNumber() - @ApiProperty() - productId: number; - - @ApiProperty() - @IsArray() - attributesValues: string[] - - @ApiProperty() - @IsNumber() - quantity: number - - @ApiProperty() - @IsNumber() - unitPrice: number - - @ApiProperty() - @IsNumber() - discount: number - -} - -export class UpdateInvoicedOrderDto { - @IsArray() - @ApiProperty({ - isArray: true, type: [InvoiceOrderItemDto], example: [ - { - orderItemId: 1, - unitPrice: 100, - } - ] - }) - @IsNotEmpty() - @ArrayMinSize(1, { message: 'At least one product is required' }) - @Type(() => InvoiceOrderItemDto) - items: InvoiceOrderItemDto[]; - - @IsArray() - @ApiProperty({ - isArray: true, type: [InvoiceOrderNewItemDto], example: [ - { - orderItemId: 1, - unitPrice: 100, - } - ] - }) - @Type(() => InvoiceOrderNewItemDto) - newItems: InvoiceOrderNewItemDto[]; - - @ApiProperty() - @IsBoolean() - enableTax: boolean - - - @ApiPropertyOptional({ example: [] }) - @IsArray() - @IsString({ each: true }) - attachments: string[] - -} - diff --git a/src/modules/order/dto/update-order.dto.ts b/src/modules/order/dto/update-order-as-user.dto.ts similarity index 62% rename from src/modules/order/dto/update-order.dto.ts rename to src/modules/order/dto/update-order-as-user.dto.ts index f141262..b2a2114 100644 --- a/src/modules/order/dto/update-order.dto.ts +++ b/src/modules/order/dto/update-order-as-user.dto.ts @@ -27,30 +27,9 @@ export class ItemDto { } -export class NewItemDto { - @IsNumber() - @ApiProperty() - productId: number; - @ApiProperty() - @IsArray() - attributesValues: string[] - @ApiProperty() - @IsNumber() - quantity: number - - @ApiProperty() - @IsNumber() - unitPrice: number - - @ApiProperty() - @IsNumber() - discount: number - -} - -export class UpdateOrderDto { +export class UpdateOrderDtoAsUser { @IsArray() @ApiProperty({ isArray: true, type: [ItemDto], example: [ @@ -65,17 +44,6 @@ export class UpdateOrderDto { @Type(() => ItemDto) items: ItemDto[]; - @IsArray() - @ApiProperty({ - isArray: true, type: [ItemDto], example: [ - { - orderItemId: 1, - unitPrice: 100, - } - ] - }) - @Type(() => NewItemDto) - newItems: NewItemDto[]; @ApiProperty() @IsBoolean() diff --git a/src/modules/order/dto/update-order-status.dto.ts b/src/modules/order/dto/update-order-status.dto.ts deleted file mode 100644 index e4026bf..0000000 --- a/src/modules/order/dto/update-order-status.dto.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { ApiPropertyOptional } from '@nestjs/swagger'; -import { IsOptional, IsString } from 'class-validator'; -export class UpdateOrderStatusDto { - @ApiPropertyOptional({ - description: 'Change Status description', - }) - @IsOptional() - @IsString() - desc?: string; -} diff --git a/src/modules/order/providers/order.service.ts b/src/modules/order/providers/order.service.ts index e9ff592..32685a2 100644 --- a/src/modules/order/providers/order.service.ts +++ b/src/modules/order/providers/order.service.ts @@ -17,7 +17,7 @@ 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 { UpdateOrderDto } from '../dto/update-order.dto'; +import { UpdateOrderDtoAsUser } from '../dto/update-order-as-user.dto'; import { Admin } from 'src/modules/admin/entities/admin.entity'; @@ -39,25 +39,55 @@ export class OrderService { private readonly paymentRepository: PaymentRepository, ) { } - async createNewOrder(userId: string, dto: CreateOrderDto) { - const { items } = dto + async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) { + const order = await this.createOrder({ ...dto, adminId }) + await this.calculateOrder(order) + this.em.flush() + return order + + } + + async createOrderAsUser(userId: string, dto: CreateOrderDto) { + const order = await this.createOrder({ ...dto, userId, status: OrderStatusEnum.CREATED }) + return order + + } + + async createOrder(dto: ICreateOrder) { + const { items, userId, attachments, designerId, enableTax, estimatedDays, paymentMethod, status, adminId } = dto + + const user = await this.userService.findById(userId) + if (!user) { + throw new BadRequestException("User not found!") + } + + let admin: null | Admin = null + if (adminId) { + admin = await this.adminRepository.findOne({ id: adminId }) + if (!admin) { + throw new BadRequestException("Admin not found") + } + } + + let designer: null | Admin = null + if (designerId) { + designer = await this.adminRepository.findOne({ id: designerId }) + if (!designer) { + throw new BadRequestException("designer not found") + } + } const order = await this.em.transactional(async (em) => { - const user = await this.userService.findById(userId) - if (!user) { - throw new BadRequestException("user not found") - } const order = this.orderRepository.create({ + creator: admin, user, - discount: 0, - subTotal: 0, - total: 0, - paidAmount: 0, - balance: 0, - status: OrderStatusEnum.CREATED, - taxAmount: 0, - enableTax: false + attachments, + designer, + enableTax, + estimatedDays, + paymentMethod, + status, }) em.persist(order) @@ -69,33 +99,22 @@ export class OrderService { if (productIds.length !== products.length) { throw new BadRequestException("some products not found") } + items.forEach(item => { - const product = products.find(p => p.id == item.productId) - if (!product) { - throw new BadRequestException(`product ${product} not found`) - } - const orderItem = this.orderItemRepository.create({ - order, - attributesValues: item.attributesValues, - product, - quantity: item.quantity, - unitPrice: 0, - total: 0, - subTotal: 0, - discount: 0, - attachments: item.attachments, - description: item.description - }) - em.persist(orderItem) + this.persistOrderItem(order, item) }); + // TODO : calculation must be done after create order - await em.flush() + await this.calculateOrder(order) + + // await em.flush() return order }) return order + } async addOrderItemAsUser(userId: string, orderId: string, dto: CreateOrderItemAsUserDto) { @@ -185,6 +204,10 @@ export class OrderService { const orderItem = await this.updateOrderItem(itemId, dto) + await this.calculateOrder(undefined, orderId) + + await this.em.flush() + return orderItem } @@ -242,6 +265,10 @@ export class OrderService { await this.removeOrderItem(itemId) + await this.calculateOrder(undefined, orderId) + + await this.em.flush() + return true } @@ -277,81 +304,6 @@ export class OrderService { return true } - async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) { - const order = await this.createOrder({ ...dto, adminId }) - return order - - } - - async createOrderAsUser(userId: string, dto: CreateOrderDto) { - const order = await this.createOrder({ ...dto, userId, status: OrderStatusEnum.CREATED }) - return order - - } - - async createOrder(dto: ICreateOrder) { - const { items, userId, attachments, designerId, enableTax, estimatedDays, paymentMethod, status, adminId } = dto - - const user = await this.userService.findById(userId) - if (!user) { - throw new BadRequestException("User not found!") - } - - let admin: null | Admin = null - if (adminId) { - admin = await this.adminRepository.findOne({ id: adminId }) - if (!admin) { - throw new BadRequestException("Admin not found") - } - } - - let designer: null | Admin = null - if (designerId) { - designer = await this.adminRepository.findOne({ id: designerId }) - if (!designer) { - throw new BadRequestException("designer not found") - } - } - - const order = await this.em.transactional(async (em) => { - - const order = this.orderRepository.create({ - creator: admin, - user, - attachments, - designer, - enableTax, - estimatedDays, - paymentMethod, - status, - }) - - em.persist(order) - - const productIds = items.map(item => item.productId) - const products = await this.productRepository.find({ - id: { $in: productIds } - }) - if (productIds.length !== products.length) { - throw new BadRequestException("some products not found") - } - - items.forEach(item => { - this.persistOrderItem(order, item) - }); - - // TODO : calculation must be done after create order - - await this.calculateOrder(order) - - // await em.flush() - - return order - }) - - return order - - } async findUserOrders(userId: string, dto: FindOrdersDto) { const orders = await this.orderRepository.findAllPaginated({ userId, ...dto }) @@ -496,4 +448,10 @@ export class OrderService { return order } + async createInvoice(orderId: string) { + const order = await this.findOneOrFail(orderId) + await this.updateStatus(orderId, OrderStatusEnum.INVOICED) + await this.calculateOrder(order) + await this.em.flush() + } }