diff --git a/.changeset/eight-trees-begin.md b/.changeset/eight-trees-begin.md new file mode 100644 index 0000000000..135d5f7dbc --- /dev/null +++ b/.changeset/eight-trees-begin.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +fix(medusa): Removing the line items should remove the tax lines as well diff --git a/packages/medusa/src/migrations/1680857773272-line-item-tax-adjustment-on-cascade-delete.ts b/packages/medusa/src/migrations/1680857773272-line-item-tax-adjustment-on-cascade-delete.ts new file mode 100644 index 0000000000..c9940d56a4 --- /dev/null +++ b/packages/medusa/src/migrations/1680857773272-line-item-tax-adjustment-on-cascade-delete.ts @@ -0,0 +1,37 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class lineItemTaxAdjustmentOnCascadeDelete1680857773272 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "line_item_tax_line" DROP CONSTRAINT "FK_5077fa54b0d037e984385dfe8ad"` + ) + await queryRunner.query( + `ALTER TABLE "line_item_tax_line" ADD CONSTRAINT "FK_5077fa54b0d037e984385dfe8ad" FOREIGN KEY ("item_id") REFERENCES "line_item"("id") ON DELETE CASCADE ON UPDATE NO ACTION` + ) + + await queryRunner.query( + `ALTER TABLE "line_item_adjustment" DROP CONSTRAINT "FK_be9aea2ccf3567007b6227da4d2"` + ) + await queryRunner.query( + `ALTER TABLE "line_item_adjustment" ADD CONSTRAINT "FK_be9aea2ccf3567007b6227da4d2" FOREIGN KEY ("item_id") REFERENCES "line_item"("id") ON DELETE CASCADE ON UPDATE NO ACTION` + ) + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "line_item_tax_line" DROP CONSTRAINT "FK_5077fa54b0d037e984385dfe8ad"` + ) + await queryRunner.query( + `ALTER TABLE "line_item_tax_line" ADD CONSTRAINT "FK_5077fa54b0d037e984385dfe8ad" FOREIGN KEY ("item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + + await queryRunner.query( + `ALTER TABLE "line_item_adjustment" DROP CONSTRAINT "FK_be9aea2ccf3567007b6227da4d2"` + ) + await queryRunner.query( + `ALTER TABLE "line_item_adjustment" ADD CONSTRAINT "FK_be9aea2ccf3567007b6227da4d2" FOREIGN KEY ("item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + } +} diff --git a/packages/medusa/src/models/line-item.ts b/packages/medusa/src/models/line-item.ts index 34d3c5ea21..f708dc14ac 100644 --- a/packages/medusa/src/models/line-item.ts +++ b/packages/medusa/src/models/line-item.ts @@ -10,8 +10,7 @@ import { } from "typeorm" import { BaseEntity } from "../interfaces" -import TaxInclusivePricingFeatureFlag - from "../loaders/feature-flags/tax-inclusive-pricing" +import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing" import { generateEntityId } from "../utils" import { DbAwareColumn } from "../utils/db-aware-column" import { FeatureFlagColumn } from "../utils/feature-flag-decorators" @@ -70,11 +69,13 @@ export class LineItem extends BaseEntity { @JoinColumn({ name: "claim_order_id" }) claim_order: ClaimOrder - @OneToMany(() => LineItemTaxLine, (tl) => tl.item, { cascade: ["insert"] }) + @OneToMany(() => LineItemTaxLine, (tl) => tl.item, { + cascade: ["insert", "remove"], + }) tax_lines: LineItemTaxLine[] @OneToMany(() => LineItemAdjustment, (lia) => lia.item, { - cascade: ["insert"], + cascade: ["insert", "remove"], }) adjustments: LineItemAdjustment[] diff --git a/packages/medusa/src/services/line-item.ts b/packages/medusa/src/services/line-item.ts index 34445b4ddd..f0f7ac8459 100644 --- a/packages/medusa/src/services/line-item.ts +++ b/packages/medusa/src/services/line-item.ts @@ -477,6 +477,7 @@ class LineItemService extends TransactionBaseService { } /** + * @deprecated no the cascade on the entity takes care of it * Deletes a line item with the tax lines. * @param id - the id of the line item to delete * @return the result of the delete operation