medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
25.93k stars 2.6k forks source link

Accessing Commerce service on Custom Service #8441

Closed jerald-dechavez closed 3 months ago

jerald-dechavez commented 3 months ago

Bug report

Describe the bug

Trying to access commerce service on new created custom service is not working

System information

Medusa version (including plugins):

Dependencies "@medusajs/api-key": "preview", "@medusajs/auth": "preview", "@medusajs/cache-inmemory": "preview", "@medusajs/cart": "preview", "@medusajs/currency": "preview", "@medusajs/customer": "preview", "@medusajs/event-bus-local": "preview", "@medusajs/file": "preview", "@medusajs/file-local-next": "preview", "@medusajs/fulfillment": "preview", "@medusajs/fulfillment-manual": "preview", "@medusajs/inventory-next": "preview", "@medusajs/medusa": "preview", "@medusajs/notification": "preview", "@medusajs/notification-local": "preview", "@medusajs/order": "preview", "@medusajs/payment": "preview", "@medusajs/pricing": "preview", "@medusajs/product": "preview", "@medusajs/promotion": "preview", "@medusajs/region": "preview", "@medusajs/sales-channel": "preview", "@medusajs/stock-location-next": "preview", "@medusajs/store": "preview", "@medusajs/tax": "preview", "@medusajs/user": "preview", "@medusajs/workflow-engine-inmemory": "preview", "express": "^4.17.2"

Dev dependencies "@medusajs/medusa-cli": "preview", "@mikro-orm/cli": "5.9.7", "@mikro-orm/core": "5.9.7", "@mikro-orm/migrations": "5.9.7", "@mikro-orm/postgresql": "5.9.7", "@stdlib/number-float64-base-normalize": "0.0.8", "@swc/core": "1.5.7", "@types/express": "^4.17.13", "@types/mime": "1.3.5", "@types/node": "^17.0.8", "@types/react": "^18.3.2", "ts-node": "^10.9.2", "typescript": "^4.5.2"

Node.js version: v20.15.1 Database: postgres Operating system: Mac Browser (if relevant): Chrome

Steps to reproduce the behavior

  1. Created new link for user and store at src/links/user-store.ts
    
    import { defineLink } from "@medusajs/utils"
    import UserModule from "@medusajs/user"
    import StoreModule from "@medusajs/store"

export default defineLink( UserModule.linkable.user, { linkable: StoreModule.linkable.store, isList: true, } )

2. Created custom module called `central` at `src/modules/central`.
3. Create central service at `src/modules/central/service.ts`

import { MedusaService } from "@medusajs/utils" import User from "@medusajs/user/dist/models/user" import Store from "@medusajs/store/dist/models/store"

import { ModulesSdkTypes, InternalModuleDeclaration, ModuleJoinerConfig, } from "@medusajs/types"

type InjectedDependencies = { userService: ModulesSdkTypes.IMedusaInternalService storeService: ModulesSdkTypes.IMedusaInternalService }

class CentralModuleService extends MedusaService<{ User: { dto: User.UserDTO }, Store: { dto: Store.StoreDTO }, }>({ User, Store, }) { protected readonly userService: ModulesSdkTypes.IMedusaInternalService protected readonly storeService: ModulesSdkTypes.IMedusaInternalService

constructor( { userService, storeService }: InjectedDependencies, ) { super(...arguments)

this.userService_ = userService
this.storeService_ = storeService

} }

export default CentralModuleService

4. Create `index.ts` at central module, `src/modules/central/index.ts`

import { Module } from "@medusajs/utils" import CentralService from "./service"

export const CENTRAL_MODULE = "centralModuleService"

export default Module(CENTRAL_MODULE, { service: CentralService, })

5. ASsign central on modules of medusa-config.js

import { loadEnv, defineConfig } from '@medusajs/utils'

import { CENTRAL_MODULE } from './src/modules/central'

loadEnv(process.env.NODE_ENV, process.cwd())

module.exports = defineConfig({ projectConfig: { databaseUrl: process.env.DATABASE_URL, http: { storeCors: process.env.STORE_CORS, adminCors: process.env.ADMIN_CORS, authCors: process.env.AUTH_CORS, jwtSecret: process.env.JWT_SECRET || "supersecret", cookieSecret: process.env.COOKIE_SECRET || "supersecret", } }, modules: {

  resolve: "./modules/central"
}

} })


### Expected behavior

I expected that I can access the commerce module on the central module. But I'm getting this error

jjdechavez@johns-mbp medusa-store-v2 % npm run dev

medusa-starter-default@0.0.1 dev medusa develop

info: Watching filesystem to reload dev server on file change redisUrl not found. A fake redis instance will be used. warn: Local Event Bus installed. This is not recommended for production. error: Error starting server AwilixResolutionError: Could not resolve 'userService'.

Resolution path: userService at resolve (/Users/jjdechavez/playground/medusa-store-v2/node_modules/awilix/src/container.ts:497:15) at Object.userService [as get] (/Users/jjdechavez/playground/medusa-store-v2/node_modules/awilix/src/container.ts:269:52) at new CentralModuleService (/Users/jjdechavez/playground/medusa-store-v2/src/modules/central/service.ts:27:7) at /Users/jjdechavez/playground/medusa-store-v2/node_modules/@medusajs/modules-sdk/src/loaders/utils/load-internal.ts:170:14 at Object.resolve (/Users/jjdechavez/playground/medusa-store-v2/node_modules/awilix/src/resolvers.ts:470:14) at Object.resolve (/Users/jjdechavez/playground/medusa-store-v2/node_modules/awilix/src/container.ts:515:33) at Function.bootstrap (/Users/jjdechavez/playground/medusa-store-v2/node_modules/@medusajs/modules-sdk/src/medusa-module.ts:387:37) at async /Users/jjdechavez/playground/medusa-store-v2/node_modules/@medusajs/modules-sdk/src/medusa-app.ts:127:23 at async Promise.all (index 26) at async loadModules (/Users/jjdechavez/playground/medusa-store-v2/node_modules/@medusajs/modules-sdk/src/medusa-app.ts:98:3)



### Additional context

Let me know if I need to elaborate the issue. thank you
srindom commented 3 months ago

Hi @jerald-dechavez - in v2, modules and their services are isolated. This means you don't have access to them within your custom modules. This is to ensure you stay on the upgrade path, for future Medusa versions. You can read more about module isolation here: https://docs.medusajs.com/v2/advanced-development/modules/isolation

In your example, you should instead create a workflow that orchestrates the interaction between the User and Store Services.

jerald-dechavez commented 3 months ago

Thank you @srindom for the clarification on how medusa modules work