diff --git a/src/modules/form-builder/entities/field-option.entity.ts b/src/modules/form-builder/entities/field-option.entity.ts index e2e81a2..04fec55 100644 --- a/src/modules/form-builder/entities/field-option.entity.ts +++ b/src/modules/form-builder/entities/field-option.entity.ts @@ -5,7 +5,7 @@ import { Field } from './field.entity'; @Entity({ tableName: 'field_option' }) export class FieldOption extends BaseEntity { @PrimaryKey({ autoincrement: true, type: 'bigint' }) - id: bigint; + id: number; @Property() value: string; diff --git a/src/modules/form-builder/entities/field.entity.ts b/src/modules/form-builder/entities/field.entity.ts index 4238c78..0da3de2 100644 --- a/src/modules/form-builder/entities/field.entity.ts +++ b/src/modules/form-builder/entities/field.entity.ts @@ -7,7 +7,7 @@ import { BaseEntity } from 'src/common/entities/base.entity'; @Entity({ tableName: 'field' }) export class Field extends BaseEntity { @PrimaryKey({ type: 'bigint', autoincrement: true }) - id: bigint + id: number @OneToMany(() => FieldOption, (attrValue) => attrValue.field) options = new Collection(this) @@ -18,7 +18,7 @@ export class Field extends BaseEntity { // Foreign key based on entityType @Property({ type: 'bigint' }) - entityId: bigint; + entityId: number; @Property() name: string; diff --git a/src/modules/notification/entities/notification.entity.ts b/src/modules/notification/entities/notification.entity.ts index 165d0d5..5b90a2c 100644 --- a/src/modules/notification/entities/notification.entity.ts +++ b/src/modules/notification/entities/notification.entity.ts @@ -7,7 +7,7 @@ import { Admin } from 'src/modules/admin/entities/admin.entity'; @Entity({ tableName: 'notifications' }) export class Notification extends BaseEntity { @PrimaryKey({ type: 'bigint', autoincrement: true }) - id: bigint + id: number @ManyToOne(() => User, { nullable: true }) user?: User; diff --git a/src/modules/order/controllers/order.controller.ts b/src/modules/order/controllers/order.controller.ts index 4af5c4a..6e45fd5 100644 --- a/src/modules/order/controllers/order.controller.ts +++ b/src/modules/order/controllers/order.controller.ts @@ -111,11 +111,18 @@ export class OrderController { } + // @Patch('admin/orders/:orderId') + // @UseGuards(AdminAuthGuard) + // @ApiOperation({ summary: 'Update Order ' }) + // updateOrder(@Param('orderId') orderId: string, @Body() dto: UpdateOrderAsAdminDto) { + // return this.orderService.updateOrderAsAdmin(orderId, dto); + // } + @Patch('admin/orders/:orderId') @UseGuards(AdminAuthGuard) @ApiOperation({ summary: 'Update Order ' }) updateOrder(@Param('orderId') orderId: string, @Body() dto: UpdateOrderAsAdminDto) { - return this.orderService.updateOrderAsAdmin(orderId, dto); + return this.orderService.updateOrderAsAdmin2(orderId, dto); } @Patch('admin/orders/:id/item/:itemId/print') diff --git a/src/modules/order/dto/create-order.dto.ts b/src/modules/order/dto/create-order.dto.ts index e33e6e3..e1c696c 100644 --- a/src/modules/order/dto/create-order.dto.ts +++ b/src/modules/order/dto/create-order.dto.ts @@ -5,7 +5,8 @@ import { ArrayMinSize, IsOptional, IsBoolean, - IsEnum + IsEnum, + IsDateString } from 'class-validator'; import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger'; import { Type } from 'class-transformer'; @@ -57,6 +58,14 @@ export class CreateOrderItemDtoAsAdmin extends CreateOrderItemAsUserDto { @IsNumber() discount: number + @ApiPropertyOptional() + @IsString() + adminDescription: string + + @ApiPropertyOptional() + @IsDateString() + confirmedAt: string + } export class CreateOrderAsUSerDto { @@ -102,8 +111,8 @@ export class CreateOrderAsAdminDto { // printAttachments: [{ url: '', type: '' }], quantity: 100, attributes: [], - unitPrice:150000, - discount:20000 + unitPrice: 150000, + discount: 20000 } ] }) diff --git a/src/modules/order/dto/update-order.dto.ts b/src/modules/order/dto/update-order.dto.ts index 64e3b04..f550eda 100644 --- a/src/modules/order/dto/update-order.dto.ts +++ b/src/modules/order/dto/update-order.dto.ts @@ -1,5 +1,14 @@ -import { OmitType, PartialType } from '@nestjs/swagger'; -import { CreateOrderAsAdminDto } from './create-order.dto'; +import { ApiProperty, OmitType, PartialType } from '@nestjs/swagger'; +import { CreateOrderAsAdminDto, CreateOrderItemDtoAsAdmin } from './create-order.dto'; +import { IsNumber } from 'class-validator'; -export class UpdateOrderAsAdminDto extends PartialType(OmitType(CreateOrderAsAdminDto, 'items' as any)) { } +export class UpdateOrderAsAdminDto extends PartialType(OmitType(CreateOrderAsAdminDto, 'items' as any)) { + @ApiProperty() + items: UpdateOrCreateOrderItem[] +} +export class UpdateOrCreateOrderItem extends CreateOrderItemDtoAsAdmin { + @ApiProperty() + @IsNumber() + id: number +} \ No newline at end of file diff --git a/src/modules/order/entities/order-item.entity.ts b/src/modules/order/entities/order-item.entity.ts index 680013a..5cf4325 100644 --- a/src/modules/order/entities/order-item.entity.ts +++ b/src/modules/order/entities/order-item.entity.ts @@ -12,7 +12,7 @@ export class OrderItem { [OptionalProps]?: 'createdAt' | 'deletedAt' | 'subTotal' | 'total'; @PrimaryKey({ type: 'bigint', autoincrement: true }) - id: bigint + id: number @ManyToOne(() => Order) order!: Order; @@ -55,8 +55,8 @@ export class OrderItem { @Property({ type: 'json', nullable: true }) attachments?: IAttachment[] // user attachments like voice and photos - @Property({ type: 'json', nullable: true }) - printAttachments?: IAttachment[] + // @Property({ type: 'json', nullable: true }) + // printAttachments?: IAttachment[] @Property({ type: 'json', nullable: true }) diff --git a/src/modules/order/providers/order.service.ts b/src/modules/order/providers/order.service.ts index af3a625..1ecd5e8 100644 --- a/src/modules/order/providers/order.service.ts +++ b/src/modules/order/providers/order.service.ts @@ -1,4 +1,4 @@ -import { Injectable, BadRequestException, Logger } from '@nestjs/common'; +import { Injectable, BadRequestException, Logger, BadGatewayException } from '@nestjs/common'; import { EntityManager } from '@mikro-orm/postgresql'; import { OrderRepository } from '../repositories/order.repository'; import { FindOrdersDto } from '../dto/find-orders.dto'; @@ -30,6 +30,7 @@ import { AdminService } from 'src/modules/admin/providers/admin.service'; import { OrderItem } from '../entities/order-item.entity'; import { UpdateOrderAsAdminDto } from '../dto/update-order.dto'; import { CreatePrintFormDto } from '../dto/create-print-form.dto'; +import { Product } from 'src/modules/product/entities/product.entity'; @Injectable() @@ -67,11 +68,10 @@ export class OrderService { } - async updateOrderAsAdmin(orderId: string, dto: UpdateOrderAsAdminDto) { const order = await this.findOrderOrFail(orderId) - const updateOrder = await this.updateOrder(order, dto) + // const updateOrder = await this.updateOrder(order, dto) // const updateOrder = await this.calculateOrder(order) @@ -79,7 +79,82 @@ export class OrderService { this.logger.log("Order updated as admin") - return updateOrder + // return updateOrder + } + + async updateOrderAsAdmin2(orderId: string, dto: UpdateOrderAsAdminDto) { + const order = await this.findOrderOrFail(orderId) + + const { items, ...rest } = dto + + return this.em.transactional(async (em) => { + + em.assign(order, rest) + + for (const item of items) { + if (item.id) { // update + const currentItem = order.items.find(i => Number(i.id) == item.id) + + if (!currentItem) { + throw new BadGatewayException("Order Item not found") + } + + const { designerId, productId, ...itemRest } = item + + let newProduct: Product | undefined + + if (productId && productId !== currentItem.product.id) { + + newProduct = await this.productService.findOneOrFail(productId) + + } + + let newDesigner: Admin | undefined + + if (designerId && designerId !== currentItem.designer?.id) { + + newDesigner = await this.adminService.findOrFail(designerId) + + } + + em.assign(currentItem, { ...itemRest, designer: newDesigner, product: newProduct }) + + + } else { + + if (!item.productId) { + throw new BadRequestException(`Product id is required for item ${item.id}`) + } + + const product = await this.productService.findOneOrFail(item.productId) + + const newOrderItem = em.create(OrderItem, { + order, + product, + quantity: item.quantity, + unitPrice: item.unitPrice, + + adminDescription: item.adminDescription, + attachments: item.attachments, + attributes: item.attributes, + description: item.description, + discount: item.discount, + confirmedAt: item.confirmedAt, + + }) + + em.persist(newOrderItem) + } + } + + await em.flush() + + this.logger.log("Order updated as admin") + + return order + }) + + } async addOrderItemAsUser(userId: string, orderId: string, dto: CreateOrderItemAsUserDto) { @@ -354,7 +429,7 @@ export class OrderService { unitPrice, product, printAttributes: printAttributes, - printAttachments + // printAttachments }) } @@ -476,9 +551,7 @@ export class OrderService { if (attributes != undefined) { orderItem.attributes = attributes } - if (printAttachments != undefined) { - orderItem.printAttachments = printAttachments - } + if (description != undefined) { orderItem.description = description } @@ -537,7 +610,7 @@ export class OrderService { async findOrderOrFail(orderId: string) { const order = await this.orderRepository.findOne({ id: orderId }, - { populate: ['items', 'items.product', 'payments', 'user'] }) + { populate: ['items', 'items.product', 'payments', 'user', 'items.designer'] }) if (!order) { throw new BadRequestException('Order not found') } diff --git a/src/modules/print/entities/print.entity.ts b/src/modules/print/entities/print.entity.ts index 1c0d967..38169e3 100644 --- a/src/modules/print/entities/print.entity.ts +++ b/src/modules/print/entities/print.entity.ts @@ -5,7 +5,7 @@ import { BaseEntity } from 'src/common/entities/base.entity'; @Entity({ tableName: 'print' }) export class Print extends BaseEntity { @PrimaryKey({ type: 'bigint', autoincrement: true }) - id: bigint + id: number // @OneToMany(() => Field, (attrValue) => attrValue.entityId, { cascade: [Cascade.PERSIST, Cascade.REMOVE] }) // fields = new Collection(this) diff --git a/src/modules/product/controllers/product.controller.ts b/src/modules/product/controllers/product.controller.ts index 8aee46e..54f736b 100644 --- a/src/modules/product/controllers/product.controller.ts +++ b/src/modules/product/controllers/product.controller.ts @@ -143,7 +143,7 @@ export class ProductController { @ApiOperation({ summary: 'Get a product by id' }) @ApiParam({ name: 'id', required: true }) findById(@Param('id') id: string,) { - return this.productService.findById(id); + return this.productService.findById(+id); } @Patch('admin/products/:id') @@ -154,7 +154,7 @@ export class ProductController { @ApiParam({ name: 'id', required: true }) @ApiBody({ type: UpdateproductDto }) update(@Param('id') id: string, @Body() updateproductDto: UpdateproductDto) { - return this.productService.update(id, updateproductDto); + return this.productService.update(+id, updateproductDto); } @Delete('admin/products/:id') @@ -164,7 +164,7 @@ export class ProductController { @ApiOperation({ summary: 'Delete (soft) a product' }) @ApiParam({ name: 'id', required: true }) remove(@Param('id') id: string,) { - return this.productService.remove(id); + return this.productService.remove(+id); } diff --git a/src/modules/product/entities/product.entity.ts b/src/modules/product/entities/product.entity.ts index e45737b..45c8460 100644 --- a/src/modules/product/entities/product.entity.ts +++ b/src/modules/product/entities/product.entity.ts @@ -11,7 +11,7 @@ export class Product extends BaseEntity { @PrimaryKey({ type: 'bigint', autoincrement: true }) - id: bigint + id: number @Property() title: string; diff --git a/src/modules/product/providers/product.service.ts b/src/modules/product/providers/product.service.ts index db55550..9f203c3 100644 --- a/src/modules/product/providers/product.service.ts +++ b/src/modules/product/providers/product.service.ts @@ -44,7 +44,7 @@ export class ProductService { } - async update(productId: string, createproductDto: Partial) { + async update(productId: number, createproductDto: Partial) { const { categoryId, ...rest } = createproductDto; const product = await this.productRepository.findOne({ id: productId }) if (!product) { @@ -70,7 +70,7 @@ export class ProductService { return this.productRepository.findAllPaginated(dto); } - async findById(productId: string) { + async findById(productId: number) { const product = await this.productRepository.findOne({ id: productId }, { populate: ['category'] }); @@ -79,7 +79,7 @@ export class ProductService { return product; } - async remove(id: string): Promise { + async remove(id: number): Promise { const product = await this.productRepository.findOne({ id }); if (!product) { throw new NotFoundException(productMessage.NOT_FOUND);