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

add simplefin batch sync to api #3821

Merged
merged 8 commits into from
Nov 18, 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
2 changes: 1 addition & 1 deletion packages/loot-core/src/client/actions/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export function syncAccounts(id?: string) {

// Perform sync operation
const res = await send('accounts-bank-sync', {
id: accountId,
ids: [accountId],
});

const success = handleSyncResponse(
Expand Down
40 changes: 34 additions & 6 deletions packages/loot-core/src/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,41 @@ handlers['api/sync'] = async function () {
};

handlers['api/bank-sync'] = async function (args) {
const { errors } = await handlers['accounts-bank-sync']({
id: args?.accountId,
});
const batchSync = args?.accountId == null;
const allErrors = [];

if (!batchSync) {
const { errors } = await handlers['accounts-bank-sync']({
ids: [args.accountId],
});

allErrors.push(errors);
} else {
const accountsData = await handlers['accounts-get']();
const accountIdsToSync = accountsData.map(a => a.id);
const simpleFinAccounts = accountsData.filter(
a => a.account_sync_source === 'simpleFin',
);
const simpleFinAccountIds = simpleFinAccounts.map(a => a.id);

if (simpleFinAccounts.length > 1) {
const res = await handlers['simplefin-batch-sync']({
ids: simpleFinAccountIds,
});

res.forEach(a => allErrors.push(...a.res.errors));
}

const { errors } = await handlers['accounts-bank-sync']({
ids: accountIdsToSync.filter(a => !simpleFinAccountIds.includes(a)),
});

allErrors.push(...errors);
}

const [firstError] = errors;
if (firstError) {
throw new Error(getBankSyncError(firstError));
const errors = allErrors.filter(e => e != null);
if (errors.length > 0) {
throw new Error(getBankSyncError(errors[0]));
}
};

Expand Down
23 changes: 16 additions & 7 deletions packages/loot-core/src/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1104,17 +1104,22 @@ function handleSyncError(err, acct) {
};
}

handlers['accounts-bank-sync'] = async function ({ id }) {
handlers['accounts-bank-sync'] = async function ({ ids = [] }) {
const [[, userId], [, userKey]] = await asyncStorage.multiGet([
'user-id',
'user-key',
]);

const accounts = await db.runQuery(
`SELECT a.*, b.bank_id as bankId FROM accounts a
LEFT JOIN banks b ON a.bank = b.id
WHERE a.tombstone = 0 AND a.closed = 0 ${id ? 'AND a.id = ?' : ''}
ORDER BY a.offbudget, a.sort_order`,
id ? [id] : [],
`
SELECT a.*, b.bank_id as bankId
FROM accounts a
LEFT JOIN banks b ON a.bank = b.id
WHERE a.tombstone = 0 AND a.closed = 0
${ids.length ? `AND a.id IN (${ids.map(() => '?').join(', ')})` : ''}
ORDER BY a.offbudget, a.sort_order
`,
ids,
true,
);

Expand Down Expand Up @@ -1167,7 +1172,11 @@ handlers['simplefin-batch-sync'] = async function ({ ids = [] }) {
const accounts = await db.runQuery(
`SELECT a.*, b.bank_id as bankId FROM accounts a
LEFT JOIN banks b ON a.bank = b.id
WHERE a.tombstone = 0 AND a.closed = 0 ${ids.length ? `AND a.id IN (${ids.map(() => '?').join(', ')})` : ''}
WHERE
a.tombstone = 0
AND a.closed = 0
AND a.account_sync_source = 'simpleFin'
${ids.length ? `AND a.id IN (${ids.map(() => '?').join(', ')})` : ''}
ORDER BY a.offbudget, a.sort_order`,
ids.length ? ids : [],
true,
Expand Down
12 changes: 7 additions & 5 deletions packages/loot-core/src/types/server-handlers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,12 @@ export interface ServerHandlers {
'simplefin-batch-sync': ({ ids }: { ids: string[] }) => Promise<
{
accountId: string;
errors;
newTransactions;
matchedTransactions;
updatedAccounts;
res: {
errors;
newTransactions;
matchedTransactions;
updatedAccounts;
};
}[]
>;

Expand All @@ -223,7 +225,7 @@ export interface ServerHandlers {
| { error: 'failed' }
>;

'accounts-bank-sync': (arg: { id?: string }) => Promise<{
'accounts-bank-sync': (arg: { ids?: AccountEntity['id'][] }) => Promise<{
errors;
newTransactions;
matchedTransactions;
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3821.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [matt-fidd]
---

Implement SimpleFin batch sync in the API