diff --git a/packages/region/integration-tests/__tests__/region-module.spec.ts b/packages/region/integration-tests/__tests__/region-module.spec.ts index 1c0435816d..0a1871cf54 100644 --- a/packages/region/integration-tests/__tests__/region-module.spec.ts +++ b/packages/region/integration-tests/__tests__/region-module.spec.ts @@ -63,11 +63,7 @@ describe("Region Module Service", () => { expect.objectContaining({ id: createdRegion.id, name: "Europe", - currency_code: "EUR", - currency: expect.objectContaining({ - code: "eur", - name: "Euro", - }), + currency_code: "eur", countries: [], }) ) @@ -80,7 +76,7 @@ describe("Region Module Service", () => { expect.objectContaining({ id: region.id, name: "Europe", - currency_code: "EUR", + currency_code: "eur", currency: expect.objectContaining({ code: "eur", name: "Euro", diff --git a/packages/region/src/migrations/.snapshot-medusa-region.json b/packages/region/src/migrations/.snapshot-medusa-region.json new file mode 100644 index 0000000000..83539e71fc --- /dev/null +++ b/packages/region/src/migrations/.snapshot-medusa-region.json @@ -0,0 +1,287 @@ +{ + "namespaces": [ + "public" + ], + "name": "public", + "tables": [ + { + "columns": { + "code": { + "name": "code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "symbol": { + "name": "symbol", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "symbol_native": { + "name": "symbol_native", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "name": { + "name": "name", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + } + }, + "name": "region_currency", + "schema": "public", + "indexes": [ + { + "keyName": "region_currency_pkey", + "columnNames": [ + "code" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": {} + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "name": { + "name": "name", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "currency_code": { + "name": "currency_code", + "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" + }, + "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": "region", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "currency_id" + ], + "composite": false, + "keyName": "IDX_region_currency_code", + "primary": false, + "unique": false + }, + { + "columnNames": [ + "deleted_at" + ], + "composite": false, + "keyName": "IDX_region_deleted_at", + "primary": false, + "unique": false + }, + { + "keyName": "region_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "region_currency_id_foreign": { + "constraintName": "region_currency_code_foreign", + "columnNames": [ + "currency_code" + ], + "localTableName": "public.region", + "referencedColumnNames": [ + "code" + ], + "referencedTableName": "public.region_currency", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "iso_2": { + "name": "iso_2", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "iso_3": { + "name": "iso_3", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "num_code": { + "name": "num_code", + "type": "int", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "integer" + }, + "name": { + "name": "name", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "display_name": { + "name": "display_name", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "region_id": { + "name": "region_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + } + }, + "name": "region_country", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "region_id" + ], + "composite": false, + "keyName": "IDX_country_region_id", + "primary": false, + "unique": false + }, + { + "keyName": "region_country_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "region_country_region_id_foreign": { + "constraintName": "region_country_region_id_foreign", + "columnNames": [ + "region_id" + ], + "localTableName": "public.region_country", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.region", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + } + ] +} \ No newline at end of file diff --git a/packages/region/src/migrations/RegionModuleSetup20240205173216.ts b/packages/region/src/migrations/RegionModuleSetup20240205173216.ts new file mode 100644 index 0000000000..601180c149 --- /dev/null +++ b/packages/region/src/migrations/RegionModuleSetup20240205173216.ts @@ -0,0 +1,53 @@ +import { Migration } from "@mikro-orm/migrations" + +export class RegionModuleSetup20240205173216 extends Migration { + async up(): Promise { + this.addSql(` +-- Create or update "region" table +CREATE TABLE IF NOT EXISTS "region" ( + "id" text NOT NULL, + "name" text NOT NULL, + "currency_code" text NOT NULL, + "metadata" jsonb NULL, + "created_at" timestamptz NOT NULL DEFAULT now(), + "updated_at" timestamptz NOT NULL DEFAULT now(), + "deleted_at" timestamptz NULL, + CONSTRAINT "region_pkey" PRIMARY KEY ("id") +); + +-- Create or update "region_currency" table +CREATE TABLE IF NOT EXISTS "region_currency" ( + "code" text NOT NULL, + "symbol" text NOT NULL, + "symbol_native" text NOT NULL, + "name" text NOT NULL, + CONSTRAINT "region_currency_pkey" PRIMARY KEY ("code") +); + +-- Adjust "region" table +ALTER TABLE "region" DROP CONSTRAINT IF EXISTS "FK_3bdd5896ec93be2f1c62a3309a5"; +ALTER TABLE "region" DROP CONSTRAINT IF EXISTS "FK_91f88052197680f9790272aaf5b"; +ALTER TABLE "region" ADD CONSTRAINT "region_currency_code_foreign" FOREIGN KEY ("currency_code") REFERENCES "region_currency" ("code") ON UPDATE CASCADE; + +CREATE INDEX IF NOT EXISTS "IDX_region_currency_code" ON "region" ("currency_code"); +CREATE INDEX IF NOT EXISTS "IDX_region_deleted_at" ON "region" ("deleted_at") WHERE "deleted_at" IS NOT NULL; + +-- Create or update "region_country" table +CREATE TABLE IF NOT EXISTS "region_country" ( + "id" text NOT NULL, + "iso_2" text NOT NULL, + "iso_3" text NOT NULL, + "num_code" int NOT NULL, + "name" text NOT NULL, + "display_name" text NOT NULL, + "region_id" text NULL, + CONSTRAINT "region_country_pkey" PRIMARY KEY ("id") +); +CREATE INDEX IF NOT EXISTS "IDX_country_region_id" ON "region_country" ("region_id"); + +-- Adjust foreign keys for "region_country" +ALTER TABLE "region_country" DROP CONSTRAINT IF EXISTS "FK_91f88052197680f9790272aaf5b"; +ALTER TABLE "region_country" ADD CONSTRAINT "region_country_region_id_foreign" FOREIGN KEY ("region_id") REFERENCES "region" ("id") ON UPDATE CASCADE; +`) + } +} diff --git a/packages/region/src/models/country.ts b/packages/region/src/models/country.ts index 1c7126eb47..4f65828288 100644 --- a/packages/region/src/models/country.ts +++ b/packages/region/src/models/country.ts @@ -1,6 +1,5 @@ import { BeforeCreate, - Cascade, Entity, ManyToOne, OnInit, @@ -31,16 +30,13 @@ export default class Country { @Property({ columnType: "text" }) display_name: string - @Property({ columnType: "text", nullable: true }) - region_id: string | null = null - @ManyToOne({ entity: () => Region, - onDelete: "cascade", + fieldName: "region_id", index: "IDX_country_region_id", - cascade: [Cascade.REMOVE, Cascade.PERSIST], + nullable: true, }) - region: Region + region: Region | null = null @BeforeCreate() onCreate() { diff --git a/packages/region/src/models/currency.ts b/packages/region/src/models/currency.ts index 11700ab198..8b2ec2b375 100644 --- a/packages/region/src/models/currency.ts +++ b/packages/region/src/models/currency.ts @@ -1,18 +1,8 @@ -import { generateEntityId } from "@medusajs/utils" -import { - BeforeCreate, - Entity, - OnInit, - PrimaryKey, - Property, -} from "@mikro-orm/core" +import { Entity, PrimaryKey, Property } from "@mikro-orm/core" @Entity({ tableName: "region_currency" }) export default class Currency { @PrimaryKey({ columnType: "text" }) - id: string - - @Property({ columnType: "text" }) code: string @Property({ columnType: "text" }) @@ -23,14 +13,4 @@ export default class Currency { @Property({ columnType: "text" }) name: string - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "reg_curr") - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "reg_curr") - } } diff --git a/packages/region/src/models/region.ts b/packages/region/src/models/region.ts index 79248fbad3..79fb01a020 100644 --- a/packages/region/src/models/region.ts +++ b/packages/region/src/models/region.ts @@ -2,7 +2,6 @@ import { DAL } from "@medusajs/types" import { DALUtils, generateEntityId } from "@medusajs/utils" import { BeforeCreate, - Cascade, Collection, Entity, Filter, @@ -37,15 +36,12 @@ export default class Region { @ManyToOne({ entity: () => Currency, - onDelete: "cascade", index: "IDX_region_currency_code", - cascade: [Cascade.PERSIST], + nullable: true, }) - currency: Currency + currency?: Currency - @OneToMany(() => Country, (country) => country.region, { - cascade: [Cascade.REMOVE], - }) + @OneToMany(() => Country, (country) => country.region) countries = new Collection(this) @Property({ columnType: "jsonb", nullable: true }) diff --git a/packages/region/src/services/region-module.ts b/packages/region/src/services/region-module.ts index ad10f20b1d..ccda59e755 100644 --- a/packages/region/src/services/region-module.ts +++ b/packages/region/src/services/region-module.ts @@ -100,10 +100,7 @@ export default class RegionModuleService< const result = await this.create_(input, sharedContext) return await this.baseRepository_.serialize( - Array.isArray(data) ? result : result[0], - { - populate: true, - } + Array.isArray(data) ? result : result[0] ) } @@ -128,7 +125,7 @@ export default class RegionModuleService< ) } - reg.currency = currencyMap.get(lowerCasedCurrency) as RegionCurrencyDTO + reg.currency_code = lowerCasedCurrency } const result = await this.regionService_.create(data, sharedContext) @@ -207,7 +204,7 @@ export default class RegionModuleService< ): Promise { const [currency] = await this.currencyService_.list( {}, - { select: ["id"], take: 1 }, + { select: ["code"], take: 1 }, sharedContext ) diff --git a/packages/types/src/region/common.ts b/packages/types/src/region/common.ts index dd5cafd17d..bb15ed6e5c 100644 --- a/packages/types/src/region/common.ts +++ b/packages/types/src/region/common.ts @@ -33,7 +33,6 @@ export interface RegionCountryDTO { } export interface RegionCurrencyDTO { - id: string code: string symbol: string name: string