diff --git a/api/ContextAccounts.tsx b/api/ContextAccounts.tsx index 11e710c10..54e62bece 100644 --- a/api/ContextAccounts.tsx +++ b/api/ContextAccounts.tsx @@ -7,9 +7,8 @@ import { getQuotaFromTerritoryOrSubsidaries, } from "@/helpers/accounts"; import { - createPayerAccountLink, + createPayerAndAccountLink, deletePayerAccountLink, - getOrCreatePayerAccount, } from "@/helpers/payers/api-actions"; import { transformNotesVersion } from "@/helpers/ui-notes-writer"; import { JSONContent } from "@tiptap/core"; @@ -430,12 +429,7 @@ export const AccountsContextProvider: FC = ({ : { ...a, payerAccounts: [...a.payerAccounts, payer] } ); if (updated) mutate(updated, false); - const payerAccountId = await getOrCreatePayerAccount(payer); - if (!payerAccountId) return; - const resultAccountId = await createPayerAccountLink( - accountId, - payerAccountId - ); + const resultAccountId = await createPayerAndAccountLink(accountId, payer); if (!resultAccountId) return; if (updated) mutate(updated); toast({ diff --git a/api/usePayer.ts b/api/usePayer.ts index b556d5d92..bd1e1cf4a 100644 --- a/api/usePayer.ts +++ b/api/usePayer.ts @@ -78,13 +78,16 @@ const usePayer = (payerId?: string) => { const createPayerAccountLink = async (accountId: string | null) => { if (!accountId) return; - if (!payer) return; + if (!payerId) return; const updatedPayer = { - ...payer, - accountIds: [...payer.accountIds, accountId], + accountNumber: payerId, + isReseller: !!payer?.isReseller, + resellerId: payer?.resellerId, + notes: payer?.notes ?? "", + accountIds: [...(payer?.accountIds ?? []), accountId], } as Payer; mutate(updatedPayer, false); - await createPayerAndAccountLink(accountId, payer.accountNumber); + await createPayerAndAccountLink(accountId, payerId); mutate(updatedPayer); }; diff --git a/helpers/payers/api-actions.ts b/helpers/payers/api-actions.ts index 8223a43ae..84bc6123f 100644 --- a/helpers/payers/api-actions.ts +++ b/helpers/payers/api-actions.ts @@ -4,40 +4,22 @@ import { generateClient } from "aws-amplify/data"; import { flow, get, identity } from "lodash/fp"; const client = generateClient(); -export const getOrCreatePayerAccount = async (payerId: string) => { - const { data, errors } = await client.models.PayerAccount.get({ - awsAccountNumber: payerId, - }); - if (errors) { - handleApiErrors(errors, "Loading payer account failed"); - throw errors; - } - if (data) return data.awsAccountNumber; - return createPayerAccount(payerId); -}; - -export const createPayerAccountLink = async ( +export const createPayerAndAccountLink = async ( accountId: string, payerId: string ) => { - const { data, errors } = await client.models.AccountPayerAccount.create({ - accountId, - awsAccountNumberId: payerId, - }); - if (errors) { - handleApiErrors(errors, "Creating payer account link failed"); - throw errors; - } - return data?.awsAccountNumberId; + const payerAccountId = await getOrCreatePayerAccount(payerId); + if (!payerAccountId) return; + return createPayerAccountLink(accountId, payerAccountId); }; -export const createPayerAndAccountLink = async ( - accountId: string, +export const createPayerAndPersonLink = async ( + personId: string | null, payerId: string ) => { const payerAccountId = await getOrCreatePayerAccount(payerId); if (!payerAccountId) return; - return createPayerAccountLink(accountId, payerAccountId); + return createPayerPersonLink(personId, payerAccountId); }; export const deletePayerAccountLink = async ( @@ -53,6 +35,45 @@ export const deletePayerAccountLink = async ( return data; }; +const getOrCreatePayerAccount = async (payerId: string) => { + const { data, errors } = await client.models.PayerAccount.get({ + awsAccountNumber: payerId, + }); + if (errors) { + handleApiErrors(errors, "Loading payer account failed"); + throw errors; + } + if (data) return data.awsAccountNumber; + return createPayerAccount(payerId); +}; + +const createPayerPersonLink = async ( + personId: string | null, + payerId: string +) => { + const { data, errors } = await client.models.PayerAccount.update({ + awsAccountNumber: payerId, + mainContactId: personId, + }); + if (errors) { + handleApiErrors(errors, "Linking person to paye failed"); + throw errors; + } + return data?.awsAccountNumber; +}; + +const createPayerAccountLink = async (accountId: string, payerId: string) => { + const { data, errors } = await client.models.AccountPayerAccount.create({ + accountId, + awsAccountNumberId: payerId, + }); + if (errors) { + handleApiErrors(errors, "Creating payer account link failed"); + throw errors; + } + return data?.awsAccountNumberId; +}; + const getAccountPayerAccountId = async (accountId: string, payerId: string) => { const { data, errors } = await client.models.AccountPayerAccount.listAccountPayerAccountByAwsAccountNumberId(