feat: Fulfillment module basic structure (#6319)

**What**
Scafold the fulfillment module basic structure

**Bonus**
Simplified module scaffolding with new factories and less directories to manage
- mikro orm connection loader factory
- initialize factory

FIXES CORE-1709
FIXES CORE-1710
This commit is contained in:
Adrien de Peretti
2024-02-06 13:29:36 +00:00
committed by GitHub
parent 2104843826
commit 12054f5c01
42 changed files with 694 additions and 1 deletions
+14
View File
@@ -0,0 +1,14 @@
import { moduleDefinition } from "./module-definition"
import { initializeFactory, Modules } from "@medusajs/modules-sdk"
export * from "./types"
export * from "./models"
export * from "./services"
export const initialize = initializeFactory({
moduleName: Modules.FULFILLMENT,
moduleDefinition,
})
export const runMigrations = moduleDefinition.runMigrations
export const revertMigration = moduleDefinition.revertMigration
export default moduleDefinition
+25
View File
@@ -0,0 +1,25 @@
import { Modules } from "@medusajs/modules-sdk"
import { ModuleJoinerConfig } from "@medusajs/types"
import { MapToConfig } from "@medusajs/utils"
// TODO manage the config
export const LinkableKeys: Record<string, string> = {}
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.FULFILLMENT,
primaryKeys: ["id"],
linkableKeys: LinkableKeys,
alias: [],
} as ModuleJoinerConfig
@@ -0,0 +1,56 @@
import { DALUtils, generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Entity,
Filter,
Index,
OnInit,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { DAL } from "@medusajs/types"
type FulfillmentSetOptionalProps = DAL.SoftDeletableEntityDateColumns
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class FulfillmentSet {
[OptionalProps]?: FulfillmentSetOptionalProps
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
name: string
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@Index({ name: "IDX_fulfillment_set_deleted_at" })
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "fuset")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "fuset")
}
}
+1
View File
@@ -0,0 +1 @@
export { default as FulfillmentSet } from "./fullfilment-set"
@@ -0,0 +1,44 @@
import { ModuleExports } from "@medusajs/types"
import * as ModuleServices from "@services"
import { FulfillmentModuleService } from "@services"
import { Modules } from "@medusajs/modules-sdk"
import * as Models from "@models"
import * as ModuleModels from "@models"
import { ModulesSdkUtils } from "@medusajs/utils"
import * as ModuleRepositories from "@repositories"
const migrationScriptOptions = {
moduleName: Modules.FULFILLMENT,
models: Models,
pathToMigrations: __dirname + "/migrations",
}
const runMigrations = ModulesSdkUtils.buildMigrationScript(
migrationScriptOptions
)
const revertMigration = ModulesSdkUtils.buildRevertMigrationScript(
migrationScriptOptions
)
const containerLoader = ModulesSdkUtils.moduleContainerLoaderFactory({
moduleModels: ModuleModels,
moduleRepositories: ModuleRepositories,
moduleServices: ModuleServices,
})
const connectionLoader = ModulesSdkUtils.mikroOrmConnectionLoaderFactory({
moduleName: Modules.FULFILLMENT,
moduleModels: Object.values(Models),
migrationsPath: __dirname + "/migrations",
})
const service = FulfillmentModuleService
const loaders = [containerLoader, connectionLoader] as any
export const moduleDefinition: ModuleExports = {
service,
loaders,
revertMigration,
runMigrations,
}
@@ -0,0 +1 @@
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
@@ -0,0 +1,29 @@
#!/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-fulfillment-seed <filePath>`
)
}
const run = ModulesSdkUtils.buildSeedScript({
moduleName: Modules.FULFILLMENT,
models: Models,
pathToMigrations: __dirname + "/../../migrations",
seedHandler: async ({ manager, data }) => {
// TODO: Add seed logic
},
})
await run({ path })
})()
@@ -0,0 +1,5 @@
describe("noop", function () {
it("should run", function () {
expect(true).toBe(true)
})
})
@@ -0,0 +1,91 @@
import {
Context,
DAL,
FulfillmentTypes,
IFulfillmentModuleService,
InternalModuleDeclaration,
ModuleJoinerConfig,
ModulesSdkTypes,
} from "@medusajs/types"
import { InjectTransactionManager, ModulesSdkUtils } from "@medusajs/utils"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
import { FulfillmentSet } from "@models"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
fulfillmentService: ModulesSdkTypes.InternalModuleService<any>
}
export default class FulfillmentModuleService<
TEntity extends FulfillmentSet = FulfillmentSet
>
extends ModulesSdkUtils.abstractModuleServiceFactory<
InjectedDependencies,
any, // TODO Create appropriate DTO
{}
>(FulfillmentSet, [], entityNameToLinkableKeysMap)
implements IFulfillmentModuleService
{
protected baseRepository_: DAL.RepositoryService
protected readonly fulfillmentService_: ModulesSdkTypes.InternalModuleService<TEntity>
constructor(
{ baseRepository, fulfillmentService }: InjectedDependencies,
protected readonly moduleDeclaration: InternalModuleDeclaration
) {
// @ts-ignore
super(...arguments)
this.baseRepository_ = baseRepository
this.fulfillmentService_ = fulfillmentService
}
__joinerConfig(): ModuleJoinerConfig {
return joinerConfig
}
create(
data: any[],
sharedContext?: Context
): Promise<FulfillmentTypes.FulfillmentDTO[]>
create(
data: any,
sharedContext?: Context
): Promise<FulfillmentTypes.FulfillmentDTO>
// TODO Implement the methods from the interface and change type
@InjectTransactionManager("baseRepository_")
async create(
data: any[] | any,
sharedContext?: Context
): Promise<
FulfillmentTypes.FulfillmentDTO | FulfillmentTypes.FulfillmentDTO[]
> {
return await Promise.resolve(
[] as FulfillmentTypes.FulfillmentDTO[] | FulfillmentTypes.FulfillmentDTO
)
}
// TODO Implement the methods from the interface and change type
update(
data: any[],
sharedContext?: Context
): Promise<FulfillmentTypes.FulfillmentDTO[]>
update(
data: any,
sharedContext?: Context
): Promise<FulfillmentTypes.FulfillmentDTO>
@InjectTransactionManager("baseRepository_")
async update(
data: any,
sharedContext?: Context
): Promise<
FulfillmentTypes.FulfillmentDTO | FulfillmentTypes.FulfillmentDTO[]
> {
return await Promise.resolve(
[] as FulfillmentTypes.FulfillmentDTO[] | FulfillmentTypes.FulfillmentDTO
)
}
}
@@ -0,0 +1 @@
export { default as FulfillmentModuleService } from "./fulfillment-module-service"
+6
View File
@@ -0,0 +1,6 @@
import { IEventBusModuleService, Logger } from "@medusajs/types"
export type InitializeModuleInjectableDependencies = {
logger?: Logger
eventBusService?: IEventBusModuleService
}