From 58a1dc153ea11a78e3c416b0134d6ac28425082e Mon Sep 17 00:00:00 2001 From: Andres Alvarez Date: Tue, 27 May 2025 16:53:54 -0400 Subject: [PATCH 1/4] Update relation between order and payment confirmation --- src/order/dto/order.ts | 18 ++++---- src/order/entities/order.entity.ts | 21 +++++---- .../entities/payment-confirmation.entity.ts | 4 +- ...13-update-one-to-one-relation-migration.ts | 43 +++++++++++++++++++ 4 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 src/payments/migrations/1748379101213-update-one-to-one-relation-migration.ts diff --git a/src/order/dto/order.ts b/src/order/dto/order.ts index aaee166..4a43918 100644 --- a/src/order/dto/order.ts +++ b/src/order/dto/order.ts @@ -125,15 +125,6 @@ export class ResponseOrderDetailDTO { @Expose() @ApiProperty({ description: 'Subtotal price of the order detail' }) subtotal: number; - - @Expose() - @ApiProperty({ - description: 'Payment confirmation data (if any)', - type: ResponsePaymentConfirmationDTO, - required: false, - }) - @Type(() => ResponsePaymentConfirmationDTO) - paymentConfirmation?: ResponsePaymentConfirmationDTO; } export class ResponseOrderDTO extends BaseDTO { @@ -157,6 +148,15 @@ export class ResponseOrderDTO extends BaseDTO { @IsEnum(PaymentMethod) @IsOptional() paymentMethod: PaymentMethod; + + @Expose() + @ApiProperty({ + description: 'Payment confirmation data (if any)', + type: ResponsePaymentConfirmationDTO, + required: false, + }) + @Type(() => ResponsePaymentConfirmationDTO) + paymentConfirmation?: ResponsePaymentConfirmationDTO; } export class ResponseOrderDetailedDTO extends ResponseOrderDTO { diff --git a/src/order/entities/order.entity.ts b/src/order/entities/order.entity.ts index 799cf46..ff7b25e 100644 --- a/src/order/entities/order.entity.ts +++ b/src/order/entities/order.entity.ts @@ -2,7 +2,14 @@ import { Branch } from 'src/branch/entities/branch.entity'; import { ProductPresentation } from 'src/products/entities/product-presentation.entity'; import { User } from 'src/user/entities/user.entity'; import { BaseModel, UUIDModel } from 'src/utils/entity'; -import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm'; +import { + Column, + Entity, + JoinColumn, + ManyToOne, + OneToMany, + OneToOne, +} from 'typeorm'; import { OrderDelivery, OrderDetailDelivery } from './order_delivery.entity'; import { PaymentMethod } from 'src/payments/entities/payment-information.entity'; import { PaymentConfirmation } from 'src/payments/entities/payment-confirmation.entity'; @@ -58,11 +65,11 @@ export class Order extends BaseModel { }) paymentMethod: PaymentMethod; - @OneToMany( + @OneToOne( () => PaymentConfirmation, (paymentConfirmation) => paymentConfirmation.order, ) - paymentConfirmations: PaymentConfirmation[]; + paymentConfirmation: PaymentConfirmation; } @Entity() @@ -94,12 +101,4 @@ export class OrderDetail extends UUIDModel { (orderDeliveryDetail) => orderDeliveryDetail.orderDetail, ) orderDetailDeliveries: OrderDetailDelivery[]; - - @ManyToOne(() => PaymentConfirmation, { - nullable: true, - eager: true, - onDelete: 'SET NULL', - }) - @JoinColumn({ name: 'payment_confirmation_id' }) - paymentConfirmation: PaymentConfirmation; } diff --git a/src/payments/entities/payment-confirmation.entity.ts b/src/payments/entities/payment-confirmation.entity.ts index 0e98477..f41e688 100644 --- a/src/payments/entities/payment-confirmation.entity.ts +++ b/src/payments/entities/payment-confirmation.entity.ts @@ -1,6 +1,6 @@ import { Order } from 'src/order/entities/order.entity'; import { BaseModel } from 'src/utils/entity'; -import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm'; +import { Column, Entity, JoinColumn, OneToOne } from 'typeorm'; @Entity('payment_confirmation') export class PaymentConfirmation extends BaseModel { @@ -16,7 +16,7 @@ export class PaymentConfirmation extends BaseModel { @Column({ type: 'character varying', name: 'phone_number' }) phoneNumber: string; - @ManyToOne(() => Order, (order) => order.paymentConfirmations, { + @OneToOne(() => Order, (order) => order.paymentConfirmation, { onDelete: 'RESTRICT', }) @JoinColumn({ name: 'order_id' }) diff --git a/src/payments/migrations/1748379101213-update-one-to-one-relation-migration.ts b/src/payments/migrations/1748379101213-update-one-to-one-relation-migration.ts new file mode 100644 index 0000000..8203577 --- /dev/null +++ b/src/payments/migrations/1748379101213-update-one-to-one-relation-migration.ts @@ -0,0 +1,43 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class UpdateOneToOneRelationMigration1748379101213 + implements MigrationInterface +{ + name = 'UpdateOneToOneRelationMigration1748379101213'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "order_detail" DROP CONSTRAINT "FK_2cb24b410baacf4bd387b22757e"`, + ); + await queryRunner.query( + `ALTER TABLE "order_detail" DROP COLUMN "payment_confirmation_id"`, + ); + await queryRunner.query( + `ALTER TABLE "payment_confirmation" DROP CONSTRAINT "FK_de3ea608b9f32c2184b551c554b"`, + ); + await queryRunner.query( + `ALTER TABLE "payment_confirmation" ADD CONSTRAINT "UQ_de3ea608b9f32c2184b551c554b" UNIQUE ("order_id")`, + ); + await queryRunner.query( + `ALTER TABLE "payment_confirmation" ADD CONSTRAINT "FK_de3ea608b9f32c2184b551c554b" FOREIGN KEY ("order_id") REFERENCES "order"("id") ON DELETE RESTRICT ON UPDATE NO ACTION`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "payment_confirmation" DROP CONSTRAINT "FK_de3ea608b9f32c2184b551c554b"`, + ); + await queryRunner.query( + `ALTER TABLE "payment_confirmation" DROP CONSTRAINT "UQ_de3ea608b9f32c2184b551c554b"`, + ); + await queryRunner.query( + `ALTER TABLE "payment_confirmation" ADD CONSTRAINT "FK_de3ea608b9f32c2184b551c554b" FOREIGN KEY ("order_id") REFERENCES "order"("id") ON DELETE RESTRICT ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "order_detail" ADD "payment_confirmation_id" uuid`, + ); + await queryRunner.query( + `ALTER TABLE "order_detail" ADD CONSTRAINT "FK_2cb24b410baacf4bd387b22757e" FOREIGN KEY ("payment_confirmation_id") REFERENCES "payment_confirmation"("id") ON DELETE SET NULL ON UPDATE NO ACTION`, + ); + } +} From 74f0a87c1a41ea0c03c05217b9e98c8f23e61eb2 Mon Sep 17 00:00:00 2001 From: Andres Alvarez Date: Tue, 27 May 2025 17:04:33 -0400 Subject: [PATCH 2/4] Add relation in query of order detail --- src/order/services/order.service.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/order/services/order.service.ts b/src/order/services/order.service.ts index 14c4b92..6ffd9f5 100644 --- a/src/order/services/order.service.ts +++ b/src/order/services/order.service.ts @@ -212,6 +212,7 @@ export class OrderService { 'details.productPresentation.product.images', 'details.productPresentation.presentation', 'orderDeliveries.employee', + 'paymentConfirmation', ], }); if (!order) { @@ -232,6 +233,7 @@ export class OrderService { 'details.productPresentation.product.images', 'details.productPresentation.presentation', 'orderDeliveries.employee', + 'paymentConfirmation', 'user', 'user.profile', ], From 032db751e8409626288220aab33b6b04d60f076c Mon Sep 17 00:00:00 2001 From: Andres Alvarez Date: Tue, 27 May 2025 17:49:34 -0400 Subject: [PATCH 3/4] Add discount to order detail --- src/order/entities/order.entity.ts | 3 +++ ...romo-discount-to-order-detail-migration.ts | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/order/migrations/1748382133862-add-promo-discount-to-order-detail-migration.ts diff --git a/src/order/entities/order.entity.ts b/src/order/entities/order.entity.ts index ff7b25e..b210278 100644 --- a/src/order/entities/order.entity.ts +++ b/src/order/entities/order.entity.ts @@ -96,6 +96,9 @@ export class OrderDetail extends UUIDModel { @Column({ type: 'int', name: 'subtotal' }) subtotal: number; + @Column({ type: 'int', default: 0 }) + discount: number; + @OneToMany( () => OrderDetailDelivery, (orderDeliveryDetail) => orderDeliveryDetail.orderDetail, diff --git a/src/order/migrations/1748382133862-add-promo-discount-to-order-detail-migration.ts b/src/order/migrations/1748382133862-add-promo-discount-to-order-detail-migration.ts new file mode 100644 index 0000000..b5a76f0 --- /dev/null +++ b/src/order/migrations/1748382133862-add-promo-discount-to-order-detail-migration.ts @@ -0,0 +1,19 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class AddPromoDiscountToOrderDetailMigration1748382133862 + implements MigrationInterface +{ + name = 'AddPromoDiscountToOrderDetailMigration1748382133862'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "order_detail" ADD "discount" integer NOT NULL DEFAULT '0'`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "order_detail" DROP COLUMN "discount"`, + ); + } +} From cd9ab0dd3bf1a9e56feb870a9708406ff1bf3ef3 Mon Sep 17 00:00:00 2001 From: Dazzlin00 Date: Tue, 27 May 2025 20:06:45 -0400 Subject: [PATCH 4/4] discount is added to subtotal --- src/order/services/order.service.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/order/services/order.service.ts b/src/order/services/order.service.ts index 6ffd9f5..9d0e345 100644 --- a/src/order/services/order.service.ts +++ b/src/order/services/order.service.ts @@ -128,12 +128,16 @@ export class OrderService { }); const order = await this.orderRepository.save(orderToCreate); const orderDetails = productsWithQuantity.map((product) => { + const priceWithDiscount = product.promo + ? product.price - (product.price * product.promo.discount) / 100 + : product.price; + return this.orderDetailRepository.create({ order, productPresentation: product, quantity: product.quantity, price: product.price, - subtotal: product.price * product.quantity, + subtotal: Math.round(priceWithDiscount) * product.quantity, }); }); await this.orderDetailRepository.save(orderDetails);