From 4ad9ac1e5f3b583514015ffd35b88d0f60b2b38a Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Tue, 10 Dec 2024 21:38:48 +0530 Subject: [PATCH] refactor: migration stock location module (#10471) Fixes: FRMW-2831 --- .changeset/purple-crabs-kick.md | 5 + .../stock-location-module-service.spec.ts | 2 +- .../.snapshot-medusa-stock-location.json | 158 ++++++++++-------- .../src/migrations/Migration20241210073813.ts | 27 +++ .../stock-location/src/models/index.ts | 4 +- .../src/models/stock-location-address.ts | 95 +++-------- .../src/models/stock-location.ts | 89 ++-------- .../src/services/stock-location-module.ts | 21 ++- 8 files changed, 171 insertions(+), 230 deletions(-) create mode 100644 .changeset/purple-crabs-kick.md create mode 100644 packages/modules/stock-location/src/migrations/Migration20241210073813.ts diff --git a/.changeset/purple-crabs-kick.md b/.changeset/purple-crabs-kick.md new file mode 100644 index 0000000000..2799c1d053 --- /dev/null +++ b/.changeset/purple-crabs-kick.md @@ -0,0 +1,5 @@ +--- +"@medusajs/stock-location": patch +--- + +refactor: migration stock location module diff --git a/packages/modules/stock-location/integration-tests/__tests__/stock-location-module-service.spec.ts b/packages/modules/stock-location/integration-tests/__tests__/stock-location-module-service.spec.ts index a3292c0169..8d1a759beb 100644 --- a/packages/modules/stock-location/integration-tests/__tests__/stock-location-module-service.spec.ts +++ b/packages/modules/stock-location/integration-tests/__tests__/stock-location-module-service.spec.ts @@ -16,8 +16,8 @@ moduleIntegrationTestRunner({ }).linkable expect(Object.keys(linkable)).toEqual([ - "stockLocationAddress", "stockLocation", + "stockLocationAddress", ]) Object.keys(linkable).forEach((key) => { diff --git a/packages/modules/stock-location/src/migrations/.snapshot-medusa-stock-location.json b/packages/modules/stock-location/src/migrations/.snapshot-medusa-stock-location.json index e12721eec8..fa4862c121 100644 --- a/packages/modules/stock-location/src/migrations/.snapshot-medusa-stock-location.json +++ b/packages/modules/stock-location/src/migrations/.snapshot-medusa-stock-location.json @@ -1,5 +1,7 @@ { - "namespaces": ["public"], + "namespaces": [ + "public" + ], "name": "public", "tables": [ { @@ -13,38 +15,6 @@ "nullable": false, "mappedType": "text" }, - "created_at": { - "name": "created_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "length": 6, - "default": "now()", - "mappedType": "datetime" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "length": 6, - "default": "now()", - "mappedType": "datetime" - }, - "deleted_at": { - "name": "deleted_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "length": 6, - "mappedType": "datetime" - }, "address_1": { "name": "address_1", "type": "text", @@ -125,6 +95,38 @@ "primary": false, "nullable": true, "mappedType": "json" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" } }, "name": "stock_location_address", @@ -132,15 +134,17 @@ "indexes": [ { "keyName": "IDX_stock_location_address_deleted_at", - "columnNames": ["deleted_at"], + "columnNames": [], "composite": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_stock_location_address_deleted_at\" ON \"stock_location_address\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_stock_location_address_deleted_at\" ON \"stock_location_address\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "stock_location_address_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "primary": true, "unique": true @@ -160,6 +164,33 @@ "nullable": false, "mappedType": "text" }, + "name": { + "name": "name", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "address_id": { + "name": "address_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, "created_at": { "name": "created_at", "type": "timestamptz", @@ -191,49 +222,32 @@ "nullable": true, "length": 6, "mappedType": "datetime" - }, - "name": { - "name": "name", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "address_id": { - "name": "address_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, - "metadata": { - "name": "metadata", - "type": "jsonb", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "json" } }, "name": "stock_location", "schema": "public", "indexes": [ { - "keyName": "IDX_stock_location_deleted_at", - "columnNames": ["deleted_at"], + "keyName": "IDX_stock_location_address_id", + "columnNames": [], "composite": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_stock_location_deleted_at\" ON \"stock_location\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_stock_location_address_id\" ON \"stock_location\" (address_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_stock_location_deleted_at", + "columnNames": [], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_stock_location_deleted_at\" ON \"stock_location\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "stock_location_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "primary": true, "unique": true @@ -243,11 +257,15 @@ "foreignKeys": { "stock_location_address_id_foreign": { "constraintName": "stock_location_address_id_foreign", - "columnNames": ["address_id"], + "columnNames": [ + "address_id" + ], "localTableName": "public.stock_location", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.stock_location_address", - "deleteRule": "set null", + "deleteRule": "cascade", "updateRule": "cascade" } } diff --git a/packages/modules/stock-location/src/migrations/Migration20241210073813.ts b/packages/modules/stock-location/src/migrations/Migration20241210073813.ts new file mode 100644 index 0000000000..ef0e06a647 --- /dev/null +++ b/packages/modules/stock-location/src/migrations/Migration20241210073813.ts @@ -0,0 +1,27 @@ +import { Migration } from "@mikro-orm/migrations" + +export class Migration20241210073813 extends Migration { + async up(): Promise { + this.addSql( + 'alter table if exists "stock_location" drop constraint if exists "stock_location_address_id_foreign";' + ) + + this.addSql( + 'alter table if exists "stock_location" add constraint "stock_location_address_id_foreign" foreign key ("address_id") references "stock_location_address" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_stock_location_address_id" ON "stock_location" (address_id) WHERE deleted_at IS NULL;' + ) + } + + async down(): Promise { + this.addSql( + 'alter table if exists "stock_location" drop constraint if exists "stock_location_address_id_foreign";' + ) + + this.addSql('drop index if exists "IDX_stock_location_address_id";') + this.addSql( + 'alter table if exists "stock_location" add constraint "stock_location_address_id_foreign" foreign key ("address_id") references "stock_location_address" ("id") on update cascade on delete set null;' + ) + } +} diff --git a/packages/modules/stock-location/src/models/index.ts b/packages/modules/stock-location/src/models/index.ts index 732b15fc23..06cfe148a0 100644 --- a/packages/modules/stock-location/src/models/index.ts +++ b/packages/modules/stock-location/src/models/index.ts @@ -1,2 +1,2 @@ -export * from "./stock-location" -export * from "./stock-location-address" +export { default as StockLocation } from "./stock-location" +export { default as StockLocationAddress } from "./stock-location-address" diff --git a/packages/modules/stock-location/src/models/stock-location-address.ts b/packages/modules/stock-location/src/models/stock-location-address.ts index b479abffbd..10efe2fd8a 100644 --- a/packages/modules/stock-location/src/models/stock-location-address.ts +++ b/packages/modules/stock-location/src/models/stock-location-address.ts @@ -1,79 +1,24 @@ -import { - BeforeCreate, - Entity, - OnInit, - PrimaryKey, - Property, -} from "@mikro-orm/core" -import { - createPsqlIndexStatementHelper, - generateEntityId, -} from "@medusajs/framework/utils" +import { model } from "@medusajs/framework/utils" +import { StockLocation } from "@models" -const StockLocationAddressDeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "stock_location_address", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}) - -@Entity() -export class StockLocationAddress { - @PrimaryKey({ columnType: "text" }) - id: string - - @Property({ - onCreate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", +const StockLocationAddress = model + .define("StockLocationAddress", { + id: model.id({ prefix: "laddr" }).primaryKey(), + address_1: model.text(), + address_2: model.text().nullable(), + company: model.text().nullable(), + city: model.text().nullable(), + country_code: model.text(), + phone: model.text().nullable(), + province: model.text().nullable(), + postal_code: model.text().nullable(), + metadata: model.json().nullable(), + stock_locations: model.hasMany(() => StockLocation, { + mappedBy: "address", + }), }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", + .cascades({ + delete: ["stock_locations"], }) - updated_at: Date - @StockLocationAddressDeletedAtIndex.MikroORMIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @Property({ columnType: "text" }) - address_1: string - - @Property({ columnType: "text", nullable: true }) - address_2: string | null - - @Property({ columnType: "text", nullable: true }) - company: string | null - - @Property({ columnType: "text", nullable: true }) - city: string | null - - @Property({ columnType: "text" }) - country_code: string - - @Property({ columnType: "text", nullable: true }) - phone: string | null - - @Property({ columnType: "text", nullable: true }) - province: string | null - - @Property({ columnType: "text", nullable: true }) - postal_code: string | null - - @Property({ columnType: "jsonb", nullable: true }) - metadata: Record | null - - @BeforeCreate() - beforeCreate(): void { - this.id = generateEntityId(this.id, "laddr") - } - - @OnInit() - onInit(): void { - this.id = generateEntityId(this.id, "laddr") - } -} +export default StockLocationAddress diff --git a/packages/modules/stock-location/src/models/stock-location.ts b/packages/modules/stock-location/src/models/stock-location.ts index 45427b92ea..feb4b32fd5 100644 --- a/packages/modules/stock-location/src/models/stock-location.ts +++ b/packages/modules/stock-location/src/models/stock-location.ts @@ -1,80 +1,15 @@ -import { - DALUtils, - Searchable, - createPsqlIndexStatementHelper, - generateEntityId, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Entity, - Filter, - ManyToOne, - OnInit, - PrimaryKey, - Property, -} from "@mikro-orm/core" +import { model } from "@medusajs/framework/utils" +import StockLocationAddress from "./stock-location-address" -import { StockLocationAddress } from "./stock-location-address" - -const StockLocationDeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "stock_location", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", +const StockLocation = model.define("StockLocation", { + id: model.id({ prefix: "sloc" }).primaryKey(), + name: model.text().searchable(), + metadata: model.json().nullable(), + address: model + .belongsTo(() => StockLocationAddress, { + mappedBy: "stock_locations", + }) + .nullable(), }) -@Entity() -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export class StockLocation { - @PrimaryKey({ columnType: "text" }) - id: string - - @Property({ - onCreate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - updated_at: Date - - @StockLocationDeletedAtIndex.MikroORMIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @Searchable() - @Property({ columnType: "text" }) - name: string - - @ManyToOne(() => StockLocationAddress, { - fieldName: "address_id", - type: "text", - mapToPk: true, - nullable: true, - onDelete: "cascade", - }) - address_id: string | null - - @ManyToOne(() => StockLocationAddress, { - nullable: true, - }) - address: StockLocationAddress | null - - @Property({ columnType: "jsonb", nullable: true }) - metadata: Record | null - - @BeforeCreate() - beforeCreate(): void { - this.id = generateEntityId(this.id, "sloc") - } - - @OnInit() - onInit(): void { - this.id = generateEntityId(this.id, "sloc") - } -} +export default StockLocation diff --git a/packages/modules/stock-location/src/services/stock-location-module.ts b/packages/modules/stock-location/src/services/stock-location-module.ts index da3f21eab2..09c9c49e9e 100644 --- a/packages/modules/stock-location/src/services/stock-location-module.ts +++ b/packages/modules/stock-location/src/services/stock-location-module.ts @@ -4,6 +4,7 @@ import { DAL, FilterableStockLocationProps, IEventBusService, + InferEntityType, InternalModuleDeclaration, IStockLocationService, ModuleJoinerConfig, @@ -44,8 +45,12 @@ export default class StockLocationModuleService { protected readonly eventBusModuleService_: IEventBusService protected baseRepository_: DAL.RepositoryService - protected readonly stockLocationService_: ModulesSdkTypes.IMedusaInternalService - protected readonly stockLocationAddressService_: ModulesSdkTypes.IMedusaInternalService + protected readonly stockLocationService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected readonly stockLocationAddressService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > constructor( { @@ -102,7 +107,7 @@ export default class StockLocationModuleService async createStockLocations_( data: CreateStockLocationInput[], @MedusaContext() context: Context = {} - ): Promise { + ): Promise[]> { return await this.stockLocationService_.create(data, context) } @@ -144,7 +149,10 @@ export default class StockLocationModuleService (location) => !location.id ) as CreateStockLocationInput[] - const operations: Promise[] = [] + const operations: Promise< + | InferEntityType[] + | InferEntityType + >[] = [] if (toCreate.length) { operations.push(this.createStockLocations_(toCreate, context)) @@ -207,7 +215,10 @@ export default class StockLocationModuleService | UpdateStockLocationInput | { data: any; selector: FilterableStockLocationProps }, @MedusaContext() context: Context = {} - ): Promise { + ): Promise< + | InferEntityType[] + | InferEntityType + > { return await this.stockLocationService_.update(data, context) }