diff --git a/src/modules/order/controllers/order.controller.ts b/src/modules/order/controllers/order.controller.ts index e88ad91..dbfc0cd 100644 --- a/src/modules/order/controllers/order.controller.ts +++ b/src/modules/order/controllers/order.controller.ts @@ -23,6 +23,7 @@ export class OrderController { @UseGuards(AuthGuard) @ApiOperation({ summary: 'create order ' }) createOrder(@UserId() userId: string, @Body() body: CreateOrderDto) { + console.log(userId) return this.orderService.createOrderAsUser(userId, body); } diff --git a/src/modules/order/entities/order-item.entity.ts b/src/modules/order/entities/order-item.entity.ts index da61674..197c982 100644 --- a/src/modules/order/entities/order-item.entity.ts +++ b/src/modules/order/entities/order-item.entity.ts @@ -8,7 +8,7 @@ import { IAttribute } from '../interface/order.interface'; @Entity({ tableName: 'order_items' }) @Index({ properties: ['order'] }) export class OrderItem extends BaseEntity { - + @PrimaryKey({ type: 'bigint', autoincrement: true }) id: bigint @@ -18,8 +18,8 @@ export class OrderItem extends BaseEntity { @ManyToOne(() => Product) product!: Product; - @Property({ type: 'json' }) - attributes: IAttribute[] + @Property({ type: 'json', nullable: true }) + attributes?: IAttribute[] @Property({ type: 'int' }) quantity!: number; diff --git a/src/modules/order/entities/order.entity.ts b/src/modules/order/entities/order.entity.ts index 97c7d1d..4d58559 100644 --- a/src/modules/order/entities/order.entity.ts +++ b/src/modules/order/entities/order.entity.ts @@ -74,14 +74,14 @@ export class Order { // @Property({ type: 'decimal', precision: 10, scale: 0, default: null, nullable: true }) // subTotal?: number; - @Formula(alias => ` - ( - SELECT COALESCE(SUM(oi.sub_total), 0) - FROM order_items oi - WHERE oi.order_id = ${alias}.id - ) - `) - subTotal!: number; +@Formula(alias => ` + ( + SELECT COALESCE(SUM(COALESCE(oi.unit_price, 0) * oi.quantity), 0) + FROM order_items oi + WHERE oi.order_id = ${alias}.id + ) +`) +subTotal!: number; // @Property({ type: 'decimal', precision: 10, scale: 0, default: null, nullable: true }) @@ -91,7 +91,7 @@ export class Order { CASE WHEN ${alias}.enable_tax = true THEN ( - SELECT COALESCE(SUM(oi.sub_total), 0) + SELECT COALESCE(SUM(COALESCE(oi.unit_price, 0) * oi.quantity), 0) FROM order_items oi WHERE oi.order_id = ${alias}.id ) * 0.1 @@ -108,7 +108,7 @@ export class Order { @Formula(alias => ` ( ( - SELECT COALESCE(SUM(oi.sub_total), 0) + SELECT COALESCE(SUM(COALESCE(oi.unit_price, 0) * oi.quantity), 0) FROM order_items oi WHERE oi.order_id = ${alias}.id ) @@ -122,7 +122,7 @@ export class Order { CASE WHEN ${alias}.enable_tax = true THEN ( - SELECT COALESCE(SUM(oi.sub_total), 0) + SELECT COALESCE(SUM(COALESCE(oi.unit_price, 0) * oi.quantity), 0) FROM order_items oi WHERE oi.order_id = ${alias}.id ) * 0.1 @@ -158,7 +158,7 @@ export class Order { ( ( ( - SELECT COALESCE(SUM(oi.sub_total), 0) + SELECT COALESCE(SUM(COALESCE(oi.unit_price, 0) * oi.quantity), 0) FROM order_items oi WHERE oi.order_id = ${alias}.id ) @@ -172,7 +172,7 @@ export class Order { CASE WHEN ${alias}.enable_tax = true THEN ( - SELECT COALESCE(SUM(oi.sub_total), 0) + SELECT COALESCE(SUM(COALESCE(oi.unit_price, 0) * oi.quantity), 0) FROM order_items oi WHERE oi.order_id = ${alias}.id ) * 0.1 @@ -227,7 +227,7 @@ export class Order { const maxOrder = await em.findOne( Order, - {}, + { id: { $ne: null } }, { orderBy: { orderNumber: 'DESC' }, fields: ['orderNumber'], diff --git a/src/modules/order/providers/order.service.ts b/src/modules/order/providers/order.service.ts index 04370f0..468bbee 100644 --- a/src/modules/order/providers/order.service.ts +++ b/src/modules/order/providers/order.service.ts @@ -80,7 +80,7 @@ export class OrderService { } } - const order = await this.em.transactional(async (em) => { + return this.em.transactional(async (em) => { const order = this.orderRepository.create({ creator: admin, @@ -96,6 +96,7 @@ export class OrderService { em.persist(order) const productIds = items.map(item => item.productId) + const products = await this.productRepository.find({ id: { $in: productIds } }) @@ -104,19 +105,18 @@ export class OrderService { } for (const item of items) { - this.persistOrderItem(order, item) + await this.persistOrderItem(order, item) } // TODO : calculation must be done after create order // await this.calculateOrder(order) - // await em.flush() + await em.flush() return order }) - return order } @@ -154,7 +154,7 @@ export class OrderService { order.attachments = attachments } - if (enableTax != undefined) { + if (enableTax != undefined) { order.enableTax = enableTax } @@ -178,7 +178,7 @@ export class OrderService { async updateOrderAsAdmin(orderId: string, dto: IUpdateOrder) { const order = await this.findOneOrFail(orderId) - const updateOrder= await this.updateOrder(order, dto) + const updateOrder = await this.updateOrder(order, dto) // const updateOrder = await this.calculateOrder(order) @@ -230,7 +230,7 @@ export class OrderService { if (found) { throw new BadRequestException(`Product already exists`) } - + console.log(productId) const product = await this.productRepository.findOne({ id: productId }) if (!product) { throw new BadRequestException(`Product not found`)