-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
sync.ts
878 lines (759 loc) · 24 KB
/
sync.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
// @ts-strict-ignore
import * as dateFns from 'date-fns';
import { v4 as uuidv4 } from 'uuid';
import * as asyncStorage from '../../platform/server/asyncStorage';
import * as monthUtils from '../../shared/months';
import { q } from '../../shared/query';
import {
makeChild as makeChildTransaction,
recalculateSplit,
} from '../../shared/transactions';
import {
hasFieldsChanged,
amountToInteger,
integerToAmount,
} from '../../shared/util';
import {
AccountEntity,
BankSyncResponse,
SimpleFinBatchSyncResponse,
TransactionEntity,
} from '../../types/models';
import { runQuery } from '../aql';
import * as db from '../db';
import { runMutator } from '../mutators';
import { post } from '../post';
import { getServer } from '../server-config';
import { batchMessages } from '../sync';
import { getStartingBalancePayee } from './payees';
import { title } from './title';
import { runRules } from './transaction-rules';
import { batchUpdateTransactions } from './transactions';
function BankSyncError(type: string, code: string) {
return { type: 'BankSyncError', category: type, code };
}
function makeSplitTransaction(trans, subtransactions) {
// We need to calculate the final state of split transactions
const { subtransactions: sub, ...parent } = recalculateSplit({
...trans,
is_parent: true,
subtransactions: subtransactions.map((transaction, idx) =>
makeChildTransaction(trans, {
...transaction,
sort_order: 0 - idx,
}),
),
});
return [parent, ...sub];
}
function getAccountBalance(account) {
// Debt account types need their balance reversed
switch (account.type) {
case 'credit':
case 'loan':
return -account.balances.current;
default:
return account.balances.current;
}
}
async function updateAccountBalance(id, balance) {
await db.runQuery('UPDATE accounts SET balance_current = ? WHERE id = ?', [
amountToInteger(balance),
id,
]);
}
async function getAccountOldestTransaction(id): Promise<TransactionEntity> {
return (
await runQuery(
q('transactions')
.filter({
account: id,
date: { $lte: monthUtils.currentDay() },
})
.select('date')
.orderBy('date')
.limit(1),
)
).data?.[0];
}
async function getAccountSyncStartDate(id) {
// Many GoCardless integrations do not support getting more than 90 days
// worth of data, so make that the earliest possible limit.
const dates = [monthUtils.subDays(monthUtils.currentDay(), 90)];
const oldestTransaction = await getAccountOldestTransaction(id);
if (oldestTransaction) dates.push(oldestTransaction.date);
return monthUtils.dayFromDate(
dateFns.max(dates.map(d => monthUtils.parseDate(d))),
);
}
export async function getGoCardlessAccounts(userId, userKey, id) {
const userToken = await asyncStorage.getItem('user-token');
if (!userToken) return;
const res = await post(
getServer().GOCARDLESS_SERVER + '/accounts',
{
userId,
key: userKey,
item_id: id,
},
{
'X-ACTUAL-TOKEN': userToken,
},
);
const { accounts } = res;
accounts.forEach(acct => {
acct.balances.current = getAccountBalance(acct);
});
return accounts;
}
async function downloadGoCardlessTransactions(
userId,
userKey,
acctId,
bankId,
since,
includeBalance = true,
) {
const userToken = await asyncStorage.getItem('user-token');
if (!userToken) return;
console.log('Pulling transactions from GoCardless');
const res = await post(
getServer().GOCARDLESS_SERVER + '/transactions',
{
userId,
key: userKey,
requisitionId: bankId,
accountId: acctId,
startDate: since,
includeBalance,
},
{
'X-ACTUAL-TOKEN': userToken,
},
);
if (res.error_code) {
throw BankSyncError(res.error_type, res.error_code);
}
if (includeBalance) {
const {
transactions: { all },
balances,
startingBalance,
} = res;
console.log('Response:', res);
return {
transactions: all,
accountBalance: balances,
startingBalance,
};
} else {
console.log('Response:', res);
return {
transactions: res.transactions.all,
};
}
}
async function downloadSimpleFinTransactions(
acctId: AccountEntity['id'] | AccountEntity['id'][],
since: string | string[],
) {
const userToken = await asyncStorage.getItem('user-token');
if (!userToken) return;
const batchSync = Array.isArray(acctId);
console.log('Pulling transactions from SimpleFin');
const res = await post(
getServer().SIMPLEFIN_SERVER + '/transactions',
{
accountId: acctId,
startDate: since,
},
{
'X-ACTUAL-TOKEN': userToken,
},
60000,
);
if (res.error_code) {
throw BankSyncError(res.error_type, res.error_code);
}
let retVal = {};
if (batchSync) {
for (const [accountId, data] of Object.entries(
res as SimpleFinBatchSyncResponse,
)) {
if (accountId === 'errors') continue;
const error = res?.errors?.[accountId]?.[0];
retVal[accountId] = {
transactions: data?.transactions?.all,
accountBalance: data?.balances,
startingBalance: data?.startingBalance,
};
if (error) {
retVal[accountId].error_type = error.error_type;
retVal[accountId].error_code = error.error_code;
}
}
} else {
const singleRes = res as BankSyncResponse;
retVal = {
transactions: singleRes.transactions.all,
accountBalance: singleRes.balances,
startingBalance: singleRes.startingBalance,
};
}
console.log('Response:', retVal);
return retVal;
}
async function resolvePayee(trans, payeeName, payeesToCreate) {
if (trans.payee == null && payeeName) {
// First check our registry of new payees (to avoid a db access)
// then check the db for existing payees
let payee = payeesToCreate.get(payeeName.toLowerCase());
payee = payee || (await db.getPayeeByName(payeeName));
if (payee != null) {
return payee.id;
} else {
// Otherwise we're going to create a new one
const newPayee = { id: uuidv4(), name: payeeName };
payeesToCreate.set(payeeName.toLowerCase(), newPayee);
return newPayee.id;
}
}
return trans.payee;
}
async function normalizeTransactions(
transactions,
acctId,
{ rawPayeeName = false } = {},
) {
const payeesToCreate = new Map();
const normalized = [];
for (let trans of transactions) {
// Validate the date because we do some stuff with it. The db
// layer does better validation, but this will give nicer errors
if (trans.date == null) {
throw new Error('`date` is required when adding a transaction');
}
// Strip off the irregular properties
const { payee_name: originalPayeeName, subtransactions, ...rest } = trans;
trans = rest;
let payee_name = originalPayeeName;
if (payee_name) {
const trimmed = payee_name.trim();
if (trimmed === '') {
payee_name = null;
} else {
payee_name = rawPayeeName ? trimmed : title(trimmed);
}
}
trans.imported_payee = trans.imported_payee || payee_name;
if (trans.imported_payee) {
trans.imported_payee = trans.imported_payee.trim();
}
// It's important to resolve both the account and payee early so
// when rules are run, they have the right data. Resolving payees
// also simplifies the payee creation process
trans.account = acctId;
trans.payee = await resolvePayee(trans, payee_name, payeesToCreate);
trans.category = trans.category ?? null;
normalized.push({
payee_name,
subtransactions: subtransactions
? subtransactions.map(t => ({ ...t, account: acctId }))
: null,
trans,
});
}
return { normalized, payeesToCreate };
}
async function normalizeBankSyncTransactions(transactions, acctId) {
const payeesToCreate = new Map();
const normalized = [];
for (const trans of transactions) {
if (!trans.amount) {
trans.amount = trans.transactionAmount.amount;
}
// Validate the date because we do some stuff with it. The db
// layer does better validation, but this will give nicer errors
if (trans.date == null) {
throw new Error('`date` is required when adding a transaction');
}
if (trans.payeeName == null) {
throw new Error('`payeeName` is required when adding a transaction');
}
trans.imported_payee = trans.imported_payee || trans.payeeName;
if (trans.imported_payee) {
trans.imported_payee = trans.imported_payee.trim();
}
// It's important to resolve both the account and payee early so
// when rules are run, they have the right data. Resolving payees
// also simplifies the payee creation process
trans.account = acctId;
trans.payee = await resolvePayee(trans, trans.payeeName, payeesToCreate);
trans.cleared = Boolean(trans.booked);
const notes =
trans.remittanceInformationUnstructured ||
(trans.remittanceInformationUnstructuredArray || []).join(', ');
normalized.push({
payee_name: trans.payeeName,
trans: {
amount: amountToInteger(trans.amount),
payee: trans.payee,
account: trans.account,
date: trans.date,
notes: notes.trim().replace('#', '##'),
category: trans.category ?? null,
imported_id: trans.transactionId,
imported_payee: trans.imported_payee,
cleared: trans.cleared,
},
});
}
return { normalized, payeesToCreate };
}
async function createNewPayees(payeesToCreate, addsAndUpdates) {
const usedPayeeIds = new Set(addsAndUpdates.map(t => t.payee));
await batchMessages(async () => {
for (const payee of payeesToCreate.values()) {
// Only create the payee if it ended up being used
if (usedPayeeIds.has(payee.id)) {
await db.insertPayee(payee);
}
}
});
}
export async function reconcileTransactions(
acctId,
transactions,
isBankSyncAccount = false,
strictIdChecking = true,
isPreview = false,
) {
console.log('Performing transaction reconciliation');
const updated = [];
const added = [];
const updatedPreview = [];
const existingPayeeMap = new Map<string, string>();
const {
payeesToCreate,
transactionsStep1,
transactionsStep2,
transactionsStep3,
} = await matchTransactions(
acctId,
transactions,
isBankSyncAccount,
strictIdChecking,
);
// Finally, generate & commit the changes
for (const { trans, subtransactions, match } of transactionsStep3) {
if (match && !trans.forceAddTransaction) {
// Skip updating already reconciled (locked) transactions
if (match.reconciled) {
updatedPreview.push({ transaction: trans, ignored: true });
continue;
}
// TODO: change the above sql query to use aql
const existing = {
...match,
cleared: match.cleared === 1,
date: db.fromDateRepr(match.date),
};
// Update the transaction
const updates = {
imported_id: trans.imported_id || null,
payee: existing.payee || trans.payee || null,
category: existing.category || trans.category || null,
imported_payee: trans.imported_payee || null,
notes: existing.notes || trans.notes || null,
cleared: trans.cleared != null ? trans.cleared : true,
};
if (hasFieldsChanged(existing, updates, Object.keys(updates))) {
updated.push({ id: existing.id, ...updates });
if (!existingPayeeMap.has(existing.payee)) {
const payee = await db.getPayee(existing.payee);
existingPayeeMap.set(existing.payee, payee?.name);
}
existing.payee_name = existingPayeeMap.get(existing.payee);
existing.amount = integerToAmount(existing.amount);
updatedPreview.push({ transaction: trans, existing });
} else {
updatedPreview.push({ transaction: trans, ignored: true });
}
if (existing.is_parent && existing.cleared !== updates.cleared) {
const children = await db.all(
'SELECT id FROM v_transactions WHERE parent_id = ?',
[existing.id],
);
for (const child of children) {
updated.push({ id: child.id, cleared: updates.cleared });
}
}
} else {
// Insert a new transaction
const { forceAddTransaction, ...newTrans } = trans;
const finalTransaction = {
...newTrans,
id: uuidv4(),
category: trans.category || null,
cleared: trans.cleared != null ? trans.cleared : true,
};
if (subtransactions && subtransactions.length > 0) {
added.push(...makeSplitTransaction(finalTransaction, subtransactions));
} else {
added.push(finalTransaction);
}
}
}
// Maintain the sort order of the server
const now = Date.now();
added.forEach((t, index) => {
t.sort_order ??= now - index;
});
if (!isPreview) {
await createNewPayees(payeesToCreate, [...added, ...updated]);
await batchUpdateTransactions({ added, updated });
}
console.log('Debug data for the operations:', {
transactionsStep1,
transactionsStep2,
transactionsStep3,
added,
updated,
updatedPreview,
});
return {
added: added.map(trans => trans.id),
updated: updated.map(trans => trans.id),
updatedPreview,
};
}
export async function matchTransactions(
acctId,
transactions,
isBankSyncAccount = false,
strictIdChecking = true,
) {
console.log('Performing transaction reconciliation matching');
const hasMatched = new Set();
const transactionNormalization = isBankSyncAccount
? normalizeBankSyncTransactions
: normalizeTransactions;
const { normalized, payeesToCreate } = await transactionNormalization(
transactions,
acctId,
);
// The first pass runs the rules, and preps data for fuzzy matching
const transactionsStep1 = [];
for (const {
payee_name,
trans: originalTrans,
subtransactions,
} of normalized) {
// Run the rules
const trans = await runRules(originalTrans);
let match = null;
let fuzzyDataset = null;
// First, match with an existing transaction's imported_id. This
// is the highest fidelity match and should always be attempted
// first.
if (trans.imported_id) {
match = await db.first(
'SELECT * FROM v_transactions WHERE imported_id = ? AND account = ?',
[trans.imported_id, acctId],
);
if (match) {
hasMatched.add(match.id);
}
}
// If it didn't match, query data needed for fuzzy matching
if (!match) {
// Fuzzy matching looks 7 days ahead and 7 days back. This
// needs to select all fields that need to be read from the
// matched transaction. See the final pass below for the needed
// fields.
const sevenDaysBefore = db.toDateRepr(monthUtils.subDays(trans.date, 7));
const sevenDaysAfter = db.toDateRepr(monthUtils.addDays(trans.date, 7));
// strictIdChecking has the added behaviour of only matching on transactions with no import ID
// if the transaction being imported has an import ID.
if (strictIdChecking) {
fuzzyDataset = await db.all(
`SELECT id, is_parent, date, imported_id, payee, imported_payee, category, notes, reconciled, cleared, amount
FROM v_transactions
WHERE
-- If both ids are set, and we didn't match earlier then skip dedup
(imported_id IS NULL OR ? IS NULL)
AND date >= ? AND date <= ? AND amount = ?
AND account = ?`,
[
trans.imported_id || null,
sevenDaysBefore,
sevenDaysAfter,
trans.amount || 0,
acctId,
],
);
} else {
fuzzyDataset = await db.all(
`SELECT id, is_parent, date, imported_id, payee, imported_payee, category, notes, reconciled, cleared, amount
FROM v_transactions
WHERE date >= ? AND date <= ? AND amount = ? AND account = ?`,
[sevenDaysBefore, sevenDaysAfter, trans.amount || 0, acctId],
);
}
// Sort the matched transactions according to the distance from the original
// transactions date. i.e. if the original transaction is in 21-02-2024 and
// the matched transactions are: 20-02-2024, 21-02-2024, 29-02-2024 then
// the resulting data-set should be: 21-02-2024, 20-02-2024, 29-02-2024.
fuzzyDataset = fuzzyDataset.sort((a, b) => {
const aDistance = Math.abs(
dateFns.differenceInMilliseconds(
dateFns.parseISO(trans.date),
dateFns.parseISO(db.fromDateRepr(a.date)),
),
);
const bDistance = Math.abs(
dateFns.differenceInMilliseconds(
dateFns.parseISO(trans.date),
dateFns.parseISO(db.fromDateRepr(b.date)),
),
);
return aDistance > bDistance ? 1 : -1;
});
}
transactionsStep1.push({
payee_name,
trans,
subtransactions: trans.subtransactions || subtransactions,
match,
fuzzyDataset,
});
}
// Next, do the fuzzy matching. This first pass matches based on the
// payee id. We do this in multiple passes so that higher fidelity
// matching always happens first, i.e. a transaction should match
// match with low fidelity if a later transaction is going to match
// the same one with high fidelity.
const transactionsStep2 = transactionsStep1.map(data => {
if (!data.match && data.fuzzyDataset) {
// Try to find one where the payees match.
const match = data.fuzzyDataset.find(
row => !hasMatched.has(row.id) && data.trans.payee === row.payee,
);
if (match) {
hasMatched.add(match.id);
return { ...data, match };
}
}
return data;
});
// The final fuzzy matching pass. This is the lowest fidelity
// matching: it just find the first transaction that hasn't been
// matched yet. Remember the dataset only contains transactions
// around the same date with the same amount.
const transactionsStep3 = transactionsStep2.map(data => {
if (!data.match && data.fuzzyDataset) {
const match = data.fuzzyDataset.find(row => !hasMatched.has(row.id));
if (match) {
hasMatched.add(match.id);
return { ...data, match };
}
}
return data;
});
return {
payeesToCreate,
transactionsStep1,
transactionsStep2,
transactionsStep3,
};
}
// This is similar to `reconcileTransactions` except much simpler: it
// does not try to match any transactions. It just adds them
export async function addTransactions(
acctId,
transactions,
{ runTransfers = true, learnCategories = false } = {},
) {
const added = [];
const { normalized, payeesToCreate } = await normalizeTransactions(
transactions,
acctId,
{ rawPayeeName: true },
);
for (const { trans: originalTrans, subtransactions } of normalized) {
// Run the rules
const trans = await runRules(originalTrans);
const finalTransaction = {
id: uuidv4(),
...trans,
account: acctId,
cleared: trans.cleared != null ? trans.cleared : true,
};
// Add split transactions if they are given
const updatedSubtransactions =
finalTransaction.subtransactions || subtransactions;
if (updatedSubtransactions && updatedSubtransactions.length > 0) {
added.push(
...makeSplitTransaction(finalTransaction, updatedSubtransactions),
);
} else {
added.push(finalTransaction);
}
}
await createNewPayees(payeesToCreate, added);
let newTransactions;
if (runTransfers || learnCategories) {
const res = await batchUpdateTransactions({
added,
learnCategories,
runTransfers,
});
newTransactions = res.added.map(t => t.id);
} else {
await batchMessages(async () => {
newTransactions = await Promise.all(
added.map(async trans => db.insertTransaction(trans)),
);
});
}
return newTransactions;
}
async function processBankSyncDownload(
download,
id,
acctRow,
initialSync = false,
) {
// If syncing an account from sync source it must not use strictIdChecking. This allows
// the fuzzy search to match transactions where the import IDs are different. It is a known quirk
// that account sync sources can give two different transaction IDs even though it's the same transaction.
const useStrictIdChecking = !acctRow.account_sync_source;
if (initialSync) {
const { transactions } = download;
let balanceToUse = download.startingBalance;
if (acctRow.account_sync_source === 'simpleFin') {
const currentBalance = download.startingBalance;
const previousBalance = transactions.reduce((total, trans) => {
return (
total - parseInt(trans.transactionAmount.amount.replace('.', ''))
);
}, currentBalance);
balanceToUse = previousBalance;
}
const oldestTransaction = transactions[transactions.length - 1];
const oldestDate =
transactions.length > 0
? oldestTransaction.date
: monthUtils.currentDay();
const payee = await getStartingBalancePayee();
return runMutator(async () => {
const initialId = await db.insertTransaction({
account: id,
amount: balanceToUse,
category: acctRow.offbudget === 0 ? payee.category : null,
payee: payee.id,
date: oldestDate,
cleared: true,
starting_balance_flag: true,
});
const result = await reconcileTransactions(
id,
transactions,
true,
useStrictIdChecking,
);
return {
...result,
added: [initialId, ...result.added],
};
});
}
const { transactions: originalTransactions, accountBalance } = download;
if (originalTransactions.length === 0) {
return { added: [], updated: [] };
}
const transactions = originalTransactions.map(trans => ({
...trans,
account: id,
}));
return runMutator(async () => {
const result = await reconcileTransactions(
id,
transactions,
true,
useStrictIdChecking,
);
if (accountBalance) await updateAccountBalance(id, accountBalance);
return result;
});
}
export async function syncAccount(
userId: string,
userKey: string,
id: string,
acctId: string,
bankId: string,
) {
const acctRow = await db.select('accounts', id);
const syncStartDate = await getAccountSyncStartDate(id);
const oldestTransaction = await getAccountOldestTransaction(id);
const newAccount = oldestTransaction == null;
let download;
if (acctRow.account_sync_source === 'simpleFin') {
download = await downloadSimpleFinTransactions(acctId, syncStartDate);
} else if (acctRow.account_sync_source === 'goCardless') {
download = await downloadGoCardlessTransactions(
userId,
userKey,
acctId,
bankId,
syncStartDate,
newAccount,
);
} else {
throw new Error(
`Unrecognized bank-sync provider: ${acctRow.account_sync_source}`,
);
}
return processBankSyncDownload(download, id, acctRow, newAccount);
}
export async function SimpleFinBatchSync(
accounts: {
id: AccountEntity['id'];
accountId: AccountEntity['account_id'];
}[],
) {
const startDates = await Promise.all(
accounts.map(async a => getAccountSyncStartDate(a.id)),
);
const res = await downloadSimpleFinTransactions(
accounts.map(a => a.accountId),
startDates,
);
const promises = [];
for (let i = 0; i < accounts.length; i++) {
const account = accounts[i];
const download = res[account.accountId];
const acctRow = await db.select('accounts', account.id);
const oldestTransaction = await getAccountOldestTransaction(account.id);
const newAccount = oldestTransaction == null;
if (download.error_code) {
promises.push(
Promise.resolve({
accountId: account.id,
res: download,
}),
);
continue;
}
promises.push(
processBankSyncDownload(download, account.id, acctRow, newAccount).then(
res => ({
accountId: account.id,
res,
}),
),
);
}
return await Promise.all(promises);
}