From 6fda27491b8ee386117e198ce97617ce75b1c8cf Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Fri, 6 Feb 2026 20:26:10 +0330 Subject: [PATCH] order item --- src/modules/order/dto/create-order.dto.ts | 2 +- .../order/entities/order-item.entity.ts | 34 +++++++++++----- .../order/interface/order.interface.ts | 2 +- src/modules/order/providers/order.service.ts | 40 +------------------ 4 files changed, 28 insertions(+), 50 deletions(-) diff --git a/src/modules/order/dto/create-order.dto.ts b/src/modules/order/dto/create-order.dto.ts index cfcf29f..fddbcf4 100644 --- a/src/modules/order/dto/create-order.dto.ts +++ b/src/modules/order/dto/create-order.dto.ts @@ -131,7 +131,7 @@ export class CreateOrderAsAdminDto { @ApiProperty({}) @IsBoolean() - enableTax: Boolean + enableTax: boolean @ApiProperty({}) @IsInt() diff --git a/src/modules/order/entities/order-item.entity.ts b/src/modules/order/entities/order-item.entity.ts index 8a06fea..15cb6d7 100644 --- a/src/modules/order/entities/order-item.entity.ts +++ b/src/modules/order/entities/order-item.entity.ts @@ -8,7 +8,7 @@ import { BaseEntity } from 'src/common/entities/base.entity'; @Entity({ tableName: 'order_items' }) @Index({ properties: ['order'] }) -export class OrderItem extends BaseEntity{ +export class OrderItem extends BaseEntity { [OptionalProps]?: 'createdAt' | 'deletedAt' | 'subTotal' | 'total'; @ManyToOne(() => Order) @@ -29,18 +29,34 @@ export class OrderItem extends BaseEntity{ @Property({ type: 'decimal', precision: 10, scale: 0, nullable: true, }) unitPrice?: number; - @Formula(alias => ` - COALESCE(${alias}.unit_price, 0) * ${alias}.quantity - `) + // @Formula(alias => ` + // COALESCE(${alias}.unit_price, 0) * ${alias}.quantity + // `) + @Property({ + type: 'decimal', precision: 10, + columnType: 'numeric(10,0) GENERATED ALWAYS AS (COALESCE(unit_price, 0) * quantity) STORED', + persist: false, + scale: 0, + nullable: true + }) subTotal!: number; - @Property({ type: 'decimal', precision: 10, scale: 0, nullable: true }) + @Property({ + type: 'decimal', precision: 10, + scale: 0, nullable: true + }) discount?: number; - @Formula(alias => ` - (COALESCE(${alias}.unit_price, 0) * ${alias}.quantity) - - COALESCE(${alias}.discount, 0) - `) + // @Formula(alias => ` + // (COALESCE(${alias}.unit_price, 0) * ${alias}.quantity) + // - COALESCE(${alias}.discount, 0) + // `) + @Property({ + type: 'decimal', precision: 10, + scale: 0, nullable: true, + columnType: 'numeric(10,0) GENERATED ALWAYS AS ((COALESCE(unit_price, 0) * quantity) - COALESCE(discount, 0)) STORED', + persist: false + }) total!: number; @Property({ type: 'text', nullable: true }) diff --git a/src/modules/order/interface/order.interface.ts b/src/modules/order/interface/order.interface.ts index f653d52..1f4b536 100644 --- a/src/modules/order/interface/order.interface.ts +++ b/src/modules/order/interface/order.interface.ts @@ -54,7 +54,7 @@ export type CreateOrderItemPopulated = Omit { - // return sum + item.subTotal - // }, 0) - - // const totalDiscount = order.items.reduce((sum, item) => { - // return sum + Number(item.discount) - // }, 0) - - - // const totalBeforeTax = subTotal - totalDiscount - // let tax = 0 - - // if (order.enableTax) { - // tax = 0.1 * totalBeforeTax - // } - - // const total = totalBeforeTax + tax - - // // Update Order financial values - // // order.subTotal = subTotal - // // order.discount = totalDiscount - // // order.taxAmount = tax - // // order.total = total - // // order.balance = total - paidAmount - - // return order - // } async confirmOrderItem(userId: string, orderId: string, orderItemId: string) {