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 4 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
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']({
id: 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
26 changes: 19 additions & 7 deletions packages/loot-core/src/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1099,17 +1099,25 @@ function handleSyncError(err, acct) {
};
}

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

if ((id && ids) || (!id && !ids)) {
throw new Error('Specify either id or ids, but not both');
}

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
${id ? 'AND a.id = ?' : ''}
${ids ? `AND a.id IN (${ids.map(() => '?').join(', ')})` : ''}
ORDER BY a.offbudget, a.sort_order`,
id ? [id] : ids || [],
true,
);

Expand Down Expand Up @@ -1162,7 +1170,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: { id?: string; ids?: string[] }) => 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