Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 optimise GoCardless bank sync to use fewer api calls #436

Merged
merged 4 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 52 additions & 22 deletions src/app-gocardless/app-gocardless.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,34 +142,64 @@ app.post(
app.post(
'/transactions',
handleError(async (req, res) => {
const { requisitionId, startDate, endDate, accountId } = req.body;
const {
requisitionId,
startDate,
endDate,
accountId,
includeBalance = true,
} = req.body;

try {
const {
balances,
institutionId,
startingBalance,
transactions: { booked, pending, all },
} = await goCardlessService.getTransactionsWithBalance(
requisitionId,
accountId,
startDate,
endDate,
);

res.send({
status: 'ok',
data: {
if (includeBalance) {
const {
balances,
institutionId,
startingBalance,
transactions: {
booked,
pending,
all,
transactions: { booked, pending, all },
} = await goCardlessService.getTransactionsWithBalance(
requisitionId,
accountId,
startDate,
endDate,
);

res.send({
status: 'ok',
data: {
balances,
institutionId,
startingBalance,
transactions: {
booked,
pending,
all,
},
},
},
});
});
} else {
const {
institutionId,
transactions: { booked, pending, all },
} = await goCardlessService.getNormalizedTransactions(
requisitionId,
accountId,
startDate,
endDate,
);

res.send({
status: 'ok',
data: {
institutionId,
transactions: {
booked,
pending,
all,
},
},
});
}
} catch (error) {
const sendErrorResponse = (data) =>
res.send({ status: 'ok', data: { ...data, details: error.details } });
Expand Down
70 changes: 59 additions & 11 deletions src/app-gocardless/services/gocardless-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,71 @@ export const goCardlessService = {
throw new AccountNotLinedToRequisition(accountId, requisitionId);
}

const [transactions, accountBalance] = await Promise.all([
goCardlessService.getTransactions({
institutionId: institution_id,
const [normalizedTransactions, accountBalance] = await Promise.all([
goCardlessService.getNormalizedTransactions(
requisitionId,
accountId,
startDate,
endDate,
}),
),
goCardlessService.getBalances(accountId),
]);

const transactions = normalizedTransactions.transactions;

const bank = BankFactory(institution_id);

const startingBalance = bank.calculateStartingBalance(
transactions.booked,
accountBalance.balances,
);

return {
balances: accountBalance.balances,
institutionId: institution_id,
startingBalance,
transactions,
};
},

/**
*
* @param requisitionId
* @param accountId
* @param startDate
* @param endDate
* @throws {AccountNotLinedToRequisition} Will throw an error if requisition not includes provided account id
* @throws {RequisitionNotLinked} Will throw an error if requisition is not in Linked
* @throws {InvalidInputDataError}
* @throws {InvalidGoCardlessTokenError}
* @throws {AccessDeniedError}
* @throws {NotFoundError}
* @throws {ResourceSuspended}
* @throws {RateLimitError}
* @throws {UnknownError}
* @throws {ServiceError}
* @returns {Promise<{institutionId: string, transactions: {booked: Array<import('../gocardless-node.types.js').Transaction>, pending: Array<import('../gocardless-node.types.js').Transaction>, all: Array<import('../gocardless.types.js').TransactionWithBookedStatus>}}>}
*/
getNormalizedTransactions: async (
requisitionId,
accountId,
startDate,
endDate,
) => {
const { institution_id, accounts: accountIds } =
await goCardlessService.getLinkedRequisition(requisitionId);

if (!accountIds.includes(accountId)) {
throw new AccountNotLinedToRequisition(accountId, requisitionId);
}

const transactions = await goCardlessService.getTransactions({
institutionId: institution_id,
accountId,
startDate,
endDate,
});

const bank = BankFactory(institution_id);
const sortedBookedTransactions = bank.sortTransactions(
transactions.transactions?.booked,
Expand All @@ -231,15 +286,8 @@ export const goCardlessService = {
);
const sortedAllTransactions = bank.sortTransactions(allTransactions);

const startingBalance = bank.calculateStartingBalance(
sortedBookedTransactions,
accountBalance.balances,
);

return {
balances: accountBalance.balances,
institutionId: institution_id,
startingBalance,
transactions: {
booked: sortedBookedTransactions,
pending: sortedPendingTransactions,
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/436.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [matt-fidd]
---

Optimise GoCardless sync to reduce API usage by removing balance information