feat(fulfillment): implementation part 2 (#6408)

**What**

> [!NOTE]  
> I can see this pr becoming huge, so I d like to get this partial one merged 👍 


- Fixes shared connection usage (mikro orm compare the instance to its own package and therefore was resulting in not trully reusing the provided connection leading to exhausting the connection pool as multiple connections was created and end up not being all destroyed properly under the hood, discovered in my integration tests)
- Create shipping options method implementation
- DTO's definition and service interface update
- integration tests 
- Re work of the indexes with new util update
- Test runner utils to remove a big chunk of the boilerplate of the packages integrations

FIXES CORE-1742
This commit is contained in:
Adrien de Peretti
2024-02-19 13:33:46 +01:00
committed by GitHub
parent 680dfcdad3
commit 1d91b7429b
59 changed files with 2213 additions and 1741 deletions

View File

@@ -0,0 +1,8 @@
---
"@medusajs/medusa": patch
"medusa-test-utils": patch
"@medusajs/product": patch
"@medusajs/types": patch
---
feat(fulfillment): implementation part 2

View File

@@ -5,14 +5,14 @@ import { TestDatabaseUtils } from "medusa-test-utils"
const pathToMigrations = "../../src/migrations"
const mikroOrmEntities = AuthModels as unknown as any[]
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig(
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const DB_URL = TestDatabaseUtils.getDatabaseURL()

View File

@@ -5,14 +5,14 @@ import * as CartModels from "@models"
const pathToMigrations = "../../src/migrations"
const mikroOrmEntities = CartModels as unknown as any[]
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig(
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const DB_URL = TestDatabaseUtils.getDatabaseURL()

View File

@@ -5,14 +5,14 @@ import * as Models from "@models"
const pathToMigrations = "../../src/migrations"
const mikroOrmEntities = Models as unknown as any[]
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig(
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const DB_URL = TestDatabaseUtils.getDatabaseURL()

View File

@@ -1,6 +0,0 @@
if (typeof process.env.DB_TEMP_NAME === "undefined") {
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
process.env.DB_TEMP_NAME = `medusa-fulfillment-integration-${tempName}`
}
process.env.MEDUSA_FULFILLMENT_DB_SCHEMA = "public"

View File

@@ -1,3 +0,0 @@
import { JestUtils } from "medusa-test-utils"
JestUtils.afterAllHookDropDatabase()

View File

@@ -1,13 +0,0 @@
import { TestDatabaseUtils } from "medusa-test-utils"
import * as Models from "@models"
const mikroOrmEntities = Models as unknown as any[]
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
mikroOrmEntities,
null,
process.env.MEDUSA_FULFILLMENT_DB_SCHEMA
)
export const DB_URL = TestDatabaseUtils.getDatabaseURL()

View File

@@ -1,33 +0,0 @@
import { Modules, ModulesDefinition } from "@medusajs/modules-sdk"
import { DB_URL } from "./database"
export function getInitModuleConfig() {
const moduleOptions = {
defaultAdapterOptions: {
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_FULFILLMENT_DB_SCHEMA,
},
},
}
const injectedDependencies = {}
const modulesConfig_ = {
[Modules.FULFILLMENT]: {
definition: ModulesDefinition[Modules.FULFILLMENT],
options: moduleOptions,
},
}
return {
injectedDependencies,
modulesConfig: modulesConfig_,
databaseConfig: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_FULFILLMENT_DB_SCHEMA,
},
joinerConfig: [],
}
}

View File

@@ -1,2 +0,0 @@
export * from "./database"
export * from "./get-init-module-config"

View File

@@ -17,6 +17,5 @@ module.exports = {
testEnvironment: `node`,
moduleFileExtensions: [`js`, `ts`],
modulePathIgnorePatterns: ["dist/"],
setupFiles: ["<rootDir>/integration-tests/setup-env.js"],
setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"],
/*setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"],*/
}

View File

@@ -29,7 +29,7 @@
"prepublishOnly": "cross-env NODE_ENV=production tsc --build && tsc-alias -p tsconfig.json",
"build": "rimraf dist && tsc --build && tsc-alias -p tsconfig.json",
"test": "jest --runInBand --bail --forceExit -- src/**/__tests__/**/*.ts",
"test:integration": "jest --runInBand --forceExit -- integration-tests/**/__tests__/**/*.ts",
"test:integration": "jest --runInBand --forceExit -- integration-tests/**/__tests__/**/*.spec.ts",
"migration:generate": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:generate",
"migration:initial": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create --initial",
"migration:create": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create",

View File

@@ -493,20 +493,7 @@
}
],
"checks": [],
"foreignKeys": {
"service_zone_fulfillment_set_id_foreign": {
"constraintName": "service_zone_fulfillment_set_id_foreign",
"columnNames": [
"fulfillment_set_id"
],
"localTableName": "public.service_zone",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.fulfillment_set",
"updateRule": "cascade"
}
}
"foreignKeys": {}
},
{
"columns": {
@@ -686,20 +673,7 @@
}
],
"checks": [],
"foreignKeys": {
"geo_zone_service_zone_id_foreign": {
"constraintName": "geo_zone_service_zone_id_foreign",
"columnNames": [
"service_zone_id"
],
"localTableName": "public.geo_zone",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.service_zone",
"updateRule": "cascade"
}
}
"foreignKeys": {}
},
{
"columns": {
@@ -828,6 +802,24 @@
"nullable": false,
"mappedType": "text"
},
"name": {
"name": "name",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"type": {
"name": "type",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"metadata": {
"name": "metadata",
"type": "jsonb",
@@ -873,6 +865,16 @@
"name": "shipping_profile",
"schema": "public",
"indexes": [
{
"keyName": "IDX_shipping_profile_name_unique",
"columnNames": [
"name"
],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_shipping_profile_name_unique\" ON \"shipping_profile\" (name) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_shipping_profile_deleted_at",
"columnNames": [
@@ -945,7 +947,7 @@
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"nullable": true,
"mappedType": "text"
},
"service_provider_id": {
@@ -954,15 +956,6 @@
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"shipping_option_type_id": {
"name": "shipping_option_type_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "text"
},
@@ -984,6 +977,15 @@
"nullable": true,
"mappedType": "json"
},
"shipping_option_type_id": {
"name": "shipping_option_type_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "text"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
@@ -1091,18 +1093,6 @@
],
"checks": [],
"foreignKeys": {
"shipping_option_service_zone_id_foreign": {
"constraintName": "shipping_option_service_zone_id_foreign",
"columnNames": [
"service_zone_id"
],
"localTableName": "public.shipping_option",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.service_zone",
"updateRule": "cascade"
},
"shipping_option_shipping_profile_id_foreign": {
"constraintName": "shipping_option_shipping_profile_id_foreign",
"columnNames": [
@@ -1113,6 +1103,7 @@
"id"
],
"referencedTableName": "public.shipping_profile",
"deleteRule": "set null",
"updateRule": "cascade"
},
"shipping_option_service_provider_id_foreign": {
@@ -1125,6 +1116,7 @@
"id"
],
"referencedTableName": "public.service_provider",
"deleteRule": "set null",
"updateRule": "cascade"
},
"shipping_option_shipping_option_type_id_foreign": {
@@ -1137,6 +1129,7 @@
"id"
],
"referencedTableName": "public.shipping_option_type",
"deleteRule": "cascade",
"updateRule": "cascade"
}
}
@@ -1224,6 +1217,16 @@
"name": "shipping_option_rule",
"schema": "public",
"indexes": [
{
"keyName": "IDX_shipping_option_rule_shipping_option_id",
"columnNames": [
"shipping_option_id"
],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_option_rule_shipping_option_id\" ON \"shipping_option_rule\" (shipping_option_id) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_shipping_option_rule_deleted_at",
"columnNames": [
@@ -1245,20 +1248,7 @@
}
],
"checks": [],
"foreignKeys": {
"shipping_option_rule_shipping_option_id_foreign": {
"constraintName": "shipping_option_rule_shipping_option_id_foreign",
"columnNames": [
"shipping_option_id"
],
"localTableName": "public.shipping_option_rule",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.shipping_option",
"updateRule": "cascade"
}
}
"foreignKeys": {}
},
{
"columns": {
@@ -1760,7 +1750,7 @@
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_fulfillment_item_fulfillment_id\" ON \"fulfillment_item\" (line_item_id) WHERE deleted_at IS NULL"
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_fulfillment_item_line_item_id\" ON \"fulfillment_item\" (line_item_id) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_fulfillment_item_inventory_item_id",
@@ -1770,7 +1760,7 @@
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_fulfillment_item_fulfillment_id\" ON \"fulfillment_item\" (inventory_item_id) WHERE deleted_at IS NULL"
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_fulfillment_item_inventory_item_id\" ON \"fulfillment_item\" (inventory_item_id) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_fulfillment_item_fulfillment_id",

View File

@@ -1,6 +1,6 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20240214103108 extends Migration {
export class Migration20240219115644 extends Migration {
async up(): Promise<void> {
this.addSql('create table if not exists "fulfillment_address" ("id" text not null, "fulfillment_id" text null, "company" text null, "first_name" text null, "last_name" text null, "address_1" text null, "address_2" text null, "city" text null, "country_code" text null, "province" text null, "postal_code" text null, "phone" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "fulfillment_address_pkey" primary key ("id"));');
@@ -30,10 +30,11 @@ export class Migration20240214103108 extends Migration {
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_shipping_option_type_shipping_option_id" ON "shipping_option_type" (shipping_option_id) WHERE deleted_at IS NULL;');
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_shipping_option_type_deleted_at" ON "shipping_option_type" (deleted_at) WHERE deleted_at IS NOT NULL;');
this.addSql('create table if not exists "shipping_profile" ("id" 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 "shipping_profile_pkey" primary key ("id"));');
this.addSql('create table if not exists "shipping_profile" ("id" text not null, "name" text not null, "type" 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 "shipping_profile_pkey" primary key ("id"));');
this.addSql('CREATE UNIQUE INDEX IF NOT EXISTS "IDX_shipping_profile_name_unique" ON "shipping_profile" (name) WHERE deleted_at IS NULL;');
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_shipping_profile_deleted_at" ON "shipping_profile" (deleted_at) WHERE deleted_at IS NOT NULL;');
this.addSql('create table if not exists "shipping_option" ("id" text not null, "name" text not null, "price_type" text check ("price_type" in (\'calculated\', \'flat\')) not null default \'calculated\', "service_zone_id" text not null, "shipping_profile_id" text not null, "service_provider_id" text not null, "shipping_option_type_id" text null, "data" jsonb null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "shipping_option_pkey" primary key ("id"));');
this.addSql('create table if not exists "shipping_option" ("id" text not null, "name" text not null, "price_type" text check ("price_type" in (\'calculated\', \'flat\')) not null default \'calculated\', "service_zone_id" text not null, "shipping_profile_id" text null, "service_provider_id" text null, "data" jsonb null, "metadata" jsonb null, "shipping_option_type_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "shipping_option_pkey" primary key ("id"));');
this.addSql('alter table if exists "shipping_option" add constraint "shipping_option_shipping_option_type_id_unique" unique ("shipping_option_type_id");');
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_shipping_option_service_zone_id" ON "shipping_option" (service_zone_id) WHERE deleted_at IS NULL;');
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_shipping_option_shipping_profile_id" ON "shipping_option" (shipping_profile_id) WHERE deleted_at IS NULL;');
@@ -42,6 +43,7 @@ export class Migration20240214103108 extends Migration {
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_shipping_option_deleted_at" ON "shipping_option" (deleted_at) WHERE deleted_at IS NOT NULL;');
this.addSql('create table if not exists "shipping_option_rule" ("id" text not null, "attribute" text not null, "operator" text not null, "value" jsonb null, "shipping_option_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "shipping_option_rule_pkey" primary key ("id"));');
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_shipping_option_rule_shipping_option_id" ON "shipping_option_rule" (shipping_option_id) WHERE deleted_at IS NULL;');
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_shipping_option_rule_deleted_at" ON "shipping_option_rule" (deleted_at) WHERE deleted_at IS NOT NULL;');
this.addSql('create table if not exists "fulfillment" ("id" text not null, "location_id" text not null, "packed_at" timestamptz null, "shipped_at" timestamptz null, "delivered_at" timestamptz null, "canceled_at" timestamptz null, "data" jsonb null, "provider_id" text not null, "shipping_option_id" text null, "metadata" jsonb null, "delivery_address_id" text not null, "items_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "fulfillment_pkey" primary key ("id"));');
@@ -55,21 +57,14 @@ export class Migration20240214103108 extends Migration {
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_fulfillment_label_deleted_at" ON "fulfillment_label" (deleted_at) WHERE deleted_at IS NOT NULL;');
this.addSql('create table if not exists "fulfillment_item" ("id" text not null, "title" text not null, "sku" text not null, "barcode" text not null, "quantity" numeric not null, "line_item_id" text null, "inventory_item_id" text null, "fulfillment_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "fulfillment_item_pkey" primary key ("id"));');
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_fulfillment_item_fulfillment_id" ON "fulfillment_item" (line_item_id) WHERE deleted_at IS NULL;');
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_fulfillment_item_fulfillment_id" ON "fulfillment_item" (inventory_item_id) WHERE deleted_at IS NULL;');
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_fulfillment_item_line_item_id" ON "fulfillment_item" (line_item_id) WHERE deleted_at IS NULL;');
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_fulfillment_item_inventory_item_id" ON "fulfillment_item" (inventory_item_id) WHERE deleted_at IS NULL;');
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_fulfillment_item_fulfillment_id" ON "fulfillment_item" (fulfillment_id) WHERE deleted_at IS NULL;');
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_fulfillment_item_deleted_at" ON "fulfillment_item" (deleted_at) WHERE deleted_at IS NOT NULL;');
this.addSql('alter table if exists "service_zone" add constraint "service_zone_fulfillment_set_id_foreign" foreign key ("fulfillment_set_id") references "fulfillment_set" ("id") on update cascade;');
this.addSql('alter table if exists "geo_zone" add constraint "geo_zone_service_zone_id_foreign" foreign key ("service_zone_id") references "service_zone" ("id") on update cascade;');
this.addSql('alter table if exists "shipping_option" add constraint "shipping_option_service_zone_id_foreign" foreign key ("service_zone_id") references "service_zone" ("id") on update cascade;');
this.addSql('alter table if exists "shipping_option" add constraint "shipping_option_shipping_profile_id_foreign" foreign key ("shipping_profile_id") references "shipping_profile" ("id") on update cascade;');
this.addSql('alter table if exists "shipping_option" add constraint "shipping_option_service_provider_id_foreign" foreign key ("service_provider_id") references "service_provider" ("id") on update cascade;');
this.addSql('alter table if exists "shipping_option" add constraint "shipping_option_shipping_option_type_id_foreign" foreign key ("shipping_option_type_id") references "shipping_option_type" ("id") on update cascade;');
this.addSql('alter table if exists "shipping_option_rule" add constraint "shipping_option_rule_shipping_option_id_foreign" foreign key ("shipping_option_id") references "shipping_option" ("id") on update cascade;');
this.addSql('alter table if exists "shipping_option" add constraint "shipping_option_shipping_profile_id_foreign" foreign key ("shipping_profile_id") references "shipping_profile" ("id") on update cascade on delete set null;');
this.addSql('alter table if exists "shipping_option" add constraint "shipping_option_service_provider_id_foreign" foreign key ("service_provider_id") references "service_provider" ("id") on update cascade on delete set null;');
this.addSql('alter table if exists "shipping_option" add constraint "shipping_option_shipping_option_type_id_foreign" foreign key ("shipping_option_type_id") references "shipping_option_type" ("id") on update cascade on delete cascade;');
this.addSql('alter table if exists "fulfillment" add constraint "fulfillment_shipping_option_id_foreign" foreign key ("shipping_option_id") references "shipping_option" ("id") on update cascade on delete set null;');
this.addSql('alter table if exists "fulfillment" add constraint "fulfillment_provider_id_foreign" foreign key ("provider_id") references "service_provider" ("id") on update cascade;');

View File

@@ -6,7 +6,6 @@ import {
import {
BeforeCreate,
Entity,
Index,
OnInit,
OptionalProps,
PrimaryKey,
@@ -15,21 +14,17 @@ import {
type OptionalAddressProps = DAL.SoftDeletableEntityDateColumns
const fulfillmentIdIndexName = "IDX_fulfillment_address_fulfillment_id"
const fulfillmentIdIndexStatement = createPsqlIndexStatementHelper({
name: fulfillmentIdIndexName,
const FulfillmentIdIndex = createPsqlIndexStatementHelper({
tableName: "fulfillment_address",
columns: "fulfillment_id",
where: "deleted_at IS NULL",
}).expression
})
const fulfillmentDeletedAtIndexName = "IDX_fulfillment_address_deleted_at"
const fulfillmentDeletedAtIndexStatement = createPsqlIndexStatementHelper({
name: fulfillmentDeletedAtIndexName,
const FulfillmentDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "fulfillment_address",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).expression
})
@Entity({ tableName: "fulfillment_address" })
export default class Address {
@@ -39,10 +34,7 @@ export default class Address {
id!: string
@Property({ columnType: "text", nullable: true })
@Index({
name: fulfillmentIdIndexName,
expression: fulfillmentIdIndexStatement,
})
@FulfillmentIdIndex.MikroORMIndex()
fulfillment_id: string | null = null
@Property({ columnType: "text", nullable: true })
@@ -94,10 +86,7 @@ export default class Address {
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
@Index({
name: fulfillmentDeletedAtIndexName,
expression: fulfillmentDeletedAtIndexStatement,
})
@FulfillmentDeletedAtIndex.MikroORMIndex()
deleted_at: Date | null = null
@BeforeCreate()

View File

@@ -9,7 +9,6 @@ import {
BeforeCreate,
Entity,
Filter,
Index,
ManyToOne,
OnInit,
OptionalProps,
@@ -20,37 +19,29 @@ import Fulfillment from "./fulfillment"
type FulfillmentItemOptionalProps = DAL.SoftDeletableEntityDateColumns
const fulfillmentIdIndexName = "IDX_fulfillment_item_fulfillment_id"
const fulfillmentIdIndexStatement = createPsqlIndexStatementHelper({
name: fulfillmentIdIndexName,
const FulfillmentIdIndex = createPsqlIndexStatementHelper({
tableName: "fulfillment_item",
columns: "fulfillment_id",
where: "deleted_at IS NULL",
}).expression
})
const lineItemIndexName = "IDX_fulfillment_item_line_item_id"
const lineItemIdIndexStatement = createPsqlIndexStatementHelper({
name: fulfillmentIdIndexName,
const LineItemIdIndex = createPsqlIndexStatementHelper({
tableName: "fulfillment_item",
columns: "line_item_id",
where: "deleted_at IS NULL",
}).expression
})
const inventoryItemIndexName = "IDX_fulfillment_item_inventory_item_id"
const inventoryItemIdIndexStatement = createPsqlIndexStatementHelper({
name: fulfillmentIdIndexName,
const InventoryItemIdIndex = createPsqlIndexStatementHelper({
tableName: "fulfillment_item",
columns: "inventory_item_id",
where: "deleted_at IS NULL",
}).expression
})
const fulfillmentItemDeletedAtIndexName = "IDX_fulfillment_item_deleted_at"
const fulfillmentItemDeletedAtIndexStatement = createPsqlIndexStatementHelper({
name: fulfillmentItemDeletedAtIndexName,
const FulfillmentItemDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "fulfillment_item",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).expression
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@@ -73,24 +64,15 @@ export default class FulfillmentItem {
quantity: number // TODO: probably allow big numbers here
@Property({ columnType: "text", nullable: true })
@Index({
name: lineItemIndexName,
expression: lineItemIdIndexStatement,
})
@LineItemIdIndex.MikroORMIndex()
line_item_id: string | null = null
@Property({ columnType: "text", nullable: true })
@Index({
name: inventoryItemIndexName,
expression: inventoryItemIdIndexStatement,
})
@InventoryItemIdIndex.MikroORMIndex()
inventory_item_id: string | null = null
@Property({ columnType: "text" })
@Index({
name: fulfillmentIdIndexName,
expression: fulfillmentIdIndexStatement,
})
@FulfillmentIdIndex.MikroORMIndex()
fulfillment_id: string
@ManyToOne(() => Fulfillment)
@@ -111,10 +93,7 @@ export default class FulfillmentItem {
})
updated_at: Date
@Index({
name: fulfillmentItemDeletedAtIndexName,
expression: fulfillmentItemDeletedAtIndexStatement,
})
@FulfillmentItemDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null

View File

@@ -9,7 +9,6 @@ import {
BeforeCreate,
Entity,
Filter,
Index,
ManyToOne,
OnInit,
OptionalProps,
@@ -20,21 +19,17 @@ import Fulfillment from "./fulfillment"
type FulfillmentLabelOptionalProps = DAL.SoftDeletableEntityDateColumns
const fulfillmentIdIndexName = "IDX_fulfillment_label_fulfillment_id"
const fulfillmentIdIndexStatement = createPsqlIndexStatementHelper({
name: fulfillmentIdIndexName,
const FulfillmentIdIndex = createPsqlIndexStatementHelper({
tableName: "fulfillment_label",
columns: "fulfillment_id",
where: "deleted_at IS NULL",
}).expression
})
const deletedAtIndexName = "IDX_fulfillment_label_deleted_at"
const deletedAtIndexStatement = createPsqlIndexStatementHelper({
name: deletedAtIndexName,
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "fulfillment_label",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).expression
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@@ -54,10 +49,7 @@ export default class FulfillmentLabel {
label_url: string
@Property({ columnType: "text" })
@Index({
name: fulfillmentIdIndexName,
expression: fulfillmentIdIndexStatement,
})
@FulfillmentIdIndex.MikroORMIndex()
fulfillment_id: string
@ManyToOne(() => Fulfillment)
@@ -79,10 +71,7 @@ export default class FulfillmentLabel {
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
@Index({
name: deletedAtIndexName,
expression: deletedAtIndexStatement,
})
@DeletedAtIndex.MikroORMIndex()
deleted_at: Date | null = null
@BeforeCreate()

View File

@@ -10,7 +10,6 @@ import {
Collection,
Entity,
Filter,
Index,
ManyToOne,
OneToMany,
OnInit,
@@ -26,39 +25,29 @@ import ShippingOption from "./shipping-option"
type FulfillmentOptionalProps = DAL.SoftDeletableEntityDateColumns
const fulfillmentDeletedAtIndexName = "IDX_fulfillment_deleted_at"
const fulfillmentDeletedAtIndexStatement = createPsqlIndexStatementHelper({
name: fulfillmentDeletedAtIndexName,
const FulfillmentDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "fulfillment",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).expression
})
const fulfillmentProviderIdIndexName = "IDX_fulfillment_provider_id"
const fulfillmentProviderIdIndexStatement = createPsqlIndexStatementHelper({
name: fulfillmentProviderIdIndexName,
const FulfillmentProviderIdIndex = createPsqlIndexStatementHelper({
tableName: "fulfillment",
columns: "provider_id",
where: "deleted_at IS NULL",
}).expression
})
const fulfillmentLocationIdIndexName = "IDX_fulfillment_location_id"
const fulfillmentLocationIdIndexStatement = createPsqlIndexStatementHelper({
name: fulfillmentLocationIdIndexName,
const FulfillmentLocationIdIndex = createPsqlIndexStatementHelper({
tableName: "fulfillment",
columns: "location_id",
where: "deleted_at IS NULL",
}).expression
})
const fulfillmentShippingOptionIdIndexName =
"IDX_fulfillment_shipping_option_id"
const fulfillmentShippingOptionIdIndexStatement =
createPsqlIndexStatementHelper({
name: fulfillmentShippingOptionIdIndexName,
tableName: "fulfillment",
columns: "shipping_option_id",
where: "deleted_at IS NULL",
}).expression
const FulfillmentShippingOptionIdIndex = createPsqlIndexStatementHelper({
tableName: "fulfillment",
columns: "shipping_option_id",
where: "deleted_at IS NULL",
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@@ -69,10 +58,7 @@ export default class Fulfillment {
id: string
@Property({ columnType: "text" })
@Index({
name: fulfillmentLocationIdIndexName,
expression: fulfillmentLocationIdIndexStatement,
})
@FulfillmentLocationIdIndex.MikroORMIndex()
location_id: string
@Property({
@@ -103,17 +89,11 @@ export default class Fulfillment {
data: Record<string, unknown> | null = null
@Property({ columnType: "text" })
@Index({
name: fulfillmentProviderIdIndexName,
expression: fulfillmentProviderIdIndexStatement,
})
@FulfillmentProviderIdIndex.MikroORMIndex()
provider_id: string
@Property({ columnType: "text", nullable: true })
@Index({
name: fulfillmentShippingOptionIdIndexName,
expression: fulfillmentShippingOptionIdIndexStatement,
})
@FulfillmentShippingOptionIdIndex.MikroORMIndex()
shipping_option_id: string | null = null
@Property({ columnType: "jsonb", nullable: true })
@@ -149,10 +129,7 @@ export default class Fulfillment {
})
updated_at: Date
@Index({
name: fulfillmentDeletedAtIndexName,
expression: fulfillmentDeletedAtIndexStatement,
})
@FulfillmentDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null

View File

@@ -11,7 +11,6 @@ import {
Collection,
Entity,
Filter,
Index,
OneToMany,
OnInit,
OptionalProps,
@@ -22,22 +21,18 @@ import ServiceZone from "./service-zone"
type FulfillmentSetOptionalProps = DAL.SoftDeletableEntityDateColumns
const deletedAtIndexName = "IDX_fulfillment_set_deleted_at"
const deletedAtIndexStatement = createPsqlIndexStatementHelper({
name: deletedAtIndexName,
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "fulfillment_set",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).expression
})
const nameIndexName = "IDX_fulfillment_set_name_unique"
const nameIndexStatement = createPsqlIndexStatementHelper({
name: nameIndexName,
const NameIndex = createPsqlIndexStatementHelper({
tableName: "fulfillment_set",
columns: "name",
unique: true,
where: "deleted_at IS NULL",
}).expression
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@@ -48,10 +43,7 @@ export default class FulfillmentSet {
id: string
@Property({ columnType: "text" })
@Index({
name: nameIndexName,
expression: nameIndexStatement,
})
@NameIndex.MikroORMIndex()
name: string
@Property({ columnType: "text" })
@@ -82,10 +74,7 @@ export default class FulfillmentSet {
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
@Index({
name: deletedAtIndexName,
expression: deletedAtIndexStatement,
})
@DeletedAtIndex.MikroORMIndex()
deleted_at: Date | null = null
@BeforeCreate()

View File

@@ -11,7 +11,6 @@ import {
Entity,
Enum,
Filter,
Index,
ManyToOne,
OnInit,
OptionalProps,
@@ -22,45 +21,35 @@ import ServiceZone from "./service-zone"
type GeoZoneOptionalProps = DAL.SoftDeletableEntityDateColumns
const deletedAtIndexName = "IDX_geo_zone_deleted_at"
const deletedAtIndexStatement = createPsqlIndexStatementHelper({
name: deletedAtIndexName,
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "geo_zone",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).expression
})
const countryCodeIndexName = "IDX_geo_zone_country_code"
const countryCodeIndexStatement = createPsqlIndexStatementHelper({
name: countryCodeIndexName,
const CountryCodeIndex = createPsqlIndexStatementHelper({
tableName: "geo_zone",
columns: "country_code",
where: "deleted_at IS NULL",
}).expression
})
const provinceCodeIndexName = "IDX_geo_zone_province_code"
const provinceCodeIndexStatement = createPsqlIndexStatementHelper({
name: provinceCodeIndexName,
const ProvinceCodeIndex = createPsqlIndexStatementHelper({
tableName: "geo_zone",
columns: "province_code",
where: "deleted_at IS NULL AND province_code IS NOT NULL",
}).expression
})
const cityIndexName = "IDX_geo_zone_city"
const cityIndexStatement = createPsqlIndexStatementHelper({
name: cityIndexName,
const CityIndex = createPsqlIndexStatementHelper({
tableName: "geo_zone",
columns: "city",
where: "deleted_at IS NULL AND city IS NOT NULL",
}).expression
})
const serviceZoneIdIndexName = "IDX_geo_zone_service_zone_id"
const serviceZoneIdStatement = createPsqlIndexStatementHelper({
name: serviceZoneIdIndexName,
const ServiceZoneIdIndex = createPsqlIndexStatementHelper({
tableName: "geo_zone",
columns: "service_zone_id",
where: "deleted_at IS NULL",
}).expression
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@@ -73,32 +62,20 @@ export default class GeoZone {
@Enum({ items: () => GeoZoneType, default: GeoZoneType.COUNTRY })
type: GeoZoneType
@Index({
name: countryCodeIndexName,
expression: countryCodeIndexStatement,
})
@CountryCodeIndex.MikroORMIndex()
@Property({ columnType: "text" })
country_code: string
@Index({
name: provinceCodeIndexName,
expression: provinceCodeIndexStatement,
})
@ProvinceCodeIndex.MikroORMIndex()
@Property({ columnType: "text", nullable: true })
province_code: string | null = null
@Index({
name: cityIndexName,
expression: cityIndexStatement,
})
@CityIndex.MikroORMIndex()
@Property({ columnType: "text", nullable: true })
city: string | null = null
@Property({ columnType: "text" })
@Index({
name: serviceZoneIdIndexName,
expression: serviceZoneIdStatement,
})
@ServiceZoneIdIndex.MikroORMIndex()
service_zone_id: string
// TODO: Do we have an example or idea of what would be stored in this field? like lat/long for example?
@@ -128,10 +105,7 @@ export default class GeoZone {
})
updated_at: Date
@Index({
name: deletedAtIndexName,
expression: deletedAtIndexStatement,
})
@DeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null

View File

@@ -10,7 +10,6 @@ import {
Collection,
Entity,
Filter,
Index,
OneToMany,
OnInit,
OptionalProps,
@@ -21,13 +20,11 @@ import ShippingOption from "./shipping-option"
type ServiceProviderOptionalProps = DAL.SoftDeletableEntityDateColumns
const deletedAtIndexName = "IDX_service_provider_deleted_at"
const deletedAtIndexStatement = createPsqlIndexStatementHelper({
name: deletedAtIndexName,
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "service_provider",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).expression
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@@ -61,10 +58,7 @@ export default class ServiceProvider {
})
updated_at: Date
@Index({
name: deletedAtIndexName,
expression: deletedAtIndexStatement,
})
@DeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null

View File

@@ -33,22 +33,18 @@ const deletedAtIndexStatement = createPsqlIndexStatementHelper({
where: "deleted_at IS NOT NULL",
}).expression
const nameIndexName = "IDX_service_zone_name_unique"
const nameIndexStatement = createPsqlIndexStatementHelper({
name: nameIndexName,
const NameIndex = createPsqlIndexStatementHelper({
tableName: "service_zone",
columns: "name",
unique: true,
where: "deleted_at IS NULL",
}).expression
})
const fulfillmentSetIdIndexName = "IDX_service_zone_fulfillment_set_id"
const fulfillmentSetIdIndexStatement = createPsqlIndexStatementHelper({
name: fulfillmentSetIdIndexName,
const FulfillmentSetIdIndex = createPsqlIndexStatementHelper({
tableName: "service_zone",
columns: "fulfillment_set_id",
where: "deleted_at IS NULL",
}).expression
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@@ -59,20 +55,14 @@ export default class ServiceZone {
id: string
@Property({ columnType: "text" })
@Index({
name: nameIndexName,
expression: nameIndexStatement,
})
@NameIndex.MikroORMIndex()
name: string
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@Property({ columnType: "text" })
@Index({
name: fulfillmentSetIdIndexName,
expression: fulfillmentSetIdIndexStatement,
})
@FulfillmentSetIdIndex.MikroORMIndex()
fulfillment_set_id: string
@ManyToOne(() => FulfillmentSet, { persist: false })

View File

@@ -9,7 +9,6 @@ import {
BeforeCreate,
Entity,
Filter,
Index,
ManyToOne,
OnInit,
OptionalProps,
@@ -20,16 +19,17 @@ import ShippingOption from "./shipping-option"
type ShippingOptionRuleOptionalProps = DAL.SoftDeletableEntityDateColumns
// TODO: need some test to see if we continue with this kind of structure or we change it
// More adjustments can appear as I move forward
const deletedAtIndexName = "IDX_shipping_option_rule_deleted_at"
const deletedAtIndexStatement = createPsqlIndexStatementHelper({
name: deletedAtIndexName,
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "shipping_option_rule",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).expression
})
const ShippingOptionIdIndex = createPsqlIndexStatementHelper({
tableName: "shipping_option_rule",
columns: "shipping_option_id",
where: "deleted_at IS NULL",
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@@ -49,9 +49,10 @@ export default class ShippingOptionRule {
value: { value: string | string[] } | null = null
@Property({ columnType: "text" })
@ShippingOptionIdIndex.MikroORMIndex()
shipping_option_id: string
@ManyToOne(() => ShippingOption)
@ManyToOne(() => ShippingOption, { persist: false })
shipping_option: ShippingOption
@Property({
@@ -69,20 +70,19 @@ export default class ShippingOptionRule {
})
updated_at: Date
@Index({
name: deletedAtIndexName,
expression: deletedAtIndexStatement,
})
@DeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "sorul")
this.shipping_option_id ??= this.shipping_option?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "sorul")
this.shipping_option_id ??= this.shipping_option?.id
}
}

View File

@@ -9,7 +9,6 @@ import {
BeforeCreate,
Entity,
Filter,
Index,
OneToOne,
OnInit,
OptionalProps,
@@ -20,21 +19,17 @@ import ShippingOption from "./shipping-option"
type ShippingOptionTypeOptionalProps = DAL.SoftDeletableEntityDateColumns
const deletedAtIndexName = "IDX_shipping_option_type_deleted_at"
const deletedAtIndexStatement = createPsqlIndexStatementHelper({
name: deletedAtIndexName,
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "shipping_option_type",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).expression
})
const shippingOptionIdIndexName = "IDX_shipping_option_type_shipping_option_id"
const shippingOptionIdIndexStatement = createPsqlIndexStatementHelper({
name: shippingOptionIdIndexName,
const ShippingOptionIdIndex = createPsqlIndexStatementHelper({
tableName: "shipping_option_type",
columns: "shipping_option_id",
where: "deleted_at IS NULL",
}).expression
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@@ -54,13 +49,12 @@ export default class ShippingOptionType {
code: string
@Property({ columnType: "text" })
@Index({
name: shippingOptionIdIndexName,
expression: shippingOptionIdIndexStatement,
})
@ShippingOptionIdIndex.MikroORMIndex()
shipping_option_id: string
@OneToOne(() => ShippingOption, (so) => so.shipping_option_type)
@OneToOne(() => ShippingOption, (so) => so.type, {
persist: false,
})
shipping_option: ShippingOption
@Property({
@@ -78,20 +72,19 @@ export default class ShippingOptionType {
})
updated_at: Date
@Index({
name: deletedAtIndexName,
expression: deletedAtIndexStatement,
})
@DeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "sotype")
this.shipping_option_id ??= this.shipping_option?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "sotype")
this.shipping_option_id ??= this.shipping_option?.id
}
}

View File

@@ -8,11 +8,11 @@ import {
import { DAL } from "@medusajs/types"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Enum,
Filter,
Index,
ManyToOne,
OneToMany,
OneToOne,
@@ -30,46 +30,35 @@ import ShippingProfile from "./shipping-profile"
type ShippingOptionOptionalProps = DAL.SoftDeletableEntityDateColumns
const deletedAtIndexName = "IDX_shipping_option_deleted_at"
const deletedAtIndexStatement = createPsqlIndexStatementHelper({
name: deletedAtIndexName,
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "shipping_option",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).expression
})
const serviceZoneIdIndexName = "IDX_shipping_option_service_zone_id"
const serviceZoneIdIndexStatement = createPsqlIndexStatementHelper({
name: serviceZoneIdIndexName,
const ServiceZoneIdIndex = createPsqlIndexStatementHelper({
tableName: "shipping_option",
columns: "service_zone_id",
where: "deleted_at IS NULL",
}).expression
})
const shippingProfileIdIndexName = "IDX_shipping_option_shipping_profile_id"
const shippingProfileIdIndexStatement = createPsqlIndexStatementHelper({
name: shippingProfileIdIndexName,
const ShippingProfileIdIndex = createPsqlIndexStatementHelper({
tableName: "shipping_option",
columns: "shipping_profile_id",
where: "deleted_at IS NULL",
}).expression
})
const serviceProviderIdIndexName = "IDX_shipping_option_service_provider_id"
const serviceProviderIdIndexStatement = createPsqlIndexStatementHelper({
name: serviceProviderIdIndexName,
const ServiceProviderIdIndex = createPsqlIndexStatementHelper({
tableName: "shipping_option",
columns: "service_provider_id",
where: "deleted_at IS NULL",
}).expression
})
const shippingOptionTypeIdIndexName =
"IDX_shipping_option_shipping_option_type_id"
const shippingOptionTypeIdIndexStatement = createPsqlIndexStatementHelper({
name: shippingOptionTypeIdIndexName,
const ShippingOptionTypeIdIndex = createPsqlIndexStatementHelper({
tableName: "shipping_option",
columns: "shipping_option_type_id",
where: "deleted_at IS NULL",
}).expression
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@@ -88,32 +77,34 @@ export default class ShippingOption {
})
price_type: ShippingOptionPriceType
@Property({ columnType: "text" })
@Index({
name: serviceZoneIdIndexName,
expression: serviceZoneIdIndexStatement,
@ManyToOne(() => ShippingProfile, {
type: "text",
fieldName: "service_zone_id",
mapToPk: true,
})
@ServiceZoneIdIndex.MikroORMIndex()
service_zone_id: string
@Property({ columnType: "text" })
@Index({
name: shippingProfileIdIndexName,
expression: shippingProfileIdIndexStatement,
@ManyToOne(() => ShippingProfile, {
type: "text",
fieldName: "shipping_profile_id",
mapToPk: true,
nullable: true,
})
shipping_profile_id: string
@ShippingProfileIdIndex.MikroORMIndex()
shipping_profile_id: string | null
@Property({ columnType: "text" })
@Index({
name: serviceProviderIdIndexName,
expression: serviceProviderIdIndexStatement,
@ManyToOne(() => ServiceProvider, {
type: "text",
fieldName: "service_provider_id",
mapToPk: true,
nullable: true,
})
@ServiceProviderIdIndex.MikroORMIndex()
service_provider_id: string
@Property({ columnType: "text", nullable: true })
@Index({
name: shippingOptionTypeIdIndexName,
expression: shippingOptionTypeIdIndexStatement,
})
@Property({ columnType: "text", persist: false })
@ShippingOptionTypeIdIndex.MikroORMIndex()
shipping_option_type_id: string | null = null
@Property({ columnType: "jsonb", nullable: true })
@@ -122,21 +113,30 @@ export default class ShippingOption {
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@ManyToOne(() => ServiceZone)
@ManyToOne(() => ServiceZone, { persist: false })
service_zone: ServiceZone
@ManyToOne(() => ShippingProfile)
shipping_profile: ShippingProfile
@ManyToOne(() => ShippingProfile, {
persist: false,
})
shipping_profile: ShippingProfile | null
@ManyToOne(() => ServiceProvider)
service_provider: ServiceProvider
@ManyToOne(() => ServiceProvider, {
persist: false,
})
service_provider: ServiceProvider | null
@OneToOne(() => ShippingOptionType, (so) => so.shipping_option, {
owner: true,
cascade: [Cascade.PERSIST, Cascade.REMOVE, "soft-remove"] as any,
fieldName: "shipping_option_type_id",
})
shipping_option_type: ShippingOptionType
type: ShippingOptionType
@OneToMany(() => ShippingOptionRule, (sor) => sor.shipping_option)
@OneToMany(() => ShippingOptionRule, "shipping_option", {
cascade: [Cascade.PERSIST, "soft-remove"] as any,
orphanRemoval: true,
})
rules = new Collection<ShippingOptionRule>(this)
@OneToMany(() => Fulfillment, (fulfillment) => fulfillment.shipping_option)
@@ -157,20 +157,19 @@ export default class ShippingOption {
})
updated_at: Date
@Index({
name: deletedAtIndexName,
expression: deletedAtIndexStatement,
})
@DeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "so")
this.shipping_option_type_id ??= this.type?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "so")
this.shipping_option_type_id ??= this.type?.id
}
}

View File

@@ -10,7 +10,6 @@ import {
Collection,
Entity,
Filter,
Index,
OneToMany,
OnInit,
OptionalProps,
@@ -21,13 +20,18 @@ import ShippingOption from "./shipping-option"
type ShippingProfileOptionalProps = DAL.SoftDeletableEntityDateColumns
const deletedAtIndexName = "IDX_shipping_profile_deleted_at"
const deletedAtIndexStatement = createPsqlIndexStatementHelper({
name: deletedAtIndexName,
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "shipping_profile",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).expression
})
const ShippingProfileTypeIndex = createPsqlIndexStatementHelper({
tableName: "shipping_profile",
columns: "name",
unique: true,
where: "deleted_at IS NULL",
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@@ -37,6 +41,13 @@ export default class ShippingProfile {
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
@ShippingProfileTypeIndex.MikroORMIndex()
name: string
@Property({ columnType: "text" })
type: string
@OneToMany(
() => ShippingOption,
(shippingOption) => shippingOption.shipping_profile
@@ -61,10 +72,7 @@ export default class ShippingProfile {
})
updated_at: Date
@Index({
name: deletedAtIndexName,
expression: deletedAtIndexStatement,
})
@DeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null

View File

@@ -34,7 +34,7 @@ const connectionLoader = ModulesSdkUtils.mikroOrmConnectionLoaderFactory({
})
const service = FulfillmentModuleService
const loaders = [containerLoader, connectionLoader] as any
const loaders = [containerLoader, connectionLoader]
export const moduleDefinition: ModuleExports = {
service,

View File

@@ -9,31 +9,46 @@ import {
UpdateFulfillmentSetDTO,
} from "@medusajs/types"
import {
getSetDifference,
InjectManager,
InjectTransactionManager,
MedusaContext,
MedusaError,
ModulesSdkUtils,
promiseAll,
getSetDifference
} from "@medusajs/utils"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
import { FulfillmentSet, GeoZone, ServiceZone, ShippingOption } from "@models"
import {
FulfillmentSet,
GeoZone,
ServiceZone,
ShippingOption,
ShippingProfile,
} from "@models"
const generateMethodForModels = [ServiceZone, ShippingOption, GeoZone]
const generateMethodForModels = [
ServiceZone,
ShippingOption,
GeoZone,
ShippingProfile,
]
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
fulfillmentSetService: ModulesSdkTypes.InternalModuleService<any>
serviceZoneService: ModulesSdkTypes.InternalModuleService<any>
geoZoneService: ModulesSdkTypes.InternalModuleService<any>
shippingProfileService: ModulesSdkTypes.InternalModuleService<any>
shippingOptionService: ModulesSdkTypes.InternalModuleService<any>
}
export default class FulfillmentModuleService<
TEntity extends FulfillmentSet = FulfillmentSet,
TServiceZoneEntity extends ServiceZone = ServiceZone,
TGeoZoneEntity extends GeoZone = GeoZone
TGeoZoneEntity extends GeoZone = GeoZone,
TShippingProfileEntity extends ShippingProfile = ShippingProfile,
TShippingOptionEntity extends ShippingOption = ShippingOption
>
extends ModulesSdkUtils.abstractModuleServiceFactory<
InjectedDependencies,
@@ -43,6 +58,7 @@ export default class FulfillmentModuleService<
ServiceZone: { dto: FulfillmentTypes.ServiceZoneDTO }
ShippingOption: { dto: FulfillmentTypes.ShippingOptionDTO }
GeoZone: { dto: FulfillmentTypes.GeoZoneDTO }
ShippingProfile: { dto: FulfillmentTypes.ShippingProfileDTO }
}
>(FulfillmentSet, generateMethodForModels, entityNameToLinkableKeysMap)
implements IFulfillmentModuleService
@@ -51,6 +67,8 @@ export default class FulfillmentModuleService<
protected readonly fulfillmentSetService_: ModulesSdkTypes.InternalModuleService<TEntity>
protected readonly serviceZoneService_: ModulesSdkTypes.InternalModuleService<TServiceZoneEntity>
protected readonly geoZoneService_: ModulesSdkTypes.InternalModuleService<TGeoZoneEntity>
protected readonly shippingProfileService_: ModulesSdkTypes.InternalModuleService<TShippingProfileEntity>
protected readonly shippingOptionService_: ModulesSdkTypes.InternalModuleService<TShippingOptionEntity>
constructor(
{
@@ -58,6 +76,8 @@ export default class FulfillmentModuleService<
fulfillmentSetService,
serviceZoneService,
geoZoneService,
shippingProfileService,
shippingOptionService,
}: InjectedDependencies,
protected readonly moduleDeclaration: InternalModuleDeclaration
) {
@@ -67,6 +87,8 @@ export default class FulfillmentModuleService<
this.fulfillmentSetService_ = fulfillmentSetService
this.serviceZoneService_ = serviceZoneService
this.geoZoneService_ = geoZoneService
this.shippingProfileService_ = shippingProfileService
this.shippingOptionService_ = shippingOptionService
}
__joinerConfig(): ModuleJoinerConfig {
@@ -179,7 +201,7 @@ export default class FulfillmentModuleService<
sharedContext?: Context
): Promise<FulfillmentTypes.ShippingOptionDTO>
@InjectTransactionManager("baseRepository_")
@InjectManager("baseRepository_")
async createShippingOptions(
data:
| FulfillmentTypes.CreateShippingOptionDTO[]
@@ -188,7 +210,93 @@ export default class FulfillmentModuleService<
): Promise<
FulfillmentTypes.ShippingOptionDTO | FulfillmentTypes.ShippingOptionDTO[]
> {
return []
const createdShippingOptions = await this.createShippingOptions_(
data,
sharedContext
)
return await this.baseRepository_.serialize<
FulfillmentTypes.ShippingOptionDTO | FulfillmentTypes.ShippingOptionDTO[]
>(createdShippingOptions, {
populate: true,
})
}
@InjectTransactionManager("baseRepository_")
async createShippingOptions_(
data:
| FulfillmentTypes.CreateShippingOptionDTO[]
| FulfillmentTypes.CreateShippingOptionDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<TShippingOptionEntity | TShippingOptionEntity[]> {
let data_ = Array.isArray(data) ? data : [data]
if (!data_.length) {
return []
}
const createdShippingOptions = await this.shippingOptionService_.create(
data_,
sharedContext
)
return Array.isArray(data)
? createdShippingOptions
: createdShippingOptions[0]
}
createShippingProfiles(
data: FulfillmentTypes.CreateShippingProfileDTO[],
sharedContext?: Context
): Promise<FulfillmentTypes.ShippingProfileDTO[]>
createShippingProfiles(
data: FulfillmentTypes.CreateShippingProfileDTO,
sharedContext?: Context
): Promise<FulfillmentTypes.ShippingProfileDTO>
@InjectTransactionManager("baseRepository_")
async createShippingProfiles(
data:
| FulfillmentTypes.CreateShippingProfileDTO[]
| FulfillmentTypes.CreateShippingProfileDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<
FulfillmentTypes.ShippingProfileDTO | FulfillmentTypes.ShippingProfileDTO[]
> {
const createdShippingProfiles = await this.createShippingProfiles_(
data,
sharedContext
)
return await this.baseRepository_.serialize<
| FulfillmentTypes.ShippingProfileDTO
| FulfillmentTypes.ShippingProfileDTO[]
>(createdShippingProfiles, {
populate: true,
})
}
@InjectTransactionManager("baseRepository_")
async createShippingProfiles_(
data:
| FulfillmentTypes.CreateShippingProfileDTO[]
| FulfillmentTypes.CreateShippingProfileDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<TShippingProfileEntity[] | TShippingProfileEntity> {
const data_ = Array.isArray(data) ? data : [data]
if (!data_.length) {
return []
}
const createdShippingProfiles = await this.shippingProfileService_.create(
data_,
sharedContext
)
return Array.isArray(data)
? createdShippingProfiles
: createdShippingProfiles[0]
}
createGeoZones(
@@ -473,7 +581,6 @@ export default class FulfillmentModuleService<
serviceZones.map((s) => [s.id, s])
)
const serviceZoneIdsToDelete: string[] = []
const geoZoneIdsToDelete: string[] = []
data_.forEach((serviceZone) => {
@@ -567,6 +674,27 @@ export default class FulfillmentModuleService<
return []
}
updateShippingProfiles(
data: FulfillmentTypes.UpdateShippingProfileDTO[],
sharedContext?: Context
): Promise<FulfillmentTypes.ShippingProfileDTO[]>
updateShippingProfiles(
data: FulfillmentTypes.UpdateShippingProfileDTO,
sharedContext?: Context
): Promise<FulfillmentTypes.ShippingProfileDTO>
@InjectTransactionManager("baseRepository_")
async updateShippingProfiles(
data:
| FulfillmentTypes.UpdateShippingProfileDTO
| FulfillmentTypes.UpdateShippingProfileDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<
FulfillmentTypes.ShippingProfileDTO | FulfillmentTypes.ShippingProfileDTO[]
> {
return []
}
updateGeoZones(
data: FulfillmentTypes.UpdateGeoZoneDTO[],
sharedContext?: Context

View File

@@ -1,24 +1,28 @@
import { MikroORM, Options, SqlEntityManager } from "@mikro-orm/postgresql"
import * as process from "process"
import { Migrator } from "@mikro-orm/migrations"
export function getDatabaseURL(): string {
export function getDatabaseURL(dbName?: string): string {
const DB_HOST = process.env.DB_HOST ?? "localhost"
const DB_USERNAME = process.env.DB_USERNAME ?? ""
const DB_PASSWORD = process.env.DB_PASSWORD
const DB_NAME = process.env.DB_TEMP_NAME
const DB_NAME = dbName ?? process.env.DB_TEMP_NAME
return `postgres://${DB_USERNAME}${
DB_PASSWORD ? `:${DB_PASSWORD}` : ""
}@${DB_HOST}/${DB_NAME}`
}
export function getMikroOrmConfig(
mikroOrmEntities: any[],
pathToMigrations: string, // deprecated, auto inferred
export function getMikroOrmConfig({
mikroOrmEntities,
pathToMigrations,
clientUrl,
schema,
}: {
mikroOrmEntities: any[]
pathToMigrations?: string
clientUrl?: string
schema?: string
): Options {
const DB_URL = getDatabaseURL()
}): Options {
const DB_URL = clientUrl ?? getDatabaseURL()
return {
type: "postgresql",
@@ -26,14 +30,18 @@ export function getMikroOrmConfig(
entities: Object.values(mikroOrmEntities),
schema: schema ?? process.env.MEDUSA_DB_SCHEMA,
debug: false,
extensions: [Migrator],
migrations: {
pathTs: pathToMigrations,
silent: true,
},
}
}
export interface TestDatabase {
mikroOrmEntities: any[]
pathToMigrations: any // deprecated, auto inferred
pathToMigrations?: string
schema?: string
clientUrl?: string
orm: MikroORM | null
manager: SqlEntityManager | null
@@ -45,14 +53,21 @@ export interface TestDatabase {
getOrm(): MikroORM
}
export function getMikroOrmWrapper(
mikroOrmEntities: any[],
pathToMigrations: string, // deprecated, auto inferred
export function getMikroOrmWrapper({
mikroOrmEntities,
pathToMigrations,
clientUrl,
schema,
}: {
mikroOrmEntities: any[]
pathToMigrations?: string
clientUrl?: string
schema?: string
): TestDatabase {
}): TestDatabase {
return {
mikroOrmEntities,
pathToMigrations, // deprecated, auto inferred
pathToMigrations,
clientUrl: clientUrl ?? getDatabaseURL(),
schema: schema ?? process.env.MEDUSA_DB_SCHEMA,
orm: null,
@@ -83,11 +98,12 @@ export function getMikroOrmWrapper(
},
async setupDatabase() {
const OrmConfig = getMikroOrmConfig(
this.mikroOrmEntities,
this.pathToMigrations,
this.schema
)
const OrmConfig = getMikroOrmConfig({
mikroOrmEntities: this.mikroOrmEntities,
pathToMigrations: this.pathToMigrations,
clientUrl: this.clientUrl,
schema: this.schema,
})
// Initializing the ORM
this.orm = await MikroORM.init(OrmConfig)
@@ -96,7 +112,9 @@ export function getMikroOrmWrapper(
try {
await this.orm.getSchemaGenerator().ensureDatabase()
} catch (err) {}
} catch (err) {
console.log(err)
}
await this.manager?.execute(
`CREATE SCHEMA IF NOT EXISTS "${this.schema ?? "public"}";`

View File

@@ -4,3 +4,4 @@ export * as JestUtils from "./jest"
export { default as MockManager } from "./mock-manager"
export { default as MockRepository } from "./mock-repository"
export * from "./init-modules"
export * from "./module-test-runner"

View File

@@ -1,13 +1,12 @@
import { knex } from "knex"
import {
MedusaApp,
MedusaModule,
MedusaModuleConfig,
ModuleJoinerConfig,
} from "@medusajs/modules-sdk"
import { ContainerRegistrationKeys } from "@medusajs/utils"
import { ContainerRegistrationKeys, ModulesSdkUtils } from "@medusajs/utils"
interface InitModulesOptions {
export interface InitModulesOptions {
injectedDependencies?: Record<string, unknown>
databaseConfig: {
clientUrl: string
@@ -15,6 +14,7 @@ interface InitModulesOptions {
}
modulesConfig: MedusaModuleConfig
joinerConfig?: ModuleJoinerConfig[]
preventConnectionDestroyWarning?: boolean
}
export async function initModules({
@@ -22,19 +22,18 @@ export async function initModules({
databaseConfig,
modulesConfig,
joinerConfig,
preventConnectionDestroyWarning = false,
}: InitModulesOptions) {
injectedDependencies ??= {}
let sharedPgConnection =
injectedDependencies?.[ContainerRegistrationKeys.PG_CONNECTION]
let shouldDestroyConnectionAutomatically = !sharedPgConnection
if (!sharedPgConnection) {
sharedPgConnection = knex<any, any>({
client: "pg",
searchPath: databaseConfig.schema,
connection: {
connectionString: databaseConfig.clientUrl,
},
sharedPgConnection = ModulesSdkUtils.createPgConnection({
clientUrl: databaseConfig.clientUrl,
schema: databaseConfig.schema,
})
injectedDependencies[ContainerRegistrationKeys.PG_CONNECTION] =
@@ -48,7 +47,16 @@ export async function initModules({
})
async function shutdown() {
await (sharedPgConnection as any).context?.destroy()
if (shouldDestroyConnectionAutomatically) {
await (sharedPgConnection as any).context?.destroy()
await (sharedPgConnection as any).destroy()
} else {
if (!preventConnectionDestroyWarning) {
console.info(
`You are using a custom shared connection. The connection won't be destroyed automatically.`
)
}
}
MedusaModule.clearInstances()
}

View File

@@ -0,0 +1,128 @@
import { getDatabaseURL, getMikroOrmWrapper, TestDatabase } from "./database"
import { MedusaAppOutput, ModulesDefinition } from "@medusajs/modules-sdk"
import { initModules, InitModulesOptions } from "./init-modules"
import { ContainerRegistrationKeys, ModulesSdkUtils } from "@medusajs/utils"
export interface SuiteOptions<TService = unknown> {
MikroOrmWrapper: TestDatabase
medusaApp: MedusaAppOutput
service: TService
dbConfig: {
schema: string
clientUrl: string
}
}
export function moduleIntegrationTestRunner({
moduleName,
moduleModels,
joinerConfig = [],
schema = "public",
testSuite,
}: {
moduleName: string
moduleModels?: any[]
joinerConfig?: any[]
schema?: string
dbName?: string
testSuite: <TService = unknown>(options: SuiteOptions<TService>) => () => void
}) {
moduleModels = Object.values(require(`${process.cwd()}/src/models`))
// migrationPath ??= process.cwd() + "/src/migrations/!(*.d).{js,ts,cjs}"
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
const dbName = `medusa-${moduleName.toLowerCase()}-integration-${tempName}`
const dbConfig = {
clientUrl: getDatabaseURL(dbName),
schema,
}
// Use a unique connection for all the entire suite
const connection = ModulesSdkUtils.createPgConnection(dbConfig)
const MikroOrmWrapper = getMikroOrmWrapper({
mikroOrmEntities: moduleModels,
clientUrl: dbConfig.clientUrl,
schema: dbConfig.schema,
})
const modulesConfig_ = {
[moduleName]: {
definition: ModulesDefinition[moduleName],
options: {
defaultAdapterOptions: {
database: dbConfig,
},
},
},
}
const moduleOptions: InitModulesOptions = {
injectedDependencies: {
[ContainerRegistrationKeys.PG_CONNECTION]: connection,
},
modulesConfig: modulesConfig_,
databaseConfig: dbConfig,
joinerConfig,
preventConnectionDestroyWarning: true,
}
let shutdown: () => Promise<void>
let moduleService
let medusaApp: MedusaAppOutput = {} as MedusaAppOutput
const options = {
MikroOrmWrapper,
medusaApp: new Proxy(
{},
{
get: (target, prop) => {
return medusaApp[prop]
},
}
) as MedusaAppOutput,
service: new Proxy(
{},
{
get: (target, prop) => {
return moduleService[prop]
},
}
),
} as SuiteOptions
const beforeEach_ = async () => {
try {
await MikroOrmWrapper.setupDatabase()
const output = await initModules(moduleOptions)
shutdown = output.shutdown
medusaApp = output.medusaApp
moduleService = output.medusaApp.modules[moduleName]
} catch (error) {
console.error("Error setting up database:", error)
}
}
const afterEach_ = async () => {
try {
await MikroOrmWrapper.clearDatabase()
await shutdown()
moduleService = {}
medusaApp = {} as MedusaAppOutput
} catch (error) {
console.error("Error tearing down database:", error)
}
}
return describe("", () => {
beforeEach(beforeEach_)
afterEach(afterEach_)
afterAll(async () => {
await (connection as any).context?.destroy()
await (connection as any).destroy()
})
testSuite(options)
})
}

View File

@@ -4,10 +4,9 @@ import * as Models from "@models"
const mikroOrmEntities = Models as unknown as any[]
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper({
mikroOrmEntities,
null,
process.env.MEDUSA_ORDER_DB_SCHEMA
)
schema: process.env.MEDUSA_ORDER_DB_SCHEMA,
})
export const DB_URL = TestDatabaseUtils.getDatabaseURL()

View File

@@ -1,18 +1,18 @@
import { TestDatabaseUtils } from "medusa-test-utils"
import {TestDatabaseUtils} from "medusa-test-utils"
import * as PaymentModules from "@models"
const pathToMigrations = "../../src/migrations"
const mikroOrmEntities = PaymentModules as unknown as any[]
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig(
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const DB_URL = TestDatabaseUtils.getDatabaseURL()

View File

@@ -5,14 +5,14 @@ import * as PricingModels from "@models"
const pathToMigrations = "../../src/migrations"
const mikroOrmEntities = PricingModels as unknown as any[]
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig(
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const DB_URL = TestDatabaseUtils.getDatabaseURL()

View File

@@ -1,7 +1,7 @@
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
import { IProductModuleService } from "@medusajs/types"
import { kebabCase } from "@medusajs/utils"
import { knex } from "knex"
import { knex } from "@mikro-orm/knex"
import { initModules } from "medusa-test-utils"
import * as CustomRepositories from "../__fixtures__/module"
import {

View File

@@ -1,18 +1,18 @@
import { TestDatabaseUtils } from "medusa-test-utils"
import {TestDatabaseUtils} from "medusa-test-utils"
import * as PromotionModels from "@models"
const pathToMigrations = "../../src/migrations"
const mikroOrmEntities = PromotionModels as unknown as any[]
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig(
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const DB_URL = TestDatabaseUtils.getDatabaseURL()

View File

@@ -1,18 +1,18 @@
import { TestDatabaseUtils } from "medusa-test-utils"
import {TestDatabaseUtils} from "medusa-test-utils"
import * as RegionModels from "@models"
const pathToMigrations = "../../src/migrations"
const mikroOrmEntities = RegionModels as unknown as any[]
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig(
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const DB_URL = TestDatabaseUtils.getDatabaseURL()

View File

@@ -1,18 +1,18 @@
import { TestDatabaseUtils } from "medusa-test-utils"
import {TestDatabaseUtils} from "medusa-test-utils"
import * as SalesChannelModels from "@models"
const pathToMigrations = "../../src/migrations"
const mikroOrmEntities = SalesChannelModels as unknown as any[]
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig(
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const DB_URL = TestDatabaseUtils.getDatabaseURL()

View File

@@ -4,10 +4,9 @@ import * as Models from "@models"
const mikroOrmEntities = Models as unknown as any[]
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper({
mikroOrmEntities,
null,
process.env.MEDUSA_TAX_DB_SCHEMA
)
schema: process.env.MEDUSA_ORDER_DB_SCHEMA,
})
export const DB_URL = TestDatabaseUtils.getDatabaseURL()

View File

@@ -1,4 +1,4 @@
import { Dictionary, FilterQuery, Order } from "./utils"
import { Dictionary, FilterQuery, OperatorMap, Order } from "./utils"
export { FilterQuery, OperatorMap } from "./utils"

View File

@@ -1,7 +1,7 @@
import { FulfillmentSetDTO } from "./fulfillment-set"
import { FilterableGeoZoneProps, GeoZoneDTO } from "./geo-zone"
import { ShippingOptionDTO } from "./shipping-option"
import { BaseFilterable } from "../../dal"
import { BaseFilterable, OperatorMap } from "../../dal"
export interface ServiceZoneDTO {
id: string
@@ -17,8 +17,8 @@ export interface ServiceZoneDTO {
export interface FilterableServiceZoneProps
extends BaseFilterable<FilterableServiceZoneProps> {
id?: string | string[]
name?: string | string[]
id?: string | string[] | OperatorMap<string | string[]>
name?: string | string[] | OperatorMap<string | string[]>
geo_zones?: FilterableGeoZoneProps
shipping_options?: any // TODO
}

View File

@@ -1,5 +1,5 @@
import { ShippingOptionDTO } from "./shipping-option"
import { BaseFilterable } from "../../dal"
import { BaseFilterable, OperatorMap } from "../../dal"
export interface ShippingOptionRuleDTO {
id: string
@@ -15,8 +15,8 @@ export interface ShippingOptionRuleDTO {
export interface FilterableShippingOptionRuleProps
extends BaseFilterable<FilterableShippingOptionRuleProps> {
id?: string | string[]
attribute?: string | string[]
operator?: string | string[]
value?: string | string[]
id?: string | string[] | OperatorMap<string | string[]>
attribute?: string | string[] | OperatorMap<string | string[]>
operator?: string | string[] | OperatorMap<string | string[]>
value?: string | string[] | OperatorMap<string | string[]>
}

View File

@@ -1,5 +1,5 @@
import { ShippingOptionDTO } from "./shipping-option"
import { BaseFilterable } from "../../dal"
import { BaseFilterable, OperatorMap } from "../../dal"
export interface ShippingOptionTypeDTO {
id: string
@@ -15,8 +15,8 @@ export interface ShippingOptionTypeDTO {
export interface FilterableShippingOptionTypeProps
extends BaseFilterable<FilterableShippingOptionTypeProps> {
id?: string | string[]
label?: string | string[]
description?: string | string[]
code?: string | string[]
id?: string | string[] | OperatorMap<string | string[]>
label?: string | string[] | OperatorMap<string | string[]>
description?: string | string[] | OperatorMap<string | string[]>
code?: string | string[] | OperatorMap<string | string[]>
}

View File

@@ -9,7 +9,7 @@ import {
FilterableShippingOptionRuleProps,
ShippingOptionRuleDTO,
} from "./shipping-option-rule"
import { BaseFilterable } from "../../dal"
import { BaseFilterable, OperatorMap } from "../../dal"
export type ShippingOptionPriceType = "calculated" | "flat"
@@ -35,9 +35,12 @@ export interface ShippingOptionDTO {
export interface FilterableShippingOptionProps
extends BaseFilterable<FilterableShippingOptionProps> {
id?: string | string[]
name?: string | string[]
price_type?: ShippingOptionPriceType | ShippingOptionPriceType[]
id?: string | string[] | OperatorMap<string | string[]>
name?: string | string[] | OperatorMap<string | string[]>
price_type?:
| ShippingOptionPriceType
| ShippingOptionPriceType[]
| OperatorMap<ShippingOptionPriceType | ShippingOptionPriceType[]>
service_zone?: FilterableServiceZoneProps
shipping_option_type?: FilterableShippingOptionTypeProps
rules?: FilterableShippingOptionRuleProps

View File

@@ -1,11 +1,24 @@
import { ShippingOptionDTO } from "./shipping-option"
import {
FilterableShippingOptionProps,
ShippingOptionDTO,
} from "./shipping-option"
import { BaseFilterable, OperatorMap } from "../../dal"
export interface ShippingProfileDTO {
id: string
name: string
type: string
metadata: Record<string, unknown> | null
shipping_options: ShippingOptionDTO[]
created_at: Date
updated_at: Date
deleted_at: Date | null
}
export interface FilterableShippingProfileProps
extends BaseFilterable<FilterableShippingProfileProps> {
id?: string | string[] | OperatorMap<string | string[]>
name?: string | string[] | OperatorMap<string | string[]>
type?: string | string[] | OperatorMap<string | string[]>
shipping_options?: FilterableShippingOptionProps
}

View File

@@ -7,21 +7,21 @@ interface CreateGeoZoneBaseDTO {
metadata?: Record<string, any> | null
}
interface CreateCountryGeoZoneDTO extends CreateGeoZoneBaseDTO {
export interface CreateCountryGeoZoneDTO extends CreateGeoZoneBaseDTO {
type: "country"
}
interface CreateProvinceGeoZoneDTO extends CreateGeoZoneBaseDTO {
export interface CreateProvinceGeoZoneDTO extends CreateGeoZoneBaseDTO {
type: "province"
province_code: string
}
interface CreateCityGeoZoneDTO extends CreateGeoZoneBaseDTO {
export interface CreateCityGeoZoneDTO extends CreateGeoZoneBaseDTO {
type: "city"
city: string
}
interface CreateZipGeoZoneDTO extends CreateGeoZoneBaseDTO {
export interface CreateZipGeoZoneDTO extends CreateGeoZoneBaseDTO {
type: "zip"
postal_expression: Record<string, any>
}
@@ -32,25 +32,25 @@ export type CreateGeoZoneDTO =
| CreateCityGeoZoneDTO
| CreateZipGeoZoneDTO
interface UpdateGeoZoneBaseDTO extends Partial<CreateGeoZoneBaseDTO> {
export interface UpdateGeoZoneBaseDTO extends Partial<CreateGeoZoneBaseDTO> {
id: string
}
interface UpdateCountryGeoZoneDTO extends UpdateGeoZoneBaseDTO {
export interface UpdateCountryGeoZoneDTO extends UpdateGeoZoneBaseDTO {
type: "country"
}
interface UpdateProvinceGeoZoneDTO extends UpdateGeoZoneBaseDTO {
export interface UpdateProvinceGeoZoneDTO extends UpdateGeoZoneBaseDTO {
type: "province"
province_code: string
}
interface UpdateCityGeoZoneDTO extends UpdateGeoZoneBaseDTO {
export interface UpdateCityGeoZoneDTO extends UpdateGeoZoneBaseDTO {
type: "city"
city: string
}
interface UpdateZipGeoZoneDTO extends UpdateGeoZoneBaseDTO {
export interface UpdateZipGeoZoneDTO extends UpdateGeoZoneBaseDTO {
type: "zip"
postal_expression: Record<string, any>
}

View File

@@ -1,3 +1,4 @@
export * from "./shipping-profile"
export * from "./shipping-option-type"
export * from "./shipping-option-rule"
export * from "./geo-zone"

View File

@@ -1,13 +1,29 @@
import { CreateGeoZoneDTO } from "./geo-zone"
import {
CreateCityGeoZoneDTO,
CreateCountryGeoZoneDTO,
CreateProvinceGeoZoneDTO,
CreateZipGeoZoneDTO,
} from "./geo-zone"
export interface CreateServiceZoneDTO {
name: string
fulfillment_set_id: string
geo_zones?: Omit<CreateGeoZoneDTO, "service_zone_id">[]
geo_zones?: (
| Omit<CreateCountryGeoZoneDTO, "service_zone_id">
| Omit<CreateProvinceGeoZoneDTO, "service_zone_id">
| Omit<CreateCityGeoZoneDTO, "service_zone_id">
| Omit<CreateZipGeoZoneDTO, "service_zone_id">
)[]
}
export interface UpdateServiceZoneDTO {
id: string
name?: string
geo_zones?: (Omit<CreateGeoZoneDTO, "service_zone_id"> | { id: string })[]
geo_zones?: (
| Omit<CreateCountryGeoZoneDTO, "service_zone_id">
| Omit<CreateProvinceGeoZoneDTO, "service_zone_id">
| Omit<CreateCityGeoZoneDTO, "service_zone_id">
| Omit<CreateZipGeoZoneDTO, "service_zone_id">
| { id: string }
)[]
}

View File

@@ -1,12 +1,6 @@
import {
CreateShippingOptionTypeDTO,
UpdateShippingOptionTypeDTO,
} from "./shipping-option-type"
import { CreateShippingOptionTypeDTO } from "./shipping-option-type"
import { ShippingOptionPriceType } from "../common"
import {
CreateShippingOptionRuleDTO,
UpdateShippingOptionRuleDTO,
} from "./shipping-option-rule"
import { CreateShippingOptionRuleDTO } from "./shipping-option-rule"
export interface CreateShippingOptionDTO {
name: string
@@ -26,12 +20,10 @@ export interface UpdateShippingOptionDTO {
service_zone_id?: string
shipping_profile_id?: string
service_provider_id?: string
type:
| Omit<CreateShippingOptionTypeDTO, "shipping_option_id">
| Omit<UpdateShippingOptionTypeDTO, "shipping_option_id">
type: Omit<CreateShippingOptionTypeDTO, "shipping_option_id"> | { id: string }
data?: Record<string, unknown> | null
rules?: (
| Omit<CreateShippingOptionRuleDTO, "shipping_option_id">
| Omit<UpdateShippingOptionRuleDTO, "shipping_option_id">
| { id: string }
)[]
}

View File

@@ -0,0 +1,8 @@
export interface CreateShippingProfileDTO {
name: string
type?: string
metadata?: Record<string, unknown>
}
export interface UpdateShippingProfileDTO
extends Partial<CreateShippingProfileDTO> {}

View File

@@ -4,10 +4,12 @@ import {
FilterableGeoZoneProps,
FilterableServiceZoneProps,
FilterableShippingOptionProps,
FilterableShippingProfileProps,
FulfillmentSetDTO,
GeoZoneDTO,
ServiceZoneDTO,
ShippingOptionDTO,
ShippingProfileDTO,
} from "./common"
import { FindConfig } from "../common"
import { Context } from "../shared-context"
@@ -22,6 +24,7 @@ import {
UpdateServiceZoneDTO,
UpdateShippingOptionDTO,
} from "./mutations"
import { CreateShippingProfileDTO } from "./mutations/shipping-profile"
export interface IFulfillmentModuleService extends IModuleService {
/**
@@ -66,6 +69,21 @@ export interface IFulfillmentModuleService extends IModuleService {
sharedContext?: Context
): Promise<ShippingOptionDTO>
/**
* Create a new shipping profile
* @param data
* @param sharedContext
*/
createShippingProfiles(
data: CreateShippingProfileDTO[],
sharedContext?: Context
): Promise<ShippingProfileDTO[]>
createShippingProfiles(
data: CreateShippingProfileDTO,
sharedContext?: Context
): Promise<ShippingProfileDTO>
/**
* Create a new geo zone
* @param data
@@ -122,6 +140,20 @@ export interface IFulfillmentModuleService extends IModuleService {
sharedContext?: Context
): Promise<ShippingOptionDTO>
/**
* Update a shipping profile
* @param data
* @param sharedContext
*/
updateShippingProfiles(
data: CreateShippingProfileDTO[],
sharedContext?: Context
): Promise<ShippingProfileDTO[]>
updateShippingProfiles(
data: CreateShippingProfileDTO,
sharedContext?: Context
): Promise<ShippingProfileDTO>
/**
* Update a geo zone
* @param data
@@ -160,6 +192,14 @@ export interface IFulfillmentModuleService extends IModuleService {
deleteShippingOptions(ids: string[], sharedContext?: Context): Promise<void>
deleteShippingOptions(id: string, sharedContext?: Context): Promise<void>
/**
* Delete a shipping profile
* @param ids
* @param sharedContext
*/
deleteShippingProfiles(ids: string[], sharedContext?: Context): Promise<void>
deleteShippingProfiles(id: string, sharedContext?: Context): Promise<void>
/**
* Delete a geo zone
* @param ids
@@ -204,6 +244,18 @@ export interface IFulfillmentModuleService extends IModuleService {
sharedContext?: Context
): Promise<ShippingOptionDTO>
/**
* Retrieve a shipping profile
* @param id
* @param config
* @param sharedContext
*/
retrieveShippingProfile(
id: string,
config?: FindConfig<ShippingProfileDTO>,
sharedContext?: Context
): Promise<ShippingProfileDTO>
/**
* Retrieve a geo zone
* @param id
@@ -252,6 +304,18 @@ export interface IFulfillmentModuleService extends IModuleService {
sharedContext?: Context
): Promise<ShippingOptionDTO[]>
/**
* List shipping profiles
* @param filters
* @param config
* @param sharedContext
*/
listShippingProfiles(
filters?: FilterableShippingProfileProps,
config?: FindConfig<ShippingProfileDTO>,
sharedContext?: Context
): Promise<ShippingProfileDTO[]>
/**
* List geo zones
* @param filters
@@ -300,6 +364,18 @@ export interface IFulfillmentModuleService extends IModuleService {
sharedContext?: Context
): Promise<[ShippingOptionDTO[], number]>
/**
* List and count shipping profiles
* @param filters
* @param config
* @param sharedContext
*/
listAndCountShippingProfiles(
filters?: FilterableShippingProfileProps,
config?: FindConfig<ShippingProfileDTO>,
sharedContext?: Context
): Promise<[ShippingProfileDTO[], number]>
/**
* List and count geo zones
* @param filters
@@ -348,6 +424,18 @@ export interface IFulfillmentModuleService extends IModuleService {
sharedContext?: Context
): Promise<Record<string, string[]> | void>
/**
* Soft delete shipping profiles
* @param shippingProfileIds
* @param config
* @param sharedContext
*/
softDeleteShippingProfiles<TReturnableLinkableKeys extends string = string>(
shippingProfileIds: string[],
config?: SoftDeleteReturn<TReturnableLinkableKeys>,
sharedContext?: Context
): Promise<Record<string, string[]> | void>
/**
* Soft delete geo zones
* @param geoZoneIds
@@ -365,4 +453,6 @@ export interface IFulfillmentModuleService extends IModuleService {
config?: RestoreReturn<TReturnableLinkableKeys>,
sharedContext?: Context
): Promise<Record<string, string[]> | void>
// TODO defined the other restore methods
}

View File

@@ -1,18 +1,18 @@
import * as UserModels from "@models"
import { TestDatabaseUtils } from "medusa-test-utils"
import {TestDatabaseUtils} from "medusa-test-utils"
const pathToMigrations = "../../src/migrations"
const mikroOrmEntities = UserModels as unknown as any[]
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig(
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig({
mikroOrmEntities,
pathToMigrations
)
pathToMigrations,
})
export const DB_URL = TestDatabaseUtils.getDatabaseURL()

View File

@@ -84,7 +84,7 @@ describe("createPsqlIndexStatementHelper", function () {
const indexStatement = createPsqlIndexStatementHelper(options)
expect(indexStatement.expression).toEqual(
`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_table_name_column_name_1_column_name_2" ON "${
`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_table_name_column_name_1_column_name_2_unique" ON "${
options.tableName
}" (${options.columns.join(", ")}) WHERE ${options.where}`
)

View File

@@ -47,7 +47,7 @@ export function createPsqlIndexStatementHelper({
const columnsName = Array.isArray(columns) ? columns.join("_") : columns
columns = Array.isArray(columns) ? columns.join(", ") : columns
name = name || `IDX_${tableName}_${columnsName}`
name = name || `IDX_${tableName}_${columnsName}${unique ? "_unique" : ""}`
const typeStr = type ? ` USING ${type}` : ""
const optionsStr = where ? ` WHERE ${where}` : ""
@@ -63,7 +63,7 @@ export function createPsqlIndexStatementHelper({
},
name,
expression,
MikroORMIndex: (options = {}) => {
MikroORMIndex: (options?: Parameters<typeof Index>[0]) => {
return Index({
name,
expression,

View File

@@ -97,13 +97,6 @@ export interface AbstractModuleServiceBase<TContainer, TMainModelDTO> {
): Promise<Record<string, string[]> | void>
}
/**
* Multiple issues on typescript around mapped types function are open, so
* when overriding a method from the base class that is mapped dynamically from the
* other models, we will have to ignore the error (2425)
*
* see: https://github.com/microsoft/TypeScript/issues/48125
*/
export type AbstractModuleService<
TContainer,
TMainModelDTO,

View File

@@ -1,4 +1,4 @@
import knex from "knex"
import { knex } from "@mikro-orm/knex"
import { ModuleServiceInitializeOptions } from "@medusajs/types"
type Options = ModuleServiceInitializeOptions["database"]

View File

@@ -3,7 +3,7 @@ import {
TransactionStepTimeoutError,
TransactionTimeoutError,
} from "@medusajs/orchestration"
import { RemoteJoinerQuery } from "@medusajs/types"
import { RemoteQueryFunction } from "@medusajs/types"
import { TransactionHandlerType } from "@medusajs/utils"
import { IWorkflowEngineService } from "@medusajs/workflows-sdk"
import { knex } from "knex"
@@ -27,10 +27,7 @@ const afterEach_ = async () => {
describe("Workflow Orchestrator module", function () {
describe("Testing basic workflow", function () {
let workflowOrcModule: IWorkflowEngineService
let query: (
query: string | RemoteJoinerQuery | object,
variables?: Record<string, unknown>
) => Promise<any>
let query: RemoteQueryFunction
afterEach(afterEach_)