Skip to content

Commit

Permalink
fix: when linking account to payer, payer is created if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Carsten Koch committed Nov 28, 2024
1 parent 8c962e2 commit f86901a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 37 deletions.
10 changes: 2 additions & 8 deletions api/ContextAccounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -430,12 +429,7 @@ export const AccountsContextProvider: FC<AccountsContextProviderProps> = ({
: { ...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({
Expand Down
11 changes: 7 additions & 4 deletions api/usePayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
71 changes: 46 additions & 25 deletions helpers/payers/api-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,22 @@ import { generateClient } from "aws-amplify/data";
import { flow, get, identity } from "lodash/fp";
const client = generateClient<Schema>();

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 (
Expand All @@ -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(
Expand Down

0 comments on commit f86901a

Please sign in to comment.