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:
533 files changed
+6469
-9769
No files matched your search
+16
-16
@@ -1,22 +1,18 @@
|
||||
import { SuiteOptions, moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
|
||||
import { IStockLocationService } from "@medusajs/types"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
jest.setTimeout(100000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<IStockLocationService>({
|
||||
moduleName: Modules.STOCK_LOCATION,
|
||||
resolve: "@medusajs/stock-location-next",
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IStockLocationService>) => {
|
||||
testSuite: ({ service }) => {
|
||||
describe("Stock Location Module Service", () => {
|
||||
describe("create", () => {
|
||||
it("should create a stock location", async () => {
|
||||
const data = { name: "location" }
|
||||
const location = await service.create(data)
|
||||
const location = await service.createStockLocations(data)
|
||||
|
||||
expect(location).toEqual(
|
||||
expect.objectContaining({ id: expect.any(String), ...data })
|
||||
@@ -25,7 +21,7 @@ moduleIntegrationTestRunner({
|
||||
|
||||
it("should create stock locations for arrray", async () => {
|
||||
const data = [{ name: "location" }, { name: "location-1" }]
|
||||
const locations = await service.create(data)
|
||||
const locations = await service.createStockLocations(data)
|
||||
|
||||
expect(locations).toEqual([
|
||||
expect.objectContaining({ id: expect.any(String), ...data[0] }),
|
||||
@@ -38,7 +34,7 @@ moduleIntegrationTestRunner({
|
||||
name: "location",
|
||||
address: { city: "city", address_1: "street", country_code: "US" },
|
||||
}
|
||||
const location = await service.create(data)
|
||||
const location = await service.createStockLocations(data)
|
||||
|
||||
expect(location).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -71,7 +67,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
},
|
||||
]
|
||||
const location = await service.create(data)
|
||||
const location = await service.createStockLocations(data)
|
||||
|
||||
expect(location).toEqual(
|
||||
expect.arrayContaining([
|
||||
@@ -99,7 +95,9 @@ moduleIntegrationTestRunner({
|
||||
describe("update", () => {
|
||||
let stockLocation
|
||||
beforeEach(async () => {
|
||||
stockLocation = await service.create({ name: "location" })
|
||||
stockLocation = await service.createStockLocations({
|
||||
name: "location",
|
||||
})
|
||||
})
|
||||
|
||||
it("should update a stock location", async () => {
|
||||
@@ -107,7 +105,7 @@ moduleIntegrationTestRunner({
|
||||
id: stockLocation.id,
|
||||
name: "updated location",
|
||||
}
|
||||
const location = await service.upsert(data)
|
||||
const location = await service.upsertStockLocations(data)
|
||||
|
||||
expect(location).toEqual(expect.objectContaining(data))
|
||||
})
|
||||
@@ -122,7 +120,7 @@ moduleIntegrationTestRunner({
|
||||
},
|
||||
}
|
||||
|
||||
const location = await service.upsert(data)
|
||||
const location = await service.upsertStockLocations(data)
|
||||
|
||||
expect(location).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -135,7 +133,7 @@ moduleIntegrationTestRunner({
|
||||
describe("updateStockLocationAddress", () => {
|
||||
let stockLocation
|
||||
beforeEach(async () => {
|
||||
stockLocation = await service.create({
|
||||
stockLocation = await service.createStockLocations({
|
||||
name: "location",
|
||||
address: { city: "city", address_1: "street", country_code: "US" },
|
||||
})
|
||||
@@ -148,7 +146,9 @@ moduleIntegrationTestRunner({
|
||||
address_1: "updated address_1",
|
||||
country_code: "updated country_code",
|
||||
}
|
||||
const location = await service.updateStockLocationAddress(data)
|
||||
const location = await (service as any).updateStockLocationAddresses(
|
||||
data
|
||||
)
|
||||
|
||||
expect(location).toEqual(expect.objectContaining(data))
|
||||
})
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"directory": "packages/stock-location"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
"node": ">=20"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
|
||||
export * from "./models"
|
||||
export * from "./services"
|
||||
import { StockLocationModuleService } from "@services"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
|
||||
const moduleDefinition: ModuleExports = {
|
||||
service: StockLocationModuleService,
|
||||
}
|
||||
export default moduleDefinition
|
||||
@@ -1,35 +1,17 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import {
|
||||
buildEntitiesNameToLinkableKeysMap,
|
||||
defineJoinerConfig,
|
||||
MapToConfig,
|
||||
} from "@medusajs/utils"
|
||||
import { StockLocation } from "./models"
|
||||
import moduleSchema from "./schema"
|
||||
|
||||
export const LinkableKeys = {
|
||||
stock_location_id: StockLocation.name,
|
||||
location_id: StockLocation.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.STOCK_LOCATION, {
|
||||
linkableKeys: {
|
||||
stock_location_id: StockLocation.name,
|
||||
location_id: StockLocation.name,
|
||||
},
|
||||
})
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.STOCK_LOCATION,
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: LinkableKeys,
|
||||
schema: moduleSchema,
|
||||
alias: [
|
||||
{
|
||||
name: ["stock_location", "stock_locations"],
|
||||
args: {
|
||||
entity: "StockLocation",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
export const entityNameToLinkableKeysMap: MapToConfig =
|
||||
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)
|
||||
@@ -1,8 +0,0 @@
|
||||
import { StockLocationModuleService } from "@services"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
|
||||
const service = StockLocationModuleService
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
|
||||
@@ -1,29 +0,0 @@
|
||||
export default `
|
||||
scalar DateTime
|
||||
scalar JSON
|
||||
type StockLocation {
|
||||
id: ID!
|
||||
created_at: DateTime!
|
||||
updated_at: DateTime!
|
||||
deleted_at: DateTime
|
||||
name: String!
|
||||
address_id: String
|
||||
address: StockLocationAddress
|
||||
metadata: JSON
|
||||
}
|
||||
type StockLocationAddress {
|
||||
id: ID!
|
||||
created_at: DateTime!
|
||||
updated_at: DateTime!
|
||||
deleted_at: DateTime
|
||||
address_1: String!
|
||||
address_2: String
|
||||
company: String
|
||||
city: String
|
||||
country_code: String!
|
||||
phone: String
|
||||
province: String
|
||||
postal_code: String
|
||||
metadata: JSON
|
||||
}
|
||||
`
|
||||
@@ -5,12 +5,12 @@ import {
|
||||
DAL,
|
||||
FilterableStockLocationProps,
|
||||
IEventBusService,
|
||||
IStockLocationServiceNext,
|
||||
IStockLocationService,
|
||||
ModuleJoinerConfig,
|
||||
ModulesSdkTypes,
|
||||
StockLocationAddressInput,
|
||||
StockLocationTypes,
|
||||
UpdateStockLocationNextInput,
|
||||
UpdateStockLocationInput,
|
||||
UpsertStockLocationInput,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
InjectTransactionManager,
|
||||
isString,
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
MedusaService,
|
||||
promiseAll,
|
||||
} from "@medusajs/utils"
|
||||
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
||||
@@ -31,28 +31,20 @@ type InjectedDependencies = {
|
||||
stockLocationAddressService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
}
|
||||
|
||||
const generateMethodForModels = { StockLocationAddress }
|
||||
|
||||
/**
|
||||
* Service for managing stock locations.
|
||||
*/
|
||||
export default class StockLocationModuleService<
|
||||
TEntity extends StockLocation = StockLocation,
|
||||
TStockLocationAddress extends StockLocationAddress = StockLocationAddress
|
||||
>
|
||||
extends ModulesSdkUtils.MedusaService<
|
||||
StockLocationTypes.StockLocationDTO,
|
||||
{
|
||||
StockLocation: { dto: StockLocationTypes.StockLocationDTO }
|
||||
StockLocationAddress: { dto: StockLocationTypes.StockLocationAddressDTO }
|
||||
}
|
||||
>(StockLocation, generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
implements IStockLocationServiceNext
|
||||
export default class StockLocationModuleService
|
||||
extends MedusaService<{
|
||||
StockLocation: { dto: StockLocationTypes.StockLocationDTO }
|
||||
StockLocationAddress: { dto: StockLocationTypes.StockLocationAddressDTO }
|
||||
}>({ StockLocation, StockLocationAddress }, entityNameToLinkableKeysMap)
|
||||
implements IStockLocationService
|
||||
{
|
||||
protected readonly eventBusModuleService_: IEventBusService
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected readonly stockLocationService_: ModulesSdkTypes.IMedusaInternalService<TEntity>
|
||||
protected readonly stockLocationAddressService_: ModulesSdkTypes.IMedusaInternalService<TStockLocationAddress>
|
||||
protected readonly stockLocationService_: ModulesSdkTypes.IMedusaInternalService<StockLocation>
|
||||
protected readonly stockLocationAddressService_: ModulesSdkTypes.IMedusaInternalService<StockLocationAddress>
|
||||
|
||||
constructor(
|
||||
{
|
||||
@@ -76,23 +68,18 @@ export default class StockLocationModuleService<
|
||||
return joinerConfig
|
||||
}
|
||||
|
||||
create(
|
||||
// @ts-expect-error
|
||||
createStockLocations(
|
||||
data: CreateStockLocationInput,
|
||||
context: Context
|
||||
): Promise<StockLocationTypes.StockLocationDTO>
|
||||
create(
|
||||
createStockLocations(
|
||||
data: CreateStockLocationInput[],
|
||||
context: Context
|
||||
): Promise<StockLocationTypes.StockLocationDTO[]>
|
||||
|
||||
/**
|
||||
* Creates a new stock location.
|
||||
* @param data - The input data for creating a Stock Location.
|
||||
* @param context
|
||||
* @returns The created stock location.
|
||||
*/
|
||||
@InjectManager("baseRepository_")
|
||||
async create(
|
||||
async createStockLocations(
|
||||
data: CreateStockLocationInput | CreateStockLocationInput[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<
|
||||
@@ -100,7 +87,7 @@ export default class StockLocationModuleService<
|
||||
> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
|
||||
const created = await this.create_(input, context)
|
||||
const created = await this.createStockLocations_(input, context)
|
||||
|
||||
const serialized = await this.baseRepository_.serialize<
|
||||
| StockLocationTypes.StockLocationDTO
|
||||
@@ -111,24 +98,24 @@ export default class StockLocationModuleService<
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async create_(
|
||||
async createStockLocations_(
|
||||
data: CreateStockLocationInput[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
): Promise<StockLocation[]> {
|
||||
return await this.stockLocationService_.create(data, context)
|
||||
}
|
||||
|
||||
async upsert(
|
||||
async upsertStockLocations(
|
||||
data: UpsertStockLocationInput,
|
||||
context?: Context
|
||||
): Promise<StockLocationTypes.StockLocationDTO>
|
||||
async upsert(
|
||||
async upsertStockLocations(
|
||||
data: UpsertStockLocationInput[],
|
||||
context?: Context
|
||||
): Promise<StockLocationTypes.StockLocationDTO[]>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async upsert(
|
||||
async upsertStockLocations(
|
||||
data: UpsertStockLocationInput | UpsertStockLocationInput[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<
|
||||
@@ -136,7 +123,7 @@ export default class StockLocationModuleService<
|
||||
> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
|
||||
const result = await this.upsert_(input, context)
|
||||
const result = await this.upsertStockLocations_(input, context)
|
||||
|
||||
return await this.baseRepository_.serialize<
|
||||
| StockLocationTypes.StockLocationDTO[]
|
||||
@@ -145,13 +132,13 @@ export default class StockLocationModuleService<
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async upsert_(
|
||||
async upsertStockLocations_(
|
||||
input: UpsertStockLocationInput[],
|
||||
@MedusaContext() context: Context = {}
|
||||
) {
|
||||
const toUpdate = input.filter(
|
||||
(location): location is UpdateStockLocationNextInput => !!location.id
|
||||
) as UpdateStockLocationNextInput[]
|
||||
(location): location is UpdateStockLocationInput => !!location.id
|
||||
) as UpdateStockLocationInput[]
|
||||
const toCreate = input.filter(
|
||||
(location) => !location.id
|
||||
) as CreateStockLocationInput[]
|
||||
@@ -159,23 +146,24 @@ export default class StockLocationModuleService<
|
||||
const operations: Promise<StockLocation[] | StockLocation>[] = []
|
||||
|
||||
if (toCreate.length) {
|
||||
operations.push(this.create_(toCreate, context))
|
||||
operations.push(this.createStockLocations_(toCreate, context))
|
||||
}
|
||||
if (toUpdate.length) {
|
||||
operations.push(this.update_(toUpdate, context))
|
||||
operations.push(this.updateStockLocations_(toUpdate, context))
|
||||
}
|
||||
|
||||
return (await promiseAll(operations)).flat()
|
||||
}
|
||||
|
||||
update(
|
||||
// @ts-expect-error
|
||||
updateStockLocations(
|
||||
id: string,
|
||||
input: UpdateStockLocationNextInput,
|
||||
input: UpdateStockLocationInput,
|
||||
context?: Context
|
||||
): Promise<StockLocationTypes.StockLocationDTO>
|
||||
update(
|
||||
updateStockLocations(
|
||||
selector: FilterableStockLocationProps,
|
||||
input: UpdateStockLocationNextInput,
|
||||
input: UpdateStockLocationInput,
|
||||
context?: Context
|
||||
): Promise<StockLocationTypes.StockLocationDTO[]>
|
||||
/**
|
||||
@@ -186,22 +174,22 @@ export default class StockLocationModuleService<
|
||||
* @returns The updated stock location.
|
||||
*/
|
||||
@InjectManager("baseRepository_")
|
||||
async update(
|
||||
async updateStockLocations(
|
||||
idOrSelector: string | FilterableStockLocationProps,
|
||||
data: UpdateStockLocationNextInput | UpdateStockLocationNextInput[],
|
||||
data: UpdateStockLocationInput | UpdateStockLocationInput[],
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<
|
||||
StockLocationTypes.StockLocationDTO | StockLocationTypes.StockLocationDTO[]
|
||||
> {
|
||||
let normalizedInput:
|
||||
| (UpdateStockLocationNextInput & { id: string })[]
|
||||
| (UpdateStockLocationInput & { id: string })[]
|
||||
| { data: any; selector: FilterableStockLocationProps } = []
|
||||
if (isString(idOrSelector)) {
|
||||
normalizedInput = [{ id: idOrSelector, ...data }]
|
||||
} else {
|
||||
normalizedInput = { data, selector: idOrSelector }
|
||||
}
|
||||
const updated = await this.update_(normalizedInput, context)
|
||||
const updated = await this.updateStockLocations_(normalizedInput, context)
|
||||
|
||||
const serialized = await this.baseRepository_.serialize<
|
||||
| StockLocationTypes.StockLocationDTO
|
||||
@@ -212,27 +200,28 @@ export default class StockLocationModuleService<
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async update_(
|
||||
async updateStockLocations_(
|
||||
data:
|
||||
| UpdateStockLocationNextInput[]
|
||||
| UpdateStockLocationNextInput
|
||||
| UpdateStockLocationInput[]
|
||||
| UpdateStockLocationInput
|
||||
| { data: any; selector: FilterableStockLocationProps },
|
||||
@MedusaContext() context: Context = {}
|
||||
): Promise<TEntity[] | TEntity> {
|
||||
): Promise<StockLocation[] | StockLocation> {
|
||||
return await this.stockLocationService_.update(data, context)
|
||||
}
|
||||
|
||||
updateStockLocationAddress(
|
||||
// @ts-expect-error
|
||||
updateStockLocationAddresses(
|
||||
data: StockLocationAddressInput & { id: string },
|
||||
context?: Context
|
||||
): Promise<StockLocationTypes.StockLocationAddressDTO>
|
||||
updateStockLocationAddress(
|
||||
updateStockLocationAddresses(
|
||||
data: (StockLocationAddressInput & { id: string })[],
|
||||
context?: Context
|
||||
): Promise<StockLocationTypes.StockLocationAddressDTO[]>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async updateStockLocationAddress(
|
||||
async updateStockLocationAddresses(
|
||||
data:
|
||||
| (StockLocationAddressInput & { id: string })
|
||||
| (StockLocationAddressInput & { id: string })[],
|
||||
@@ -240,7 +229,7 @@ export default class StockLocationModuleService<
|
||||
) {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
|
||||
const updated = await this.updateStockLocationAddress_(input, context)
|
||||
const updated = await this.updateStockLocationAddresses_(input, context)
|
||||
|
||||
const serialized = await this.baseRepository_.serialize<
|
||||
| StockLocationTypes.StockLocationAddressDTO
|
||||
@@ -251,7 +240,7 @@ export default class StockLocationModuleService<
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
private async updateStockLocationAddress_(
|
||||
private async updateStockLocationAddresses_(
|
||||
input: (StockLocationAddressInput & { id: string })[],
|
||||
@MedusaContext() context: Context
|
||||
) {
|
||||
|
||||
Reference in new issue
Block a user