Skip to content

Commit

Permalink
Only pull single account data from SimpleFIN if syncing one account (#…
Browse files Browse the repository at this point in the history
…483)

* only pull single account data from SimpleFIN if syncing one account

* add release note

* fix linter

* remove debug print

* Update src/app-simplefin/app-simplefin.js

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
psybers and coderabbitai[bot] authored Oct 30, 2024
1 parent e659ccf commit b5f8aa4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/app-simplefin/app-simplefin.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ app.post(
}

try {
const accounts = await getAccounts(accessKey, null, null, true);
const accounts = await getAccounts(accessKey, null, null, null, true);

res.send({
status: 'ok',
Expand Down Expand Up @@ -91,7 +91,11 @@ app.post(
: startDate;
let results;
try {
results = await getTransactions(accessKey, new Date(earliestStartDate));
results = await getTransactions(
accessKey,
Array.isArray(accountId) ? accountId : [accountId],
new Date(earliestStartDate),
);
} catch (e) {
if (e.message === 'Forbidden') {
invalidToken(res);
Expand Down Expand Up @@ -293,12 +297,12 @@ async function getAccessKey(base64Token) {
});
}

async function getTransactions(accessKey, startDate, endDate) {
async function getTransactions(accessKey, accounts, startDate, endDate) {
const now = new Date();
startDate = startDate || new Date(now.getFullYear(), now.getMonth(), 1);
endDate = endDate || new Date(now.getFullYear(), now.getMonth() + 1, 1);
console.log(`${getDate(startDate)} - ${getDate(endDate)}`);
return await getAccounts(accessKey, startDate, endDate);
return await getAccounts(accessKey, accounts, startDate, endDate);
}

function getDate(date) {
Expand All @@ -311,6 +315,7 @@ function normalizeDate(date) {

async function getAccounts(
accessKey,
accounts,
startDate,
endDate,
noTransactions = false,
Expand All @@ -337,6 +342,12 @@ async function getAccounts(
params.push(`balances-only=1`);
}

if (accounts) {
accounts.forEach((id) => {
params.push(`account=${encodeURIComponent(id)}`);
});
}

let queryString = '';
if (params.length > 0) {
queryString += '?' + params.join('&');
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/483.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [psybers]
---

SimpleFIN: when sync'ing a single account, only pull transactions for that account.

0 comments on commit b5f8aa4

Please sign in to comment.