-
Notifications
You must be signed in to change notification settings - Fork 0
/
ynab.js
59 lines (46 loc) · 1.49 KB
/
ynab.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
const ynab = require('ynab');
const {YNAB_PAYEES, YNAB_ACCOUNTS, YNAB_TOKEN, YNAB_BUDGET} = require('./secrets');
function mapTransactionVendorToPayee(vendor) {
if (vendor === null) {
return null;
}
// @see https://api.youneedabudget.com/v1#/Payees/getPayees
const payee = YNAB_PAYEES.find(({pattern}) => pattern.test(vendor));
if (!payee) return null;
return payee.id;
}
function mapTransactionToYnab(account, { amountInGr, date, vendor }) {
let accountId = YNAB_ACCOUNTS[account];
if (!accountId) {
throw new Error(`No account ID for: ${account}`);
}
let payeeId = mapTransactionVendorToPayee(vendor);
return {
account_id: accountId,
payee_id: payeeId,
memo: payeeId ? null : vendor,
cleared: ynab.SaveTransaction.ClearedEnum.Uncleared,
approved: false,
flag_color: ynab.SaveTransaction.FlagColorEnum.Yellow,
date,
// https://api.youneedabudget.com/#formats
amount: amountInGr * 10,
};
}
async function sendToYnab(account, transaction, dryRun) {
const ynabTransaction = mapTransactionToYnab(account, transaction);
console.log('Sending to YNAB:');
console.log(ynabTransaction);
if (dryRun) {
return;
}
try {
const ynabAPI = new ynab.API(YNAB_TOKEN);
await ynabAPI.transactions.createTransaction(YNAB_BUDGET, { transaction: ynabTransaction });
} catch (e) {
throw e;
}
}
module.exports = {
sendToYnab,
}