Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/order/dto/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
24 changes: 13 additions & 11 deletions src/order/entities/order.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -58,11 +65,11 @@ export class Order extends BaseModel {
})
paymentMethod: PaymentMethod;

@OneToMany(
@OneToOne(
() => PaymentConfirmation,
(paymentConfirmation) => paymentConfirmation.order,
)
paymentConfirmations: PaymentConfirmation[];
paymentConfirmation: PaymentConfirmation;
}

@Entity()
Expand All @@ -89,17 +96,12 @@ export class OrderDetail extends UUIDModel {
@Column({ type: 'int', name: 'subtotal' })
subtotal: number;

@Column({ type: 'int', default: 0 })
discount: number;

@OneToMany(
() => OrderDetailDelivery,
(orderDeliveryDetail) => orderDeliveryDetail.orderDetail,
)
orderDetailDeliveries: OrderDetailDelivery[];

@ManyToOne(() => PaymentConfirmation, {
nullable: true,
eager: true,
onDelete: 'SET NULL',
})
@JoinColumn({ name: 'payment_confirmation_id' })
paymentConfirmation: PaymentConfirmation;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddPromoDiscountToOrderDetailMigration1748382133862
implements MigrationInterface
{
name = 'AddPromoDiscountToOrderDetailMigration1748382133862';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "order_detail" ADD "discount" integer NOT NULL DEFAULT '0'`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "order_detail" DROP COLUMN "discount"`,
);
}
}
8 changes: 7 additions & 1 deletion src/order/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -212,6 +216,7 @@ export class OrderService {
'details.productPresentation.product.images',
'details.productPresentation.presentation',
'orderDeliveries.employee',
'paymentConfirmation',
],
});
if (!order) {
Expand All @@ -232,6 +237,7 @@ export class OrderService {
'details.productPresentation.product.images',
'details.productPresentation.presentation',
'orderDeliveries.employee',
'paymentConfirmation',
'user',
'user.profile',
],
Expand Down
4 changes: 2 additions & 2 deletions src/payments/entities/payment-confirmation.entity.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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' })
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class UpdateOneToOneRelationMigration1748379101213
implements MigrationInterface
{
name = 'UpdateOneToOneRelationMigration1748379101213';

public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
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`,
);
}
}
Loading