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:
Adrien de Peretti
2024-06-19 13:02:16 +00:00
committed by GitHub
co-authored by Stevche Radevski Oli Juhl
parent 2895ccfba8
commit 48963f55ef
533 changed files with 6469 additions and 9769 deletions
@@ -1,20 +1,17 @@
import { Modules } from "@medusajs/modules-sdk"
import { IStoreModuleService } from "@medusajs/types"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import { moduleIntegrationTestRunner } from "medusa-test-utils"
import { createStoreFixture } from "../__fixtures__"
jest.setTimeout(100000)
moduleIntegrationTestRunner({
moduleIntegrationTestRunner<IStoreModuleService>({
moduleName: Modules.STORE,
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IStoreModuleService>) => {
testSuite: ({ service }) => {
describe("Store Module Service", () => {
describe("creating a store", () => {
it("should get created successfully", async function () {
const store = await service.create(createStoreFixture)
const store = await service.createStores(createStoreFixture)
expect(store).toEqual(
expect.objectContaining({
@@ -32,7 +29,7 @@ moduleIntegrationTestRunner({
it("should fail to get created if default currency code is not in list of supported currency codes", async function () {
const err = await service
.create({ ...createStoreFixture, default_currency_code: "jpy" })
.createStores({ ...createStoreFixture, default_currency_code: "jpy" })
.catch((err) => err.message)
expect(err).toEqual("Store does not have currency: jpy")
@@ -40,7 +37,7 @@ moduleIntegrationTestRunner({
describe("upserting a store", () => {
it("should get created if it does not exist", async function () {
const store = await service.upsert(createStoreFixture)
const store = await service.upsertStores(createStoreFixture)
expect(store).toEqual(
expect.objectContaining({
@@ -55,8 +52,10 @@ moduleIntegrationTestRunner({
})
it("should get created if it does not exist", async function () {
const createdStore = await service.upsert(createStoreFixture)
const upsertedStore = await service.upsert({ name: "Upserted store" })
const createdStore = await service.upsertStores(createStoreFixture)
const upsertedStore = await service.upsertStores({
name: "Upserted store",
})
expect(upsertedStore).toEqual(
expect.objectContaining({
@@ -69,17 +68,17 @@ moduleIntegrationTestRunner({
describe("updating a store", () => {
it("should update the name successfully", async function () {
const createdStore = await service.create(createStoreFixture)
const updatedStore = await service.update(createdStore.id, {
title: "Updated store",
const createdStore = await service.createStores(createStoreFixture)
const updatedStore = await service.updateStores(createdStore.id, {
name: "Updated store",
})
expect(updatedStore.title).toEqual("Updated store")
expect(updatedStore.name).toEqual("Updated store")
})
it("should fail updating default currency code to an unsupported one", async function () {
const createdStore = await service.create(createStoreFixture)
const createdStore = await service.createStores(createStoreFixture)
const updateErr = await service
.update(createdStore.id, {
.updateStores(createdStore.id, {
default_currency_code: "jpy",
})
.catch((err) => err.message)
@@ -88,9 +87,9 @@ moduleIntegrationTestRunner({
})
it("should fail updating default currency code to an unsupported one if the supported currencies are also updated", async function () {
const createdStore = await service.create(createStoreFixture)
const createdStore = await service.createStores(createStoreFixture)
const updateErr = await service
.update(createdStore.id, {
.updateStores(createdStore.id, {
supported_currency_codes: ["mkd"],
default_currency_code: "jpy",
})
@@ -100,12 +99,12 @@ moduleIntegrationTestRunner({
})
it("should fail updating supported currencies if one of them is used as a default one", async function () {
const createdStore = await service.create({
const createdStore = await service.createStores({
...createStoreFixture,
default_currency_code: "eur",
})
const updateErr = await service
.update(createdStore.id, {
.updateStores(createdStore.id, {
supported_currency_codes: ["jpy"],
})
.catch((err) => err.message)
@@ -118,26 +117,26 @@ moduleIntegrationTestRunner({
describe("deleting a store", () => {
it("should successfully delete existing stores", async function () {
const createdStore = await service.create([
const createdStore = await service.createStores([
createStoreFixture,
createStoreFixture,
])
await service.delete([createdStore[0].id, createdStore[1].id])
await service.deleteStores([createdStore[0].id, createdStore[1].id])
const storeInDatabase = await service.list()
const storeInDatabase = await service.listStores()
expect(storeInDatabase).toHaveLength(0)
})
})
describe("retrieving a store", () => {
it("should successfully return all existing stores", async function () {
await service.create([
await service.createStores([
createStoreFixture,
{ ...createStoreFixture, name: "Another store" },
])
const storesInDatabase = await service.list()
const storesInDatabase = await service.listStores()
expect(storesInDatabase).toHaveLength(2)
expect(storesInDatabase.map((s) => s.name)).toEqual(
expect.arrayContaining(["Test store", "Another store"])
+1 -4
View File
@@ -8,10 +8,7 @@
"dist"
],
"engines": {
"node": ">=16"
},
"bin": {
"medusa-store-seed": "dist/scripts/bin/run-seed.js"
"node": ">=20"
},
"repository": {
"type": "git",
+5 -4
View File
@@ -1,7 +1,8 @@
import { moduleDefinition } from "./module-definition"
import { StoreModuleService } from "@services"
import { ModuleExports } from "@medusajs/types"
export * from "./types"
export * from "./models"
export * from "./services"
export const moduleDefinition: ModuleExports = {
service: StoreModuleService,
}
export default moduleDefinition
+8 -26
View File
@@ -1,29 +1,11 @@
import { Modules } from "@medusajs/modules-sdk"
import { ModuleJoinerConfig } from "@medusajs/types"
import { MapToConfig } from "@medusajs/utils"
import Store from "./models/store"
import {
buildEntitiesNameToLinkableKeysMap,
defineJoinerConfig,
MapToConfig,
} from "@medusajs/utils"
export const LinkableKeys: Record<string, string> = {}
export const joinerConfig = defineJoinerConfig(Modules.STORE)
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.STORE,
primaryKeys: ["id"],
linkableKeys: LinkableKeys,
alias: [
{
name: ["store", "stores"],
args: { entity: Store.name },
},
],
} as ModuleJoinerConfig
export const entityNameToLinkableKeysMap: MapToConfig =
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)
@@ -1,8 +0,0 @@
import { ModuleExports } from "@medusajs/types"
import { StoreModuleService } from "@services"
const service = StoreModuleService
export const moduleDefinition: ModuleExports = {
service,
}
@@ -1 +0,0 @@
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
@@ -1,29 +0,0 @@
#!/usr/bin/env node
import { ModulesSdkUtils } from "@medusajs/utils"
import { Modules } from "@medusajs/modules-sdk"
import * as Models from "@models"
import { EOL } from "os"
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-store-seed <filePath>`
)
}
const run = ModulesSdkUtils.buildSeedScript({
moduleName: Modules.STORE,
models: Models,
pathToMigrations: __dirname + "/../../migrations",
seedHandler: async ({ manager, data }) => {
// TODO: Add seed logic
},
})
await run({ path })
})()
@@ -13,7 +13,7 @@ import {
isString,
MedusaContext,
MedusaError,
ModulesSdkUtils,
MedusaService,
promiseAll,
removeUndefined,
} from "@medusajs/utils"
@@ -22,24 +22,19 @@ import { Store } from "@models"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
import { UpdateStoreInput } from "@types"
const generateMethodForModels = {}
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
storeService: ModulesSdkTypes.IMedusaInternalService<any>
}
export default class StoreModuleService<TEntity extends Store = Store>
extends ModulesSdkUtils.MedusaService<
StoreTypes.StoreDTO,
{
Store: { dto: StoreTypes.StoreDTO }
}
>(Store, generateMethodForModels, entityNameToLinkableKeysMap)
export default class StoreModuleService
extends MedusaService<{
Store: { dto: StoreTypes.StoreDTO }
}>({ Store }, entityNameToLinkableKeysMap)
implements IStoreModuleService
{
protected baseRepository_: DAL.RepositoryService
protected readonly storeService_: ModulesSdkTypes.IMedusaInternalService<TEntity>
protected readonly storeService_: ModulesSdkTypes.IMedusaInternalService<Store>
constructor(
{ baseRepository, storeService }: InjectedDependencies,
@@ -55,16 +50,17 @@ export default class StoreModuleService<TEntity extends Store = Store>
return joinerConfig
}
async create(
// @ts-expect-error
async createStores(
data: StoreTypes.CreateStoreDTO[],
sharedContext?: Context
): Promise<StoreTypes.StoreDTO[]>
async create(
async createStores(
data: StoreTypes.CreateStoreDTO,
sharedContext?: Context
): Promise<StoreTypes.StoreDTO>
@InjectManager("baseRepository_")
async create(
async createStores(
data: StoreTypes.CreateStoreDTO | StoreTypes.CreateStoreDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<StoreTypes.StoreDTO | StoreTypes.StoreDTO[]> {
@@ -88,16 +84,16 @@ export default class StoreModuleService<TEntity extends Store = Store>
return await this.storeService_.create(normalizedInput, sharedContext)
}
async upsert(
async upsertStores(
data: StoreTypes.UpsertStoreDTO[],
sharedContext?: Context
): Promise<StoreTypes.StoreDTO[]>
async upsert(
async upsertStores(
data: StoreTypes.UpsertStoreDTO,
sharedContext?: Context
): Promise<StoreTypes.StoreDTO>
@InjectTransactionManager("baseRepository_")
async upsert(
async upsertStores(
data: StoreTypes.UpsertStoreDTO | StoreTypes.UpsertStoreDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<StoreTypes.StoreDTO | StoreTypes.StoreDTO[]> {
@@ -124,18 +120,19 @@ export default class StoreModuleService<TEntity extends Store = Store>
>(Array.isArray(data) ? result : result[0])
}
async update(
// @ts-expect-error
async updateStores(
id: string,
data: StoreTypes.UpdateStoreDTO,
sharedContext?: Context
): Promise<StoreTypes.StoreDTO>
async update(
async updateStores(
selector: StoreTypes.FilterableStoreProps,
data: StoreTypes.UpdateStoreDTO,
sharedContext?: Context
): Promise<StoreTypes.StoreDTO[]>
@InjectManager("baseRepository_")
async update(
async updateStores(
idOrSelector: string | StoreTypes.FilterableStoreProps,
data: StoreTypes.UpdateStoreDTO,
@MedusaContext() sharedContext: Context = {}