From e0109540fa49f4f0ed8011de86b84743e291a42f Mon Sep 17 00:00:00 2001 From: Stevche Radevski Date: Wed, 12 Mar 2025 16:05:38 +0100 Subject: [PATCH] feat: Change customer to account_holder to be one-to-many (#11803) --- .../workflows/create-payment-session.ts | 21 +++++++++++++------ .../definitions/customer-account-holder.ts | 6 +++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/core/core-flows/src/payment-collection/workflows/create-payment-session.ts b/packages/core/core-flows/src/payment-collection/workflows/create-payment-session.ts index 77266f5813..8f18d7b0ee 100644 --- a/packages/core/core-flows/src/payment-collection/workflows/create-payment-session.ts +++ b/packages/core/core-flows/src/payment-collection/workflows/create-payment-session.ts @@ -89,7 +89,7 @@ export const createPaymentSessionsWorkflow = createWorkflow( return !!data.input.customer_id } ).then(() => { - const customer: CustomerDTO & { account_holder: AccountHolderDTO } = + const customer: CustomerDTO & { account_holders: AccountHolderDTO[] } = useRemoteQueryStep({ entry_point: "customer", fields: [ @@ -100,7 +100,7 @@ export const createPaymentSessionsWorkflow = createWorkflow( "last_name", "phone", "addresses.*", - "account_holder.*", + "account_holders.*", "metadata", ], variables: { id: input.customer_id }, @@ -116,11 +116,17 @@ export const createPaymentSessionsWorkflow = createWorkflow( } }) + const existingAccountHolder = transform({ customer, input }, (data) => { + return data.customer.account_holders.find( + (ac) => ac.provider_id === data.input.provider_id + ) + }) + const accountHolderInput = { provider_id: input.provider_id, context: { // The module is idempotent, so if there already is a linked account holder, the module will simply return it back. - account_holder: customer.account_holder, + account_holder: existingAccountHolder, customer: paymentCustomer, }, } @@ -131,11 +137,14 @@ export const createPaymentSessionsWorkflow = createWorkflow( when( "account-holder-created", - { paymentCustomer, accountHolder }, + { paymentCustomer, accountHolder, input }, (data) => { return ( - !isPresent(data.paymentCustomer?.account_holder) && - isPresent(data.accountHolder) + !isPresent( + data.paymentCustomer?.account_holders.find( + (ac) => ac.provider_id === data.input.provider_id + ) + ) && isPresent(data.accountHolder) ) } ).then(() => { diff --git a/packages/modules/link-modules/src/definitions/customer-account-holder.ts b/packages/modules/link-modules/src/definitions/customer-account-holder.ts index 6b1ee7a1d1..9963e627cf 100644 --- a/packages/modules/link-modules/src/definitions/customer-account-holder.ts +++ b/packages/modules/link-modules/src/definitions/customer-account-holder.ts @@ -42,13 +42,17 @@ export const CustomerAccountHolder: ModuleJoinerConfig = { serviceName: Modules.CUSTOMER, entity: "Customer", fieldAlias: { - account_holder: "account_holder_link.account_holder", + account_holders: { + path: "account_holder_link.account_holder", + isList: true, + }, }, relationship: { serviceName: LINKS.CustomerAccountHolder, primaryKey: "customer_id", foreignKey: "id", alias: "account_holder_link", + isList: true, }, }, {