-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathynab5.ts
550 lines (494 loc) · 17.6 KB
/
ynab5.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
// @ts-strict-ignore
// This is a special usage of the API because this package is embedded
// into Actual itself. We only want to pull in the methods in that
// case and ignore everything else; otherwise we'd be pulling in the
// entire backend bundle from the API
import * as actual from '@actual-app/api/methods';
import { v4 as uuidv4 } from 'uuid';
import * as monthUtils from '../../shared/months';
import { sortByKey, groupBy } from '../../shared/util';
import { CategoryGroupEntity } from '../../types/models';
import { YNAB5 } from './ynab5-types';
function amountFromYnab(amount: number) {
// ynabs multiplies amount by 1000 and actual by 100
// so, this function divides by 10
return Math.round(amount / 10);
}
function importAccounts(data: YNAB5.Budget, entityIdMap: Map<string, string>) {
return Promise.all(
data.accounts.map(async account => {
if (!account.deleted) {
const id = await actual.createAccount({
name: account.name,
offbudget: account.on_budget ? false : true,
closed: account.closed,
});
entityIdMap.set(account.id, id);
}
}),
);
}
async function importCategories(
data: YNAB5.Budget,
entityIdMap: Map<string, string>,
) {
// Hidden categories are put in its own group by YNAB,
// so it's already handled.
const categories = await actual.getCategories();
const incomeCatId = findIdByName(categories, 'Income');
const ynabIncomeCategories = ['To be Budgeted', 'Inflow: Ready to Assign'];
function checkSpecialCat(cat) {
if (
cat.category_group_id ===
findIdByName(data.category_groups, 'Internal Master Category')
) {
if (
ynabIncomeCategories.some(ynabIncomeCategory =>
equalsIgnoreCase(cat.name, ynabIncomeCategory),
)
) {
return 'income';
} else {
return 'internal';
}
} else if (
cat.category_group_id ===
findIdByName(data.category_groups, 'Credit Card Payments')
) {
return 'creditCard';
} else if (
cat.category_group_id === findIdByName(data.category_groups, 'Income')
) {
return 'income';
}
}
// Can't be done in parallel to have
// correct sort order.
for (const group of data.category_groups) {
if (!group.deleted) {
let groupId;
// Ignores internal category and credit cards
if (
!equalsIgnoreCase(group.name, 'Internal Master Category') &&
!equalsIgnoreCase(group.name, 'Credit Card Payments') &&
!equalsIgnoreCase(group.name, 'Hidden Categories') &&
!equalsIgnoreCase(group.name, 'Income')
) {
let run = true;
const MAX_RETRY = 10;
let count = 1;
const origName = group.name;
while (run) {
try {
groupId = await actual.createCategoryGroup({
name: group.name,
is_income: false,
hidden: group.hidden,
});
entityIdMap.set(group.id, groupId);
run = false;
} catch (e) {
group.name = origName + '-' + count.toString();
count += 1;
if (count >= MAX_RETRY) {
run = false;
throw Error(e.message);
}
}
}
}
if (equalsIgnoreCase(group.name, 'Income')) {
groupId = incomeCatId;
entityIdMap.set(group.id, groupId);
}
const cats = data.categories.filter(
cat => cat.category_group_id === group.id,
);
for (const cat of cats.reverse()) {
if (!cat.deleted) {
// Handles special categories. Starting balance is a payee
// in YNAB so it's handled in importTransactions
switch (checkSpecialCat(cat)) {
case 'income': {
// doesn't create new category, only assigns id
const id = incomeCatId;
entityIdMap.set(cat.id, id);
break;
}
case 'creditCard': // ignores it
case 'internal': // uncategorized is ignored too, handled by actual
break;
default: {
let run = true;
const MAX_RETRY = 10;
let count = 1;
const origName = cat.name;
while (run) {
try {
const id = await actual.createCategory({
name: cat.name,
group_id: groupId,
hidden: cat.hidden,
});
entityIdMap.set(cat.id, id);
run = false;
} catch (e) {
cat.name = origName + '-' + count.toString();
count += 1;
if (count >= MAX_RETRY) {
run = false;
throw Error(e.message);
}
}
}
}
}
}
}
}
}
}
function importPayees(data: YNAB5.Budget, entityIdMap: Map<string, string>) {
return Promise.all(
data.payees.map(async payee => {
if (!payee.deleted) {
const id = await actual.createPayee({
name: payee.name,
});
entityIdMap.set(payee.id, id);
}
}),
);
}
async function importTransactions(
data: YNAB5.Budget,
entityIdMap: Map<string, string>,
) {
const payees = await actual.getPayees();
const categories = await actual.getCategories();
const incomeCatId = findIdByName(categories, 'Income');
const startingBalanceCatId = findIdByName(categories, 'Starting Balances'); //better way to do it?
const startingPayeeYNAB = findIdByName(data.payees, 'Starting Balance');
const transactionsGrouped = groupBy(data.transactions, 'account_id');
const subtransactionsGrouped = groupBy(
data.subtransactions,
'transaction_id',
);
const payeesByTransferAcct = payees
.filter(payee => payee?.transfer_acct)
.map(payee => [payee.transfer_acct, payee] as [string, YNAB5.Payee]);
const payeeTransferAcctHashMap = new Map<string, YNAB5.Payee>(
payeesByTransferAcct,
);
const orphanTransferMap = new Map<string, YNAB5.Transaction[]>();
const orphanSubtransfer = [] as YNAB5.Subtransaction[];
const orphanSubtransferTrxId = [] as string[];
const orphanSubtransferAcctIdByTrxIdMap = new Map<string, string>();
const orphanSubtransferDateByTrxIdMap = new Map<string, string>();
// Go ahead and generate ids for all of the transactions so we can
// reliably resolve transfers
// Also identify orphan transfer transactions and subtransactions.
for (const transaction of data.subtransactions) {
entityIdMap.set(transaction.id, uuidv4());
if (transaction.transfer_account_id) {
orphanSubtransfer.push(transaction);
orphanSubtransferTrxId.push(transaction.transaction_id);
}
}
for (const transaction of data.transactions) {
entityIdMap.set(transaction.id, uuidv4());
if (
transaction.transfer_account_id &&
!transaction.transfer_transaction_id
) {
const key =
transaction.account_id + '#' + transaction.transfer_account_id;
if (!orphanTransferMap.has(key)) {
orphanTransferMap.set(key, [transaction]);
} else {
orphanTransferMap.get(key).push(transaction);
}
}
if (orphanSubtransferTrxId.includes(transaction.id)) {
orphanSubtransferAcctIdByTrxIdMap.set(
transaction.id,
transaction.account_id,
);
orphanSubtransferDateByTrxIdMap.set(transaction.id, transaction.date);
}
}
// Compute link between subtransaction transfers and orphaned transaction
// transfers. The goal is to match each transfer subtransaction to the related
// transfer transaction according to the accounts, date, amount and memo.
const orphanSubtransferMap = orphanSubtransfer.reduce(
(map, subtransaction) => {
const key =
subtransaction.transfer_account_id +
'#' +
orphanSubtransferAcctIdByTrxIdMap.get(subtransaction.transaction_id);
if (!map.has(key)) {
map.set(key, [subtransaction]);
} else {
map.get(key).push(subtransaction);
}
return map;
},
new Map<string, YNAB5.Subtransaction[]>(),
);
// The comparator will be used to order transfer transactions and their
// corresponding tranfer subtransaction in two aligned list. Hopefully
// for every list index in the transactions list, the related subtransaction
// will be at the same index.
const orphanTransferComparator = (
a: YNAB5.Transaction | YNAB5.Subtransaction,
b: YNAB5.Transaction | YNAB5.Subtransaction,
) => {
// a and b can be a YNAB5.Transaction (having a date attribute) or a
// YNAB5.Subtransaction (missing that date attribute)
const date_a =
'date' in a
? a.date
: orphanSubtransferDateByTrxIdMap.get(a.transaction_id);
const date_b =
'date' in b
? b.date
: orphanSubtransferDateByTrxIdMap.get(b.transaction_id);
// A transaction and the related subtransaction have inverted amounts.
// To have those in the same order, the subtransaction has to be reversed
// to have the same amount.
const amount_a = 'date' in a ? a.amount : -a.amount;
const amount_b = 'date' in b ? b.amount : -b.amount;
// Transaction are ordered first by date, then by amount, and lastly by memo
if (date_a > date_b) return 1;
if (date_a < date_b) return -1;
if (amount_a > amount_b) return 1;
if (amount_a < amount_b) return -1;
if (a.memo > b.memo) return 1;
if (a.memo < b.memo) return -1;
return 0;
};
const orphanTrxIdSubtrxIdMap = new Map<string, string>();
orphanTransferMap.forEach((transactions, key) => {
const subtransactions = orphanSubtransferMap.get(key);
if (subtransactions) {
transactions.sort(orphanTransferComparator);
subtransactions.sort(orphanTransferComparator);
// Iterate on the two sorted lists transactions and subtransactions and
// find matching data to identify the related transaction ids.
let transactionIdx = 0;
let subtransactionIdx = 0;
do {
switch (
orphanTransferComparator(
transactions[transactionIdx],
subtransactions[subtransactionIdx],
)
) {
case 0:
// The current list indexes are matching: the transaction and
// subtransaction are related (same date, amount and memo)
orphanTrxIdSubtrxIdMap.set(
transactions[transactionIdx].id,
entityIdMap.get(subtransactions[subtransactionIdx].id),
);
orphanTrxIdSubtrxIdMap.set(
subtransactions[subtransactionIdx].id,
entityIdMap.get(transactions[transactionIdx].id),
);
transactionIdx++;
subtransactionIdx++;
break;
case -1:
// The current list indexes are not matching:
// The current transaction is "smaller" than the current subtransaction
// (earlier date, smaller amount, memo value sorted before)
// So we advance to the next transaction and see if it match with
// the current subtransaction
transactionIdx++;
break;
case 1:
// Inverse of the previous case:
// The current subtransaction is "smaller" than the current transaction
// So we advance to the next subtransaction
subtransactionIdx++;
break;
}
} while (
transactionIdx < transactions.length &&
subtransactionIdx < subtransactions.length
);
}
});
await Promise.all(
[...transactionsGrouped.keys()].map(async accountId => {
const transactions = transactionsGrouped.get(accountId);
const toImport = transactions
.map(transaction => {
if (transaction.deleted) {
return null;
}
const subtransactions = subtransactionsGrouped.get(transaction.id);
// Add transaction
const newTransaction = {
id: entityIdMap.get(transaction.id),
account: entityIdMap.get(transaction.account_id),
date: transaction.date,
amount: amountFromYnab(transaction.amount),
category: entityIdMap.get(transaction.category_id) || null,
cleared: ['cleared', 'reconciled'].includes(transaction.cleared),
reconciled: transaction.cleared === 'reconciled',
notes: transaction.memo || null,
imported_id: transaction.import_id || null,
transfer_id:
entityIdMap.get(transaction.transfer_transaction_id) ||
orphanTrxIdSubtrxIdMap.get(transaction.id) ||
null,
subtransactions: subtransactions
? subtransactions.map(subtrans => {
return {
id: entityIdMap.get(subtrans.id),
amount: amountFromYnab(subtrans.amount),
category: entityIdMap.get(subtrans.category_id) || null,
notes: subtrans.memo,
transfer_id:
orphanTrxIdSubtrxIdMap.get(subtrans.id) || null,
payee: null,
imported_payee: null,
};
})
: null,
payee: null,
imported_payee: null,
};
// Handle transactions and subtransactions payee
const transactionPayeeUpdate = (
trx: YNAB5.Transaction | YNAB5.Subtransaction,
newTrx,
) => {
if (trx.transfer_account_id) {
const mappedTransferAccountId = entityIdMap.get(
trx.transfer_account_id,
);
newTrx.payee = payeeTransferAcctHashMap.get(
mappedTransferAccountId,
)?.id;
} else {
newTrx.payee = entityIdMap.get(trx.payee_id);
newTrx.imported_payee = data.payees.find(
p => !p.deleted && p.id === trx.payee_id,
)?.name;
}
};
transactionPayeeUpdate(transaction, newTransaction);
if (newTransaction.subtransactions) {
subtransactions.forEach(subtrans => {
const newSubtransaction = newTransaction.subtransactions.find(
newSubtrans => newSubtrans.id === entityIdMap.get(subtrans.id),
);
transactionPayeeUpdate(subtrans, newSubtransaction);
});
}
// Handle starting balances
if (
transaction.payee_id === startingPayeeYNAB &&
entityIdMap.get(transaction.category_id) === incomeCatId
) {
newTransaction.category = startingBalanceCatId;
newTransaction.payee = null;
}
return newTransaction;
})
.filter(x => x);
await actual.addTransactions(entityIdMap.get(accountId), toImport, {
learnCategories: true,
});
}),
);
}
async function importBudgets(
data: YNAB5.Budget,
entityIdMap: Map<string, string>,
) {
// There should be info in the docs to deal with
// no credit card category and how YNAB and Actual
// handle differently the amount To be Budgeted
// i.e. Actual considers the cc debt while YNAB doesn't
//
// Also, there could be a way to set rollover using
// Deferred Income Subcat and Immediate Income Subcat
const budgets = sortByKey(data.months, 'month');
const internalCatIdYnab = findIdByName(
data.category_groups,
'Internal Master Category',
);
const creditcardCatIdYnab = findIdByName(
data.category_groups,
'Credit Card Payments',
);
await actual.batchBudgetUpdates(async () => {
for (const budget of budgets) {
const month = monthUtils.monthFromDate(budget.month);
await Promise.all(
budget.categories.map(async catBudget => {
const catId = entityIdMap.get(catBudget.id);
const amount = Math.round(catBudget.budgeted / 10);
if (
!catId ||
catBudget.category_group_id === internalCatIdYnab ||
catBudget.category_group_id === creditcardCatIdYnab
) {
return;
}
await actual.setBudgetAmount(month, catId, amount);
}),
);
}
});
}
// Utils
export async function doImport(data: YNAB5.Budget) {
const entityIdMap = new Map<string, string>();
console.log('Importing Accounts...');
await importAccounts(data, entityIdMap);
console.log('Importing Categories...');
await importCategories(data, entityIdMap);
console.log('Importing Payees...');
await importPayees(data, entityIdMap);
console.log('Importing Transactions...');
await importTransactions(data, entityIdMap);
console.log('Importing Budgets...');
await importBudgets(data, entityIdMap);
console.log('Setting up...');
}
export function parseFile(buffer: Buffer): YNAB5.Budget {
let data = JSON.parse(buffer.toString());
if (data.data) {
data = data.data;
}
if (data.budget) {
data = data.budget;
}
return data;
}
export function getBudgetName(_filepath: string, data: YNAB5.Budget) {
return data.budget_name || data.name;
}
function equalsIgnoreCase(stringa: string, stringb: string): boolean {
return (
stringa.localeCompare(stringb, undefined, {
sensitivity: 'base',
}) === 0
);
}
function findByNameIgnoreCase(
categories: (YNAB5.CategoryGroup | CategoryGroupEntity)[],
name: string,
) {
return categories.find(cat => equalsIgnoreCase(cat.name, name));
}
function findIdByName(
categories: (YNAB5.CategoryGroup | CategoryGroupEntity)[],
name: string,
) {
return findByNameIgnoreCase(categories, name)?.id;
}