This repository has been archived by the owner on Dec 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
187 lines (175 loc) · 7.49 KB
/
index.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var async = require('async');
var _ = require('lodash');
var request = require('request');
var fs = require('fs');
var moment = require('moment-timezone');
var settings = require('./settings.json');
var j = request.jar();
var cookie = request.cookie('_splitwise_session=' + settings.splitwiseSession);
j.setCookie(cookie, 'https://secure.splitwise.com/');
var postToSlack = function(payload, done) {
if (_.isArray(payload)) {
return async.each(payload, postToSlack, done);
}
return request({
method: 'post',
url: settings.slack.webhookUrl,
json: payload
}, function(err, resp, body) {
return done(err, body);
});
};
return async.auto({
expenses: function(next) {
return request({
url: 'https://secure.splitwise.com/api/v3.0/get_expenses',
jar: j,
json: true
}, function(err, resp, body) {
return next(err, body);
});
},
currencies: function (next) {
return request({
url: 'https://secure.splitwise.com/api/v3.0/get_currencies',
jar: j,
json: true
}, function(err, resp, body) {
return next(err, body);
});
},
group: function(next) {
return request({
url: 'https://secure.splitwise.com/api/v3.0/get_groups',
jar: j,
json: true
}, function(err, resp, body) {
return next(err, body);
});
},
readState: function (next) {
return fs.readFile(settings.stateFilePath, function (err, str) {
if (err) {
if (err.code === 'ENOENT') {
console.log('State file does not exist, will create it on completion');
return next();
}
return next(err);
}
try {
return next(null, JSON.parse(str));
} catch (e) {
console.warn('Unable to parse state file, perhaps corrupted');
return next(e);
}
});
},
formatPayload: ['expenses', 'group', 'currencies', 'readState', function(next, args) {
var cMap = _.indexBy(args.currencies.currencies, 'currency_code');
var stateMap = {};
if (args.readState) {
stateMap = _.indexBy(args.readState.expenses, 'id');
}
var out = _.chain(args.expenses.expenses)
.filter(function (expense) {
// do smarter checks here later, for now just check if ID exists in state to ignore it
if (expense.deleted_at) return false;
return !stateMap[expense.id];
//return true;
})
.sortBy('created_at')
.map(function(expense) {
var currency = cMap[expense.currency_code].unit;
var description, payload;
var expenseDate = moment.tz(expense.date, settings.timezone).format(settings.dateformat);
var creator = expense.created_by.first_name + ' ' + expense.created_by
.last_name;
if (expense.creation_method === 'payment') {
description = 'A payment of ' + currency + expense.cost + ' was recorded on ' + expenseDate;
payload = {
channel: settings.slack.channel,
attachments: [{
author_name: creator,
author_icon: (expense.created_by.picture ?
expense.created_by.picture.medium : undefined),
title: currency + expense.cost + ' paid back on ' + expenseDate,
title_link: 'https://secure.splitwise.com/#/groups/' + expense.group_id,
pretext: 'New payment added',
fallback: description,
color: '#00D000',
fields: _.chain(expense.users)
.map(function (user) {
return {
title: user.user.first_name + ' ' + user.user.last_name,
value: (parseFloat(user.paid_share) > 0.0) ?
('Paid ' + currency + user.paid_share) :
('Received ' + currency + user.owed_share),
short: true
};
})
.value()
}]
};
} else {
description = creator + ' added a ' + currency + expense.cost +
' receipt for ' + expense.description + ' on ' + expenseDate;
payload = {
channel: settings.slack.channel,
attachments: [{
author_name: creator,
author_icon: (expense.created_by.picture ?
expense.created_by.picture.medium : undefined),
title: currency + expense.cost + ' for ' + expense.description + ' on ' + expenseDate,
title_link: 'https://secure.splitwise.com/#/groups/' + expense.group_id,
pretext: 'New receipt added',
fallback: description,
color: '#D00000',
fields: _.chain(expense.users)
.map(function (user) {
return {
title: user.user.first_name + ' ' + user.user.last_name,
value: (parseFloat(user.paid_share) > 0.0) ?
('Paid ' + currency + user.paid_share +
' and is owed ' + currency + user.net_balance) :
('Owes ' + currency + user.owed_share),
short: true
};
})
.value()
}]
};
if (expense.details) {
payload.attachments.push({
fallback: 'Details',
color: '#bebebe',
fields: [{
title: 'Details',
value: expense.details,
short: false
}]
});
}
}
return payload;
})
.value();
return next(null, out);
}],
saveState: ['expenses', 'formatPayload', function (next, args) {
return fs.writeFile(settings.stateFilePath, JSON.stringify(args.expenses), function (err) {
if (err) return next(err);
return next();
});
}],
sendToSlack: ['formatPayload', function(next, args) {
//return next(null, args.formatPayload);
return postToSlack(args.formatPayload, next);
}]
}, function(err, data) {
if (err) {
console.warn('Error!', err, err.message);
return;
}
console.log('payload sent: ' + JSON.stringify(data.formatPayload, null, 2));
return;
});