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
@@ -1,76 +1,49 @@
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { MikroOrmWrapper } from "../../utils"
|
||||
import { getInitModuleConfig } from "../../utils/get-init-module-config"
|
||||
import { createPaymentCollections } from "../../__fixtures__"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("Payment Module Service", () => {
|
||||
let service: IPaymentModuleService
|
||||
let repositoryManager: SqlEntityManager
|
||||
let shutdownFunc: () => Promise<void>
|
||||
moduleIntegrationTestRunner<IPaymentModuleService>({
|
||||
moduleName: Modules.PAYMENT,
|
||||
testSuite: ({ service }) => {
|
||||
describe("Payment Module Service", () => {
|
||||
describe("providers", () => {
|
||||
it("should load payment plugins", async () => {
|
||||
let error = await service
|
||||
.createPaymentCollections([
|
||||
{
|
||||
amount: 200,
|
||||
region_id: "req_123",
|
||||
} as any,
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
beforeAll(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
service = medusaApp.modules[Modules.PAYMENT]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
repositoryManager = await MikroOrmWrapper.forkManager()
|
||||
|
||||
await createPaymentCollections(repositoryManager)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
describe("providers", () => {
|
||||
it("should load payment plugins", async () => {
|
||||
let error = await service
|
||||
.createPaymentCollections([
|
||||
{
|
||||
amount: 200,
|
||||
region_id: "req_123",
|
||||
} as any,
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toContain(
|
||||
"Value for PaymentCollection.currency_code is required, 'undefined' found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a payment collection successfully", async () => {
|
||||
const [createdPaymentCollection] = await service.createPaymentCollections(
|
||||
[{ currency_code: "USD", amount: 200, region_id: "reg_123" }]
|
||||
)
|
||||
|
||||
expect(createdPaymentCollection).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
status: "not_paid",
|
||||
payment_providers: [],
|
||||
payment_sessions: [],
|
||||
payments: [],
|
||||
currency_code: "USD",
|
||||
amount: 200,
|
||||
expect(error.message).toContain(
|
||||
"Value for PaymentCollection.currency_code is required, 'undefined' found"
|
||||
)
|
||||
})
|
||||
)
|
||||
|
||||
it("should create a payment collection successfully", async () => {
|
||||
const [createdPaymentCollection] =
|
||||
await service.createPaymentCollections([
|
||||
{ currency_code: "USD", amount: 200, region_id: "reg_123" },
|
||||
])
|
||||
|
||||
expect(createdPaymentCollection).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
status: "not_paid",
|
||||
payment_providers: [],
|
||||
payment_sessions: [],
|
||||
payments: [],
|
||||
currency_code: "USD",
|
||||
amount: 200,
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
+3
-9
@@ -1,10 +1,7 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { promiseAll } from "@medusajs/utils"
|
||||
import {
|
||||
moduleIntegrationTestRunner,
|
||||
SuiteOptions,
|
||||
} from "medusa-test-utils/dist"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils/dist"
|
||||
import {
|
||||
createPaymentCollections,
|
||||
createPayments,
|
||||
@@ -13,12 +10,9 @@ import {
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<IPaymentModuleService>({
|
||||
moduleName: Modules.PAYMENT,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IPaymentModuleService>) => {
|
||||
testSuite: ({ MikroOrmWrapper, service }) => {
|
||||
describe("Payment Module Service", () => {
|
||||
describe("Payment Flow", () => {
|
||||
it("complete payment flow successfully", async () => {
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
if (typeof process.env.DB_TEMP_NAME === "undefined") {
|
||||
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
|
||||
process.env.DB_TEMP_NAME = `medusa-payment-integration-${tempName}`
|
||||
}
|
||||
|
||||
process.env.MEDUSA_PAYMENT_DB_SCHEMA = "public"
|
||||
@@ -1,3 +0,0 @@
|
||||
import { JestUtils } from "medusa-test-utils"
|
||||
|
||||
JestUtils.afterAllHookDropDatabase()
|
||||
@@ -1,6 +0,0 @@
|
||||
import { ModuleServiceInitializeOptions } from "@medusajs/types"
|
||||
|
||||
export const databaseOptions: ModuleServiceInitializeOptions["database"] = {
|
||||
schema: "public",
|
||||
clientUrl: "medusa-payment-test",
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import {TestDatabaseUtils} from "medusa-test-utils"
|
||||
|
||||
import * as PaymentModules from "@models"
|
||||
|
||||
const pathToMigrations = "../../src/migrations"
|
||||
const mikroOrmEntities = PaymentModules as unknown as any[]
|
||||
|
||||
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper({
|
||||
mikroOrmEntities,
|
||||
pathToMigrations,
|
||||
})
|
||||
|
||||
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig({
|
||||
mikroOrmEntities,
|
||||
pathToMigrations,
|
||||
})
|
||||
|
||||
export const DB_URL = TestDatabaseUtils.getDatabaseURL()
|
||||
@@ -1,42 +0,0 @@
|
||||
import { Modules, ModulesDefinition } from "@medusajs/modules-sdk"
|
||||
|
||||
import { DB_URL } from "./database"
|
||||
|
||||
export function getInitModuleConfig() {
|
||||
const moduleOptions = {
|
||||
providers: [
|
||||
{
|
||||
resolve: "@medusajs/payment-stripe",
|
||||
options: {
|
||||
config: {
|
||||
dkk: {
|
||||
apiKey: "pk_test_123",
|
||||
},
|
||||
usd: {
|
||||
apiKey: "pk_test_456",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const injectedDependencies = {}
|
||||
|
||||
const modulesConfig_ = {
|
||||
[Modules.PAYMENT]: {
|
||||
definition: ModulesDefinition[Modules.PAYMENT],
|
||||
options: moduleOptions,
|
||||
},
|
||||
}
|
||||
|
||||
return {
|
||||
injectedDependencies,
|
||||
modulesConfig: modulesConfig_,
|
||||
databaseConfig: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_PAYMENT_DB_SCHEMA,
|
||||
},
|
||||
joinerConfig: [],
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from "./config"
|
||||
export * from "./database"
|
||||
@@ -16,6 +16,4 @@ module.exports = {
|
||||
testEnvironment: `node`,
|
||||
moduleFileExtensions: [`js`, `ts`],
|
||||
modulePathIgnorePatterns: ["dist/"],
|
||||
setupFiles: ["<rootDir>/integration-tests/setup-env.js"],
|
||||
setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"],
|
||||
}
|
||||
|
||||
@@ -8,10 +8,7 @@
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
"bin": {
|
||||
"medusa-payment-seed": "dist/scripts/bin/run-seed.js"
|
||||
"node": ">=20"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
|
||||
import { PaymentModuleService } from "@services"
|
||||
import loadProviders from "./loaders/providers"
|
||||
import loadDefaults from "./loaders/defaults"
|
||||
|
||||
const service = PaymentModuleService
|
||||
const loaders = [loadProviders, loadDefaults] as any
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
loaders,
|
||||
}
|
||||
|
||||
export default moduleDefinition
|
||||
|
||||
export * from "./types"
|
||||
export { PaymentModuleOptions } from "./types"
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import {
|
||||
buildEntitiesNameToLinkableKeysMap,
|
||||
defineJoinerConfig,
|
||||
MapToConfig,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
Payment,
|
||||
PaymentCollection,
|
||||
@@ -8,54 +11,19 @@ import {
|
||||
PaymentSession,
|
||||
} from "@models"
|
||||
|
||||
export const LinkableKeys = {
|
||||
payment_id: Payment.name,
|
||||
payment_collection_id: PaymentCollection.name,
|
||||
payment_provider_id: PaymentProvider.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.PAYMENT, {
|
||||
entityQueryingConfig: [
|
||||
Payment,
|
||||
PaymentCollection,
|
||||
PaymentProvider,
|
||||
PaymentSession,
|
||||
],
|
||||
linkableKeys: {
|
||||
payment_id: Payment.name,
|
||||
payment_collection_id: PaymentCollection.name,
|
||||
payment_provider_id: PaymentProvider.name,
|
||||
},
|
||||
})
|
||||
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.PAYMENT,
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: LinkableKeys,
|
||||
alias: [
|
||||
{
|
||||
name: ["payment", "payments"],
|
||||
args: {
|
||||
entity: Payment.name,
|
||||
methodSuffix: "Payments",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["payment_collection", "payment_collections"],
|
||||
args: {
|
||||
entity: PaymentCollection.name,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["payment_session", "payment_sessions"],
|
||||
args: {
|
||||
entity: PaymentSession.name,
|
||||
methodSuffix: "PaymentSessions",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["payment_provider", "payment_providers"],
|
||||
args: {
|
||||
entity: PaymentProvider.name,
|
||||
methodSuffix: "PaymentProviders",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
export const entityNameToLinkableKeysMap: MapToConfig =
|
||||
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
|
||||
import { PaymentModuleService } from "@services"
|
||||
import loadProviders from "./loaders/providers"
|
||||
import loadDefaults from "./loaders/defaults"
|
||||
|
||||
const service = PaymentModuleService
|
||||
const loaders = [loadProviders, loadDefaults] as any
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
loaders,
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
|
||||
@@ -1,33 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { EOL } from "os"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
import * as PaymentModels from "@models"
|
||||
|
||||
import { createPayments } from "../seed-utils"
|
||||
|
||||
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-payment-seed <filePath>`
|
||||
)
|
||||
}
|
||||
|
||||
const run = ModulesSdkUtils.buildSeedScript({
|
||||
moduleName: Modules.PAYMENT,
|
||||
models: PaymentModels,
|
||||
pathToMigrations: __dirname + "/../../migrations",
|
||||
seedHandler: async ({ manager, data }) => {
|
||||
const { paymentsData } = data
|
||||
await createPayments(manager, paymentsData)
|
||||
},
|
||||
})
|
||||
await run({ path })
|
||||
})()
|
||||
@@ -1,8 +0,0 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export async function createPayments(
|
||||
manager: SqlEntityManager,
|
||||
paymentsData: any[]
|
||||
): Promise<any[]> {
|
||||
return []
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
DAL,
|
||||
FilterablePaymentCollectionProps,
|
||||
FilterablePaymentProviderProps,
|
||||
FilterablePaymentSessionProps,
|
||||
FindConfig,
|
||||
InternalModuleDeclaration,
|
||||
IPaymentModuleService,
|
||||
@@ -62,38 +63,29 @@ type InjectedDependencies = {
|
||||
|
||||
const generateMethodForModels = {
|
||||
PaymentCollection,
|
||||
Payment,
|
||||
PaymentSession,
|
||||
Payment,
|
||||
Capture,
|
||||
Refund,
|
||||
}
|
||||
|
||||
export default class PaymentModuleService<
|
||||
TPaymentCollection extends PaymentCollection = PaymentCollection,
|
||||
TPayment extends Payment = Payment,
|
||||
TCapture extends Capture = Capture,
|
||||
TRefund extends Refund = Refund,
|
||||
TPaymentSession extends PaymentSession = PaymentSession
|
||||
>
|
||||
extends ModulesSdkUtils.MedusaService<
|
||||
PaymentCollectionDTO,
|
||||
{
|
||||
PaymentCollection: { dto: PaymentCollectionDTO }
|
||||
PaymentSession: { dto: PaymentSessionDTO }
|
||||
Payment: { dto: PaymentDTO }
|
||||
Capture: { dto: CaptureDTO }
|
||||
Refund: { dto: RefundDTO }
|
||||
}
|
||||
>(PaymentCollection, generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
export default class PaymentModuleService
|
||||
extends ModulesSdkUtils.MedusaService<{
|
||||
PaymentCollection: { dto: PaymentCollectionDTO }
|
||||
PaymentSession: { dto: PaymentSessionDTO }
|
||||
Payment: { dto: PaymentDTO }
|
||||
Capture: { dto: CaptureDTO }
|
||||
Refund: { dto: RefundDTO }
|
||||
}>(generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
implements IPaymentModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
|
||||
protected paymentService_: ModulesSdkTypes.IMedusaInternalService<TPayment>
|
||||
protected captureService_: ModulesSdkTypes.IMedusaInternalService<TCapture>
|
||||
protected refundService_: ModulesSdkTypes.IMedusaInternalService<TRefund>
|
||||
protected paymentSessionService_: ModulesSdkTypes.IMedusaInternalService<TPaymentSession>
|
||||
protected paymentCollectionService_: ModulesSdkTypes.IMedusaInternalService<TPaymentCollection>
|
||||
protected paymentService_: ModulesSdkTypes.IMedusaInternalService<Payment>
|
||||
protected captureService_: ModulesSdkTypes.IMedusaInternalService<Capture>
|
||||
protected refundService_: ModulesSdkTypes.IMedusaInternalService<Refund>
|
||||
protected paymentSessionService_: ModulesSdkTypes.IMedusaInternalService<PaymentSession>
|
||||
protected paymentCollectionService_: ModulesSdkTypes.IMedusaInternalService<PaymentCollection>
|
||||
protected paymentProviderService_: PaymentProviderService
|
||||
|
||||
constructor(
|
||||
@@ -155,7 +147,7 @@ export default class PaymentModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async createPaymentCollections_(
|
||||
data: CreatePaymentCollectionDTO[],
|
||||
@MedusaContext() sharedContext?: Context
|
||||
@@ -527,6 +519,38 @@ export default class PaymentModuleService<
|
||||
return payment
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
// @ts-expect-error
|
||||
async retrievePaymentSession(
|
||||
id: string,
|
||||
config: FindConfig<PaymentSessionDTO> = {},
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<PaymentSessionDTO> {
|
||||
const session = await this.paymentSessionService_.retrieve(
|
||||
id,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize(session)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
// @ts-expect-error
|
||||
async listPaymentSessions(
|
||||
filters?: FilterablePaymentSessionProps,
|
||||
config?: FindConfig<PaymentSessionDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<PaymentSessionDTO[]> {
|
||||
const sessions = await this.paymentSessionService_.list(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<PaymentSessionDTO[]>(sessions)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async updatePayment(
|
||||
data: UpdatePaymentDTO,
|
||||
@@ -822,7 +846,7 @@ export default class PaymentModuleService<
|
||||
case PaymentActions.SUCCESSFUL: {
|
||||
const [payment] = await this.listPayments(
|
||||
{
|
||||
session_id: event.data.resource_id,
|
||||
payment_session_id: event.data.resource_id,
|
||||
},
|
||||
{},
|
||||
sharedContext
|
||||
|
||||
Reference in New Issue
Block a user