-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer.js
76 lines (62 loc) · 2 KB
/
transformer.js
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
const moment = require('moment');
const transform = require('stream-transform/lib/sync');
// To generate the import_id fields, we need an occurrence counter which increments for transactions
// of the same amount on the same day. This allows us to generate those occurrence indices.
const newOccurrenceProxy = () => (
new Proxy({}, {
get: (target, amountAndDate) => (amountAndDate in target ? target[amountAndDate] : 0),
})
);
let transactionCounterByAmountAndDate;
const generateImportId = (amount, date) => {
transactionCounterByAmountAndDate[amount + date] += 1;
const occurrence = transactionCounterByAmountAndDate[amount + date];
return `YNAB:${amount}:${date}:${occurrence}`;
};
const transformDate = date => (
moment(date, 'DD/MM/YYYY').format('YYYY-MM-DD')
);
const transformPayee = (payee) => {
const cardPurchaseRE = /^PURCHASE WITH CARD ([\dX]+) */;
return payee.replace(cardPurchaseRE, '').substring(0, 50);
};
const inferMemo = (cardNumber) => {
switch (cardNumber.slice(-4)) {
case '2052':
return 'Odette';
case '8020':
return 'Jim';
default:
return '';
}
};
const transformAmount = amount => (
Math.round(Number.parseFloat(amount) * 1000)
);
const sabadellToYnab = transaction => (
new exports.YNABTransaction(
transformDate(transaction.date),
transformPayee(transaction.payee),
inferMemo(transaction.cardNumber),
transformAmount(transaction.amount),
)
);
exports.resetOccurrenceCounter = () => {
transactionCounterByAmountAndDate = newOccurrenceProxy();
};
exports.resetOccurrenceCounter();
exports.YNABTransaction = function YNABTransaction(date, payee, memo, amount) {
return {
date,
payee,
memo,
amount,
cleared: 'cleared',
importId: generateImportId(amount, date),
};
};
exports.transform = (sabadellTransactions, logger) => {
const ynabTransactions = transform(sabadellTransactions, sabadellToYnab);
logger.info(`Transformed into ${ynabTransactions.length} YNAB transactions`);
return ynabTransactions;
};