The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.
TypeScript b2b-marketplacecomposable-commerceecommerceenterpriseheadless-commercemarketplacemedusamedusajsmulti-vendortypescript
fix: validate duplicate payout account and fix onboarding idempotency key
vholikcbb9ab3a5d0f
diff --git a/packages/core-plugin/src/modules/payout/services/payout-module-service.ts b/packages/core-plugin/src/modules/payout/services/payout-module-service.tsindex 5f7574d41..6a4131a8e 100644--- a/packages/core-plugin/src/modules/payout/services/payout-module-service.ts+++ b/packages/core-plugin/src/modules/payout/services/payout-module-service.ts@@ -112,7 +112,7 @@ export default class PayoutModuleService extends MedusaService({const providerData = await this.payoutProviderService_.createOnboarding({context: {- idempotency_key: input.account_id,+ idempotency_key: `${input.account_id}_onboarding`,...input.context},data: {diff --git a/packages/core-plugin/src/workflows/payout/steps/index.ts b/packages/core-plugin/src/workflows/payout/steps/index.tsindex a7345b450..c382ea1eb 100644--- a/packages/core-plugin/src/workflows/payout/steps/index.ts+++ b/packages/core-plugin/src/workflows/payout/steps/index.ts@@ -3,3 +3,4 @@ export * from "./create-onboarding"export * from "./create-payout"export * from "./update-payout-account"export * from "./update-payout"+export * from "./validate-seller-payout-account"diff --git a/packages/core-plugin/src/workflows/payout/steps/validate-seller-payout-account.ts b/packages/core-plugin/src/workflows/payout/steps/validate-seller-payout-account.tsnew file mode 100644index 000000000..6fec98e93--- /dev/null+++ b/packages/core-plugin/src/workflows/payout/steps/validate-seller-payout-account.ts@@ -0,0 +1,32 @@+import { Query } from "@medusajs/framework"+import { MedusaError } from "@medusajs/framework/utils"+import { ContainerRegistrationKeys } from "@medusajs/framework/utils"+import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"++interface ValidateSellerPayoutAccountInput {+ seller_id: string+}++export const validateSellerPayoutAccountStep = createStep(+ "validate-seller-payout-account",+ async (input: ValidateSellerPayoutAccountInput, { container }) => {+ const query = container.resolve<Query>(ContainerRegistrationKeys.QUERY)++ const { data } = await query.graph({+ entity: "seller_payout_account",+ filters: {+ seller_id: input.seller_id,+ },+ fields: ["seller_id", "payout_account_id"],+ })++ if (data.length > 0) {+ throw new MedusaError(+ MedusaError.Types.INVALID_DATA,+ `Seller already has a payout account`+ )+ }++ return new StepResponse(void 0)+ }+)diff --git a/packages/core-plugin/src/workflows/payout/workflows/create-payout-account.ts b/packages/core-plugin/src/workflows/payout/workflows/create-payout-account.tsindex b3b10908c..8079a6f66 100644--- a/packages/core-plugin/src/workflows/payout/workflows/create-payout-account.ts+++ b/packages/core-plugin/src/workflows/payout/workflows/create-payout-account.ts@@ -4,6 +4,7 @@ import { CreatePayoutAccountDTO, MercurModules } from "@mercurjs/types"import {createPayoutAccountStep,+ validateSellerPayoutAccountStep,} from "../steps"interface CreatePayoutAccountInput extends CreatePayoutAccountDTO {@@ -13,6 +14,10 @@ interface CreatePayoutAccountInput extends CreatePayoutAccountDTO {export const createPayoutAccountWorkflow = createWorkflow("create-payout-account-for-seller",function (input: CreatePayoutAccountInput) {+ validateSellerPayoutAccountStep({+ seller_id: input.seller_id,+ })+const payoutAccount = createPayoutAccountStep({context: input.context,data: input.data,