Skip to content

Commit

Permalink
Merge branch 'master' into matiss/release-reportBudget
Browse files Browse the repository at this point in the history
  • Loading branch information
MatissJanis authored Nov 18, 2024
2 parents 5fae5eb + 7010ab1 commit ef4de7c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 19 deletions.
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 @@ -1099,17 +1099,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 @@ -1162,7 +1167,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

0 comments on commit ef4de7c

Please sign in to comment.