This repository has been archived by the owner on Jan 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
238 lines (206 loc) · 7.32 KB
/
server.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
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
// Config
var Habitat = require("habitat");
Habitat.load();
var env = new Habitat();
var config = env.get("SERVER");
config.port = env.get("PORT");
var Boom = require("boom");
var Hapi = require("hapi");
var pg = require('pg');
var pgNative = pg.native;
if(pgNative) {
pg = pgNative;
}
var paypal_total_query = `SELECT SUM(settle_amount)::numeric FROM paypal WHERE thunderbird = FALSE AND timestamp > $1 AND timestamp < $2
AND ((type IN ('Donation', 'Payment', 'Recurring Payment') AND status = 'Completed')
OR type = 'Temporary Hold');`;
var stripe_total_query = `SELECT SUM(settle_amount)::numeric FROM stripe WHERE thunderbird = FALSE AND refunded = '0.00'
AND status = 'succeeded' AND timestamp > $1 AND timestamp < $2;`;
var bycountry_query = `SELECT country_code, sum(total)::numeric AS total, sum(donors) AS donors FROM (
SELECT country_code, sum(settle_amount)::numeric AS total, count(*) AS donors FROM paypal
WHERE thunderbird = FALSE AND timestamp > $1 AND timestamp < $2
AND ((type IN ('Donation', 'Payment', 'Recurring Payment') AND status = 'Completed')
OR type = 'Temporary Hold') AND country_code IS NOT NULL
GROUP BY country_code
UNION ALL
SELECT country_code, sum(settle_amount)::numeric, count(*) FROM stripe
WHERE thunderbird = FALSE AND timestamp > $1 AND timestamp < $2
AND refunded = '0.00' AND status = 'succeeded'
GROUP BY country_code
) AS bycountry GROUP BY bycountry.country_code ORDER BY bycountry.country_code`;
var byday_query = `SELECT day, SUM(total) AS total FROM
(SELECT SUM(settle_amount)::numeric as total, date_trunc('day', timestamp) as day FROM paypal
WHERE thunderbird = FALSE AND timestamp > $1 AND timestamp < $2
AND ((type IN ('Donation', 'Payment', 'Recurring Payment') AND status = 'Completed') OR type = 'Temporary Hold')
GROUP BY date_trunc('day', timestamp)
UNION ALL
SELECT
SUM(settle_amount)::numeric as total, date_trunc('day', timestamp) as day FROM stripe
WHERE thunderbird = FALSE AND timestamp > $1 AND timestamp < $2
AND refunded = '0.00' AND status = 'succeeded'
GROUP BY date_trunc('day', timestamp)
) as combined GROUP BY day ORDER BY day`;
var server = new Hapi.Server({
app: {
stripe_secret: config.stripe_secret
},
cache: require("catbox-memory")
});
server.connection({
host: config.host,
port: config.port
});
server.register(require("hapi-auth-bearer-token"), function(err) {});
server.auth.strategy("stripe", "bearer-access-token", {
validateFunc: function(token, callback) {
// this = request
callback(null, token === this.server.settings.app.stripe_secret, { token: token });
},
accessTokenName: "token"
});
server.method("total", function(start_date, end_date, next) {
pg.connect(config.db_connection_string, function(pool_error, client, pg_done) {
if (pool_error) {
return next(Boom.badImplementation("A database pool connection error occurred", pool_error));
}
client.query(paypal_total_query, [start_date, end_date], function(paypal_error, paypal_result) {
if (paypal_error) {
return next(Boom.badImplementation("A paypal database query error occurred", paypal_error));
}
client.query(stripe_total_query, [start_date, end_date], function(stripe_error, stripe_result) {
pg_done();
if (stripe_error) {
return next(Boom.badImplementation("A stripe database query error occurred", stripe_error));
}
next(null, {
sum: parseFloat(paypal_result.rows[0].sum, 10) + parseFloat(stripe_result.rows[0].sum, 10)
});
});
});
});
}, {
cache: {
expiresIn: 10 * 1000,
generateTimeout: 2 * 1000
}
});
server.method("total_by_country", (start_date, end_date, next) => {
pg.connect(config.db_connection_string, (pool_error, client, pg_done) => {
if (pool_error) {
return next(Boom.badImplementation("A database pool connection error occurred", pool_error));
}
client.query(bycountry_query, [start_date, end_date], (bycountry_error, bycountry_result) => {
pg_done();
if (bycountry_error) {
return next(Boom.badImplementation("A bycountry database query error occurred", bycountry_error));
}
next(null, bycountry_result.rows);
});
});
}, {
cache: {
expiresIn: 10 * 1000,
generateTimeout: 2 * 1000
}
});
server.method("total_by_day", (start_date, end_date, next) => {
pg.connect(config.db_connection_string, (pool_error, client, pg_done) => {
if (pool_error) {
return next(Boom.badImplementation("A database pool connection error occurred", pool_error));
}
client.query(byday_query, [start_date, end_date], (byday_error, byday_result) => {
pg_done();
if (byday_error) {
return next(Boom.badImplementation("A byday database query error occurred", byday_error));
}
next(null, byday_result.rows);
});
});
}, {
cache: {
expiresIn: 10 * 1000,
generateTimeout: 2 * 1000
}
});
server.route({
method: "GET",
path: "/",
handler: function(request, reply) {
reply({
total_url: `${request.server.info.uri}/eoy-2016-total`,
total_bycountry_url: `${request.server.info.uri}/eoy-2016-bycountry`,
total_byday_url: `${request.server.info.uri}/eoy-byday`
});
}
});
server.route({
method: "GET",
path: "/eoy-2016-bycountry",
handler: function(request, reply) {
server.methods.total_by_country(config.start_date, config.end_date, (query_error, results) => {
if (query_error) {
return reply(query_error);
}
reply(results.map((r) => {
return {
country_code: r.country_code,
sum: parseFloat(r.total, 10),
count: parseInt(r.donors, 10)
};
}));
});
},
config: {
cors: true,
jsonp: "callback"
}
});
server.route({
method: "GET",
path: "/eoy-byday",
handler: function(request, reply) {
server.methods.total_by_day(config.start_date, config.end_date, (query_error, results) => {
if (query_error) {
return reply(query_error);
}
reply(results.map((r) => {
return {
day: r.day,
sum: parseFloat(r.total, 10)
};
}));
});
},
config: {
cors: true,
jsonp: "callback"
}
});
server.route({
method: "GET",
path: "/eoy-2016-total",
handler: function(request, reply) {
server.methods.total(config.start_date, config.end_date, (query_error, results) => {
if (query_error) {
return reply(query_error);
}
reply(results);
});
},
config: {
cors: true,
jsonp: "callback"
}
});
// set up webhook url as `https://{server_uri}/stripe/callback?token={stripe_secret}` using the stripe dashboard
server.route({
method: "POST",
path: "/stripe/callback",
config: {
auth: "stripe"
},
handler: require("./stripe.js")
});
server.start(function() {
console.log('Server running at: %s', server.info.uri);
});