feat(types,modules-sdk): basic module setup for promotions (#5920)
RESOLVES CORE-1580
This commit is contained in:
6
.changeset/itchy-knives-stare.md
Normal file
6
.changeset/itchy-knives-stare.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@medusajs/modules-sdk": patch
|
||||
"@medusajs/types": patch
|
||||
---
|
||||
|
||||
feat(types,modules-sdk): basic module setup for promotions
|
||||
@@ -4,6 +4,8 @@ import {
|
||||
ModuleDefinition,
|
||||
} from "@medusajs/types"
|
||||
|
||||
import { upperCaseFirst } from "@medusajs/utils"
|
||||
|
||||
export enum Modules {
|
||||
EVENT_BUS = "eventBus",
|
||||
STOCK_LOCATION = "stockLocationService",
|
||||
@@ -11,6 +13,7 @@ export enum Modules {
|
||||
CACHE = "cacheService",
|
||||
PRODUCT = "productService",
|
||||
PRICING = "pricingService",
|
||||
PROMOTION = "promotion",
|
||||
}
|
||||
|
||||
export enum ModuleRegistrationName {
|
||||
@@ -20,6 +23,7 @@ export enum ModuleRegistrationName {
|
||||
CACHE = "cacheService",
|
||||
PRODUCT = "productModuleService",
|
||||
PRICING = "pricingModuleService",
|
||||
PROMOTION = "promotionModuleService",
|
||||
}
|
||||
|
||||
export const MODULE_PACKAGE_NAMES = {
|
||||
@@ -29,6 +33,7 @@ export const MODULE_PACKAGE_NAMES = {
|
||||
[Modules.INVENTORY]: "@medusajs/inventory",
|
||||
[Modules.CACHE]: "@medusajs/cache-inmemory",
|
||||
[Modules.PRICING]: "@medusajs/pricing",
|
||||
[Modules.PROMOTION]: "@medusajs/promotion",
|
||||
}
|
||||
|
||||
export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
@@ -38,7 +43,7 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
isLegacy: true,
|
||||
registrationName: ModuleRegistrationName.EVENT_BUS,
|
||||
defaultPackage: MODULE_PACKAGE_NAMES[Modules.EVENT_BUS],
|
||||
label: "EventBusModuleService",
|
||||
label: upperCaseFirst(ModuleRegistrationName.EVENT_BUS),
|
||||
canOverride: true,
|
||||
isRequired: true,
|
||||
dependencies: ["logger"],
|
||||
@@ -52,7 +57,7 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
isLegacy: true,
|
||||
registrationName: ModuleRegistrationName.STOCK_LOCATION,
|
||||
defaultPackage: false,
|
||||
label: "StockLocationService",
|
||||
label: upperCaseFirst(ModuleRegistrationName.STOCK_LOCATION),
|
||||
isRequired: false,
|
||||
canOverride: true,
|
||||
isQueryable: true,
|
||||
@@ -67,7 +72,7 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
isLegacy: true,
|
||||
registrationName: ModuleRegistrationName.INVENTORY,
|
||||
defaultPackage: false,
|
||||
label: "InventoryService",
|
||||
label: upperCaseFirst(ModuleRegistrationName.INVENTORY),
|
||||
isRequired: false,
|
||||
canOverride: true,
|
||||
isQueryable: true,
|
||||
@@ -82,7 +87,7 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
isLegacy: true,
|
||||
registrationName: ModuleRegistrationName.CACHE,
|
||||
defaultPackage: MODULE_PACKAGE_NAMES[Modules.CACHE],
|
||||
label: "CacheService",
|
||||
label: upperCaseFirst(ModuleRegistrationName.CACHE),
|
||||
isRequired: true,
|
||||
canOverride: true,
|
||||
defaultModuleDeclaration: {
|
||||
@@ -94,7 +99,7 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
key: Modules.PRODUCT,
|
||||
registrationName: ModuleRegistrationName.PRODUCT,
|
||||
defaultPackage: false,
|
||||
label: "ProductModuleService",
|
||||
label: upperCaseFirst(ModuleRegistrationName.PRODUCT),
|
||||
isRequired: false,
|
||||
canOverride: true,
|
||||
isQueryable: true,
|
||||
@@ -108,7 +113,21 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
key: Modules.PRICING,
|
||||
registrationName: ModuleRegistrationName.PRICING,
|
||||
defaultPackage: false,
|
||||
label: "PricingModuleService",
|
||||
label: upperCaseFirst(ModuleRegistrationName.PRICING),
|
||||
isRequired: false,
|
||||
canOverride: true,
|
||||
isQueryable: true,
|
||||
dependencies: ["logger"],
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
},
|
||||
},
|
||||
[Modules.PROMOTION]: {
|
||||
key: Modules.PROMOTION,
|
||||
registrationName: ModuleRegistrationName.PROMOTION,
|
||||
defaultPackage: false,
|
||||
label: upperCaseFirst(ModuleRegistrationName.PROMOTION),
|
||||
isRequired: false,
|
||||
canOverride: true,
|
||||
isQueryable: true,
|
||||
|
||||
@@ -3,9 +3,10 @@ import {
|
||||
InternalModuleDeclaration,
|
||||
LinkModuleDefinition,
|
||||
LoadedModule,
|
||||
MedusaContainer,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
MedusaContainer,
|
||||
ModuleBootstrapDeclaration,
|
||||
ModuleDefinition,
|
||||
ModuleExports,
|
||||
ModuleJoinerConfig,
|
||||
@@ -52,7 +53,7 @@ type ModuleAlias = {
|
||||
export type ModuleBootstrapOptions = {
|
||||
moduleKey: string
|
||||
defaultPath: string
|
||||
declaration?: InternalModuleDeclaration | ExternalModuleDeclaration
|
||||
declaration?: ModuleBootstrapDeclaration
|
||||
moduleExports?: ModuleExports
|
||||
sharedContainer?: MedusaContainer
|
||||
moduleDefinition?: ModuleDefinition
|
||||
|
||||
6
packages/promotion/.gitignore
vendored
Normal file
6
packages/promotion/.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
/dist
|
||||
node_modules
|
||||
.DS_store
|
||||
.env*
|
||||
.env
|
||||
*.sql
|
||||
0
packages/promotion/CHANGELOG.md
Normal file
0
packages/promotion/CHANGELOG.md
Normal file
3
packages/promotion/README.md
Normal file
3
packages/promotion/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# Promotion Module
|
||||
|
||||
The PromotionModule is Medusa’s promotion engine. It offers functionality to discount carts through coupon codes with a given set of rules governing when and how the code should be applied.
|
||||
@@ -0,0 +1,5 @@
|
||||
describe("Noop test", () => {
|
||||
it("noop check", async () => {
|
||||
expect(true).toBe(true)
|
||||
})
|
||||
})
|
||||
6
packages/promotion/integration-tests/setup-env.js
Normal file
6
packages/promotion/integration-tests/setup-env.js
Normal file
@@ -0,0 +1,6 @@
|
||||
if (typeof process.env.DB_TEMP_NAME === "undefined") {
|
||||
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
|
||||
process.env.DB_TEMP_NAME = `medusa-promotion-integration-${tempName}`
|
||||
}
|
||||
|
||||
process.env.MEDUSA_PROMOTION_DB_SCHEMA = "public"
|
||||
3
packages/promotion/integration-tests/setup.js
Normal file
3
packages/promotion/integration-tests/setup.js
Normal file
@@ -0,0 +1,3 @@
|
||||
import { JestUtils } from "medusa-test-utils"
|
||||
|
||||
JestUtils.afterAllHookDropDatabase()
|
||||
6
packages/promotion/integration-tests/utils/config.ts
Normal file
6
packages/promotion/integration-tests/utils/config.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { ModuleServiceInitializeOptions } from "@medusajs/types"
|
||||
|
||||
export const databaseOptions: ModuleServiceInitializeOptions["database"] = {
|
||||
schema: "public",
|
||||
clientUrl: "medusa-promotion-test",
|
||||
}
|
||||
18
packages/promotion/integration-tests/utils/database.ts
Normal file
18
packages/promotion/integration-tests/utils/database.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
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(
|
||||
mikroOrmEntities,
|
||||
pathToMigrations
|
||||
)
|
||||
|
||||
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig(
|
||||
mikroOrmEntities,
|
||||
pathToMigrations
|
||||
)
|
||||
|
||||
export const DB_URL = TestDatabaseUtils.getDatabaseURL()
|
||||
2
packages/promotion/integration-tests/utils/index.ts
Normal file
2
packages/promotion/integration-tests/utils/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./config"
|
||||
export * from "./database"
|
||||
21
packages/promotion/jest.config.js
Normal file
21
packages/promotion/jest.config.js
Normal file
@@ -0,0 +1,21 @@
|
||||
module.exports = {
|
||||
moduleNameMapper: {
|
||||
"^@models": "<rootDir>/src/models",
|
||||
"^@services": "<rootDir>/src/services",
|
||||
"^@repositories": "<rootDir>/src/repositories",
|
||||
},
|
||||
transform: {
|
||||
"^.+\\.[jt]s?$": [
|
||||
"ts-jest",
|
||||
{
|
||||
tsConfig: "tsconfig.spec.json",
|
||||
isolatedModules: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
testEnvironment: `node`,
|
||||
moduleFileExtensions: [`js`, `ts`],
|
||||
modulePathIgnorePatterns: ["dist/"],
|
||||
setupFiles: ["<rootDir>/integration-tests/setup-env.js"],
|
||||
setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"],
|
||||
}
|
||||
8
packages/promotion/mikro-orm.config.dev.ts
Normal file
8
packages/promotion/mikro-orm.config.dev.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import * as entities from "./src/models"
|
||||
|
||||
module.exports = {
|
||||
entities: Object.values(entities),
|
||||
schema: "public",
|
||||
clientUrl: "postgres://postgres@localhost/medusa-promotion",
|
||||
type: "postgresql",
|
||||
}
|
||||
63
packages/promotion/package.json
Normal file
63
packages/promotion/package.json
Normal file
@@ -0,0 +1,63 @@
|
||||
{
|
||||
"name": "@medusajs/promotion",
|
||||
"version": "0.0.1",
|
||||
"description": "Medusa Promotion module",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
"bin": {
|
||||
"medusa-promotion-migrations-down": "dist/scripts/bin/run-migration-down.js",
|
||||
"medusa-promotion-migrations-up": "dist/scripts/bin/run-migration-up.js",
|
||||
"medusa-promotion-seed": "dist/scripts/bin/run-seed.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/medusajs/medusa",
|
||||
"directory": "packages/promotion"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"author": "Medusa",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"watch": "tsc --build --watch",
|
||||
"watch:test": "tsc --build tsconfig.spec.json --watch",
|
||||
"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",
|
||||
"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",
|
||||
"migration:up": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:up",
|
||||
"orm:cache:clear": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm cache:clear"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@mikro-orm/cli": "5.7.12",
|
||||
"cross-env": "^5.2.1",
|
||||
"jest": "^29.6.3",
|
||||
"medusa-test-utils": "^1.1.40",
|
||||
"rimraf": "^3.0.2",
|
||||
"ts-jest": "^29.1.1",
|
||||
"ts-node": "^10.9.1",
|
||||
"tsc-alias": "^1.8.6",
|
||||
"typescript": "^5.1.6"
|
||||
},
|
||||
"dependencies": {
|
||||
"@medusajs/modules-sdk": "^1.12.5",
|
||||
"@medusajs/types": "^1.11.9",
|
||||
"@medusajs/utils": "^1.11.2",
|
||||
"@mikro-orm/core": "5.7.12",
|
||||
"@mikro-orm/migrations": "5.7.12",
|
||||
"@mikro-orm/postgresql": "5.7.12",
|
||||
"awilix": "^8.0.0",
|
||||
"dotenv": "^16.1.4",
|
||||
"knex": "2.4.2"
|
||||
}
|
||||
}
|
||||
7
packages/promotion/src/index.ts
Normal file
7
packages/promotion/src/index.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
|
||||
export default moduleDefinition
|
||||
|
||||
export * from "./initialize"
|
||||
export * from "./loaders"
|
||||
export * from "./scripts"
|
||||
27
packages/promotion/src/initialize/index.ts
Normal file
27
packages/promotion/src/initialize/index.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import {
|
||||
ExternalModuleDeclaration,
|
||||
InternalModuleDeclaration,
|
||||
MODULE_PACKAGE_NAMES,
|
||||
MedusaModule,
|
||||
Modules,
|
||||
} from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService, ModulesSdkTypes } from "@medusajs/types"
|
||||
import { moduleDefinition } from "../module-definition"
|
||||
import { InitializeModuleInjectableDependencies } from "../types"
|
||||
|
||||
export const initialize = async (
|
||||
options?: ModulesSdkTypes.ModuleBootstrapDeclaration,
|
||||
injectedDependencies?: InitializeModuleInjectableDependencies
|
||||
): Promise<IPromotionModuleService> => {
|
||||
const loaded = await MedusaModule.bootstrap<IPromotionModuleService>({
|
||||
moduleKey: Modules.PROMOTION,
|
||||
defaultPath: MODULE_PACKAGE_NAMES[Modules.PROMOTION],
|
||||
declaration: options as
|
||||
| InternalModuleDeclaration
|
||||
| ExternalModuleDeclaration,
|
||||
injectedDependencies,
|
||||
moduleExports: moduleDefinition,
|
||||
})
|
||||
|
||||
return loaded[Modules.PROMOTION]
|
||||
}
|
||||
31
packages/promotion/src/joiner-config.ts
Normal file
31
packages/promotion/src/joiner-config.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import { Promotion } from "@models"
|
||||
|
||||
export const LinkableKeys = {
|
||||
promotion_id: Promotion.name,
|
||||
}
|
||||
|
||||
const entityLinkableKeysMap: MapToConfig = {}
|
||||
Object.entries(LinkableKeys).forEach(([key, value]) => {
|
||||
entityLinkableKeysMap[value] ??= []
|
||||
entityLinkableKeysMap[value].push({
|
||||
mapTo: key,
|
||||
valueFrom: key.split("_").pop()!,
|
||||
})
|
||||
})
|
||||
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.PROMOTION,
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: LinkableKeys,
|
||||
alias: {
|
||||
name: ["promotion", "promotions"],
|
||||
args: {
|
||||
entity: Promotion.name,
|
||||
},
|
||||
},
|
||||
}
|
||||
30
packages/promotion/src/loaders/connection.ts
Normal file
30
packages/promotion/src/loaders/connection.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { InternalModuleDeclaration, LoaderOptions, Modules } from "@medusajs/modules-sdk"
|
||||
import { ModulesSdkTypes } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import * as PromotionModels from "../models"
|
||||
|
||||
export default async (
|
||||
{
|
||||
options,
|
||||
container,
|
||||
logger,
|
||||
}: LoaderOptions<
|
||||
| ModulesSdkTypes.ModuleServiceInitializeOptions
|
||||
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
|
||||
>,
|
||||
moduleDeclaration?: InternalModuleDeclaration
|
||||
): Promise<void> => {
|
||||
const entities = Object.values(PromotionModels) as unknown as EntitySchema[]
|
||||
const pathToMigrations = __dirname + "/../migrations"
|
||||
|
||||
await ModulesSdkUtils.mikroOrmConnectionLoader({
|
||||
moduleName: Modules.PROMOTION,
|
||||
entities,
|
||||
container,
|
||||
options,
|
||||
moduleDeclaration,
|
||||
logger,
|
||||
pathToMigrations,
|
||||
})
|
||||
}
|
||||
38
packages/promotion/src/loaders/container.ts
Normal file
38
packages/promotion/src/loaders/container.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import * as defaultRepositories from "@repositories"
|
||||
|
||||
import { LoaderOptions } from "@medusajs/modules-sdk"
|
||||
import { ModulesSdkTypes } from "@medusajs/types"
|
||||
import { loadCustomRepositories } from "@medusajs/utils"
|
||||
import { asClass } from "awilix"
|
||||
|
||||
export default async ({
|
||||
container,
|
||||
options,
|
||||
}: LoaderOptions<
|
||||
| ModulesSdkTypes.ModuleServiceInitializeOptions
|
||||
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
|
||||
>): Promise<void> => {
|
||||
const customRepositories = (
|
||||
options as ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
|
||||
)?.repositories
|
||||
|
||||
container.register({
|
||||
// promotionService: asClass(defaultServices.PromotionService).singleton(),
|
||||
})
|
||||
|
||||
if (customRepositories) {
|
||||
loadCustomRepositories({
|
||||
defaultRepositories,
|
||||
customRepositories,
|
||||
container,
|
||||
})
|
||||
} else {
|
||||
loadDefaultRepositories({ container })
|
||||
}
|
||||
}
|
||||
|
||||
function loadDefaultRepositories({ container }) {
|
||||
container.register({
|
||||
baseRepository: asClass(defaultRepositories.BaseRepository).singleton(),
|
||||
})
|
||||
}
|
||||
2
packages/promotion/src/loaders/index.ts
Normal file
2
packages/promotion/src/loaders/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./connection"
|
||||
export * from "./container"
|
||||
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"namespaces": [
|
||||
"public"
|
||||
],
|
||||
"name": "public",
|
||||
"tables": [
|
||||
{
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "text",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": false,
|
||||
"mappedType": "text"
|
||||
}
|
||||
},
|
||||
"name": "promotion",
|
||||
"schema": "public",
|
||||
"indexes": [
|
||||
{
|
||||
"keyName": "promotion_pkey",
|
||||
"columnNames": [
|
||||
"id"
|
||||
],
|
||||
"composite": false,
|
||||
"primary": true,
|
||||
"unique": true
|
||||
}
|
||||
],
|
||||
"checks": [],
|
||||
"foreignKeys": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
export class Migration20231218142613 extends Migration {
|
||||
|
||||
async up(): Promise<void> {
|
||||
this.addSql('create table "promotion" ("id" text not null, constraint "promotion_pkey" primary key ("id"));');
|
||||
}
|
||||
|
||||
}
|
||||
1
packages/promotion/src/models/index.ts
Normal file
1
packages/promotion/src/models/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { default as Promotion } from "./promotion"
|
||||
18
packages/promotion/src/models/promotion.ts
Normal file
18
packages/promotion/src/models/promotion.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { generateEntityId } from "@medusajs/utils"
|
||||
import { BeforeCreate, Entity, PrimaryKey, OnInit } from "@mikro-orm/core"
|
||||
|
||||
@Entity()
|
||||
export default class Promotion {
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id!: string
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "promo")
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "promo")
|
||||
}
|
||||
}
|
||||
12
packages/promotion/src/module-definition.ts
Normal file
12
packages/promotion/src/module-definition.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { PromotionModuleService } from "@services"
|
||||
import loadConnection from "./loaders/connection"
|
||||
import loadContainer from "./loaders/container"
|
||||
|
||||
const service = PromotionModuleService
|
||||
const loaders = [loadContainer, loadConnection] as any
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
loaders,
|
||||
}
|
||||
1
packages/promotion/src/repositories/index.ts
Normal file
1
packages/promotion/src/repositories/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
|
||||
8
packages/promotion/src/scripts/bin/run-migration-down.ts
Normal file
8
packages/promotion/src/scripts/bin/run-migration-down.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
export default (async () => {
|
||||
const { revertMigration } = await import("../migration-down")
|
||||
const { config } = await import("dotenv")
|
||||
config()
|
||||
await revertMigration()
|
||||
})()
|
||||
8
packages/promotion/src/scripts/bin/run-migration-up.ts
Normal file
8
packages/promotion/src/scripts/bin/run-migration-up.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
export default (async () => {
|
||||
const { runMigrations } = await import("../migration-up")
|
||||
const { config } = await import("dotenv")
|
||||
config()
|
||||
await runMigrations()
|
||||
})()
|
||||
19
packages/promotion/src/scripts/bin/run-seed.ts
Normal file
19
packages/promotion/src/scripts/bin/run-seed.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { EOL } from "os"
|
||||
import { run } from "../seed"
|
||||
|
||||
const args = process.argv
|
||||
const path = args.pop() as string
|
||||
|
||||
export default (async () => {
|
||||
const { config } = await import("dotenv")
|
||||
config()
|
||||
if (!path) {
|
||||
throw new Error(
|
||||
`filePath is required.${EOL}Example: medusa-promotion-seed <filePath>`
|
||||
)
|
||||
}
|
||||
|
||||
await run({ path })
|
||||
})()
|
||||
2
packages/promotion/src/scripts/index.ts
Normal file
2
packages/promotion/src/scripts/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./migration-up"
|
||||
export * from "./migration-down"
|
||||
43
packages/promotion/src/scripts/migration-down.ts
Normal file
43
packages/promotion/src/scripts/migration-down.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types"
|
||||
import { DALUtils, ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import * as PromotionModels from "@models"
|
||||
|
||||
/**
|
||||
* This script is only valid for mikro orm managers. If a user provide a custom manager
|
||||
* he is in charge of reverting the migrations.
|
||||
* @param options
|
||||
* @param logger
|
||||
* @param moduleDeclaration
|
||||
*/
|
||||
export async function revertMigration({
|
||||
options,
|
||||
logger,
|
||||
}: Pick<
|
||||
LoaderOptions<ModulesSdkTypes.ModuleServiceInitializeOptions>,
|
||||
"options" | "logger"
|
||||
> = {}) {
|
||||
logger ??= console as unknown as Logger
|
||||
|
||||
const dbData = ModulesSdkUtils.loadDatabaseConfig(Modules.PROMOTION, options)!
|
||||
const entities = Object.values(PromotionModels) as unknown as EntitySchema[]
|
||||
const pathToMigrations = __dirname + "/../migrations"
|
||||
|
||||
const orm = await DALUtils.mikroOrmCreateConnection(
|
||||
dbData,
|
||||
entities,
|
||||
pathToMigrations
|
||||
)
|
||||
|
||||
try {
|
||||
const migrator = orm.getMigrator()
|
||||
await migrator.down()
|
||||
|
||||
logger?.info("Promotion module migration executed")
|
||||
} catch (error) {
|
||||
logger?.error(`Promotion module migration failed to run - Error: ${error}`)
|
||||
}
|
||||
|
||||
await orm.close()
|
||||
}
|
||||
55
packages/promotion/src/scripts/migration-up.ts
Normal file
55
packages/promotion/src/scripts/migration-up.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types"
|
||||
import { DALUtils, ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import * as PromotionModels from "@models"
|
||||
|
||||
/**
|
||||
* This script is only valid for mikro orm managers. If a user provide a custom manager
|
||||
* he is in charge of running the migrations.
|
||||
* @param options
|
||||
* @param logger
|
||||
* @param moduleDeclaration
|
||||
*/
|
||||
export async function runMigrations({
|
||||
options,
|
||||
logger,
|
||||
}: Pick<
|
||||
LoaderOptions<ModulesSdkTypes.ModuleServiceInitializeOptions>,
|
||||
"options" | "logger"
|
||||
> = {}) {
|
||||
logger ??= console as unknown as Logger
|
||||
|
||||
const dbData = ModulesSdkUtils.loadDatabaseConfig(Modules.PROMOTION, options)!
|
||||
const entities = Object.values(PromotionModels) as unknown as EntitySchema[]
|
||||
const pathToMigrations = __dirname + "/../migrations"
|
||||
|
||||
const orm = await DALUtils.mikroOrmCreateConnection(
|
||||
dbData,
|
||||
entities,
|
||||
pathToMigrations
|
||||
)
|
||||
|
||||
try {
|
||||
const migrator = orm.getMigrator()
|
||||
|
||||
const pendingMigrations = await migrator.getPendingMigrations()
|
||||
logger.info(
|
||||
`Running pending migrations: ${JSON.stringify(
|
||||
pendingMigrations,
|
||||
null,
|
||||
2
|
||||
)}`
|
||||
)
|
||||
|
||||
await migrator.up({
|
||||
migrations: pendingMigrations.map((m) => m.name),
|
||||
})
|
||||
|
||||
logger.info("Promotion module migration executed")
|
||||
} catch (error) {
|
||||
logger.error(`Promotion module migration failed to run - Error: ${error}`)
|
||||
}
|
||||
|
||||
await orm.close()
|
||||
}
|
||||
58
packages/promotion/src/scripts/seed.ts
Normal file
58
packages/promotion/src/scripts/seed.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types"
|
||||
import { DALUtils, ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import * as PromotionModels from "@models"
|
||||
import { EOL } from "os"
|
||||
import { resolve } from "path"
|
||||
|
||||
export async function run({
|
||||
options,
|
||||
logger,
|
||||
path,
|
||||
}: Partial<
|
||||
Pick<
|
||||
LoaderOptions<ModulesSdkTypes.ModuleServiceInitializeOptions>,
|
||||
"options" | "logger"
|
||||
>
|
||||
> & {
|
||||
path: string
|
||||
}) {
|
||||
logger ??= console as unknown as Logger
|
||||
|
||||
logger.info(`Loading seed data from ${path}...`)
|
||||
|
||||
const { promotionsData } = await import(resolve(process.cwd(), path)).catch(
|
||||
(e) => {
|
||||
logger?.error(
|
||||
`Failed to load seed data from ${path}. Please, provide a relative path and check that you export the following: promotionsData.${EOL}${e}`
|
||||
)
|
||||
throw e
|
||||
}
|
||||
)
|
||||
|
||||
const dbData = ModulesSdkUtils.loadDatabaseConfig(Modules.PROMOTION, options)!
|
||||
const entities = Object.values(PromotionModels) as unknown as EntitySchema[]
|
||||
const pathToMigrations = __dirname + "/../migrations"
|
||||
|
||||
const orm = await DALUtils.mikroOrmCreateConnection(
|
||||
dbData,
|
||||
entities,
|
||||
pathToMigrations
|
||||
)
|
||||
|
||||
const manager = orm.em.fork()
|
||||
|
||||
try {
|
||||
logger.info("Inserting promotions..")
|
||||
|
||||
// TODO: implement promotions seed data
|
||||
// await createPromotions(manager, promotionsData)
|
||||
} catch (e) {
|
||||
logger.error(
|
||||
`Failed to insert the seed data in the PostgreSQL database ${dbData.clientUrl}.${EOL}${e}`
|
||||
)
|
||||
}
|
||||
|
||||
await orm.close(true)
|
||||
}
|
||||
5
packages/promotion/src/services/__tests__/index.spec.ts
Normal file
5
packages/promotion/src/services/__tests__/index.spec.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
describe("Noop test", () => {
|
||||
it("noop check", async () => {
|
||||
expect(true).toBe(true)
|
||||
})
|
||||
})
|
||||
1
packages/promotion/src/services/index.ts
Normal file
1
packages/promotion/src/services/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { default as PromotionModuleService } from "./promotion-module"
|
||||
32
packages/promotion/src/services/promotion-module.ts
Normal file
32
packages/promotion/src/services/promotion-module.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import {
|
||||
DAL,
|
||||
InternalModuleDeclaration,
|
||||
ModuleJoinerConfig,
|
||||
PromotionTypes,
|
||||
} from "@medusajs/types"
|
||||
|
||||
import { Promotion } from "@models"
|
||||
|
||||
import { joinerConfig } from "../joiner-config"
|
||||
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
}
|
||||
|
||||
export default class PromotionModuleService<
|
||||
TPromotion extends Promotion = Promotion
|
||||
> implements PromotionTypes.IPromotionModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
|
||||
constructor(
|
||||
{ baseRepository }: InjectedDependencies,
|
||||
protected readonly moduleDeclaration: InternalModuleDeclaration
|
||||
) {
|
||||
this.baseRepository_ = baseRepository
|
||||
}
|
||||
|
||||
__joinerConfig(): ModuleJoinerConfig {
|
||||
return joinerConfig
|
||||
}
|
||||
}
|
||||
5
packages/promotion/src/types/index.ts
Normal file
5
packages/promotion/src/types/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { Logger } from "@medusajs/types"
|
||||
|
||||
export type InitializeModuleInjectableDependencies = {
|
||||
logger?: Logger
|
||||
}
|
||||
36
packages/promotion/tsconfig.json
Normal file
36
packages/promotion/tsconfig.json
Normal file
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": ["es2020"],
|
||||
"target": "es2020",
|
||||
"outDir": "./dist",
|
||||
"esModuleInterop": true,
|
||||
"declaration": true,
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"sourceMap": false,
|
||||
"noImplicitReturns": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"noImplicitThis": true,
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"downlevelIteration": true, // to use ES5 specific tooling
|
||||
"baseUrl": ".",
|
||||
"resolveJsonModule": true,
|
||||
"paths": {
|
||||
"@models": ["./src/models"],
|
||||
"@services": ["./src/services"],
|
||||
"@repositories": ["./src/repositories"]
|
||||
}
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": [
|
||||
"dist",
|
||||
"./src/**/__tests__",
|
||||
"./src/**/__mocks__",
|
||||
"./src/**/__fixtures__",
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
8
packages/promotion/tsconfig.spec.json
Normal file
8
packages/promotion/tsconfig.spec.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"include": ["src", "integration-tests"],
|
||||
"exclude": ["node_modules", "dist"],
|
||||
"compilerOptions": {
|
||||
"sourceMap": true
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ export * as LoggerTypes from "./logger"
|
||||
export * as ModulesSdkTypes from "./modules-sdk"
|
||||
export * as PricingTypes from "./pricing"
|
||||
export * as ProductTypes from "./product"
|
||||
export * as PromotionTypes from "./promotion"
|
||||
export * as RegionTypes from "./region"
|
||||
export * as SalesChannelTypes from "./sales-channel"
|
||||
export * as SearchTypes from "./search"
|
||||
|
||||
@@ -16,6 +16,7 @@ export * from "./modules-sdk"
|
||||
export * from "./pricing"
|
||||
export * from "./product"
|
||||
export * from "./product-category"
|
||||
export * from "./promotion"
|
||||
export * from "./region"
|
||||
export * from "./sales-channel"
|
||||
export * from "./search"
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import { JoinerRelationship, JoinerServiceConfig, RemoteJoinerQuery } from "../joiner"
|
||||
import {
|
||||
JoinerRelationship,
|
||||
JoinerServiceConfig,
|
||||
RemoteJoinerQuery,
|
||||
} from "../joiner"
|
||||
|
||||
import { Logger } from "../logger"
|
||||
import { MedusaContainer } from "../common"
|
||||
import { RepositoryService } from "../dal"
|
||||
import { Logger } from "../logger"
|
||||
|
||||
export type Constructor<T> = new (...args: any[]) => T
|
||||
export * from "../common/medusa-container"
|
||||
@@ -261,7 +265,22 @@ export type ModuleServiceInitializeCustomDataLayerOptions = {
|
||||
}
|
||||
}
|
||||
|
||||
export type ModuleBootstrapDeclaration =
|
||||
| InternalModuleDeclaration
|
||||
| ExternalModuleDeclaration
|
||||
// TODO: These should be added back when the chain of types are fixed
|
||||
// | ModuleServiceInitializeOptions
|
||||
// | ModuleServiceInitializeCustomDataLayerOptions
|
||||
|
||||
export type RemoteQueryFunction = (
|
||||
query: string | RemoteJoinerQuery | object,
|
||||
variables?: Record<string, unknown>
|
||||
) => Promise<any> | null
|
||||
) => Promise<any> | null
|
||||
|
||||
export interface IModuleService {
|
||||
__joinerConfig?(): ModuleJoinerConfig
|
||||
|
||||
__hooks?: {
|
||||
onApplicationStart?: () => Promise<void>
|
||||
}
|
||||
}
|
||||
|
||||
1
packages/types/src/promotion/index.ts
Normal file
1
packages/types/src/promotion/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./service"
|
||||
3
packages/types/src/promotion/service.ts
Normal file
3
packages/types/src/promotion/service.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { IModuleService } from "../modules-sdk"
|
||||
|
||||
export interface IPromotionModuleService extends IModuleService {}
|
||||
29
yarn.lock
29
yarn.lock
@@ -7904,6 +7904,35 @@ __metadata:
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@medusajs/promotion@workspace:packages/promotion":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@medusajs/promotion@workspace:packages/promotion"
|
||||
dependencies:
|
||||
"@medusajs/modules-sdk": ^1.12.5
|
||||
"@medusajs/types": ^1.11.9
|
||||
"@medusajs/utils": ^1.11.2
|
||||
"@mikro-orm/cli": 5.7.12
|
||||
"@mikro-orm/core": 5.7.12
|
||||
"@mikro-orm/migrations": 5.7.12
|
||||
"@mikro-orm/postgresql": 5.7.12
|
||||
awilix: ^8.0.0
|
||||
cross-env: ^5.2.1
|
||||
dotenv: ^16.1.4
|
||||
jest: ^29.6.3
|
||||
knex: 2.4.2
|
||||
medusa-test-utils: ^1.1.40
|
||||
rimraf: ^3.0.2
|
||||
ts-jest: ^29.1.1
|
||||
ts-node: ^10.9.1
|
||||
tsc-alias: ^1.8.6
|
||||
typescript: ^5.1.6
|
||||
bin:
|
||||
medusa-promotion-migrations-down: dist/scripts/bin/run-migration-down.js
|
||||
medusa-promotion-migrations-up: dist/scripts/bin/run-migration-up.js
|
||||
medusa-promotion-seed: dist/scripts/bin/run-seed.js
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@medusajs/stock-location@workspace:packages/stock-location":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@medusajs/stock-location@workspace:packages/stock-location"
|
||||
|
||||
Reference in New Issue
Block a user