-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
369 lines (251 loc) · 7.8 KB
/
app.js
File metadata and controls
369 lines (251 loc) · 7.8 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
Pot = new Mongo.Collection('summary'); // Single summary object
Credit = new Mongo.Collection('credit'); // Credit entries
if (Meteor.isClient) {
Meteor.subscribe('pot');
Meteor.subscribe('credit');
Template.main.helpers({
currentBalance: function() {
// Get the current balance from the pot
var pot = Pot.findOne({current: true});
// Return the balance (pence) formatted in pounds
return pot ? (pot.balance / 100).toFixed(2) : 0.00;
},
recentCredits: function() {
return Credit.find();
},
charities: function() {
return Session.get('charities');
},
localCharities: function() {
return Meteor.settings.public.localCharities;
}
});
Meteor.autosubscribe(function() {
Pot.find().observe({
added: function() {
if (Pot.findOne().balance === 0) {
sweetAlert('Thanks!', 'Your donation has been sent!', 'success');
}
}
});
});
Template.main.events({
'keydown input#search': function(event, template) {
if (event.which === 13) {
var input = template.find('#search');
var query = input.value;
HTTP.call(
'GET',
'https://api.justgiving.com/' + Meteor.settings.jgAPI + '/v1/onesearch?i=charity&limit=3&q=' + query,
{
content: 'application/json',
headers: {
accept: 'application/json'
}
},
function(err, res) {
if (err) {
console.log('Error querying Just Giving: ', err);
return;
}
var results = JSON.parse(res.content).GroupedResults[0];
var charities = results ? results.Results : [];
Session.set('charities', charities);
input.select();
}
);
}
}
});
Template.charity.events({
'click .donate': function() {
var pot = Pot.findOne({current: true});
var amount = (pot.balance / 100).toFixed(2);
var charityId = this.Id;
if (amount < 2) {
sweetAlert('Hang on', 'You can\'t donate less than £2', 'info');
} else {
sweetAlert({
title: 'Donate £' + amount + ' to ' + this.Name + '?',
type: 'info',
showCancelButton: true,
cancelButtonText: 'Close',
confirmButtonText: 'Donate!',
closeOnConfirm: true
}, function(isConfirm) {
if (isConfirm) {
Meteor.call('donate', charityId, amount);
}
});
}
},
'click .info': function() {
sweetAlert({
title: this.Name,
text: this.Description,
type: 'info',
showCancelButton: true,
cancelButtonText: 'Close',
confirmButtonText: 'Donate!',
closeOnConfirm: true
}, function(isConfirm) {
if (isConfirm) {
var pot = Pot.findOne({current: true});
var amount = (pot.balance / 100).toFixed(2);
var charityId = this.Id;
if (amount < 2) {
sweetAlert('Hang on', 'You can\'t donate less than £2', 'info');
} else {
sweetAlert({
title: 'Donate £' + amount + ' to ' + this.Name + '?',
type: 'info',
showCancelButton: true,
cancelButtonText: 'Close',
confirmButtonText: 'Donate!',
closeOnConfirm: true
}, function(isConfirm) {
if (isConfirm) {
Meteor.call('donate', charityId, amount);
}
});
}
return;
}
});
},
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
if (Pot.find().count() !== 1) {
// If we don't have a pot, create one,
// or if we have more than one remove all documents
// and then create one
Pot.remove({});
Pot.insert({
balance: 0,
previousBalance: 0,
current: true
});
}
});
Meteor.publish('pot', function() {
return Pot.find({current: true});
});
Meteor.publish('credit', function() {
return Credit.find({}, {sort: {timestamp: -1}, limit: 5});
});
Meteor.methods({
donate: function(charityId, amount) {
var sys = Npm.require('sys');
var exec = Npm.require('child_process').exec;
var path = '/Users/joshfarrant/Projects/hackference/justgiving-donate/donate.js';
var command = 'casperjs '+path+' --charity="'+charityId+'" --amount="'+amount+'"';
console.log('Donating...');
console.log('');
console.log('command: ', command);
console.log('');
exec(command, function (error, stdout, stderr) {
console.log(stdout);
console.log(stderr);
if (error) {
console.log('exec error: ' + error);
return;
} else {
console.log('Donation successful');
}
});
Pot.remove({});
Pot.insert({
balance: 0,
previousBalance: 0,
current: true
});
}
});
}
Router.route('/', function () {
this.render('main', {
data: function () {
}
});
});
Router.map(function () {
this.route('credit', {path: '/credit/',
where: 'server',
action: function(){
if (this.request.method == 'POST') {
// If we receive a POST
var body = this.request.body;
var response, httpStatus;
if (body.auth === Meteor.settings.auth) {
// If the recieved auth matches our auth
// Get the current pot
var pot = Pot.findOne({current: true});
var newBalance, addedBalance;
if (pot && body.hasOwnProperty('balance')) {
if (body.balance >= 0 && body.balance <= pot.previousBalance) {
// If the recieved balance is less than the current balance
// add the received balance to the pot balance
addedBalance = Number(body.balance);
} else {
// If the received balance is greater than the current balance
// find the difference between the received balance and the
// previous balance, then add that
addedBalance = (body.balance - pot.previousBalance);
}
newBalance = pot.balance + addedBalance;
// Update the Pot in the db with the new balance,
// as well as the previous balance received in the request
Pot.update({
current: true
}, {
$set: {
balance: newBalance,
previousBalance: body.balance
}
});
Credit.insert({
balance: addedBalance,
timestamp: Math.floor(new Date().getTime() / 1000) // Current timestamp in seconds
});
// Return a 201, along with the new balance
httpStatus = 201;
response = {
balance: newBalance
};
} else {
// If a balance is not provided
httpStatus = 400;
response = {
errors: [
{
property: 'balance',
error: 'not provided'
}
]
};
}
} else {
// If the received auth doesn't match our auth
httpStatus = 401;
response = {
errors: [
{
property: 'auth',
error: 'not valid'
}
]
};
}
// Write the response code and headers
this.response.writeHead(httpStatus, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
});
// Send the stringified response
this.response.end(JSON.stringify(response));
}
}
});
});