-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
424 lines (406 loc) · 10.9 KB
/
server.js
File metadata and controls
424 lines (406 loc) · 10.9 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Dependencies.
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const bodyParser = require('body-parser');
const pgp = require('pg-promise')();
const bcrypt = require('bcrypt');
require('dotenv').config();
// Server and socket.io.
const app = express();
const server = http.Server(app);
const io = socketIo(server);
// Heroku and localhost database.
const db = pgp({
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 5432,
database: process.env.DB_NAME,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
});
// bcrypt salt rounds.
const saltRounds = 10;
app.use(bodyParser.json());
app.use('/static', express.static('static'));
app.set('view engine', 'hbs');
app.get('/', (req, res) => {
res.render('index');
});
// Create user endpoint.
app.post('/api/new-user', (req, res) => {
const { username, password, email, phone, avatar } = req.body;
bcrypt.hash(password, saltRounds, (err, hash) => {
db.one(
`
INSERT INTO "user" (username, password, email, phone, avatar)
VALUES ($1, $2, $3, $4, $5)
RETURNING id
`,
[username, hash, email, phone, avatar],
)
.then((userId) => {
res.json({
status: 200,
data: userId,
});
})
.catch((error) => {
res.status(401).json({
status: 401,
message: 'E-mail address already registered to another user',
details: error,
});
});
});
});
// User login endpoint.
app.post('/api/login', (req, res) => {
const { email, password } = req.body;
db.oneOrNone('SELECT * FROM "user" WHERE email = $1', [email])
.then((user) => {
if (!user) {
res.status(404).json({
status: 404,
message: 'Incorrect e-mail',
});
} else {
bcrypt.compare(password, user.password, (err, result) => {
if (result) {
res.json({
status: 200,
data: user,
});
} else {
res.status(401).json({
status: 401,
message: 'Incorrect password',
details: err,
});
}
});
}
})
.catch((error) => {
console.log(error);
res.json({ error });
});
});
// Create a new round instance.
app.post('/api/new-round', (req, res) => {
const { buyerId, recipients, roundName } = req.body;
db.one(
`
INSERT INTO round (user_id, name, time)
VALUES ($1, $2, now())
RETURNING id
`,
[buyerId, roundName],
)
.then((data) => {
const roundId = data.id;
return Promise.all(
Object.keys(recipients)
.map((recipientId) => [
db.one(
`
INSERT INTO transaction (user_id, counterpart_id, round_id, amount, type, time)
VALUES ($1, $2, $3, $4, 'round', now())
RETURNING round_id
`,
[buyerId, recipientId, roundId, -recipients[recipientId].amount],
),
db.one(
`
INSERT INTO transaction (user_id, counterpart_id, round_id, amount, type, time)
VALUES ($1, $2, $3, $4, $5, now())
RETURNING round_id
`,
[
recipientId,
buyerId,
roundId,
recipients[recipientId].amount,
buyerId === parseInt(recipientId) ? 'self' : 'round',
],
),
db.one(
`
INSERT INTO round_user (round_id, counterpart_id)
VALUES ($1, $2)
RETURNING round_id
`,
[roundId, recipientId],
),
])
.reduce((a, b) => a.concat(b)),
);
})
.then((data) => {
const roundId = data[0].round_id;
res.json({
status: 200,
data: { roundId },
});
})
.then(() => io.emit('refresh'))
.catch((error) => {
console.log(error);
res.json({ error });
});
});
// Get relevent balances of a users contacts.
app.get('/api/get-balances/:userId', (req, res) => {
const userId = parseInt(req.params.userId);
db.any(
`
(
SELECT "user".username, counterpart_id, SUM(amount)
FROM transaction, "user"
WHERE user_id = $1
AND transaction.counterpart_id = "user".id
GROUP BY counterpart_id, "user".username)
UNION ALL
(
SELECT "user".username, contact_id, 0.00 as sum
FROM contact_user, "user"
WHERE contact_user.user_id = $1
AND contact_user.contact_id = "user".id
AND contact_user.contact_id NOT IN (
SELECT DISTINCT counterpart_id FROM transaction
WHERE transaction.user_id = contact_user.user_id
)
)
`,
[userId],
)
.then((data) => {
if (!data.length) {
res.status(404).json({
status: 404,
message: 'No data available',
});
} else {
const balances = {};
data.map((balance) => Object.assign(balances, { [balance.counterpart_id]: balance }));
res.json({
status: 200,
data: { balances },
});
}
})
.catch((error) => {
console.log(error);
res.json({ error });
});
});
// Approve a new contact.
app.post('/api/approve-contact', (req, res) => {
const { userId, contactId } = req.body;
db.one(
`
UPDATE contact_user
SET approved = true
WHERE user_id = $1
AND contact_id = $2
RETURNING approved;
`,
[userId, contactId],
)
.then((data) => {
res.json({
status: 200,
data,
});
})
.catch((error) => {
console.log(error);
res.json({ error });
});
});
// Import a users contacts.
app.get('/api/get-contacts/:userId', (req, res) => {
const { userId } = req.params;
db.any(
`
SELECT contact_id, username, email, phone, avatar, contact_user.approved
FROM contact_user, "user"
WHERE contact_id = "user".id
AND user_id = $1;
`,
[userId],
)
.then((data) => {
if (!data.length) {
res.status(404).json({
status: 404,
message: 'No data available',
});
} else {
res.json({
status: 200,
data,
});
}
})
.catch((error) => {
console.log(error);
res.json({ error });
});
});
// Search for a user to add as a contact.
app.get('/api/get-contact/:username', (req, res) => {
const username = `${req.params.username.toLowerCase()}%`;
db.any(
`
SELECT id, username, avatar, email
FROM "user"
WHERE lower(username) LIKE $1;
`,
[username],
)
.then((user) => {
if (!user.length) {
res.status(404).json({
status: 404,
message: 'User not found',
});
} else {
res.json({
status: 200,
data: { user },
});
}
})
.catch((error) => {
console.log(error);
res.json({ error });
});
});
// Add a new contact.
app.post('/api/add-contact', (req, res) => {
const { userId, contactId } = req.body;
Promise.all([
db.none(
`
INSERT INTO contact_user (user_id, contact_id, approved)
VALUES ($1, $2, true)
`,
[userId, contactId],
),
db.none(
`
INSERT INTO contact_user (user_id, contact_id, approved)
VALUES ($1, $2, false)
`,
[contactId, userId],
),
])
.then(() => {
res.json({ status: 200 });
})
.then(() => io.emit('refresh'))
.catch((error) =>
res.status(400).json({
status: 400,
message: 'Error while adding user to contacts',
details: error,
}),
);
});
// Make a payment to a contact - currently sets balance between two contacts to £0.00.
app.post('/api/make-payment', (req, res) => {
const payerId = parseInt(req.body.payerId);
const receiverId = parseInt(req.body.receiverId);
const amount = Number(req.body.amount).toFixed(2);
Promise.all([
db.none(
`INSERT INTO transaction (user_id, counterpart_id, amount, type, time)
VALUES ($1, $2, $3, 'payment', now())`,
[payerId, receiverId, -amount],
),
db.none(
`INSERT INTO transaction (user_id, counterpart_id, amount, type, time)
VALUES ($1, $2, $3, 'payment', now())`,
[receiverId, payerId, amount],
),
])
.then(() => {
res.json({ status: 200 });
})
.then(() => io.emit('refresh'))
.catch((error) =>
res.status(400).json({
status: 400,
message: 'Error while making payment',
details: error,
}),
);
});
// Fetches all rounds that the user has bought and rounds in which they have been a counterpart.
app.get('/api/get-rounds/:userId', (req, res) => {
const { userId } = req.params;
db.any(
`SELECT round_id FROM round_user WHERE counterpart_id = $1
UNION
SELECT id FROM "round" WHERE user_id = $1 `,
[userId],
)
.then((response) => {
const promisesArray = response.map((round) =>
db.any(
`
SELECT transaction.id, transaction.user_id, transaction.counterpart_id,
transaction.round_id, transaction.amount, transaction.type, round.name,
transaction.time, "user".username
FROM transaction, round, "user"
WHERE round_id = ${round.round_id}
AND amount < 0
AND transaction.round_id = round.id
AND "user".id = transaction.counterpart_id
`,
),
);
return Promise.all(promisesArray);
})
.then((response) => {
const roundStore = response
.map((round) => {
const reducedRound = round.reduce((acc, curr) => {
const counterparts = !acc.counterparts
? { [curr.counterpart_id]: { username: curr.username, amount: curr.amount } }
: acc.counterparts;
acc = {
roundId: curr.round_id,
roundName: curr.name,
userId: curr.user_id,
counterparts: Object.assign({}, counterparts, {
[curr.counterpart_id]: {
username: curr.username,
id: curr.counterpart_id,
amount: curr.amount,
},
}),
roundTime: curr.time,
};
return acc;
}, {});
const roundTotal = Object.values(reducedRound.counterparts).reduce(
(acc, item) => parseFloat(acc) + parseFloat(item.amount),
0,
);
const roundWithTotal = Object.assign({}, reducedRound, {
roundTotal: roundTotal.toFixed(2),
});
return roundWithTotal;
})
.sort((a, b) => new Date(b.roundTime) - new Date(a.roundTime));
res.json(roundStore);
})
.catch((error) => {
console.log(error);
res.json({ error });
});
});
server.listen(process.env.PORT || 8080, () => {
console.log('Listening on port 8080');
});