forked from JerryXie98/Rapid-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
295 lines (246 loc) · 8.3 KB
/
main.js
File metadata and controls
295 lines (246 loc) · 8.3 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
const express = require('express')
var admin = require('firebase-admin')
const bodyParser = require('body-parser')
var paypal = require('paypal-rest-sdk')
// Firebase setup
const config = {
"type": process.env.TYPE,
"project_id": process.env.PROJECT_ID,
"private_key_id": process.env.PRIVATE_KEY_ID,
"private_key": JSON.parse(process.env.PRIVATE_KEY),
"client_email": process.env.CLIENT_EMAIL,
"client_id": process.env.CLIENT_ID,
"auth_uri": process.env.AUTH_URI,
"token_uri": process.env.TOKEN_URI,
"auth_provider_x509_cert_url": process.env.AUTH_PROVIDER,
"client_x509_cert_url": process.env.CLIENT_CERT
}
admin.initializeApp({
credential: admin.credential.cert(config),
databaseURL: 'https://rapid-14d88.firebaseio.com'
})
// paypal.configure({
// 'mode': 'sandbox', //sandbox or live
// 'client_id': process.env.PAYPAL_CLIENT,
// 'client_secret': process.env.PAYPAL_SECRET
// })
var app = express()
app.use(bodyParser.json())
app.get('/', (req, res) => res.send('Rapid API is up and running! ' + config['private_key']))
app.get('/api/login', (req, res) => {
email = req.query['email']
password = req.query['password']
admin.database().ref().child('users').once('value').then((snap) => {
data = snap.val()
filtered = Object.keys(data).forEach(el => {
if (data[el]['email'] == email) {
if (data[el]['password'] == password) {
res.send({'user': data[el]})
} else {
res.status(401).send({'Error': 'Authentication failed.'})
}
}
})
res.status(404).send({'Error': 'No user found.'})
})
})
// User Routes
// Add user to group
app.post('/api/users/:userId/addGroup', async (req, res) => {
const userId = req.params['userId']
const groupId = req.query['groupId']
let user = await admin.database().ref('/users/' + userId).once('value')
let group = await admin.database().ref('/groups/' + groupId).once('value')
let updates = {}
updates['/groups/' + groupId + '/users/' + userId] = {'name': user.val()['name'], 'email': user.val()['email']}
updates['/users/' + userId + '/groups/' + groupId] = {'name': group.val()['name'], 'funds': group.val()['funds']}
admin.database().ref().update(updates)
res.send({'Success': user.val()['name'] + ' was added to: ' + group.val()['name'] + '.'})
})
// Contribute to pool
app.post('/api/users/:userId/contribute', async (req, res) => {
const userId = req.params['userId']
data = {
'userId': userId,
'groupId': req.body['groupId'],
'amount': req.body['amount']
}
const transId = admin.database().ref().child('transctions').push().key
// Update table funds
let groupFunds = await admin.database().ref('/groups/' + data.groupId + '/funds').once('value')
let userFunds = await admin.database().ref('/users/' + data.userId + '/funds').once('value')
const newGroupFund = parseFloat(groupFunds.val()) + parseFloat(data.amount)
const newUserFund = parseFloat(userFunds.val()) - parseFloat(data.amount)
if (newUserFund > 0) {
updates = {}
updates['/groups/' + data.groupId + '/funds'] = newGroupFund
updates['/users/' + data.userId + '/funds'] = newUserFund
admin.database().ref().update(updates)
// Add transaction
var trans = {}
trans['/transactions/' + transId] = data
trans['/groups/' + data.groupId + '/transactions/' + transId] = data
trans['/users/' + userId + '/transactions/' + transId] = data
admin.database().ref().update(trans)
// Update group info under users
userUpdates = {}
let users = await admin.database().ref('/groups/' + data.groupId + '/users').once('value')
let groupName = await admin.database().ref('/groups/' + data.groupId + '/name').once('value')
const userIdArray = Object.keys(users.val())
userIdArray.forEach(uid => {
userUpdates['/users/' + uid + '/groups/' + data.groupId] = {'name': groupName.val(), 'funds': newGroupFund}
})
admin.database().ref().update(userUpdates)
res.send({'Success': 'Transaction: ' + transId + ' was processed.'})
} else {
res.status(422).send({'Error': 'Insufficient funds.'})
}
})
// Get all users
app.get('/api/users', (req, res) => {
email = req.query['email']
admin.database().ref().child('users').once('value').then((snap) => {
if (email) {
data = snap.val()
filtered = Object.keys(data).filter(el => data[el]['email'] == email)
if (filtered.length == 1) {
res.send(data[filtered[0]])
} else {
res.status(404).send({'Error': 'No single user with email: ' + email + ' found.'})
}
res.send(Object.keys(data).filter(el => data[el]['email'] == email))
} else {
res.send(snap.val())
}
})
})
// Get user by id
app.get('/api/users/:userId', (req, res) => {
userId = req.params['userId']
admin.database().ref('/users/' + userId).once('value').then((snap) => {
if (snap.val() != null) {
res.send(snap.val())
} else {
res.status(404).send({'Error': 'No user found.'})
}
})
})
// Add user
app.post('/api/users', (req, res) => {
data = {
'name': req.body['name'],
'email': req.body['email'],
'password': req.body['password'],
'funds': req.body['funds']
}
const userId = insert('users', data)
res.send({'id': userId})
})
// Update user
app.put('/api/users/:userId', (req, res) => {
const userId = req.params['userId']
data = {
'name': req.body['name'],
'email': req.body['email'],
'password': req.body['password'],
'funds': req.body['funds']
}
admin.database().ref('/users/' + userId).set(data)
res.send({'id': userId})
})
// Delete user
app.delete('/api/users/:userId', (req, res) => {
const userId = req.params['userId']
let updates = {}
updates['/users/' + userId] = null
admin.database().ref().update(updates)
res.send({'Success': 'Group removed.'})
})
// Group Routes
// Get all groups
app.get('/api/groups', (req, res) => {
admin.database().ref('/groups').once('value').then((snap) => {
res.send(snap.val())
})
})
// Get group by id
app.get('/api/groups/:groupId', (req, res) => {
groupId = req.params['groupId']
admin.database().ref('/groups/' + groupId).once('value').then((snap) => {
if (snap.val() != null) {
res.send(snap.val())
} else {
res.status(404).send({'Error': 'No group found.'})
}
})
})
// Add group
app.post('/api/groups', (req, res) => {
data = {
'name': req.body['name'],
'funds': 0
}
const groupId = insert('groups', data)
res.send({'id': groupId})
})
// Update group
app.put('/api/groups/:groupId', (req, res) => {
const groupId = req.params['groupId']
data = {
'name': req.body['name'],
'funds': req.funds['funds']
}
admin.database().ref('/groups/' + groupId).set(data)
res.send({'id': groupId})
})
// Delete group
app.delete('/api/groups/:groupId', (req, res) => {
const groupId = req.params['groupId']
let updates = {}
updates['/groups/' + groupId] = null
admin.database().ref().update(updates)
res.send({'Success': 'Group removed.'})
})
// Get Transactions
app.get('/api/transactions', (req, res) => {
admin.database().ref('/transactions').once('value').then((snap) => {
res.send(snap.val())
})
})
// Get Transction by Id
app.get('/api/transactions/:transId', (req, res) => {
transId = req.params['transId']
admin.database().ref('/transactions/' + transId).once('value').then((snap) => {
if (snap.val() != null) {
res.send(snap.val())
} else {
res.status(404).send({'Error': 'No transaction found.'})
}
})
})
// Clear database
app.delete('/api/clear', (req, res) => {
updates = {}
updates['/groups'] = null
updates['/users'] = null
updates['/transactions'] = null
admin.database().ref().update(updates)
res.send({'Success': 'Database cleared.'})
})
// Clear element
// Example: users/aweduasi13 delete user with that id: aweduasi13
app.delete('/api/clear/:elementString', (req, res) => {
const elementString = req.params['elementString']
let updates = {}
updates['/' + elementString]
admin.database().ref().update(updates)
res.send({'Success': 'Element cleared.'})
})
function insert(dbName, data) {
var id = admin.database().ref().child(dbName).push().key
var update = {}
update['/' + dbName + '/' + id] = data
admin.database().ref().update(update)
return id
}
app.listen(process.env.PORT ? process.env.PORT : 3000, () => console.log('Example app listening on port 3000!'))