-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccounts.js
299 lines (267 loc) · 6.52 KB
/
accounts.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
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
const db = require('./crud');
const url = 'mongodb+srv://dbapiuser:[email protected]/COP4331?retryWrites=true&w=majority';
const id = require('mongodb').ObjectId;
const sgMail = require("@sendgrid/mail");
require('dotenv').config();
const mail = require('./email');
const BASE_URL = 'https://cop4331-g25-app.herokuapp.com/';
// const url = 'mongodb+srv://RickLeinecker:[email protected]/COP4331?retryWrites=true&w=majority';
db.setUrl(url);
db.connect();
const login = async(res, table, user, pass) =>
{
var results = await db.find(table, {userName: user, Password: pass});
var result;
if(results.Results.length <= 0)
{
result = {error:'username/password incorrect'}
db.sendjson(res, result);
return -1;
}
result = results.Results[0];
result.error = results.error;
delete result._id;
delete result.Password;
db.sendjson(res, result);
};
const register = async(res, table, user, pass, first, last, email, admin=0) =>
{
var error = '';
var json = {
isVerified: false,
userName: user,
Password: pass,
firstName: first,
lastName: last,
email: email
};
if(admin != 0)
{
json.admin = admin;
}
var username = await db.find('Users', {userName: user});
if(username.Results.length > 0)
{
db.sendjson(res, {Results:'', error: 'username in use'});
return -1;
}
username = await db.find('Admins', {userName: user});
if(username.Results.length > 0)
{
db.sendjson(res, {Results:'', error: 'username in use'});
return -1;
}
if(admin != 0)
{
var adminId = await db.find('Admins', {userName: admin});
if(adminId.Results.length <= 0 )
{
error = 'admin does not exist';
}
else
{
json.admin = adminId.Results[0]._id;
}
}
var results = await db.add(table, json);
if(error != '')
{
results.error = error;
}
var User = await db.find(table, json);
// var text = BASE_URL + 'api/Verify?userName='+user+'&Password='+pass;
var text = BASE_URL + 'api/Verify?Id=' + User.Results[0]._id;
mail.send(res, email, 'Hello', 'this is a verification email. go to this link: ' + text);
db.sendjson(res, results);
};
const makeTrip = async(res, userName, startLocation, destination, purpose, weather=0, startTime=0) =>
{
var json = {
userId: -1,
adminId: -1,
startLocation: startLocation,
destination: destination,
locations: [startLocation],
progress: 0,
startTime: startTime,
endTime: '',
purpose: purpose,
isApproved: false,
isNew: true,
comments: '',
weather: weather,
timeMade: Date.now()
}
var username = await db.find('Users', {userName: userName});
if(username.Results.length <= 0)
{
db.sendjson(res, {Results:'', error: 'username not valid'});
return -1;
}
username = username.Results[0];
json.userId = id.ObjectId(username._id);
json.adminId = id.ObjectId(username.admin);
var results = await db.add('Trips', json);
db.sendjson(res, results);
};
const approveTrip = async(res, tripId, isApproved, comments) =>
{
var og = await db.find('Trips', {_id: id.ObjectId(tripId)});
// console.log(og);
if(og.Results.length <= 0)
{
db.sendjson(res, {Results:'', error: 'could not find trip/trip does not exist'});
return -1;
}
og = og.Results[0];
var mod = {
isNew: false,
isApproved: isApproved,
comments: comments
};
var results = db.update('Trips', og, mod);
db.sendjson(res, results);
};
const getUserId = async(userName) =>
{
var user = await db.find('Users', {userName: userName});
var results;
if(user.Results.length <= 0)
{
results = {Results: false, error: 'user does not exits'};
}
else
{
results = {Results: user.Results[0]._id, error: ''};
}
return results;
};
const getAdminId = async(userName) =>
{
var user = await db.find('Admins', {userName: userName});
var results;
if(user.Results.length <= 0)
{
results = {Results: false, error: 'user does not exits'};
}
else
{
results = {Results: user.Results[0]._id, error: ''};
}
return results;
};
const listTripsByUser = async(res, userName) =>
{
var userId = await getUserId(userName);
var results;
if(userId.Results == false)
{
results = {error: 'user does not exist'};
}
else
{
results = await db.find('Trips', {userId: id.ObjectId(userId.Results)});
results.Results.sort((a, b) => (a.timeMade < b.timeMade) ? 1 : -1);
}
db.sendjson(res, results);
};
const listTripsByAdmin = async(res, userName) =>
{
var userId = await getAdminId(userName);
var results;
if(userId.Results == false)
{
results = {error: 'admin does not exist'};
}
else
{
results = await db.find('Trips', {adminId: id.ObjectId(userId.Results)});
results.Results.sort((a, b) => (a.timeMade < b.timeMade) ? 1 : -1);
}
db.sendjson(res, results);
};
const listAdmins = async(res) =>
{
var admins = await db.find('Admins', {});
if(admins.Results.length <= 0)
{
db.sendjson(res, {Results:'', error: 'no admins found in database'});
return -1;
}
for(i = 0; i < admins.Results.length; i++)
{
delete admins.Results[i]._id;
delete admins.Results[i].isVerified;
delete admins.Results[i].Password;
delete admins.Results[i].admin;
delete admins.Results[i].email;
}
db.sendjson(res, admins);
};
const verify = async(res, Id) =>
{
var login = {_id: id.ObjectId(Id)};
var change = {isVerified: true};
var result = await db.update('Users', login, change);
var result = await db.update('Admins', login, change);
// db.sendjson(res, {Results: 'Account Verified!',error:''});
res.redirect('https://cop4331-g25-app.herokuapp.com/');
};
const editTrip = async(res, _id, newData)=>
{
if(newData.hasOwnProperty('userId'))
{
newData.userId = id.ObjectId(newData.userId);
}
if(newData.hasOwnProperty('adminId'))
{
newData.adminId = id.ObjectId(newData.adminId);
}
var results = await db.update('Trips', {_id:id.ObjectId(_id)}, newData);
db.sendjson(res, results);
};
const getById = async(res, Id) =>
{
var json =
{
_id:id.ObjectId(Id)
};
var user = await db.find('Users', json);
var admin = await db.find('Admins', json);
var results;
if(user.error == '')
{
results = user.Results[0];
results.error = '';
delete results._id;
delete results.Password;
}
else if(admin.error == '')
{
results = admin.Results[0];
results.error = '';
delete results._id;
delete results.Password;
}
else
{
results =
{
error: 'no user/admin with that id'
};
}
db.sendjson(res, results);
};
module.exports = {
db: db,
login: login,
register: register,
makeTrip: makeTrip,
approveTrip: approveTrip,
listAdmins: listAdmins,
verify: verify,
listTripsByAdmin: listTripsByAdmin,
listTripsByUser: listTripsByUser,
editTrip: editTrip,
getById: getById
}