Chore/rm main entity concept (#7709)
**What** Update the `MedusaService` class, factory and types to remove the concept of main modules. The idea being that all method will be explicitly named and suffixes to represent the object you are trying to manipulate. This pr also includes various fixes in different modules Co-authored-by: Stevche Radevski <4820812+sradevski@users.noreply.github.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Stevche Radevski
Oli Juhl
parent
2895ccfba8
commit
48963f55ef
+3
-6
@@ -6,7 +6,7 @@ import {
|
||||
PricingTypes,
|
||||
} from "@medusajs/types"
|
||||
import { PriceListStatus, PriceListType } from "@medusajs/utils"
|
||||
import { SuiteOptions, moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { seedPriceData } from "../../../__fixtures__/seed-price-data"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
@@ -49,12 +49,9 @@ const createPriceLists = async (
|
||||
])
|
||||
}
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
moduleName: Modules.PRICING,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IPricingModuleService>) => {
|
||||
testSuite: ({ MikroOrmWrapper, service }) => {
|
||||
describe("PricingModule Service - Calculate Price", () => {
|
||||
describe("calculatePrices", () => {
|
||||
beforeEach(async () => {
|
||||
|
||||
+3
-6
@@ -1,19 +1,16 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { createPriceLists } from "../../../__fixtures__/price-list"
|
||||
import { createPriceListRules } from "../../../__fixtures__/price-list-rules"
|
||||
import { createRuleTypes } from "../../../__fixtures__/rule-type"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
moduleName: Modules.PRICING,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IPricingModuleService>) => {
|
||||
testSuite: ({ MikroOrmWrapper, service }) => {
|
||||
describe("PriceListRule Service", () => {
|
||||
let testManager: SqlEntityManager
|
||||
beforeEach(async () => {
|
||||
|
||||
+5
-9
@@ -3,7 +3,6 @@ import { IPricingModuleService } from "@medusajs/types"
|
||||
import {
|
||||
MockEventBusService,
|
||||
moduleIntegrationTestRunner,
|
||||
SuiteOptions,
|
||||
} from "medusa-test-utils"
|
||||
import { createPriceLists } from "../../../__fixtures__/price-list"
|
||||
import { createPriceSets } from "../../../__fixtures__/price-set"
|
||||
@@ -11,12 +10,9 @@ import { CommonEvents, composeMessage, PricingEvents } from "@medusajs/utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
moduleName: Modules.PRICING,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IPricingModuleService>) => {
|
||||
testSuite: ({ MikroOrmWrapper, service }) => {
|
||||
let eventBusEmitSpy
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -886,7 +882,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("updatePriceListPrices", () => {
|
||||
it("should update a price to a priceList successfully", async () => {
|
||||
const [priceSet] = await service.create([
|
||||
const [priceSet] = await service.createPriceSets([
|
||||
{
|
||||
rules: [
|
||||
{ rule_attribute: "region_id" },
|
||||
@@ -988,7 +984,7 @@ moduleIntegrationTestRunner({
|
||||
{ name: "region_id", rule_attribute: "region_id" },
|
||||
])
|
||||
|
||||
const [priceSet] = await service.create([
|
||||
const [priceSet] = await service.createPriceSets([
|
||||
{ rules: [{ rule_attribute: "region_id" }] },
|
||||
])
|
||||
|
||||
@@ -1031,7 +1027,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("removePrices", () => {
|
||||
it("should remove prices from a priceList successfully", async () => {
|
||||
const [priceSet] = await service.create([
|
||||
const [priceSet] = await service.createPriceSets([
|
||||
{ rules: [{ rule_attribute: "region_id" }] },
|
||||
])
|
||||
|
||||
|
||||
+5
-8
@@ -1,8 +1,8 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { SuiteOptions, moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { Price } from "../../../../src"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { Price } from "../../../../src/models"
|
||||
import { createPrices } from "../../../__fixtures__/price"
|
||||
import { createPriceRules } from "../../../__fixtures__/price-rule"
|
||||
import { createPriceSets } from "../../../__fixtures__/price-set"
|
||||
@@ -10,12 +10,9 @@ import { createRuleTypes } from "../../../__fixtures__/rule-type"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
moduleName: Modules.PRICING,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IPricingModuleService>) => {
|
||||
testSuite: ({ MikroOrmWrapper, service }) => {
|
||||
describe("PricingModule Service - PriceRule", () => {
|
||||
let testManager: SqlEntityManager
|
||||
beforeEach(async () => {
|
||||
@@ -279,7 +276,7 @@ moduleIntegrationTestRunner({
|
||||
price_set_id: "price-set-1",
|
||||
title: "test",
|
||||
rules_count: 0,
|
||||
})
|
||||
} as Price)
|
||||
|
||||
await testManager.persist(price).flush()
|
||||
|
||||
|
||||
+43
-44
@@ -8,9 +8,8 @@ import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import {
|
||||
MockEventBusService,
|
||||
moduleIntegrationTestRunner,
|
||||
SuiteOptions,
|
||||
} from "medusa-test-utils"
|
||||
import { PriceSetRuleType } from "../../../../src"
|
||||
import { PriceSetRuleType } from "../../../../src/models"
|
||||
import { seedPriceData } from "../../../__fixtures__/seed-price-data"
|
||||
import { CommonEvents, composeMessage, PricingEvents } from "@medusajs/utils"
|
||||
|
||||
@@ -31,12 +30,9 @@ async function createPriceSetPriceRules(
|
||||
await manager.persistAndFlush(priceSetRules)
|
||||
}
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
moduleName: Modules.PRICING,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IPricingModuleService>) => {
|
||||
testSuite: ({ MikroOrmWrapper, service }) => {
|
||||
let eventBusEmitSpy
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -65,7 +61,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("list", () => {
|
||||
it("list priceSets", async () => {
|
||||
const priceSetsResult = await service.list()
|
||||
const priceSetsResult = await service.listPriceSets()
|
||||
|
||||
expect(priceSetsResult).toEqual([
|
||||
expect.objectContaining({
|
||||
@@ -81,7 +77,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("list priceSets by id", async () => {
|
||||
const priceSetsResult = await service.list({
|
||||
const priceSetsResult = await service.listPriceSets({
|
||||
id: ["price-set-1"],
|
||||
})
|
||||
|
||||
@@ -93,7 +89,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("list priceSets with relations and selects", async () => {
|
||||
const priceSetsResult = await service.list(
|
||||
const priceSetsResult = await service.listPriceSets(
|
||||
{
|
||||
id: ["price-set-1"],
|
||||
},
|
||||
@@ -121,7 +117,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("listAndCount", () => {
|
||||
it("should return priceSets and count", async () => {
|
||||
const [priceSetsResult, count] = await service.listAndCount()
|
||||
const [priceSetsResult, count] = await service.listAndCountPriceSets()
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(priceSetsResult).toEqual([
|
||||
@@ -138,7 +134,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should return priceSets and count when filtered", async () => {
|
||||
const [priceSetsResult, count] = await service.listAndCount({
|
||||
const [priceSetsResult, count] = await service.listAndCountPriceSets({
|
||||
id: ["price-set-1"],
|
||||
})
|
||||
|
||||
@@ -151,7 +147,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("list priceSets with relations and selects", async () => {
|
||||
const [priceSetsResult, count] = await service.listAndCount(
|
||||
const [priceSetsResult, count] = await service.listAndCountPriceSets(
|
||||
{
|
||||
id: ["price-set-1"],
|
||||
},
|
||||
@@ -178,7 +174,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should return priceSets and count when using skip and take", async () => {
|
||||
const [priceSetsResult, count] = await service.listAndCount(
|
||||
const [priceSetsResult, count] = await service.listAndCountPriceSets(
|
||||
{},
|
||||
{ skip: 1, take: 1 }
|
||||
)
|
||||
@@ -192,7 +188,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should return requested fields", async () => {
|
||||
const [priceSetsResult, count] = await service.listAndCount(
|
||||
const [priceSetsResult, count] = await service.listAndCountPriceSets(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
@@ -215,7 +211,7 @@ moduleIntegrationTestRunner({
|
||||
const id = "price-set-1"
|
||||
|
||||
it("should return priceSet for the given id", async () => {
|
||||
const priceSet = await service.retrieve(id)
|
||||
const priceSet = await service.retrievePriceSet(id)
|
||||
|
||||
expect(priceSet).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -228,7 +224,7 @@ moduleIntegrationTestRunner({
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
await service.retrievePriceSet("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
@@ -242,7 +238,7 @@ moduleIntegrationTestRunner({
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve(undefined as unknown as string)
|
||||
await service.retrievePriceSet(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
@@ -251,7 +247,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should return priceSet based on config select param", async () => {
|
||||
const priceSet = await service.retrieve(id, {
|
||||
const priceSet = await service.retrievePriceSet(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
@@ -267,9 +263,9 @@ moduleIntegrationTestRunner({
|
||||
const id = "price-set-1"
|
||||
|
||||
it("should delete the priceSets given an id successfully", async () => {
|
||||
await service.delete([id])
|
||||
await service.deletePriceSets([id])
|
||||
|
||||
const priceSets = await service.list({
|
||||
const priceSets = await service.listPriceSets({
|
||||
id: [id],
|
||||
})
|
||||
|
||||
@@ -282,7 +278,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
it("should throw an error when an id does not exist", async () => {
|
||||
let error = await service
|
||||
.update("does-not-exist", {})
|
||||
.updatePriceSets("does-not-exist", {})
|
||||
.catch((e) => e.message)
|
||||
|
||||
expect(error).toEqual(
|
||||
@@ -291,18 +287,21 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should create, update, and delete prices to a price set", async () => {
|
||||
const priceSetBefore = await service.retrieve(id, {
|
||||
const priceSetBefore = await service.retrievePriceSet(id, {
|
||||
relations: ["prices"],
|
||||
})
|
||||
|
||||
const updateResponse = await service.update(priceSetBefore.id, {
|
||||
prices: [
|
||||
{ amount: 100, currency_code: "USD" },
|
||||
{ amount: 200, currency_code: "EUR" },
|
||||
],
|
||||
})
|
||||
const updateResponse = await service.updatePriceSets(
|
||||
priceSetBefore.id,
|
||||
{
|
||||
prices: [
|
||||
{ amount: 100, currency_code: "USD" },
|
||||
{ amount: 200, currency_code: "EUR" },
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
const priceSetAfter = await service.retrieve(id, {
|
||||
const priceSetAfter = await service.retrievePriceSet(id, {
|
||||
relations: ["prices"],
|
||||
})
|
||||
expect(priceSetBefore.prices).toHaveLength(1)
|
||||
@@ -347,7 +346,7 @@ moduleIntegrationTestRunner({
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.create([
|
||||
await service.createPriceSets([
|
||||
{
|
||||
rules: [{ rule_attribute: "does-not-exist" }],
|
||||
},
|
||||
@@ -365,7 +364,7 @@ moduleIntegrationTestRunner({
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.create([
|
||||
await service.createPriceSets([
|
||||
{
|
||||
rules: [{ rule_attribute: "region_id" }],
|
||||
prices: [
|
||||
@@ -388,7 +387,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should create a price set with rule types", async () => {
|
||||
const [priceSet] = await service.create([
|
||||
const [priceSet] = await service.createPriceSets([
|
||||
{
|
||||
rules: [{ rule_attribute: "region_id" }],
|
||||
},
|
||||
@@ -406,7 +405,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should create a price set with rule types and money amounts", async () => {
|
||||
const [priceSet] = await service.create([
|
||||
const [priceSet] = await service.createPriceSets([
|
||||
{
|
||||
rules: [{ rule_attribute: "region_id" }],
|
||||
prices: [
|
||||
@@ -475,7 +474,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should create a price set with money amounts with and without rules", async () => {
|
||||
const [priceSet] = await service.create([
|
||||
const [priceSet] = await service.createPriceSets([
|
||||
{
|
||||
rules: [{ rule_attribute: "region_id" }],
|
||||
prices: [
|
||||
@@ -516,7 +515,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should create a price set with rule types and money amounts", async () => {
|
||||
const [priceSet] = await service.create([
|
||||
const [priceSet] = await service.createPriceSets([
|
||||
{
|
||||
rules: [{ rule_attribute: "region_id" }],
|
||||
prices: [
|
||||
@@ -554,13 +553,13 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should create a priceSet successfully", async () => {
|
||||
await service.create([
|
||||
await service.createPriceSets([
|
||||
{
|
||||
id: "price-set-new",
|
||||
} as unknown as CreatePriceSetDTO,
|
||||
])
|
||||
|
||||
const [priceSet] = await service.list({
|
||||
const [priceSet] = await service.listPriceSets({
|
||||
id: ["price-set-new"],
|
||||
})
|
||||
|
||||
@@ -574,7 +573,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
describe("removeRules", () => {
|
||||
it("should delete prices for a price set associated to the rules that are deleted", async () => {
|
||||
const createdPriceSet = await service.create([
|
||||
const createdPriceSet = await service.createPriceSets([
|
||||
{
|
||||
rules: [
|
||||
{ rule_attribute: "region_id" },
|
||||
@@ -604,7 +603,7 @@ moduleIntegrationTestRunner({
|
||||
{ id: createdPriceSet[0].id, rules: ["region_id"] },
|
||||
])
|
||||
|
||||
let priceSet = await service.list(
|
||||
let priceSet = await service.listPriceSets(
|
||||
{ id: [createdPriceSet[0].id] },
|
||||
{ relations: ["rule_types", "prices", "price_rules"] }
|
||||
)
|
||||
@@ -640,7 +639,7 @@ moduleIntegrationTestRunner({
|
||||
{ id: createdPriceSet[0].id, rules: ["currency_code"] },
|
||||
])
|
||||
|
||||
priceSet = await service.list(
|
||||
priceSet = await service.listPriceSets(
|
||||
{ id: [createdPriceSet[0].id] },
|
||||
{ relations: ["rule_types", "prices", "price_rules"] }
|
||||
)
|
||||
@@ -673,7 +672,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const [priceSet] = await service.list(
|
||||
const [priceSet] = await service.listPriceSets(
|
||||
{ id: ["price-set-1"] },
|
||||
{ relations: ["prices", "prices.price_rules"] }
|
||||
)
|
||||
@@ -734,7 +733,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const priceSets = await service.list(
|
||||
const priceSets = await service.listPriceSets(
|
||||
{ id: ["price-set-1", "price-set-2"] },
|
||||
{ relations: ["prices"] }
|
||||
)
|
||||
@@ -791,7 +790,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
])
|
||||
|
||||
const [priceSet] = await service.list(
|
||||
const [priceSet] = await service.listPriceSets(
|
||||
{ id: ["price-set-1"] },
|
||||
{ relations: ["rule_types"] }
|
||||
)
|
||||
|
||||
+3
-6
@@ -1,16 +1,13 @@
|
||||
import { createRuleTypes } from "../../../__fixtures__/rule-type"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
moduleName: Modules.PRICING,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IPricingModuleService>) => {
|
||||
testSuite: ({ MikroOrmWrapper, service }) => {
|
||||
describe("PricingModuleService ruleType", () => {
|
||||
beforeEach(async () => {
|
||||
const testManager = MikroOrmWrapper.forkManager()
|
||||
|
||||
@@ -8,10 +8,7 @@
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
"bin": {
|
||||
"medusa-pricing-seed": "dist/scripts/bin/run-seed.js"
|
||||
"node": ">=20"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { PricingModuleService } from "@services"
|
||||
|
||||
const service = PricingModuleService
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
}
|
||||
|
||||
export default moduleDefinition
|
||||
|
||||
export * from "./models"
|
||||
export * from "./services"
|
||||
export * from "./types"
|
||||
|
||||
@@ -1,56 +1,20 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import {
|
||||
buildEntitiesNameToLinkableKeysMap,
|
||||
defineJoinerConfig,
|
||||
MapToConfig,
|
||||
} from "@medusajs/utils"
|
||||
import { Price, PriceList, PriceSet, RuleType } from "@models"
|
||||
import schema from "./schema"
|
||||
|
||||
export const LinkableKeys = {
|
||||
price_set_id: PriceSet.name,
|
||||
price_list_id: PriceList.name,
|
||||
price_id: Price.name,
|
||||
rule_type_id: RuleType.name,
|
||||
}
|
||||
|
||||
const entityLinkableKeysMap: MapToConfig = {}
|
||||
Object.entries(LinkableKeys).forEach(([key, value]) => {
|
||||
entityLinkableKeysMap[value] ??= []
|
||||
entityLinkableKeysMap[value].push({
|
||||
mapTo: key,
|
||||
valueFrom: key.split("_").pop()!,
|
||||
})
|
||||
export const joinerConfig = defineJoinerConfig(Modules.PRICING, {
|
||||
entityQueryingConfig: [PriceSet, PriceList, Price, RuleType],
|
||||
linkableKeys: {
|
||||
price_set_id: PriceSet.name,
|
||||
price_list_id: PriceList.name,
|
||||
price_id: Price.name,
|
||||
rule_type_id: RuleType.name,
|
||||
},
|
||||
})
|
||||
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.PRICING,
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: LinkableKeys,
|
||||
schema,
|
||||
alias: [
|
||||
{
|
||||
name: ["price_set", "price_sets"],
|
||||
args: {
|
||||
entity: "PriceSet",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["price_list", "price_lists"],
|
||||
args: {
|
||||
methodSuffix: "PriceLists",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["price", "prices"],
|
||||
args: {
|
||||
methodSuffix: "Prices",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["rule_type", "rule_types"],
|
||||
args: {
|
||||
methodSuffix: "RuleTypes",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
export const entityNameToLinkableKeysMap: MapToConfig =
|
||||
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { PricingModuleService } from "@services"
|
||||
|
||||
const service = PricingModuleService
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
#!/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-pricing-seed <filePath>`
|
||||
)
|
||||
}
|
||||
|
||||
await run({ path })
|
||||
})()
|
||||
@@ -1,84 +0,0 @@
|
||||
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types"
|
||||
import { DALUtils, ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { EntitySchema, RequiredEntityData } from "@mikro-orm/core"
|
||||
import { PostgreSqlDriver, SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import * as PricingModels 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 { priceSetsData, pricesData } = 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: priceSetsData and pricesData.${EOL}${e}`
|
||||
)
|
||||
throw e
|
||||
})
|
||||
|
||||
const dbData = ModulesSdkUtils.loadDatabaseConfig("pricing", options)!
|
||||
const entities = Object.values(PricingModels) 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 price_sets & prices")
|
||||
|
||||
await createPriceSets(manager, priceSetsData)
|
||||
await createPrices(manager, pricesData)
|
||||
} catch (e) {
|
||||
logger.error(
|
||||
`Failed to insert the seed data in the PostgreSQL database ${dbData.clientUrl}.${EOL}${e}`
|
||||
)
|
||||
}
|
||||
|
||||
await orm.close(true)
|
||||
}
|
||||
|
||||
async function createPriceSets(
|
||||
manager: SqlEntityManager<PostgreSqlDriver>,
|
||||
data: RequiredEntityData<PricingModels.PriceSet>[]
|
||||
) {
|
||||
const priceSets = data.map((priceSetData) => {
|
||||
return manager.create(PricingModels.PriceSet, priceSetData)
|
||||
})
|
||||
|
||||
await manager.persistAndFlush(priceSets)
|
||||
|
||||
return priceSets
|
||||
}
|
||||
|
||||
async function createPrices(
|
||||
manager: SqlEntityManager<PostgreSqlDriver>,
|
||||
data: RequiredEntityData<PricingModels.Price>[]
|
||||
) {
|
||||
const prices = data.map((priceData) => {
|
||||
return manager.create(PricingModels.Price, priceData)
|
||||
})
|
||||
|
||||
await manager.persistAndFlush(prices)
|
||||
|
||||
return prices
|
||||
}
|
||||
@@ -68,6 +68,7 @@ type InjectedDependencies = {
|
||||
}
|
||||
|
||||
const generateMethodForModels = {
|
||||
PriceSet,
|
||||
PriceList,
|
||||
PriceListRule,
|
||||
PriceListRuleValue,
|
||||
@@ -77,46 +78,35 @@ const generateMethodForModels = {
|
||||
RuleType,
|
||||
}
|
||||
|
||||
export default class PricingModuleService<
|
||||
TPriceSet extends PriceSet = PriceSet,
|
||||
TRuleType extends RuleType = RuleType,
|
||||
TPriceRule extends PriceRule = PriceRule,
|
||||
TPriceSetRuleType extends PriceSetRuleType = PriceSetRuleType,
|
||||
TPrice extends Price = Price,
|
||||
TPriceList extends PriceList = PriceList,
|
||||
TPriceListRule extends PriceListRule = PriceListRule,
|
||||
TPriceListRuleValue extends PriceListRuleValue = PriceListRuleValue
|
||||
>
|
||||
extends ModulesSdkUtils.MedusaService<
|
||||
PricingTypes.PriceSetDTO,
|
||||
{
|
||||
Price: { dto: PricingTypes.PriceDTO }
|
||||
PriceRule: {
|
||||
dto: PricingTypes.PriceRuleDTO
|
||||
create: PricingTypes.CreatePriceRuleDTO
|
||||
update: PricingTypes.UpdatePriceRuleDTO
|
||||
}
|
||||
RuleType: {
|
||||
dto: PricingTypes.RuleTypeDTO
|
||||
create: PricingTypes.CreateRuleTypeDTO
|
||||
update: PricingTypes.UpdateRuleTypeDTO
|
||||
}
|
||||
PriceList: { dto: PricingTypes.PriceListDTO }
|
||||
PriceListRule: { dto: PricingTypes.PriceListRuleDTO }
|
||||
export default class PricingModuleService
|
||||
extends ModulesSdkUtils.MedusaService<{
|
||||
PriceSet: { dto: PricingTypes.PriceSetDTO }
|
||||
Price: { dto: PricingTypes.PriceDTO }
|
||||
PriceRule: {
|
||||
dto: PricingTypes.PriceRuleDTO
|
||||
create: PricingTypes.CreatePriceRuleDTO
|
||||
update: PricingTypes.UpdatePriceRuleDTO
|
||||
}
|
||||
>(PriceSet, generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
RuleType: {
|
||||
dto: PricingTypes.RuleTypeDTO
|
||||
create: PricingTypes.CreateRuleTypeDTO
|
||||
update: PricingTypes.UpdateRuleTypeDTO
|
||||
}
|
||||
PriceList: { dto: PricingTypes.PriceListDTO }
|
||||
PriceListRule: { dto: PricingTypes.PriceListRuleDTO }
|
||||
}>(generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
implements PricingTypes.IPricingModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected readonly pricingRepository_: PricingRepositoryService
|
||||
protected readonly ruleTypeService_: RuleTypeService<TRuleType>
|
||||
protected readonly priceSetService_: ModulesSdkTypes.IMedusaInternalService<TPriceSet>
|
||||
protected readonly priceRuleService_: ModulesSdkTypes.IMedusaInternalService<TPriceRule>
|
||||
protected readonly priceSetRuleTypeService_: ModulesSdkTypes.IMedusaInternalService<TPriceSetRuleType>
|
||||
protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService<TPrice>
|
||||
protected readonly priceListService_: PriceListService<TPriceList>
|
||||
protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService<TPriceListRule>
|
||||
protected readonly priceListRuleValueService_: ModulesSdkTypes.IMedusaInternalService<TPriceListRuleValue>
|
||||
protected readonly ruleTypeService_: RuleTypeService<RuleType>
|
||||
protected readonly priceSetService_: ModulesSdkTypes.IMedusaInternalService<PriceSet>
|
||||
protected readonly priceRuleService_: ModulesSdkTypes.IMedusaInternalService<PriceRule>
|
||||
protected readonly priceSetRuleTypeService_: ModulesSdkTypes.IMedusaInternalService<PriceSetRuleType>
|
||||
protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService<Price>
|
||||
protected readonly priceListService_: PriceListService<PriceList>
|
||||
protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService<PriceListRule>
|
||||
protected readonly priceListRuleValueService_: ModulesSdkTypes.IMedusaInternalService<PriceListRuleValue>
|
||||
|
||||
constructor(
|
||||
{
|
||||
@@ -174,14 +164,15 @@ export default class PricingModuleService<
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async list(
|
||||
// @ts-expect-error
|
||||
async listPriceSets(
|
||||
filters: PricingTypes.FilterablePriceSetProps = {},
|
||||
config: FindConfig<PricingTypes.PriceSetDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PriceSetDTO[]> {
|
||||
const pricingContext = this.setupCalculatedPriceConfig_(filters, config)
|
||||
|
||||
const priceSets = await super.list(filters, config, sharedContext)
|
||||
const priceSets = await super.listPriceSets(filters, config, sharedContext)
|
||||
|
||||
if (pricingContext && priceSets.length) {
|
||||
const priceSetIds: string[] = []
|
||||
@@ -207,14 +198,15 @@ export default class PricingModuleService<
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async listAndCount(
|
||||
// @ts-expect-error
|
||||
async listAndCountPriceSets(
|
||||
filters: PricingTypes.FilterablePriceSetProps = {},
|
||||
config: FindConfig<PricingTypes.PriceSetDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[PriceSetDTO[], number]> {
|
||||
const pricingContext = this.setupCalculatedPriceConfig_(filters, config)
|
||||
|
||||
const [priceSets, count] = await super.listAndCount(
|
||||
const [priceSets, count] = await super.listAndCountPriceSets(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
@@ -315,27 +307,28 @@ export default class PricingModuleService<
|
||||
return JSON.parse(JSON.stringify(calculatedPrices))
|
||||
}
|
||||
|
||||
async create(
|
||||
// @ts-expect-error
|
||||
async createPriceSets(
|
||||
data: PricingTypes.CreatePriceSetDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<PriceSetDTO>
|
||||
|
||||
async create(
|
||||
async createPriceSets(
|
||||
data: PricingTypes.CreatePriceSetDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<PriceSetDTO[]>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
@EmitEvents()
|
||||
async create(
|
||||
async createPriceSets(
|
||||
data: PricingTypes.CreatePriceSetDTO | PricingTypes.CreatePriceSetDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PriceSetDTO | PriceSetDTO[]> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
const priceSets = await this.create_(input, sharedContext)
|
||||
const priceSets = await this.createPriceSets_(input, sharedContext)
|
||||
|
||||
// TODO: Remove the need to refetch the data here
|
||||
const dbPriceSets = await this.list(
|
||||
const dbPriceSets = await this.listPriceSets(
|
||||
{ id: priceSets.map((p) => p.id) },
|
||||
{
|
||||
relations: [
|
||||
@@ -358,17 +351,17 @@ export default class PricingModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
async upsert(
|
||||
async upsertPriceSets(
|
||||
data: UpsertPriceSetDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<PriceSetDTO[]>
|
||||
async upsert(
|
||||
async upsertPriceSets(
|
||||
data: UpsertPriceSetDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<PriceSetDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async upsert(
|
||||
async upsertPriceSets(
|
||||
data: UpsertPriceSetDTO | UpsertPriceSetDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PriceSetDTO | PriceSetDTO[]> {
|
||||
@@ -383,10 +376,10 @@ export default class PricingModuleService<
|
||||
const operations: Promise<PriceSet[]>[] = []
|
||||
|
||||
if (forCreate.length) {
|
||||
operations.push(this.create_(forCreate, sharedContext))
|
||||
operations.push(this.createPriceSets_(forCreate, sharedContext))
|
||||
}
|
||||
if (forUpdate.length) {
|
||||
operations.push(this.update_(forUpdate, sharedContext))
|
||||
operations.push(this.updatePriceSets_(forUpdate, sharedContext))
|
||||
}
|
||||
|
||||
const result = (await promiseAll(operations)).flat()
|
||||
@@ -395,19 +388,20 @@ export default class PricingModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
async update(
|
||||
// @ts-expect-error
|
||||
async updatePriceSets(
|
||||
id: string,
|
||||
data: PricingTypes.UpdatePriceSetDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<PriceSetDTO>
|
||||
async update(
|
||||
async updatePriceSets(
|
||||
selector: PricingTypes.FilterablePriceSetProps,
|
||||
data: PricingTypes.UpdatePriceSetDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<PriceSetDTO[]>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async update(
|
||||
async updatePriceSets(
|
||||
idOrSelector: string | PricingTypes.FilterablePriceSetProps,
|
||||
data: PricingTypes.UpdatePriceSetDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -430,7 +424,10 @@ export default class PricingModuleService<
|
||||
}))
|
||||
}
|
||||
|
||||
const updateResult = await this.update_(normalizedInput, sharedContext)
|
||||
const updateResult = await this.updatePriceSets_(
|
||||
normalizedInput,
|
||||
sharedContext
|
||||
)
|
||||
const priceSets = await this.baseRepository_.serialize<
|
||||
PriceSetDTO[] | PriceSetDTO
|
||||
>(updateResult)
|
||||
@@ -487,7 +484,7 @@ export default class PricingModuleService<
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async update_(
|
||||
protected async updatePriceSets_(
|
||||
data: ServiceTypes.UpdatePriceSetInput[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PriceSet[]> {
|
||||
@@ -549,7 +546,7 @@ export default class PricingModuleService<
|
||||
|
||||
const priceSets = await this.addRules_(inputs, sharedContext)
|
||||
|
||||
const dbPriceSets = await this.list(
|
||||
const dbPriceSets = await this.listPriceSets(
|
||||
{ id: priceSets.map(({ id }) => id) },
|
||||
{ relations: ["rule_types"] }
|
||||
)
|
||||
@@ -581,7 +578,7 @@ export default class PricingModuleService<
|
||||
|
||||
await this.addPrices_(input, sharedContext)
|
||||
|
||||
const dbPrices = await this.list(
|
||||
const dbPrices = await this.listPriceSets(
|
||||
{ id: input.map((d) => d.priceSetId) },
|
||||
{ relations: ["prices"] },
|
||||
sharedContext
|
||||
@@ -719,7 +716,7 @@ export default class PricingModuleService<
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async create_(
|
||||
protected async createPriceSets_(
|
||||
data: PricingTypes.CreatePriceSetDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
@@ -767,11 +764,11 @@ export default class PricingModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
const ruleSetRuleTypeToCreateMap: Map<string, TPriceSetRuleType> = new Map()
|
||||
const ruleSetRuleTypeToCreateMap: Map<string, PriceSetRuleType> = new Map()
|
||||
|
||||
const toCreate = input.map((inputData) => {
|
||||
const id = generateEntityId(
|
||||
(inputData as unknown as TPriceSet).id,
|
||||
(inputData as unknown as PriceSet).id,
|
||||
PriceSetIdPrefix
|
||||
)
|
||||
|
||||
@@ -783,7 +780,7 @@ export default class PricingModuleService<
|
||||
const priceSetRuleType = {
|
||||
rule_type_id: ruleTypeMap.get(rule.rule_attribute).id,
|
||||
price_set_id: id,
|
||||
} as TPriceSetRuleType
|
||||
} as PriceSetRuleType
|
||||
|
||||
ruleSetRuleTypeToCreateMap.set(
|
||||
JSON.stringify(priceSetRuleType),
|
||||
@@ -810,7 +807,7 @@ export default class PricingModuleService<
|
||||
const priceSetRuleType = {
|
||||
rule_type_id: ruleTypeMap.get(attribute).id,
|
||||
price_set_id: id,
|
||||
} as TPriceSetRuleType
|
||||
} as PriceSetRuleType
|
||||
|
||||
ruleSetRuleTypeToCreateMap.set(
|
||||
JSON.stringify(priceSetRuleType),
|
||||
@@ -897,7 +894,7 @@ export default class PricingModuleService<
|
||||
protected async addRules_(
|
||||
inputs: PricingTypes.AddRulesDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TPriceSet[]> {
|
||||
): Promise<PriceSet[]> {
|
||||
const priceSets = await this.priceSetService_.list(
|
||||
{ id: inputs.map((d) => d.priceSetId) },
|
||||
{ relations: ["rule_types"] },
|
||||
@@ -981,7 +978,7 @@ export default class PricingModuleService<
|
||||
input: AddPricesDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const priceSets = await this.list(
|
||||
const priceSets = await this.listPriceSets(
|
||||
{ id: input.map((d) => d.priceSetId) },
|
||||
{ relations: ["rule_types"] },
|
||||
sharedContext
|
||||
@@ -1137,7 +1134,7 @@ export default class PricingModuleService<
|
||||
const priceListsToCreate: PricingTypes.CreatePriceListDTO[] = data.map(
|
||||
(priceListData) => {
|
||||
const id = generateEntityId(
|
||||
(priceListData as unknown as TPriceList).id,
|
||||
(priceListData as unknown as PriceList).id,
|
||||
PriceListIdPrefix
|
||||
)
|
||||
|
||||
@@ -1364,7 +1361,7 @@ export default class PricingModuleService<
|
||||
protected async updatePriceListPrices_(
|
||||
data: PricingTypes.UpdatePriceListPricesDTO[],
|
||||
sharedContext: Context = {}
|
||||
): Promise<TPrice[]> {
|
||||
): Promise<Price[]> {
|
||||
const ruleTypeAttributes: string[] = []
|
||||
const priceListIds: string[] = []
|
||||
const priceIds: string[] = []
|
||||
@@ -1401,7 +1398,7 @@ export default class PricingModuleService<
|
||||
ruleTypes.map((rt) => [rt.rule_attribute, rt])
|
||||
)
|
||||
|
||||
const priceSets = await this.list(
|
||||
const priceSets = await this.listPriceSets(
|
||||
{ id: priceSetIds },
|
||||
{ relations: ["rule_types"] },
|
||||
sharedContext
|
||||
@@ -1456,7 +1453,7 @@ export default class PricingModuleService<
|
||||
|
||||
const priceListMap = new Map(priceLists.map((p) => [p.id, p]))
|
||||
|
||||
const pricesToUpdate: Partial<TPrice>[] = []
|
||||
const pricesToUpdate: Partial<Price>[] = []
|
||||
const priceRuleIdsToDelete: string[] = []
|
||||
const priceRulesToCreate: CreatePriceRuleDTO[] = []
|
||||
|
||||
@@ -1487,7 +1484,7 @@ export default class PricingModuleService<
|
||||
pricesToUpdate.push({
|
||||
...rest,
|
||||
rules_count: Object.keys(rules).length,
|
||||
} as unknown as TPrice)
|
||||
} as unknown as Price)
|
||||
|
||||
priceRuleIdsToDelete.push(...priceRules.map((pr) => pr.id))
|
||||
}
|
||||
@@ -1515,7 +1512,7 @@ export default class PricingModuleService<
|
||||
protected async addPriceListPrices_(
|
||||
data: PricingTypes.AddPriceListPricesDTO[],
|
||||
sharedContext: Context = {}
|
||||
): Promise<TPrice[]> {
|
||||
): Promise<Price[]> {
|
||||
const ruleTypeAttributes: string[] = []
|
||||
const priceListIds: string[] = []
|
||||
const priceSetIds: string[] = []
|
||||
@@ -1535,7 +1532,7 @@ export default class PricingModuleService<
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const priceSets = await this.list(
|
||||
const priceSets = await this.listPriceSets(
|
||||
{ id: priceSetIds },
|
||||
{ relations: ["rule_types"] },
|
||||
sharedContext
|
||||
@@ -1593,7 +1590,7 @@ export default class PricingModuleService<
|
||||
|
||||
const priceListMap = new Map(priceLists.map((p) => [p.id, p]))
|
||||
|
||||
const pricesToCreate: Partial<TPrice>[] = []
|
||||
const pricesToCreate: Partial<Price>[] = []
|
||||
|
||||
for (const { price_list_id: priceListId, prices } of data) {
|
||||
const priceList = priceListMap.get(priceListId)
|
||||
@@ -1626,7 +1623,7 @@ export default class PricingModuleService<
|
||||
price_list_id: priceList.id,
|
||||
rules_count: noOfRules,
|
||||
price_rules: priceRulesToCreate,
|
||||
} as unknown as TPrice
|
||||
} as unknown as Price
|
||||
})
|
||||
|
||||
pricesToCreate.push(...priceListPricesToCreate)
|
||||
@@ -1676,7 +1673,7 @@ export default class PricingModuleService<
|
||||
protected async setPriceListRules_(
|
||||
data: PricingTypes.SetPriceListRulesDTO[],
|
||||
sharedContext: Context = {}
|
||||
): Promise<TPriceList[]> {
|
||||
): Promise<PriceList[]> {
|
||||
// TODO: re think this method
|
||||
|
||||
const priceLists = await this.priceListService_.list(
|
||||
@@ -1791,7 +1788,7 @@ export default class PricingModuleService<
|
||||
protected async removePriceListRules_(
|
||||
data: PricingTypes.RemovePriceListRulesDTO[],
|
||||
sharedContext: Context = {}
|
||||
): Promise<TPriceList[]> {
|
||||
): Promise<PriceList[]> {
|
||||
const priceLists = await this.priceListService_.list(
|
||||
{ id: data.map((d) => d.price_list_id) },
|
||||
{ relations: ["price_list_rules", "price_list_rules.rule_type"] },
|
||||
|
||||
Reference in New Issue
Block a user