A local-first personal finance app
🧹 optimise GoCardless bank sync to use fewer api calls (#436)
Matt Fiddamand613a6be6e6d
diff --git a/src/app-gocardless/app-gocardless.js b/src/app-gocardless/app-gocardless.jsindex 85cc249202..44d92c1327 100644--- a/src/app-gocardless/app-gocardless.js+++ b/src/app-gocardless/app-gocardless.js@@ -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 } });diff --git a/src/app-gocardless/services/gocardless-service.js b/src/app-gocardless/services/gocardless-service.jsindex 61efd6eeec..41e8f93fe3 100644--- a/src/app-gocardless/services/gocardless-service.js+++ b/src/app-gocardless/services/gocardless-service.js@@ -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,@@ -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,diff --git a/upcoming-release-notes/436.md b/upcoming-release-notes/436.mdnew file mode 100644index 0000000000..7f9796280a--- /dev/null+++ b/upcoming-release-notes/436.md@@ -0,0 +1,6 @@+---+category: Maintenance+authors: [matt-fidd]+---++Optimise GoCardless sync to reduce API usage by removing balance information