From 97be92e9049651cb58875c068da4f7327e3c5958 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 09:51:53 +0200 Subject: [PATCH 01/22] feat: add OE feature flag --- .../medusa/src/loaders/feature-flags/order-editing.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 packages/medusa/src/loaders/feature-flags/order-editing.ts diff --git a/packages/medusa/src/loaders/feature-flags/order-editing.ts b/packages/medusa/src/loaders/feature-flags/order-editing.ts new file mode 100644 index 0000000000000..2e6601fc4b3e5 --- /dev/null +++ b/packages/medusa/src/loaders/feature-flags/order-editing.ts @@ -0,0 +1,10 @@ +import { FlagSettings } from "../../types/feature-flags" + +const OrderEditingFeatureFlag: FlagSettings = { + key: "order_editing", + default_val: false, + env_key: "MEDUSA_FF_ORDER_EDITING", + description: "[WIP] Enable the order editing feature", +} + +export default OrderEditingFeatureFlag From 96256516843b7f7a57acd39b24723605544087e1 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 10:43:37 +0200 Subject: [PATCH 02/22] feat: add OrderItemChange entity --- .../medusa/src/models/order-item-change.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 packages/medusa/src/models/order-item-change.ts diff --git a/packages/medusa/src/models/order-item-change.ts b/packages/medusa/src/models/order-item-change.ts new file mode 100644 index 0000000000000..a45d1284816c4 --- /dev/null +++ b/packages/medusa/src/models/order-item-change.ts @@ -0,0 +1,40 @@ +import { BeforeInsert, JoinColumn, ManyToOne, OneToOne } from "typeorm" + +import { SoftDeletableEntity } from "../interfaces" +import { FeatureFlagEntity } from "../utils/feature-flag-decorators" +import { generateEntityId } from "../utils" +import { DbAwareColumn } from "../utils/db-aware-column" +import { OrderEdit } from "./order-edit" +import { LineItem } from "./line-item" + +export enum OrderEditItemChangeType { + ITEM_ADD = "item_add", + ITEM_REMOVE = "item_remove", + QUANTITY_CHANGE = "quantity_change", +} + +@FeatureFlagEntity("order_editing") +export class OrderItemChange extends SoftDeletableEntity { + @DbAwareColumn({ + type: "enum", + nullable: true, + enum: OrderEditItemChangeType, + }) + type: OrderEditItemChangeType + + @ManyToOne(() => OrderEdit, (oe) => oe.changes) + order_edit: OrderEdit + + @OneToOne(() => LineItem, { nullable: true }) + @JoinColumn({ name: "original_line_item_id" }) + original_line_item: LineItem + + @OneToOne(() => LineItem, { nullable: true }) + @JoinColumn({ name: "line_item_id" }) + line_item: LineItem + + @BeforeInsert() + private beforeInsert(): void { + this.id = generateEntityId(this.id, "oec") + } +} From c8581f99ba4956e177b5ca82feae3e734636ca13 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 10:54:32 +0200 Subject: [PATCH 03/22] feat: order and order edit relation --- packages/medusa/src/models/order-edit.ts | 25 ++++++++++++++++++++++++ packages/medusa/src/models/order.ts | 17 ++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 packages/medusa/src/models/order-edit.ts diff --git a/packages/medusa/src/models/order-edit.ts b/packages/medusa/src/models/order-edit.ts new file mode 100644 index 0000000000000..cfef3d7c9dc9b --- /dev/null +++ b/packages/medusa/src/models/order-edit.ts @@ -0,0 +1,25 @@ +import { BeforeInsert, JoinColumn, ManyToOne, OneToMany } from "typeorm" + +import { SoftDeletableEntity } from "../interfaces" +import { FeatureFlagEntity } from "../utils/feature-flag-decorators" +import { generateEntityId } from "../utils" +import { OrderItemChange } from "./order-item-change" +import { Order } from "./order" + +@FeatureFlagEntity("order_editing") +export class OrderEdit extends SoftDeletableEntity { + @ManyToOne(() => Order, (o) => o.edits) + @JoinColumn({ name: "order_id" }) + order: Order + + @OneToMany(() => OrderItemChange, (oic) => oic.order_edit, { + eager: true, + cascade: true, + }) + changes: OrderItemChange[] + + @BeforeInsert() + private beforeInsert(): void { + this.id = generateEntityId(this.id, "oe") + } +} diff --git a/packages/medusa/src/models/order.ts b/packages/medusa/src/models/order.ts index 39c17a2a397e3..eae3d5f8092d6 100644 --- a/packages/medusa/src/models/order.ts +++ b/packages/medusa/src/models/order.ts @@ -42,6 +42,7 @@ import { ShippingMethod } from "./shipping-method" import { Swap } from "./swap" import { generateEntityId } from "../utils/generate-entity-id" import { manualAutoIncrement } from "../utils/manual-auto-increment" +import { OrderEdit } from "./order-edit" export enum OrderStatus { PENDING = "pending", @@ -208,6 +209,14 @@ export class Order extends BaseEntity { @JoinColumn({ name: "draft_order_id" }) draft_order: DraftOrder + @FeatureFlagDecorators("order_editing", [ + OneToMany( + () => OrderEdit, + (oe) => oe.order + ), + ]) + edits: OrderEdit[] + @OneToMany(() => LineItem, (lineItem) => lineItem.order, { cascade: ["insert"], }) @@ -406,25 +415,25 @@ export class Order extends BaseEntity { * description: The returns associated with the order. Available if the relation `returns` is expanded. * items: * type: object - * description: A return object. + * description: A return object. * claims: * type: array * description: The claims associated with the order. Available if the relation `claims` is expanded. * items: * type: object - * description: A claim order object. + * description: A claim order object. * refunds: * type: array * description: The refunds associated with the order. Available if the relation `refunds` is expanded. * items: * type: object - * description: A refund object. + * description: A refund object. * swaps: * type: array * description: The swaps associated with the order. Available if the relation `swaps` is expanded. * items: * type: object - * description: A swap object. + * description: A swap object. * draft_order_id: * type: string * description: The ID of the draft order this order is associated with. From d584ea75da7a379c7981e3b87fbf77b82506d774 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 10:55:52 +0200 Subject: [PATCH 04/22] fix: add missing columns to OEC --- packages/medusa/src/models/order-item-change.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/medusa/src/models/order-item-change.ts b/packages/medusa/src/models/order-item-change.ts index a45d1284816c4..088a947912310 100644 --- a/packages/medusa/src/models/order-item-change.ts +++ b/packages/medusa/src/models/order-item-change.ts @@ -1,4 +1,4 @@ -import { BeforeInsert, JoinColumn, ManyToOne, OneToOne } from "typeorm" +import { BeforeInsert, Column, JoinColumn, ManyToOne, OneToOne } from "typeorm" import { SoftDeletableEntity } from "../interfaces" import { FeatureFlagEntity } from "../utils/feature-flag-decorators" @@ -25,10 +25,16 @@ export class OrderItemChange extends SoftDeletableEntity { @ManyToOne(() => OrderEdit, (oe) => oe.changes) order_edit: OrderEdit + @Column({ nullable: true }) + original_line_item_id: string + @OneToOne(() => LineItem, { nullable: true }) @JoinColumn({ name: "original_line_item_id" }) original_line_item: LineItem + @Column({ nullable: true }) + line_item_id: string + @OneToOne(() => LineItem, { nullable: true }) @JoinColumn({ name: "line_item_id" }) line_item: LineItem From 08ada356d94ef7082943b3554ea4aad4f3ffa46c Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 11:37:27 +0200 Subject: [PATCH 05/22] feat: order edit properties --- packages/medusa/src/models/order-edit.ts | 73 +++++++++++++++++++++++- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/packages/medusa/src/models/order-edit.ts b/packages/medusa/src/models/order-edit.ts index cfef3d7c9dc9b..efacb5a91df1c 100644 --- a/packages/medusa/src/models/order-edit.ts +++ b/packages/medusa/src/models/order-edit.ts @@ -1,10 +1,20 @@ -import { BeforeInsert, JoinColumn, ManyToOne, OneToMany } from "typeorm" +import { + BeforeInsert, + Column, + CreateDateColumn, + JoinColumn, + ManyToOne, + OneToMany, +} from "typeorm" -import { SoftDeletableEntity } from "../interfaces" import { FeatureFlagEntity } from "../utils/feature-flag-decorators" -import { generateEntityId } from "../utils" +import { resolveDbType } from "../utils/db-aware-column" import { OrderItemChange } from "./order-item-change" +import { SoftDeletableEntity } from "../interfaces" +import { generateEntityId } from "../utils" +import { LineItem } from "./line-item" import { Order } from "./order" +import { User } from "./user" @FeatureFlagEntity("order_editing") export class OrderEdit extends SoftDeletableEntity { @@ -18,6 +28,63 @@ export class OrderEdit extends SoftDeletableEntity { }) changes: OrderItemChange[] + @Column({ nullable: true }) + internal_note?: string + + @Column() + created_by: string + + @ManyToOne(() => User) + @JoinColumn({ name: "created_by" }) + created_by_user: User + + @CreateDateColumn({ type: resolveDbType("timestamptz") }) + created_at: Date + + @Column({ nullable: true }) + requested_by?: string + + @ManyToOne(() => User) + @JoinColumn({ name: "created_by" }) + requested_by_user: User + + @CreateDateColumn({ type: resolveDbType("timestamptz"), nullable: true }) + requested_at?: Date + + @Column({ nullable: true }) + confirmed_by?: string // customer or user ID + + @CreateDateColumn({ type: resolveDbType("timestamptz"), nullable: true }) + confirmed_at?: Date + + @Column({ nullable: true }) + declined_by?: string // customer or user ID + + @Column({ nullable: true }) + declined_reason?: string + + @CreateDateColumn({ type: resolveDbType("timestamptz"), nullable: true }) + declined_at?: Date + + @Column({ nullable: true }) + canceled_by?: string + + @ManyToOne(() => User) + @JoinColumn({ name: "canceled_by" }) + canceled_by_user: User + + @CreateDateColumn({ type: resolveDbType("timestamptz"), nullable: true }) + canceled_at?: Date + + // Computed + subtotal: number + discount_total?: number + tax_total: number + total: number + difference_due: number + + items: LineItem[] + @BeforeInsert() private beforeInsert(): void { this.id = generateEntityId(this.id, "oe") From 0343b44596755edab37136025f2bff2ddb220607 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 11:52:34 +0200 Subject: [PATCH 06/22] fix: add missing columns --- packages/medusa/src/models/order-edit.ts | 3 +++ packages/medusa/src/models/order-item-change.ts | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/packages/medusa/src/models/order-edit.ts b/packages/medusa/src/models/order-edit.ts index efacb5a91df1c..94af642141ca3 100644 --- a/packages/medusa/src/models/order-edit.ts +++ b/packages/medusa/src/models/order-edit.ts @@ -18,6 +18,9 @@ import { User } from "./user" @FeatureFlagEntity("order_editing") export class OrderEdit extends SoftDeletableEntity { + @Column() + order_id: string + @ManyToOne(() => Order, (o) => o.edits) @JoinColumn({ name: "order_id" }) order: Order diff --git a/packages/medusa/src/models/order-item-change.ts b/packages/medusa/src/models/order-item-change.ts index 088a947912310..a2432d51c6a89 100644 --- a/packages/medusa/src/models/order-item-change.ts +++ b/packages/medusa/src/models/order-item-change.ts @@ -22,7 +22,11 @@ export class OrderItemChange extends SoftDeletableEntity { }) type: OrderEditItemChangeType + @Column() + order_edit_id: string + @ManyToOne(() => OrderEdit, (oe) => oe.changes) + @JoinColumn({ name: "order_edit_id" }) order_edit: OrderEdit @Column({ nullable: true }) From f6fc43b6311f6c8f5da9ecc3a97591d2fe16c993 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 12:10:03 +0200 Subject: [PATCH 07/22] wip: migrations --- .../migrations/1662976816913-order-editing.ts | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 packages/medusa/src/migrations/1662976816913-order-editing.ts diff --git a/packages/medusa/src/migrations/1662976816913-order-editing.ts b/packages/medusa/src/migrations/1662976816913-order-editing.ts new file mode 100644 index 0000000000000..a51c34f7d45db --- /dev/null +++ b/packages/medusa/src/migrations/1662976816913-order-editing.ts @@ -0,0 +1,37 @@ +import {MigrationInterface, QueryRunner} from "typeorm"; + +export class orderEditing1662976816913 implements MigrationInterface { + name = 'orderEditing1662976816913' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TYPE "order_item_change_type_enum" AS ENUM('item_add', 'item_remove', 'quantity_change')`); + await queryRunner.query(`CREATE TABLE "order_item_change" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "type" "public"."order_item_change_type_enum", "original_line_item_id" character varying, "line_item_id" character varying, "orderEditId" character varying, CONSTRAINT "REL_b4d53b8d03c9f5e7d4317e818d" UNIQUE ("original_line_item_id"), CONSTRAINT "REL_5f9688929761f7df108b630e64" UNIQUE ("line_item_id"), CONSTRAINT "PK_d6eb138f77ffdee83567b85af0c" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE TABLE "order_edit" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "internal_note" character varying, "created_by" character varying NOT NULL, "requested_by" character varying, "requested_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "confirmed_by" character varying, "confirmed_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "declined_by" character varying, "declined_reason" character varying, "declined_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "canceled_by" character varying, "canceled_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "order_id" character varying, CONSTRAINT "PK_58ab6acf2e84b4e827f5f846f7a" PRIMARY KEY ("id"))`); + + await queryRunner.query(`ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_a3851ad9d890e2265ba878c5eef" FOREIGN KEY ("orderEditId") REFERENCES "order_edit"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_b4d53b8d03c9f5e7d4317e818d9" FOREIGN KEY ("original_line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_5f9688929761f7df108b630e64a" FOREIGN KEY ("line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); + + await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd" FOREIGN KEY ("order_id") REFERENCES "order"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd" FOREIGN KEY ("created_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd" FOREIGN KEY ("created_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_62b7765bab66da06a02bb07e195" FOREIGN KEY ("canceled_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); + + + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_62b7765bab66da06a02bb07e195"`); + await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd"`); + await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd"`); + await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd"`); + await queryRunner.query(`ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_5f9688929761f7df108b630e64a"`); + await queryRunner.query(`ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_b4d53b8d03c9f5e7d4317e818d9"`); + await queryRunner.query(`ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_a3851ad9d890e2265ba878c5eef"`); + + await queryRunner.query(`DROP TABLE "order_edit"`); + await queryRunner.query(`DROP TABLE "order_item_change"`); + await queryRunner.query(`DROP TYPE "order_item_change_type_enum"`); + } + +} From 67fb94fcfeb52acd252c2faa2fc777d3038ac512 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 12:18:30 +0200 Subject: [PATCH 08/22] fix: col reference --- .../migrations/1662976816913-order-editing.ts | 94 ++++++++++++------- packages/medusa/src/models/order-edit.ts | 2 +- 2 files changed, 61 insertions(+), 35 deletions(-) diff --git a/packages/medusa/src/migrations/1662976816913-order-editing.ts b/packages/medusa/src/migrations/1662976816913-order-editing.ts index a51c34f7d45db..77365f4bd3a5f 100644 --- a/packages/medusa/src/migrations/1662976816913-order-editing.ts +++ b/packages/medusa/src/migrations/1662976816913-order-editing.ts @@ -1,37 +1,63 @@ -import {MigrationInterface, QueryRunner} from "typeorm"; +import { MigrationInterface, QueryRunner } from "typeorm" export class orderEditing1662976816913 implements MigrationInterface { - name = 'orderEditing1662976816913' - - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`CREATE TYPE "order_item_change_type_enum" AS ENUM('item_add', 'item_remove', 'quantity_change')`); - await queryRunner.query(`CREATE TABLE "order_item_change" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "type" "public"."order_item_change_type_enum", "original_line_item_id" character varying, "line_item_id" character varying, "orderEditId" character varying, CONSTRAINT "REL_b4d53b8d03c9f5e7d4317e818d" UNIQUE ("original_line_item_id"), CONSTRAINT "REL_5f9688929761f7df108b630e64" UNIQUE ("line_item_id"), CONSTRAINT "PK_d6eb138f77ffdee83567b85af0c" PRIMARY KEY ("id"))`); - await queryRunner.query(`CREATE TABLE "order_edit" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "internal_note" character varying, "created_by" character varying NOT NULL, "requested_by" character varying, "requested_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "confirmed_by" character varying, "confirmed_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "declined_by" character varying, "declined_reason" character varying, "declined_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "canceled_by" character varying, "canceled_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "order_id" character varying, CONSTRAINT "PK_58ab6acf2e84b4e827f5f846f7a" PRIMARY KEY ("id"))`); - - await queryRunner.query(`ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_a3851ad9d890e2265ba878c5eef" FOREIGN KEY ("orderEditId") REFERENCES "order_edit"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_b4d53b8d03c9f5e7d4317e818d9" FOREIGN KEY ("original_line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_5f9688929761f7df108b630e64a" FOREIGN KEY ("line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); - - await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd" FOREIGN KEY ("order_id") REFERENCES "order"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd" FOREIGN KEY ("created_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd" FOREIGN KEY ("created_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_62b7765bab66da06a02bb07e195" FOREIGN KEY ("canceled_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); - - - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_62b7765bab66da06a02bb07e195"`); - await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd"`); - await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd"`); - await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd"`); - await queryRunner.query(`ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_5f9688929761f7df108b630e64a"`); - await queryRunner.query(`ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_b4d53b8d03c9f5e7d4317e818d9"`); - await queryRunner.query(`ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_a3851ad9d890e2265ba878c5eef"`); - - await queryRunner.query(`DROP TABLE "order_edit"`); - await queryRunner.query(`DROP TABLE "order_item_change"`); - await queryRunner.query(`DROP TYPE "order_item_change_type_enum"`); - } - + name = "orderEditing1662976816913" + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TYPE "order_item_change_type_enum" AS ENUM('item_add', 'item_remove', 'quantity_change')` + ) + await queryRunner.query( + `CREATE TABLE "order_item_change" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "type" "public"."order_item_change_type_enum", "original_line_item_id" character varying, "line_item_id" character varying, "orderEditId" character varying, CONSTRAINT "REL_b4d53b8d03c9f5e7d4317e818d" UNIQUE ("original_line_item_id"), CONSTRAINT "REL_5f9688929761f7df108b630e64" UNIQUE ("line_item_id"), CONSTRAINT "PK_d6eb138f77ffdee83567b85af0c" PRIMARY KEY ("id"))` + ) + await queryRunner.query( + `CREATE TABLE "order_edit" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "internal_note" character varying, "created_by" character varying NOT NULL, "requested_by" character varying, "requested_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "confirmed_by" character varying, "confirmed_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "declined_by" character varying, "declined_reason" character varying, "declined_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "canceled_by" character varying, "canceled_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "order_id" character varying, CONSTRAINT "PK_58ab6acf2e84b4e827f5f846f7a" PRIMARY KEY ("id"))` + ) + + await queryRunner.query( + `ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_a3851ad9d890e2265ba878c5eef" FOREIGN KEY ("orderEditId") REFERENCES "order_edit"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + await queryRunner.query( + `ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_b4d53b8d03c9f5e7d4317e818d9" FOREIGN KEY ("original_line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + await queryRunner.query( + `ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_5f9688929761f7df108b630e64a" FOREIGN KEY ("line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + + await queryRunner.query( + `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd" FOREIGN KEY ("order_id") REFERENCES "order"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + await queryRunner.query( + `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd" FOREIGN KEY ("created_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + await queryRunner.query( + `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_62b7765bab66da06a02bb07e195" FOREIGN KEY ("canceled_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_62b7765bab66da06a02bb07e195"` + ) + await queryRunner.query( + `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd"` + ) + await queryRunner.query( + `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd"` + ) + + await queryRunner.query( + `ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_5f9688929761f7df108b630e64a"` + ) + await queryRunner.query( + `ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_b4d53b8d03c9f5e7d4317e818d9"` + ) + await queryRunner.query( + `ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_a3851ad9d890e2265ba878c5eef"` + ) + + await queryRunner.query(`DROP TABLE "order_edit"`) + await queryRunner.query(`DROP TABLE "order_item_change"`) + await queryRunner.query(`DROP TYPE "order_item_change_type_enum"`) + } } diff --git a/packages/medusa/src/models/order-edit.ts b/packages/medusa/src/models/order-edit.ts index 94af642141ca3..9b279c577f041 100644 --- a/packages/medusa/src/models/order-edit.ts +++ b/packages/medusa/src/models/order-edit.ts @@ -48,7 +48,7 @@ export class OrderEdit extends SoftDeletableEntity { requested_by?: string @ManyToOne(() => User) - @JoinColumn({ name: "created_by" }) + @JoinColumn({ name: "requested_by" }) requested_by_user: User @CreateDateColumn({ type: resolveDbType("timestamptz"), nullable: true }) From e8992481f37af4b7e4d9139460e1fbc6a8c6b025 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 13:11:49 +0200 Subject: [PATCH 09/22] fix: add nullables --- packages/medusa/src/models/order-edit.ts | 4 ++-- packages/medusa/src/models/order-item-change.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/medusa/src/models/order-edit.ts b/packages/medusa/src/models/order-edit.ts index 9b279c577f041..4fbcbb268f75f 100644 --- a/packages/medusa/src/models/order-edit.ts +++ b/packages/medusa/src/models/order-edit.ts @@ -18,7 +18,7 @@ import { User } from "./user" @FeatureFlagEntity("order_editing") export class OrderEdit extends SoftDeletableEntity { - @Column() + @Column({ nullable: true }) order_id: string @ManyToOne(() => Order, (o) => o.edits) @@ -34,7 +34,7 @@ export class OrderEdit extends SoftDeletableEntity { @Column({ nullable: true }) internal_note?: string - @Column() + @Column({ nullable: true }) created_by: string @ManyToOne(() => User) diff --git a/packages/medusa/src/models/order-item-change.ts b/packages/medusa/src/models/order-item-change.ts index a2432d51c6a89..dee4683096a92 100644 --- a/packages/medusa/src/models/order-item-change.ts +++ b/packages/medusa/src/models/order-item-change.ts @@ -22,7 +22,7 @@ export class OrderItemChange extends SoftDeletableEntity { }) type: OrderEditItemChangeType - @Column() + @Column({ nullable: true }) order_edit_id: string @ManyToOne(() => OrderEdit, (oe) => oe.changes) @@ -30,14 +30,14 @@ export class OrderItemChange extends SoftDeletableEntity { order_edit: OrderEdit @Column({ nullable: true }) - original_line_item_id: string + original_line_item_id?: string @OneToOne(() => LineItem, { nullable: true }) @JoinColumn({ name: "original_line_item_id" }) original_line_item: LineItem @Column({ nullable: true }) - line_item_id: string + line_item_id?: string @OneToOne(() => LineItem, { nullable: true }) @JoinColumn({ name: "line_item_id" }) @@ -45,6 +45,6 @@ export class OrderItemChange extends SoftDeletableEntity { @BeforeInsert() private beforeInsert(): void { - this.id = generateEntityId(this.id, "oec") + this.id = generateEntityId(this.id, "oic") } } From 224f0fbb09857c71f1f45aa79f12d1359e54de5c Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 13:24:03 +0200 Subject: [PATCH 10/22] feat: recreate migration --- .../migrations/1662976816913-order-editing.ts | 63 ------------------- .../migrations/1662981581869-order_editing.ts | 34 ++++++++++ .../medusa/src/models/order-item-change.ts | 4 +- 3 files changed, 36 insertions(+), 65 deletions(-) delete mode 100644 packages/medusa/src/migrations/1662976816913-order-editing.ts create mode 100644 packages/medusa/src/migrations/1662981581869-order_editing.ts diff --git a/packages/medusa/src/migrations/1662976816913-order-editing.ts b/packages/medusa/src/migrations/1662976816913-order-editing.ts deleted file mode 100644 index 77365f4bd3a5f..0000000000000 --- a/packages/medusa/src/migrations/1662976816913-order-editing.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { MigrationInterface, QueryRunner } from "typeorm" - -export class orderEditing1662976816913 implements MigrationInterface { - name = "orderEditing1662976816913" - - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query( - `CREATE TYPE "order_item_change_type_enum" AS ENUM('item_add', 'item_remove', 'quantity_change')` - ) - await queryRunner.query( - `CREATE TABLE "order_item_change" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "type" "public"."order_item_change_type_enum", "original_line_item_id" character varying, "line_item_id" character varying, "orderEditId" character varying, CONSTRAINT "REL_b4d53b8d03c9f5e7d4317e818d" UNIQUE ("original_line_item_id"), CONSTRAINT "REL_5f9688929761f7df108b630e64" UNIQUE ("line_item_id"), CONSTRAINT "PK_d6eb138f77ffdee83567b85af0c" PRIMARY KEY ("id"))` - ) - await queryRunner.query( - `CREATE TABLE "order_edit" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "internal_note" character varying, "created_by" character varying NOT NULL, "requested_by" character varying, "requested_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "confirmed_by" character varying, "confirmed_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "declined_by" character varying, "declined_reason" character varying, "declined_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "canceled_by" character varying, "canceled_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "order_id" character varying, CONSTRAINT "PK_58ab6acf2e84b4e827f5f846f7a" PRIMARY KEY ("id"))` - ) - - await queryRunner.query( - `ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_a3851ad9d890e2265ba878c5eef" FOREIGN KEY ("orderEditId") REFERENCES "order_edit"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` - ) - await queryRunner.query( - `ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_b4d53b8d03c9f5e7d4317e818d9" FOREIGN KEY ("original_line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` - ) - await queryRunner.query( - `ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_5f9688929761f7df108b630e64a" FOREIGN KEY ("line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` - ) - - await queryRunner.query( - `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd" FOREIGN KEY ("order_id") REFERENCES "order"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` - ) - await queryRunner.query( - `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd" FOREIGN KEY ("created_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` - ) - await queryRunner.query( - `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_62b7765bab66da06a02bb07e195" FOREIGN KEY ("canceled_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` - ) - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query( - `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_62b7765bab66da06a02bb07e195"` - ) - await queryRunner.query( - `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd"` - ) - await queryRunner.query( - `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd"` - ) - - await queryRunner.query( - `ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_5f9688929761f7df108b630e64a"` - ) - await queryRunner.query( - `ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_b4d53b8d03c9f5e7d4317e818d9"` - ) - await queryRunner.query( - `ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_a3851ad9d890e2265ba878c5eef"` - ) - - await queryRunner.query(`DROP TABLE "order_edit"`) - await queryRunner.query(`DROP TABLE "order_item_change"`) - await queryRunner.query(`DROP TYPE "order_item_change_type_enum"`) - } -} diff --git a/packages/medusa/src/migrations/1662981581869-order_editing.ts b/packages/medusa/src/migrations/1662981581869-order_editing.ts new file mode 100644 index 0000000000000..5fb045b667c4b --- /dev/null +++ b/packages/medusa/src/migrations/1662981581869-order_editing.ts @@ -0,0 +1,34 @@ +import {MigrationInterface, QueryRunner} from "typeorm"; + +export class orderEditing1662981581869 implements MigrationInterface { + name = 'orderEditing1662981581869' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TYPE "order_item_change_type_enum" AS ENUM('item_add', 'item_remove', 'quantity_change')`); + await queryRunner.query(`CREATE TABLE "order_item_change" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "type" "public"."order_item_change_type_enum", "order_edit_id" character varying NOT NULL, "original_line_item_id" character varying, "line_item_id" character varying, CONSTRAINT "REL_b4d53b8d03c9f5e7d4317e818d" UNIQUE ("original_line_item_id"), CONSTRAINT "REL_5f9688929761f7df108b630e64" UNIQUE ("line_item_id"), CONSTRAINT "PK_d6eb138f77ffdee83567b85af0c" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE TABLE "order_edit" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "order_id" character varying NOT NULL, "internal_note" character varying, "created_by" character varying NOT NULL, "requested_by" character varying, "requested_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "confirmed_by" character varying, "confirmed_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "declined_by" character varying, "declined_reason" character varying, "declined_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "canceled_by" character varying, "canceled_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), CONSTRAINT "PK_58ab6acf2e84b4e827f5f846f7a" PRIMARY KEY ("id"))`); + + await queryRunner.query(`ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_44feeebb258bf4cfa4cc4202281" FOREIGN KEY ("order_edit_id") REFERENCES "order_edit"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_b4d53b8d03c9f5e7d4317e818d9" FOREIGN KEY ("original_line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_5f9688929761f7df108b630e64a" FOREIGN KEY ("line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd" FOREIGN KEY ("order_id") REFERENCES "order"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd" FOREIGN KEY ("created_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd" FOREIGN KEY ("created_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_62b7765bab66da06a02bb07e195" FOREIGN KEY ("canceled_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_62b7765bab66da06a02bb07e195"`); + await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd"`); + await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd"`); + await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd"`); + await queryRunner.query(`ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_5f9688929761f7df108b630e64a"`); + await queryRunner.query(`ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_b4d53b8d03c9f5e7d4317e818d9"`); + await queryRunner.query(`ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_44feeebb258bf4cfa4cc4202281"`); + + await queryRunner.query(`DROP TABLE "order_edit"`); + await queryRunner.query(`DROP TABLE "order_item_change"`); + await queryRunner.query(`DROP TYPE "order_item_change_type_enum"`); + } + +} diff --git a/packages/medusa/src/models/order-item-change.ts b/packages/medusa/src/models/order-item-change.ts index dee4683096a92..59861e2067053 100644 --- a/packages/medusa/src/models/order-item-change.ts +++ b/packages/medusa/src/models/order-item-change.ts @@ -34,14 +34,14 @@ export class OrderItemChange extends SoftDeletableEntity { @OneToOne(() => LineItem, { nullable: true }) @JoinColumn({ name: "original_line_item_id" }) - original_line_item: LineItem + original_line_item?: LineItem @Column({ nullable: true }) line_item_id?: string @OneToOne(() => LineItem, { nullable: true }) @JoinColumn({ name: "line_item_id" }) - line_item: LineItem + line_item?: LineItem @BeforeInsert() private beforeInsert(): void { From cee6c2b74ebd8e1052c1c592f23d34d9f10b3641 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 13:24:46 +0200 Subject: [PATCH 11/22] feat: add feature flag to the migration and format --- .../migrations/1662981581869-order_editing.ts | 89 +++++++++++++------ 1 file changed, 62 insertions(+), 27 deletions(-) diff --git a/packages/medusa/src/migrations/1662981581869-order_editing.ts b/packages/medusa/src/migrations/1662981581869-order_editing.ts index 5fb045b667c4b..d37e23a29b9a4 100644 --- a/packages/medusa/src/migrations/1662981581869-order_editing.ts +++ b/packages/medusa/src/migrations/1662981581869-order_editing.ts @@ -1,34 +1,69 @@ -import {MigrationInterface, QueryRunner} from "typeorm"; +import { MigrationInterface, QueryRunner } from "typeorm" -export class orderEditing1662981581869 implements MigrationInterface { - name = 'orderEditing1662981581869' +export const featureFlag = "order_editing" - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`CREATE TYPE "order_item_change_type_enum" AS ENUM('item_add', 'item_remove', 'quantity_change')`); - await queryRunner.query(`CREATE TABLE "order_item_change" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "type" "public"."order_item_change_type_enum", "order_edit_id" character varying NOT NULL, "original_line_item_id" character varying, "line_item_id" character varying, CONSTRAINT "REL_b4d53b8d03c9f5e7d4317e818d" UNIQUE ("original_line_item_id"), CONSTRAINT "REL_5f9688929761f7df108b630e64" UNIQUE ("line_item_id"), CONSTRAINT "PK_d6eb138f77ffdee83567b85af0c" PRIMARY KEY ("id"))`); - await queryRunner.query(`CREATE TABLE "order_edit" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "order_id" character varying NOT NULL, "internal_note" character varying, "created_by" character varying NOT NULL, "requested_by" character varying, "requested_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "confirmed_by" character varying, "confirmed_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "declined_by" character varying, "declined_reason" character varying, "declined_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "canceled_by" character varying, "canceled_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), CONSTRAINT "PK_58ab6acf2e84b4e827f5f846f7a" PRIMARY KEY ("id"))`); +export class orderEditing1662981581869 implements MigrationInterface { + name = "orderEditing1662981581869" - await queryRunner.query(`ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_44feeebb258bf4cfa4cc4202281" FOREIGN KEY ("order_edit_id") REFERENCES "order_edit"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_b4d53b8d03c9f5e7d4317e818d9" FOREIGN KEY ("original_line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_5f9688929761f7df108b630e64a" FOREIGN KEY ("line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd" FOREIGN KEY ("order_id") REFERENCES "order"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd" FOREIGN KEY ("created_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd" FOREIGN KEY ("created_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "order_edit" ADD CONSTRAINT "FK_62b7765bab66da06a02bb07e195" FOREIGN KEY ("canceled_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); - } + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TYPE "order_item_change_type_enum" AS ENUM('item_add', 'item_remove', 'quantity_change')` + ) + await queryRunner.query( + `CREATE TABLE "order_item_change" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "type" "public"."order_item_change_type_enum", "order_edit_id" character varying NOT NULL, "original_line_item_id" character varying, "line_item_id" character varying, CONSTRAINT "REL_b4d53b8d03c9f5e7d4317e818d" UNIQUE ("original_line_item_id"), CONSTRAINT "REL_5f9688929761f7df108b630e64" UNIQUE ("line_item_id"), CONSTRAINT "PK_d6eb138f77ffdee83567b85af0c" PRIMARY KEY ("id"))` + ) + await queryRunner.query( + `CREATE TABLE "order_edit" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "order_id" character varying NOT NULL, "internal_note" character varying, "created_by" character varying NOT NULL, "requested_by" character varying, "requested_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "confirmed_by" character varying, "confirmed_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "declined_by" character varying, "declined_reason" character varying, "declined_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "canceled_by" character varying, "canceled_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), CONSTRAINT "PK_58ab6acf2e84b4e827f5f846f7a" PRIMARY KEY ("id"))` + ) - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_62b7765bab66da06a02bb07e195"`); - await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd"`); - await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd"`); - await queryRunner.query(`ALTER TABLE "order_edit" DROP CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd"`); - await queryRunner.query(`ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_5f9688929761f7df108b630e64a"`); - await queryRunner.query(`ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_b4d53b8d03c9f5e7d4317e818d9"`); - await queryRunner.query(`ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_44feeebb258bf4cfa4cc4202281"`); + await queryRunner.query( + `ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_44feeebb258bf4cfa4cc4202281" FOREIGN KEY ("order_edit_id") REFERENCES "order_edit"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + await queryRunner.query( + `ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_b4d53b8d03c9f5e7d4317e818d9" FOREIGN KEY ("original_line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + await queryRunner.query( + `ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_5f9688929761f7df108b630e64a" FOREIGN KEY ("line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + await queryRunner.query( + `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd" FOREIGN KEY ("order_id") REFERENCES "order"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + await queryRunner.query( + `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd" FOREIGN KEY ("created_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + await queryRunner.query( + `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd" FOREIGN KEY ("created_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + await queryRunner.query( + `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_62b7765bab66da06a02bb07e195" FOREIGN KEY ("canceled_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + ) + } - await queryRunner.query(`DROP TABLE "order_edit"`); - await queryRunner.query(`DROP TABLE "order_item_change"`); - await queryRunner.query(`DROP TYPE "order_item_change_type_enum"`); - } + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_62b7765bab66da06a02bb07e195"` + ) + await queryRunner.query( + `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd"` + ) + await queryRunner.query( + `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd"` + ) + await queryRunner.query( + `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd"` + ) + await queryRunner.query( + `ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_5f9688929761f7df108b630e64a"` + ) + await queryRunner.query( + `ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_b4d53b8d03c9f5e7d4317e818d9"` + ) + await queryRunner.query( + `ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_44feeebb258bf4cfa4cc4202281"` + ) + await queryRunner.query(`DROP TABLE "order_edit"`) + await queryRunner.query(`DROP TABLE "order_item_change"`) + await queryRunner.query(`DROP TYPE "order_item_change_type_enum"`) + } } From 9a02c6673187993759b0954e7128ec6ff5840b11 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 13:40:02 +0200 Subject: [PATCH 12/22] fix: recreate migration --- ...r_editing.ts => 1662982509900-order_editing.ts} | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) rename packages/medusa/src/migrations/{1662981581869-order_editing.ts => 1662982509900-order_editing.ts} (72%) diff --git a/packages/medusa/src/migrations/1662981581869-order_editing.ts b/packages/medusa/src/migrations/1662982509900-order_editing.ts similarity index 72% rename from packages/medusa/src/migrations/1662981581869-order_editing.ts rename to packages/medusa/src/migrations/1662982509900-order_editing.ts index d37e23a29b9a4..7f7555374be7a 100644 --- a/packages/medusa/src/migrations/1662981581869-order_editing.ts +++ b/packages/medusa/src/migrations/1662982509900-order_editing.ts @@ -2,18 +2,18 @@ import { MigrationInterface, QueryRunner } from "typeorm" export const featureFlag = "order_editing" -export class orderEditing1662981581869 implements MigrationInterface { - name = "orderEditing1662981581869" +export class orderEditing1662982509900 implements MigrationInterface { + name = "orderEditing1662982509900" public async up(queryRunner: QueryRunner): Promise { await queryRunner.query( `CREATE TYPE "order_item_change_type_enum" AS ENUM('item_add', 'item_remove', 'quantity_change')` ) await queryRunner.query( - `CREATE TABLE "order_item_change" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "type" "public"."order_item_change_type_enum", "order_edit_id" character varying NOT NULL, "original_line_item_id" character varying, "line_item_id" character varying, CONSTRAINT "REL_b4d53b8d03c9f5e7d4317e818d" UNIQUE ("original_line_item_id"), CONSTRAINT "REL_5f9688929761f7df108b630e64" UNIQUE ("line_item_id"), CONSTRAINT "PK_d6eb138f77ffdee83567b85af0c" PRIMARY KEY ("id"))` + `CREATE TABLE "order_item_change" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "type" "public"."order_item_change_type_enum", "order_edit_id" character varying, "original_line_item_id" character varying, "line_item_id" character varying, CONSTRAINT "REL_b4d53b8d03c9f5e7d4317e818d" UNIQUE ("original_line_item_id"), CONSTRAINT "REL_5f9688929761f7df108b630e64" UNIQUE ("line_item_id"), CONSTRAINT "PK_d6eb138f77ffdee83567b85af0c" PRIMARY KEY ("id"))` ) await queryRunner.query( - `CREATE TABLE "order_edit" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "order_id" character varying NOT NULL, "internal_note" character varying, "created_by" character varying NOT NULL, "requested_by" character varying, "requested_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "confirmed_by" character varying, "confirmed_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "declined_by" character varying, "declined_reason" character varying, "declined_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "canceled_by" character varying, "canceled_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), CONSTRAINT "PK_58ab6acf2e84b4e827f5f846f7a" PRIMARY KEY ("id"))` + `CREATE TABLE "order_edit" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "order_id" character varying, "internal_note" character varying, "created_by" character varying, "requested_by" character varying, "requested_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "confirmed_by" character varying, "confirmed_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "declined_by" character varying, "declined_reason" character varying, "declined_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "canceled_by" character varying, "canceled_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), CONSTRAINT "PK_58ab6acf2e84b4e827f5f846f7a" PRIMARY KEY ("id"))` ) await queryRunner.query( @@ -25,6 +25,7 @@ export class orderEditing1662981581869 implements MigrationInterface { await queryRunner.query( `ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_5f9688929761f7df108b630e64a" FOREIGN KEY ("line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` ) + await queryRunner.query( `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd" FOREIGN KEY ("order_id") REFERENCES "order"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` ) @@ -32,7 +33,7 @@ export class orderEditing1662981581869 implements MigrationInterface { `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd" FOREIGN KEY ("created_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` ) await queryRunner.query( - `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd" FOREIGN KEY ("created_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` + `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_d0578a4a0a2baf6f853f573b44b" FOREIGN KEY ("requested_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` ) await queryRunner.query( `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_62b7765bab66da06a02bb07e195" FOREIGN KEY ("canceled_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` @@ -44,7 +45,7 @@ export class orderEditing1662981581869 implements MigrationInterface { `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_62b7765bab66da06a02bb07e195"` ) await queryRunner.query( - `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd"` + `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_d0578a4a0a2baf6f853f573b44b"` ) await queryRunner.query( `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd"` @@ -52,6 +53,7 @@ export class orderEditing1662981581869 implements MigrationInterface { await queryRunner.query( `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd"` ) + await queryRunner.query( `ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_5f9688929761f7df108b630e64a"` ) From 9f2ce1ba0ea2bd0f44074d7ca9c83f998e52dd53 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 13:52:14 +0200 Subject: [PATCH 13/22] feat: add repositories --- packages/medusa/src/repositories/order-edit.ts | 6 ++++++ packages/medusa/src/repositories/order-item-change.ts | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 packages/medusa/src/repositories/order-edit.ts create mode 100644 packages/medusa/src/repositories/order-item-change.ts diff --git a/packages/medusa/src/repositories/order-edit.ts b/packages/medusa/src/repositories/order-edit.ts new file mode 100644 index 0000000000000..84187e7b39bb7 --- /dev/null +++ b/packages/medusa/src/repositories/order-edit.ts @@ -0,0 +1,6 @@ +import { EntityRepository, Repository } from "typeorm" + +import { OrderEdit } from "../models/order-edit" + +@EntityRepository(OrderEdit) +export class OrderEditRepository extends Repository {} diff --git a/packages/medusa/src/repositories/order-item-change.ts b/packages/medusa/src/repositories/order-item-change.ts new file mode 100644 index 0000000000000..ea78b61e484a7 --- /dev/null +++ b/packages/medusa/src/repositories/order-item-change.ts @@ -0,0 +1,6 @@ +import { EntityRepository, Repository } from "typeorm" + +import { OrderItemChange } from "../models/order-item-change" + +@EntityRepository(OrderItemChange) +export class OrderItemChangeRepository extends Repository {} From 1e4b170eea8b19ddb1f3d61e86fda906bd751a1b Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 14:56:35 +0200 Subject: [PATCH 14/22] fix: remove public schema --- packages/medusa/src/migrations/1662982509900-order_editing.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/medusa/src/migrations/1662982509900-order_editing.ts b/packages/medusa/src/migrations/1662982509900-order_editing.ts index 7f7555374be7a..706c8e729e0b6 100644 --- a/packages/medusa/src/migrations/1662982509900-order_editing.ts +++ b/packages/medusa/src/migrations/1662982509900-order_editing.ts @@ -10,7 +10,7 @@ export class orderEditing1662982509900 implements MigrationInterface { `CREATE TYPE "order_item_change_type_enum" AS ENUM('item_add', 'item_remove', 'quantity_change')` ) await queryRunner.query( - `CREATE TABLE "order_item_change" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "type" "public"."order_item_change_type_enum", "order_edit_id" character varying, "original_line_item_id" character varying, "line_item_id" character varying, CONSTRAINT "REL_b4d53b8d03c9f5e7d4317e818d" UNIQUE ("original_line_item_id"), CONSTRAINT "REL_5f9688929761f7df108b630e64" UNIQUE ("line_item_id"), CONSTRAINT "PK_d6eb138f77ffdee83567b85af0c" PRIMARY KEY ("id"))` + `CREATE TABLE "order_item_change" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "type" "order_item_change_type_enum", "order_edit_id" character varying, "original_line_item_id" character varying, "line_item_id" character varying, CONSTRAINT "REL_b4d53b8d03c9f5e7d4317e818d" UNIQUE ("original_line_item_id"), CONSTRAINT "REL_5f9688929761f7df108b630e64" UNIQUE ("line_item_id"), CONSTRAINT "PK_d6eb138f77ffdee83567b85af0c" PRIMARY KEY ("id"))` ) await queryRunner.query( `CREATE TABLE "order_edit" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "order_id" character varying, "internal_note" character varying, "created_by" character varying, "requested_by" character varying, "requested_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "confirmed_by" character varying, "confirmed_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "declined_by" character varying, "declined_reason" character varying, "declined_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "canceled_by" character varying, "canceled_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), CONSTRAINT "PK_58ab6acf2e84b4e827f5f846f7a" PRIMARY KEY ("id"))` From c3c86c253da97ef31b74a24c9800c1e98bd86b87 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 15:56:47 +0200 Subject: [PATCH 15/22] fix: use FF key, remove eager, remove user FKs --- .../migrations/1662982509900-order_editing.ts | 4 +++- packages/medusa/src/models/order-edit.ts | 21 ++++--------------- .../medusa/src/models/order-item-change.ts | 4 ++-- packages/medusa/src/models/order.ts | 3 ++- 4 files changed, 11 insertions(+), 21 deletions(-) diff --git a/packages/medusa/src/migrations/1662982509900-order_editing.ts b/packages/medusa/src/migrations/1662982509900-order_editing.ts index 706c8e729e0b6..1e28cf58429f4 100644 --- a/packages/medusa/src/migrations/1662982509900-order_editing.ts +++ b/packages/medusa/src/migrations/1662982509900-order_editing.ts @@ -1,6 +1,8 @@ import { MigrationInterface, QueryRunner } from "typeorm" -export const featureFlag = "order_editing" +import OrderEditingFeatureFlag from "../loaders/feature-flags/order-editing" + +export const featureFlag = OrderEditingFeatureFlag.key export class orderEditing1662982509900 implements MigrationInterface { name = "orderEditing1662982509900" diff --git a/packages/medusa/src/models/order-edit.ts b/packages/medusa/src/models/order-edit.ts index 4fbcbb268f75f..c50d12b96bcf7 100644 --- a/packages/medusa/src/models/order-edit.ts +++ b/packages/medusa/src/models/order-edit.ts @@ -7,6 +7,7 @@ import { OneToMany, } from "typeorm" +import OrderEditingFeatureFlag from "../loaders/feature-flags/order-editing" import { FeatureFlagEntity } from "../utils/feature-flag-decorators" import { resolveDbType } from "../utils/db-aware-column" import { OrderItemChange } from "./order-item-change" @@ -14,9 +15,8 @@ import { SoftDeletableEntity } from "../interfaces" import { generateEntityId } from "../utils" import { LineItem } from "./line-item" import { Order } from "./order" -import { User } from "./user" -@FeatureFlagEntity("order_editing") +@FeatureFlagEntity(OrderEditingFeatureFlag.key) export class OrderEdit extends SoftDeletableEntity { @Column({ nullable: true }) order_id: string @@ -26,7 +26,6 @@ export class OrderEdit extends SoftDeletableEntity { order: Order @OneToMany(() => OrderItemChange, (oic) => oic.order_edit, { - eager: true, cascade: true, }) changes: OrderItemChange[] @@ -35,21 +34,13 @@ export class OrderEdit extends SoftDeletableEntity { internal_note?: string @Column({ nullable: true }) - created_by: string - - @ManyToOne(() => User) - @JoinColumn({ name: "created_by" }) - created_by_user: User + created_by: string // customer or user ID @CreateDateColumn({ type: resolveDbType("timestamptz") }) created_at: Date @Column({ nullable: true }) - requested_by?: string - - @ManyToOne(() => User) - @JoinColumn({ name: "requested_by" }) - requested_by_user: User + requested_by?: string // customer or user ID @CreateDateColumn({ type: resolveDbType("timestamptz"), nullable: true }) requested_at?: Date @@ -72,10 +63,6 @@ export class OrderEdit extends SoftDeletableEntity { @Column({ nullable: true }) canceled_by?: string - @ManyToOne(() => User) - @JoinColumn({ name: "canceled_by" }) - canceled_by_user: User - @CreateDateColumn({ type: resolveDbType("timestamptz"), nullable: true }) canceled_at?: Date diff --git a/packages/medusa/src/models/order-item-change.ts b/packages/medusa/src/models/order-item-change.ts index 59861e2067053..54d9a65cffb54 100644 --- a/packages/medusa/src/models/order-item-change.ts +++ b/packages/medusa/src/models/order-item-change.ts @@ -1,6 +1,7 @@ import { BeforeInsert, Column, JoinColumn, ManyToOne, OneToOne } from "typeorm" import { SoftDeletableEntity } from "../interfaces" +import OrderEditingFeatureFlag from "../loaders/feature-flags/order-editing" import { FeatureFlagEntity } from "../utils/feature-flag-decorators" import { generateEntityId } from "../utils" import { DbAwareColumn } from "../utils/db-aware-column" @@ -13,11 +14,10 @@ export enum OrderEditItemChangeType { QUANTITY_CHANGE = "quantity_change", } -@FeatureFlagEntity("order_editing") +@FeatureFlagEntity(OrderEditingFeatureFlag.key) export class OrderItemChange extends SoftDeletableEntity { @DbAwareColumn({ type: "enum", - nullable: true, enum: OrderEditItemChangeType, }) type: OrderEditItemChangeType diff --git a/packages/medusa/src/models/order.ts b/packages/medusa/src/models/order.ts index eae3d5f8092d6..d0c5893019fcf 100644 --- a/packages/medusa/src/models/order.ts +++ b/packages/medusa/src/models/order.ts @@ -43,6 +43,7 @@ import { Swap } from "./swap" import { generateEntityId } from "../utils/generate-entity-id" import { manualAutoIncrement } from "../utils/manual-auto-increment" import { OrderEdit } from "./order-edit" +import OrderEditingFeatureFlag from "../loaders/feature-flags/order-editing" export enum OrderStatus { PENDING = "pending", @@ -209,7 +210,7 @@ export class Order extends BaseEntity { @JoinColumn({ name: "draft_order_id" }) draft_order: DraftOrder - @FeatureFlagDecorators("order_editing", [ + @FeatureFlagDecorators(OrderEditingFeatureFlag.key, [ OneToMany( () => OrderEdit, (oe) => oe.order From d15e788c57327b5c9671a210b939c6b8222c595f Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 12 Sep 2022 17:30:44 +0200 Subject: [PATCH 16/22] fix: change relation type and add constraints --- packages/medusa/src/models/order-item-change.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/medusa/src/models/order-item-change.ts b/packages/medusa/src/models/order-item-change.ts index 54d9a65cffb54..41c87761eb3c4 100644 --- a/packages/medusa/src/models/order-item-change.ts +++ b/packages/medusa/src/models/order-item-change.ts @@ -1,4 +1,11 @@ -import { BeforeInsert, Column, JoinColumn, ManyToOne, OneToOne } from "typeorm" +import { + BeforeInsert, + Column, + JoinColumn, + ManyToOne, + OneToOne, + Unique, +} from "typeorm" import { SoftDeletableEntity } from "../interfaces" import OrderEditingFeatureFlag from "../loaders/feature-flags/order-editing" @@ -15,6 +22,8 @@ export enum OrderEditItemChangeType { } @FeatureFlagEntity(OrderEditingFeatureFlag.key) +@Unique(["order_edit_id", "original_line_item_id"]) +@Unique(["order_edit_id", "line_item_id"]) export class OrderItemChange extends SoftDeletableEntity { @DbAwareColumn({ type: "enum", @@ -32,7 +41,7 @@ export class OrderItemChange extends SoftDeletableEntity { @Column({ nullable: true }) original_line_item_id?: string - @OneToOne(() => LineItem, { nullable: true }) + @ManyToOne(() => LineItem, { nullable: true }) @JoinColumn({ name: "original_line_item_id" }) original_line_item?: LineItem From 5337f5e5645bd4268ba4fe6a5175cdf425e61345 Mon Sep 17 00:00:00 2001 From: fPolic Date: Tue, 13 Sep 2022 10:08:55 +0200 Subject: [PATCH 17/22] fix: change date decorator, remove nullable from ids --- packages/medusa/src/models/order-edit.ts | 10 +++++----- packages/medusa/src/models/order-item-change.ts | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/medusa/src/models/order-edit.ts b/packages/medusa/src/models/order-edit.ts index c50d12b96bcf7..977f58529dc74 100644 --- a/packages/medusa/src/models/order-edit.ts +++ b/packages/medusa/src/models/order-edit.ts @@ -18,7 +18,7 @@ import { Order } from "./order" @FeatureFlagEntity(OrderEditingFeatureFlag.key) export class OrderEdit extends SoftDeletableEntity { - @Column({ nullable: true }) + @Column() order_id: string @ManyToOne(() => Order, (o) => o.edits) @@ -42,13 +42,13 @@ export class OrderEdit extends SoftDeletableEntity { @Column({ nullable: true }) requested_by?: string // customer or user ID - @CreateDateColumn({ type: resolveDbType("timestamptz"), nullable: true }) + @Column({ type: resolveDbType("timestamptz"), nullable: true }) requested_at?: Date @Column({ nullable: true }) confirmed_by?: string // customer or user ID - @CreateDateColumn({ type: resolveDbType("timestamptz"), nullable: true }) + @Column({ type: resolveDbType("timestamptz"), nullable: true }) confirmed_at?: Date @Column({ nullable: true }) @@ -57,13 +57,13 @@ export class OrderEdit extends SoftDeletableEntity { @Column({ nullable: true }) declined_reason?: string - @CreateDateColumn({ type: resolveDbType("timestamptz"), nullable: true }) + @Column({ type: resolveDbType("timestamptz"), nullable: true }) declined_at?: Date @Column({ nullable: true }) canceled_by?: string - @CreateDateColumn({ type: resolveDbType("timestamptz"), nullable: true }) + @Column({ type: resolveDbType("timestamptz"), nullable: true }) canceled_at?: Date // Computed diff --git a/packages/medusa/src/models/order-item-change.ts b/packages/medusa/src/models/order-item-change.ts index 41c87761eb3c4..e5ef113a9e3c9 100644 --- a/packages/medusa/src/models/order-item-change.ts +++ b/packages/medusa/src/models/order-item-change.ts @@ -18,7 +18,7 @@ import { LineItem } from "./line-item" export enum OrderEditItemChangeType { ITEM_ADD = "item_add", ITEM_REMOVE = "item_remove", - QUANTITY_CHANGE = "quantity_change", + ITEM_UPDATE = "item_update", } @FeatureFlagEntity(OrderEditingFeatureFlag.key) @@ -31,7 +31,7 @@ export class OrderItemChange extends SoftDeletableEntity { }) type: OrderEditItemChangeType - @Column({ nullable: true }) + @Column() order_edit_id: string @ManyToOne(() => OrderEdit, (oe) => oe.changes) From 041a2aac549d31a6bea0ebeb8f277985c0b06cbf Mon Sep 17 00:00:00 2001 From: fPolic Date: Tue, 13 Sep 2022 10:38:23 +0200 Subject: [PATCH 18/22] fix: remove nullable from `created_by` --- packages/medusa/src/models/order-edit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/medusa/src/models/order-edit.ts b/packages/medusa/src/models/order-edit.ts index 977f58529dc74..a69e2489b2c84 100644 --- a/packages/medusa/src/models/order-edit.ts +++ b/packages/medusa/src/models/order-edit.ts @@ -33,7 +33,7 @@ export class OrderEdit extends SoftDeletableEntity { @Column({ nullable: true }) internal_note?: string - @Column({ nullable: true }) + @Column() created_by: string // customer or user ID @CreateDateColumn({ type: resolveDbType("timestamptz") }) From efebe13d6a9b62dcacc7e69eb35b23a817dd7141 Mon Sep 17 00:00:00 2001 From: fPolic Date: Tue, 13 Sep 2022 11:13:17 +0200 Subject: [PATCH 19/22] feat: new migration --- ...ting.ts => 1663059812399-order_editing.ts} | 30 ++++--------------- 1 file changed, 5 insertions(+), 25 deletions(-) rename packages/medusa/src/migrations/{1662982509900-order_editing.ts => 1663059812399-order_editing.ts} (56%) diff --git a/packages/medusa/src/migrations/1662982509900-order_editing.ts b/packages/medusa/src/migrations/1663059812399-order_editing.ts similarity index 56% rename from packages/medusa/src/migrations/1662982509900-order_editing.ts rename to packages/medusa/src/migrations/1663059812399-order_editing.ts index 1e28cf58429f4..a5dfbbcc9f1a4 100644 --- a/packages/medusa/src/migrations/1662982509900-order_editing.ts +++ b/packages/medusa/src/migrations/1663059812399-order_editing.ts @@ -4,18 +4,18 @@ import OrderEditingFeatureFlag from "../loaders/feature-flags/order-editing" export const featureFlag = OrderEditingFeatureFlag.key -export class orderEditing1662982509900 implements MigrationInterface { - name = "orderEditing1662982509900" +export class orderEditing1663059812399 implements MigrationInterface { + name = "orderEditing1663059812399" public async up(queryRunner: QueryRunner): Promise { await queryRunner.query( - `CREATE TYPE "order_item_change_type_enum" AS ENUM('item_add', 'item_remove', 'quantity_change')` + `CREATE TYPE "order_item_change_type_enum" AS ENUM('item_add', 'item_remove', 'item_update')` ) await queryRunner.query( - `CREATE TABLE "order_item_change" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "type" "order_item_change_type_enum", "order_edit_id" character varying, "original_line_item_id" character varying, "line_item_id" character varying, CONSTRAINT "REL_b4d53b8d03c9f5e7d4317e818d" UNIQUE ("original_line_item_id"), CONSTRAINT "REL_5f9688929761f7df108b630e64" UNIQUE ("line_item_id"), CONSTRAINT "PK_d6eb138f77ffdee83567b85af0c" PRIMARY KEY ("id"))` + `CREATE TABLE "order_item_change" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "type" "order_item_change_type_enum" NOT NULL, "order_edit_id" character varying NOT NULL, "original_line_item_id" character varying, "line_item_id" character varying, CONSTRAINT "UQ_da93cee3ca0dd50a5246268c2e9" UNIQUE ("order_edit_id", "line_item_id"), CONSTRAINT "UQ_5b7a99181e4db2ea821be0b6196" UNIQUE ("order_edit_id", "original_line_item_id"), CONSTRAINT "REL_5f9688929761f7df108b630e64" UNIQUE ("line_item_id"), CONSTRAINT "PK_d6eb138f77ffdee83567b85af0c" PRIMARY KEY ("id"))` ) await queryRunner.query( - `CREATE TABLE "order_edit" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "order_id" character varying, "internal_note" character varying, "created_by" character varying, "requested_by" character varying, "requested_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "confirmed_by" character varying, "confirmed_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "declined_by" character varying, "declined_reason" character varying, "declined_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), "canceled_by" character varying, "canceled_at" TIMESTAMP WITH TIME ZONE DEFAULT now(), CONSTRAINT "PK_58ab6acf2e84b4e827f5f846f7a" PRIMARY KEY ("id"))` + `CREATE TABLE "order_edit" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP WITH TIME ZONE, "order_id" character varying NOT NULL, "internal_note" character varying, "created_by" character varying NOT NULL, "requested_by" character varying, "requested_at" TIMESTAMP WITH TIME ZONE, "confirmed_by" character varying, "confirmed_at" TIMESTAMP WITH TIME ZONE, "declined_by" character varying, "declined_reason" character varying, "declined_at" TIMESTAMP WITH TIME ZONE, "canceled_by" character varying, "canceled_at" TIMESTAMP WITH TIME ZONE, CONSTRAINT "PK_58ab6acf2e84b4e827f5f846f7a" PRIMARY KEY ("id"))` ) await queryRunner.query( @@ -27,35 +27,15 @@ export class orderEditing1662982509900 implements MigrationInterface { await queryRunner.query( `ALTER TABLE "order_item_change" ADD CONSTRAINT "FK_5f9688929761f7df108b630e64a" FOREIGN KEY ("line_item_id") REFERENCES "line_item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` ) - await queryRunner.query( `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd" FOREIGN KEY ("order_id") REFERENCES "order"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` ) - await queryRunner.query( - `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd" FOREIGN KEY ("created_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` - ) - await queryRunner.query( - `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_d0578a4a0a2baf6f853f573b44b" FOREIGN KEY ("requested_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` - ) - await queryRunner.query( - `ALTER TABLE "order_edit" ADD CONSTRAINT "FK_62b7765bab66da06a02bb07e195" FOREIGN KEY ("canceled_by") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` - ) } public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query( - `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_62b7765bab66da06a02bb07e195"` - ) - await queryRunner.query( - `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_d0578a4a0a2baf6f853f573b44b"` - ) - await queryRunner.query( - `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_fe2a275ff60947aad4b8d663bdd"` - ) await queryRunner.query( `ALTER TABLE "order_edit" DROP CONSTRAINT "FK_1f3a251488a91510f57e1bf93cd"` ) - await queryRunner.query( `ALTER TABLE "order_item_change" DROP CONSTRAINT "FK_5f9688929761f7df108b630e64a"` ) From 1f1f23e320374ccd294429d50512911f382febda Mon Sep 17 00:00:00 2001 From: fPolic Date: Tue, 13 Sep 2022 12:49:07 +0200 Subject: [PATCH 20/22] feat: remove `create_at`, extend oas for `Order` --- packages/medusa/src/models/order-edit.ts | 12 +----------- packages/medusa/src/models/order.ts | 5 +++++ 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/packages/medusa/src/models/order-edit.ts b/packages/medusa/src/models/order-edit.ts index a69e2489b2c84..c72d5126e5301 100644 --- a/packages/medusa/src/models/order-edit.ts +++ b/packages/medusa/src/models/order-edit.ts @@ -1,11 +1,4 @@ -import { - BeforeInsert, - Column, - CreateDateColumn, - JoinColumn, - ManyToOne, - OneToMany, -} from "typeorm" +import { BeforeInsert, Column, JoinColumn, ManyToOne, OneToMany } from "typeorm" import OrderEditingFeatureFlag from "../loaders/feature-flags/order-editing" import { FeatureFlagEntity } from "../utils/feature-flag-decorators" @@ -36,9 +29,6 @@ export class OrderEdit extends SoftDeletableEntity { @Column() created_by: string // customer or user ID - @CreateDateColumn({ type: resolveDbType("timestamptz") }) - created_at: Date - @Column({ nullable: true }) requested_by?: string // customer or user ID diff --git a/packages/medusa/src/models/order.ts b/packages/medusa/src/models/order.ts index d0c5893019fcf..dd7771a075520 100644 --- a/packages/medusa/src/models/order.ts +++ b/packages/medusa/src/models/order.ts @@ -447,6 +447,11 @@ export class Order extends BaseEntity { * description: The line items that belong to the order. Available if the relation `items` is expanded. * items: * $ref: "#/components/schemas/line_item" + * edits: + * type: array + * description: [EXPERIMENTAL] Order edits done on the order. Available if the relation `edits` is expanded. + * items: + * $ref: "#/components/schemas/order_edit" * gift_card_transactions: * type: array * description: The gift card transactions used in the order. Available if the relation `gift_card_transactions` is expanded. From 673ec00d4821def9847f73b95cdddc873f9c41ee Mon Sep 17 00:00:00 2001 From: fPolic Date: Tue, 13 Sep 2022 13:18:37 +0200 Subject: [PATCH 21/22] feat: order item change oas --- .../medusa/src/models/order-item-change.ts | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/packages/medusa/src/models/order-item-change.ts b/packages/medusa/src/models/order-item-change.ts index e5ef113a9e3c9..3205377922e5c 100644 --- a/packages/medusa/src/models/order-item-change.ts +++ b/packages/medusa/src/models/order-item-change.ts @@ -57,3 +57,46 @@ export class OrderItemChange extends SoftDeletableEntity { this.id = generateEntityId(this.id, "oic") } } + +/** + * @schema order_item_change + * title: "Order Item Change" + * description: "Represents an order edit item change" + * x-resourceId: order_item_change + * required: + * - type + * - order_edit_id + * properties: + * id: + * type: string + * description: The order item change's ID + * example: oic_01G8TJSYT9M6AVS5N4EMNFS1EK + * type: + * type: string + * description: The order's status + * enum: + * - item_add + * - item_remove + * - item_update + * order_edit_id: + * type: string + * description: The ID of the order edit + * example: oe_01G2SG30J8C85S4A5CHM2S1NS2 + * order_edit: + * description: Order edit object + * $ref: "#/components/schemas/order_edit" + * original_line_item_id: + * type: string + * description: The ID of the original line item in the order + * example: item_01G8ZC9GWT6B2GP5FSXRXNFNGN + * original_line_item: + * description: Original line item object. + * $ref: "#/components/schemas/line_item" + * line_item_id: + * type: string + * description: The ID of the cloned line item. + * example: item_01G8ZC9GWT6B2GP5FSXRXNFNGN + * line_item: + * description: Line item object. + * $ref: "#/components/schemas/line_item" + */ From 2ec443b7fe20b6f6c29e381a521e1e4239d575b5 Mon Sep 17 00:00:00 2001 From: fPolic Date: Tue, 13 Sep 2022 14:05:15 +0200 Subject: [PATCH 22/22] feat: order edit oas --- packages/medusa/src/models/order-edit.ts | 85 ++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/packages/medusa/src/models/order-edit.ts b/packages/medusa/src/models/order-edit.ts index c72d5126e5301..9150477a6f72e 100644 --- a/packages/medusa/src/models/order-edit.ts +++ b/packages/medusa/src/models/order-edit.ts @@ -70,3 +70,88 @@ export class OrderEdit extends SoftDeletableEntity { this.id = generateEntityId(this.id, "oe") } } + +/** + * @schema order_edit + * title: "Order Edit" + * description: "Order edit keeps track of order items changes." + * x-resourceId: order_edit + * required: + * - order_id + * - order + * - changes + * - created_by + * properties: + * id: + * type: string + * description: The order edit's ID + * example: oe_01G8TJSYT9M6AVS5N4EMNFS1EK + * order_id: + * type: string + * description: The ID of the order that is edited + * example: order_01G2SG30J8C85S4A5CHM2S1NS2 + * order: + * description: Order object + * $ref: "#/components/schemas/order" + * changes: + * type: array + * description: Line item changes array. + * items: + * $ref: "#/components/schemas/order_item_changes" + * internal_note: + * description: "An optional note with additional details about the order edit." + * type: string + * example: Included two more items B to the order. + * created_by: + * type: string + * description: "The unique identifier of the user or customer who created the order edit." + * requested_by: + * type: string + * description: "The unique identifier of the user or customer who requested the order edit." + * requested_at: + * type: string + * description: "The date with timezone at which the edit was requested." + * format: date-time + * confirmed_by: + * type: string + * description: "The unique identifier of the user or customer who confirmed the order edit." + * confirmed_at: + * type: string + * description: "The date with timezone at which the edit was confirmed." + * format: date-time + * declined_by: + * type: string + * description: "The unique identifier of the user or customer who declined the order edit." + * declined_at: + * type: string + * description: "The date with timezone at which the edit was declined." + * format: date-time + * declined_reason: + * description: "An optional note why the order edit is declined." + * type: string + * subtotal: + * type: integer + * description: The subtotal for line items computed from changes. + * example: 8000 + * discount_total: + * type: integer + * description: The total of discount + * example: 800 + * tax_total: + * type: integer + * description: The total of tax + * example: 0 + * total: + * type: integer + * description: The total amount of the edited order. + * example: 8200 + * difference_due: + * type: integer + * description: The difference between the total amount of the order and total amount of edited order. + * example: 8200 + * items: + * type: array + * description: Computed line items from the changes. + * items: + * $ref: "#/components/schemas/line_item" + */