Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

1.0 KB
Raw Blame
import { MedusaError } from "@medusajs/framework/utils"
import { ICommissionProvider } from "@mercurjs/types"

export const CommissionProviderRegistrationPrefix = "cp_"
export const CommissionDefaultProvider = "commission_default_provider"

type InjectedDependencies = {
  [key: `${typeof CommissionProviderRegistrationPrefix}${string}`]: ICommissionProvider
}

export default class CommissionProviderService {
  protected readonly container_: InjectedDependencies

  constructor(container: InjectedDependencies) {
    this.container_ = container
  }

  retrieveProvider(providerId: string): ICommissionProvider {
    try {
      return this.container_[
        `${CommissionProviderRegistrationPrefix}${providerId}`
      ]
    } catch {
      throw new MedusaError(
        MedusaError.Types.NOT_FOUND,
        `Unable to retrieve the commission provider with id: ${providerId}. Please make sure that the provider is registered in the container and it is configured correctly in your project configuration file.`
      )
    }
  }
}