-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.js
484 lines (441 loc) · 14.5 KB
/
manager.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Setup
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
// Load the Schemas
const userSchema = require('./msc-user.js');
const ProgramSchema = require('./msc-program.js');
const adminSchema = require('./msc-admin');
module.exports = function (mongoDBConnectionString) {
// Defined on Connection to the New Database
let Users;
let Programs;
let Admins;
return {
// Establish Connection With the Database
connect: function () {
return new Promise(function (resolve, reject) {
const db = mongoose.createConnection(mongoDBConnectionString);
db.on('error', (error) => {
reject(error);
});
db.once('open', () => {
Users = db.model('Users', userSchema, 'user');
Programs = db.model('Programs', ProgramSchema, 'program');
Admins = db.model('Admins', adminSchema, 'admins');
resolve();
});
});
},
// Get One User By Id
usersGetById: function (itemId) {
return new Promise(function (resolve, reject) {
// Find One Specific Document
Users.findById(itemId, (error, item) => {
if (error) {
// Match Is Not Found
return reject(error.message);
}
// Check For an Item
if (item) {
// If Found, One Object Will Be Returned
return resolve(item);
} else {
return reject(new Error('Not found'));
}
});
});
},
// Users Register
usersRegister: function (userData) {
// debugged
return new Promise(function (resolve, reject) {
// Incoming data package has user name (email address), full name,
// two identical passwords
// { email: xxx, password: yyy, passwordConfirm: yyy }
// check if passwords match
if (userData.password !== userData.passwordConfirm) {
return reject(new Error('Passwords do not match'));
}
// Generate a "salt" value
const salt = bcrypt.genSaltSync(10);
// Hash the result
const hash = bcrypt.hashSync(userData.password, salt);
// Update the incoming data
userData.password = hash;
// Create a new user account document
const newUser = new Users(userData);
// Attempt to save
newUser.save((error) => {
if (error) {
if (error.code === 11000) {
reject(new Error('User creation - cannot create; user already exists'));
} else {
reject(new Error(`User creation - ${error.message}`));
}
} else {
resolve(newUser);
}
}); // newUser.save
}); // return new Promise
}, // usersRegister
// Users login // debugged
usersLogin: function (userData) {
return new Promise(function (resolve, reject) {
// Incoming data package has user name (email address) and password
// { email: xxx, password: yyy }
Users.findOne({ email: userData.email }, (error, item) => {
if (error) {
// Query error
return reject(new Error(`Login - ${error.message}`));
}
// Check for an item
if (item) {
// Compare password with stored value
const isPasswordMatch = bcrypt.compareSync(userData.password, item.password);
if (isPasswordMatch) {
return resolve(item);
} else {
return reject(new Error('Login was not successful'));
}
} else {
return reject(new Error('Login - not found'));
}
}); // Users.findOneAndUpdate
}); // return new Promise
}, // usersLogin
// User Update // debugged
userUpdate: function (_id, user) {
return new Promise(function (resolve, reject) {
Users.findByIdAndUpdate(_id, user, { new: true }, (error, item) => {
if (error) {
// Cannot edit item
return reject(error.message);
}
// Check for an item
if (item) {
// Success message will be returned
return resolve('User updated');
} else {
return reject(new Error('Not found'));
}
});
});
},
// User Save results of the questionnaire
userSaveResults: function (_id, user) {
return new Promise(function (resolve, reject) {
Users.findByIdAndUpdate(_id, user, { new: true }, (error, item) => {
if (error) {
// Cannot edit item
return reject(error.message);
}
// Check for an item
if (item) {
// Success message will be returned
return resolve('User updated');
} else {
return reject(new Error('Not found'));
}
});
});
},
// *** Program Functions ***
// Get All Programs
programGetAll: function () {
return new Promise(function (resolve, reject) {
// Fetch All Documents
Programs.find()
.sort({ name: 'asc' })
.exec((error, items) => {
if (error) {
// Query Error
return reject(error.message);
}
// If Found, a Collection Will Be Returned
return resolve(items);
});
});
},
// Get Matched Programs
programGetMatched: function (userID) {
return new Promise(function (resolve, reject) {
let tag, user;
async function getUser() {
let promise = new Promise(function (resolve, reject) {
Users.findById(userID, (error, item) => {
if (error) {
// Match Is Not Found
console.log('1 ne nashel usera');
reject(error.message);
}
// Check For an Item
if (item) {
// If Found, One Object Will Be Returned
user = item;
resolve(item);
} else {
console.log('hz');
reject(new Error('User not found'));
}
});
});
let result = await promise;
return result;
}
getUser()
.then(() => {
let results = [],
data = [],
part1 = [],
part2 = [],
part3 = [],
part4 = [];
async function getPrograms() {
let getPart1 = new Promise(function (resolve, reject) {
tag = user.interests.i1.raisecTag[0];
console.log('p1 tag = ' + tag);
// Fetch Documents that match first interest
tag = '^' + tag;
let re = new RegExp(tag);
Programs.find({ categoryTag: { $regex: re } })
.sort({ name: 'asc' })
.exec((error, items) => {
data = items;
//console.log('items 1 = ' + items);
resolve(1);
});
});
let getPart2 = new Promise(function (resolve, reject) {
tag = user.interests.i2.raisecTag[0] + user.interests.i1.raisecTag[0];
console.log('p2 tag = ' + tag);
tag = '^' + tag;
let re = new RegExp(tag);
// Fetch Documents that match first two interests swapped
Programs.find({ categoryTag: { $regex: re } })
.sort({ name: 'asc' })
.exec((error, items) => {
if (error) reject(error);
part2 = items;
//console.log('items 2 = ' + items);
resolve(1);
});
});
await getPart1;
await getPart2;
console.log('data1 = ' + data);
console.log('data2 = ' + part2);
return;
}
getPrograms()
.then(() => {
console.log('data =' + data);
for (itm of data) {
if (itm.categoryTag[1] === user.interests.i2.raisecTag[0]) {
if (itm.categoryTag[2] === user.interests.i3.raisecTag[0]) {
part1.push(itm);
} else {
part3.push(itm);
}
} else {
part4.push(itm);
}
}
results = [...part1, ...part3, ...part2, ...part4];
if (results.length === 0) {
return reject('No Programs Found');
} else return resolve(results);
})
.catch((error) => {
return reject(error);
});
})
.catch((error) => {
return reject(error);
});
});
},
// Get One Program By Id
programGetById: function (itemId) {
return new Promise(function (resolve, reject) {
// Find One Specific Document
Programs.findById(itemId, (error, item) => {
if (error) {
// Match Is Not Found
return reject(error.message);
}
// Check For an Item
if (item) {
// If Found, One Object Will Be Returned
return resolve(item);
} else {
return reject('Not found');
}
});
});
},
// Add new Program
programAdd: function (someId) {
return new Promise(function (resolve, reject) {
Programs.create(someId, (error, object) => {
if (error) {
return reject(error.message);
}
return resolve(object);
});
});
},
// Edit a Program
programEdit: function (someId) {
return new Promise(function (resolve, reject) {
Programs.findByIdAndUpdate(someId._id, someId, { new: true }, (error, object) => {
if (error) {
return reject(error.message);
}
if (object) {
return resolve(object);
} else {
return reject('Not found');
}
});
});
},
// Delete a Program
programDelete: function (someId) {
return new Promise(function (resolve, reject) {
Programs.findByIdAndRemove(someId, (error) => {
if (error) {
return reject(error.message);
}
return resolve();
});
});
},
// Admin Functions
// Admin Get By ID
adminGetById: function (itemId) {
return new Promise(function (resolve, reject) {
Admins.findById(itemId, (error, item) => {
if (error) {
return reject(error.message);
}
if (item) {
return resolve(item);
} else {
return reject(new Error('Not found'));
}
});
});
},
// Admin Register
adminRegister: function (adminData) {
return new Promise(function (resolve, reject) {
if (adminData.password !== adminData.passwordConfirm) {
return reject(new Error('Passwords do not match'));
}
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync(adminData.password, salt);
adminData.password = hash;
const newAdmin = new Admins(adminData);
newAdmin.save((error) => {
if (error) {
if (error.code === 11000) {
reject(new Error('Admin creation - cannot create; admin already exists'));
} else {
reject(new Error(`Admin creation - ${error.message}`));
}
} else {
resolve(newAdmin);
}
});
});
},
// Admin login // debugged
adminLogin: function (adminData) {
return new Promise(function (resolve, reject) {
Admins.findOne({ email: adminData.email }, (error, item) => {
if (error) {
return reject(new Error(`Login - ${error.message}`));
}
if (item) {
const isPasswordMatch = bcrypt.compareSync(adminData.password, item.password);
if (isPasswordMatch) {
return resolve(item);
} else {
return reject(new Error('Login was not successful'));
}
} else {
return reject(new Error('Login - not found'));
}
});
});
},
// Admin Update // debugged
adminUpdate: function (_id, admin) {
return new Promise(function (resolve, reject) {
Admins.findByIdAndUpdate(_id, admin, { new: true }, (error, item) => {
if (error) {
return reject(error.message);
}
if (item) {
return resolve('Admin updated!');
} else {
return reject(new Error('Not found'));
}
});
});
},
//Full Admin Cart Save
adminCartSaveFull: function (_id, CourseArray) {
var wrappedItem = { "finalPrograms": CourseArray };
return new Promise(function (resolve, reject) {
Admins.findByIdAndUpdate(_id, wrappedItem, { new: true }, (error, object) => {
if (error) {
return reject(error.message);
}
if (object) {
return resolve(object);
} else {
return reject('Not found');
}
});
});
},
//Temporary Admin Cart Save
adminCartSave: function (_id, CourseArray) {
var wrappedItem = { "tempPrograms": CourseArray };
return new Promise(function (resolve, reject) {
Admins.findByIdAndUpdate(_id, wrappedItem, { new: true }, (error, object) => {
if (error) {
return reject(error.message);
}
if (object) {
return resolve(object);
} else {
return reject('Not found');
}
});
});
},
// Admin Password Reset
adminPassReset: function (adminData, newPassword) {
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync(newPassword, salt);
newPassword = hash;
var wrappedItem = { "password": newPassword }
return new Promise(function (resolve, reject) {
Admins.findOneAndUpdate({ "email": adminData }, wrappedItem, { new: true }, (error, object) => {
if (error) {
return reject(error.message);
}
if (object) {
return resolve(object);
} else {
return reject('Not found');
}
});
});
},
};
};