-
Notifications
You must be signed in to change notification settings - Fork 0
/
ynab.js
83 lines (80 loc) · 3.33 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const axios = require('axios');
const ynab = require("ynab");
const ynab_url = 'https://api.youneedabudget.com/v1/budgets/' + process.env.BUDGET_ID
const _ = require('lodash');
const config = {
'headers': {'Authorization': "bearer " + process.env.YNAB_TOKEN},
};
console.log(ynab_url);
module.exports = {
updateTransaction(transactionId, transaction) {
return axios.put(ynab_url + '/transactions/' + transactionId, transaction, config)
.then(function(response) {
console.log(response.data)
})
.catch(reason => {
if (reason.response.status === 409) {
console.log('Transaction already uploaded');
} else {
console.log(reason.response.status);
console.log(reason.response.data);
console.log(transaction);
console.log(transaction.transaction.subtransactions);
}
});
},
createTransaction: function (transaction) {
console.log(ynab_url + '/transactions');
return axios.post(ynab_url + '/transactions', transaction, config)
.then(function(response) {
console.log(response.data.data.transaction_ids)
})
.catch(reason => {
if (reason.response.status === 409) {
console.log('Transaction already uploaded');
} else {
console.log(reason.response.status);
console.log(reason.response.data);
console.log(transaction);
console.log(transaction.transaction.subtransactions);
}
});
},
createTransactions: function (transactions) {
console.log(ynab_url + '/transactions');
return axios.post(ynab_url + '/transactions', {
'transactions': transactions
}, config)
.then(function(response) {
console.log("Duplicate import ids: " + response.data.data.duplicate_import_ids.length);
console.log("Newly Imported Transactions: " + response.data.data.transaction_ids.length);
if (response.data.data.transaction_ids.length > 0) {
console.log(response.data.data.transaction_ids)
}
})
.catch(reason => {
if (reason.response.status === 409) {
console.log('Transaction already uploaded');
} else if (reason.response.status === 400) {
console.log(reason.response.data);
let errors = reason.response.data.error.detail.split(', ');
_.forEach(errors, function(error) {
const indexRegex = /.*\(index: (?<index>\d+)\)/;
const found = error.match(indexRegex);
console.log(error);
console.log(transactions[found.groups.index]);
})
} else {
console.log(reason.response.status);
console.log(reason.response.data);
}
});
},
getTransactions: function (sinceDate) {
const url = ynab_url + '/transactions?since_date='+sinceDate;
return axios.get(url, config)
.then(function(response) {
return response.data.data.transactions;
});
}
};