-
Notifications
You must be signed in to change notification settings - Fork 11
/
monzo.js
124 lines (109 loc) · 3.74 KB
/
monzo.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
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
'use strict';
const request = require('request-promise');
const _ = require('underscore');
const utils = require('./utils');
const moment = require('moment');
function requestOptionsForRoute (path, token) {
return {
url: `https://api.getmondo.co.uk${path}`,
json: true,
auth: {
bearer: token
}
};
}
module.exports = {
monzoUser (alexaRequest) {
this.authToken = utils.getAndValidiateMonzoAuthToken(alexaRequest);
if (!this.authToken) return null;
// TODO Move to config file?
const CACHE_TIMEOUTS = {
ACCOUNT: moment().subtract(2, 'minutes'),
CARDS: moment().subtract(2, 'minutes'),
// Not currently cached
TARGETS: moment().subtract(10, 'minutes'),
TRANSACTIONS: moment().subtract(2, 'minutes')
};
this.getProfile = function () {
const requestOptions = requestOptionsForRoute('/profile', this.authToken);
return request(requestOptions);
};
this.getAccounts = function () {
if (alexaRequest.attributes.accounts && moment(alexaRequest.attributes.accounts.lastFetch).isAfter(CACHE_TIMEOUTS.ACCOUNT))
return new Promise((resolve, reject) => { resolve(alexaRequest.attributes.accounts.slice()); }); // Return a "copy" of the object
const requestOptions = _.extend(
requestOptionsForRoute('/accounts', this.authToken),
{
transform2xxOnly: true,
transform (body, response, resolveWithFullResponse) {
return body.accounts;
}
});
return request(requestOptions).then((accounts) => {
accounts.lastFetch = moment().toString();
alexaRequest.attributes.accounts = accounts;
return accounts;
});
};
this.getCards = function (accountId) {
if (alexaRequest.attributes.cards && moment(alexaRequest.attributes.cards.lastFetch).isAfter(CACHE_TIMEOUTS.CARDS))
return new Promise((resolve, reject) => { resolve(alexaRequest.attributes.cards.slice()); });
const requestOptions = _.extend(
requestOptionsForRoute('/card/list', this.authToken),
{
qs: { account_id: accountId },
transform2xxOnly: true,
transform (body, response, resolveWithFullResponse) {
return body.cards;
}
});
return request(requestOptions).then((cards) => {
cards.lastFetch = moment().toString();
alexaRequest.attributes.cards = cards;
return cards;
});
};
this.setCardState = function (cardId, enabled) {
const requestOptions = _.extend(
requestOptionsForRoute('/card/toggle', this.authToken),
{
qs: {
card_id: cardId,
status: enabled ? 'ACTIVE' : 'INACTIVE'
},
method: 'PUT'
});
return request(requestOptions);
};
this.getBalance = function (accountId) {
const requestOptions = _.extend(
requestOptionsForRoute('/balance', this.authToken),
{
qs: { account_id: accountId }
});
return request(requestOptions);
};
this.getTargets = function (accountId) {
const requestOptions = _.extend(
requestOptionsForRoute('/targets', this.authToken),
{
qs: { account_id: accountId }
});
return request(requestOptions);
};
this.getTransactions = function (accountId, options) {
options = _.extend(options, { account_id: accountId });
const requestOptions = _.extend(
requestOptionsForRoute('/transactions', this.authToken),
{
qs: options,
transform2xxOnly: true,
transform (body, response, resolveWithFullResponse) {
return body.transactions;
}
});
return request(requestOptions);
};
return this;
}
};