-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxeroHelper.js
98 lines (89 loc) · 2.82 KB
/
xeroHelper.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
function getPagedResource(resourceName, xeroAuth, access_obj) {
function fetchResource(page = 1) {
return xeroAuth.get('api.xro/2.0/' + resourceName + '?page=' + String(page), access_obj)
.then((items) => {
let newItems = items[resourceName];
if (newItems.length == 100) {
return fetchResource(page + 1).then((moreItems) => {
return newItems.concat(moreItems);
});
} else {
return newItems;
}
});
}
return fetchResource;
}
function getAccountDetails(xeroAuth, access_obj) {
return xeroAuth.get('api.xro/2.0/Accounts', access_obj)
.then((acRes) => acRes.Accounts);
}
function getAccountsByCode(xeroAuth, access_obj) {
return getAccountDetails(xeroAuth, access_obj)
.then((accounts) => {
return accounts.reduce((idxObj, account) => {
if (account.Code) {
idxObj[account.Code] = account;
}
return idxObj;
}, {});
});
}
function getBankTransactions(xeroAuth, access_obj) {
return getPagedResource('BankTransactions', xeroAuth, access_obj)()
.then((trans) => trans.filter(t => t.Status != 'DELETED'));
}
function groupByContact(bank_trans) {
let contact_trans = {};
// transfers don't have contacts, so filter those out
bank_trans.filter(t => t.Contact).forEach(function(t) {
if (!contact_trans[t.Contact.ContactID]) {
contact_trans[t.Contact.ContactID] = [];
}
contact_trans[t.Contact.ContactID].push(t);
});
return contact_trans;
}
function groupByTypeAndAccount(trans) {
const res = {
spend: {},
receive: {}
};
trans.forEach((tran) => {
tran.LineItems.forEach((li) => {
if (!res[tran.Type.toLowerCase()][li.AccountCode]) {
res[tran.Type.toLowerCase()][li.AccountCode] = {
sum: 0,
transactions: []
};
}
if (res[tran.Type.toLowerCase()][li.AccountCode].transactions.indexOf(tran) === -1) {
res[tran.Type.toLowerCase()][li.AccountCode].transactions.push(tran);
}
res[tran.Type.toLowerCase()][li.AccountCode].sum += li.LineAmount;
});
});
return res;
}
function transactionByDate(a, b) {
const aDate = new Date(a.DateString);
const bDate = new Date(b.DateString);
if (aDate < bDate) {
return -1;
} else if (aDate > bDate) {
return 1;
}
return 0;
}
function getContactDetails(xeroAuth, access_obj) {
return getPagedResource('Contacts', xeroAuth, access_obj)();
}
module.exports = {
getAccountsByCode,
getBankTransactions,
groupByContact,
groupByTypeAndAccount,
transactionByDate,
getContactDetails,
};