-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
697 lines (667 loc) · 15.4 KB
/
app.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
const netchick = require("inquirer");
const viewMovie = require("./movies.js");
const admin = require("./admin.js");
const fs = require("fs");
const database = JSON.parse(fs.readFileSync("./database.json"));
// ===========================================VARIABLES=============================================================
/**
*Important variables declared at the top here so therefore they are global to all scopes
*/
let onlineUser;
let movieObj = {};
let userObj = {};
welcome();
// ===========================================OPERATIONS TO CHOOSE FROM=============================================================
/**
* These operations allow you to log in as a USER or as an ADMIN;
* If logged in as a USER customer views/ access only user functionalities
* And if logged in as an ADMIN, admin acesses functionalities under the admin scope
*/
function welcome() {
console.clear();
netchick
.prompt([
{
type: "list",
name: "welcome",
message:
"Welcome to Netchick and Flex.\n because Netflix is overkill \n Login as: ",
choices: ["Admin", "User"],
},
])
.then(answer => {
if (answer.welcome === "User") {
return login();
} else {
return adminOperations();
}
});
}
function login() {
console.clear();
netchick
.prompt([
{
type: "list",
name: "login",
message:
"Sign in if you have an account\n Create new account if you dont",
choices: ["Sign in", "Create account"],
},
])
.then(answer => {
if (answer.login == "Sign in") {
return userName();
} else if (answer.login == "Create account") {
return createUserName();
}
});
}
function createUserName() {
netchick
.prompt([
{
type: "input",
name: "username",
message: "Enter your Username: ",
},
])
.then(answer => {
if (answer.username != "") {
userObj.username = answer.username;
return createPassword();
} else {
console.log("Invalid Username!");
return createUserName();
}
});
}
function createPassword() {
netchick
.prompt([
{
type: "input",
name: "password",
mask: true,
message: "Enter your Password: ",
mask: true,
},
])
.then(answer => {
if (answer.password != "") {
userObj.password = answer.password;
return createEmail();
} else {
console.log("Invalid Email!");
return createPassword;
}
});
}
function createEmail() {
netchick
.prompt([
{
type: "input",
name: "email",
message: "Enter your Email: ",
},
])
.then(answer => {
if (answer.email != "") {
userObj.email = answer.email;
return createAccount();
}
});
}
function createAccount() {
netchick
.prompt([
{
type: "list",
name: "create",
message: "Do you want to create account? ",
choices: ["Create account"],
},
])
.then(answer => {
if (answer.create == "Create account") {
userObj.rentedMovies = [];
userObj.wallet = 0;
database.users.push(userObj);
updateJsonfile();
console.log(`Sign Up successful!`);
userName();
}
});
}
// ===========================================OPERATIONS OF THE USER=============================================================
/**
* These operations of the Admin include;
* =================Login to Netchick
* =================View Movies
* =================Rent Movies
* =================Recharge Wallet
* =================Return Movies
*/
//=================Verifies User and matches password, email to the users profile or account. This cannot be bipassed
function userExists(username) {
for (user of database.users) {
if (user.username == username) {
onlineUser = user;
return true;
}
}
return false;
}
// Inquires user details like Username, Password, Email.
function userName() {
console.clear();
netchick
.prompt([
{
type: "input",
name: "username",
message: "Please login to access you account\n Enter your Username: ",
},
])
.then(answer => {
if (userExists(answer.username)) {
return userEmail();
}
console.log("Invalid username!");
userName();
});
}
function userEmail() {
netchick
.prompt([
{
type: "input",
name: "email",
message: "Enter your Email: ",
},
])
.then(answer => {
if (onlineUser.email == answer.email) {
return password();
}
console.log("Invalid email");
console.clear();
return userEmail();
});
}
function password() {
//console.clear();
netchick
.prompt([
{
type: "password",
name: "password",
mask: true,
message: "Enter your Password: ",
},
])
.then(answer => {
if (onlineUser.password == answer.password) {
return services();
}
console.log("Invalid password");
console.clear();
return password();
});
}
function services() {
console.clear();
netchick
.prompt([
{
type: "list",
name: "services",
message:
"Login Successful! \n Welcome to Netchick and Flex\n Please select a service ",
choices: ["Rent Movie(s)", "View Rented Movie(s)", "Wallet"],
},
])
.then(answer => {
if (answer.services === "Rent Movie(s)") {
rentMovie();
} else if (answer.services === "Wallet") {
return walletoptions();
} else if (answer.services === "View Rented Movie(s)") {
return viewRentedMovies();
}
});
}
function walletoptions() {
console.clear();
netchick
.prompt([
{
type: "list",
name: "wallet",
message: `Netchick and Flex Wallet `,
choices: ["Check Balance", "TopUp account"],
},
])
.then(answer => {
if (answer.wallet == "Check Balance") {
return checkBalance();
} else {
return topUp();
}
});
}
function rentMovie() {
// console.clear();
netchick
.prompt([
{
type: "list",
name: "selection",
message: "List of NetChick Movies: ",
choices: database.movies.map((movie, index) => movie.title),
},
])
.then(answer => {
database.movies.forEach(movie => {
if (movie.title == answer.selection) {
viewMovie(movie);
return leaseMovie(movie);
}
});
});
}
function leaseMovie(movie) {
netchick
.prompt([
{
type: "list",
name: "lease",
message: "Would you like to rent movie? ",
choices: ["Rent Movie", "Back"],
},
])
.then(answer => {
if (answer.lease == "Rent Movie") {
if (onlineUser.wallet < movie.price) {
console.log("Insuffient Balance to rent movie");
insuffient();
} else if (onlineUser.rentedMovies.includes(movie.title)) {
console.log(
"Sorry but you've rented this movie already! Try renting another movie."
);
rentMovie();
} else {
onlineUser.rentedMovies.push(movie.title);
console.log("Movie rented!!!");
onlineUser.wallet -= movie.price;
updateJsonfile();
}
} else if (answer.lease == "Back") {
console.clear();
return rentMovie();
}
});
}
function viewRentedMovies() {
netchick
.prompt([
{
type: "list",
name: "movieList",
message: " List of Rented movies",
choices: database.users.map(movie => movie.rentedMovies),
},
])
.then(answer => {
console.log("Hello There");
});
}
function insuffient() {
netchick
.prompt([
{
type: "list",
name: "insuffient",
message: "Would you like to toUp? ",
choices: ["Yes", "No"],
},
])
.then(answer => {
if (answer.insuffient == "Yes") {
return topUp();
}
return quitProcess();
});
}
function topUp() {
netchick
.prompt([
{
type: "number",
name: "amount",
message: "Enter Amount to top Up",
},
])
.then(answer => {
onlineUser.wallet += answer.amount;
updateJsonfile();
console.log(
`Congratulation ${
answer.amount
} Successfully credited to you Wallet \nNew balance: ${
onlineUser.wallet
}`
);
quitProcess();
});
}
function checkBalance() {
console.log(`Netchick account balance: `);
}
function quitProcess() {
netchick
.prompt([
{
type: "list",
name: "option",
message: `Would you like to go Home?`,
choices: ["Yes sure", "No quit"],
},
])
.then(answer => {
if (answer.option == "Yes sure") {
return services();
}
console.log("Exiting Netchick... Bye!");
quitProcess();
});
}
// ===========================================OPERATIONS OF THE ADMIN=============================================================
/**
* These operations of the Admin include;
* =================Add Movie
* =================Remove Movie
* =================View Users
* =================Remove Users
*/
function adminOperations() {
console.clear();
netchick
.prompt([
{
type: "list",
name: "operation",
message: "Welcome Admin\n Please select an operation: ",
choices: [
"View Users",
"View Movie(s)",
"Add Movie(s)",
"Remove Movie",
"Back",
],
},
])
.then(answer => {
if (answer.operation === "View Users") {
return viewUsers();
} else if (answer.operation === "Remove Movie") {
return removeMovie();
} else if (answer.operation === "View Movie(s)") {
return viewAllMovies();
} else if (answer.operation === "Add Movie(s)") {
return addTitle();
}
});
}
function back(message, action, action2) {
netchick
.prompt([
{
type: "list",
name: "back",
message: `${message}`,
choices: ["Yes", "No"],
},
])
.then(answer => {
if (answer.back == "Yes") {
action;
} else {
action2;
}
});
}
function exit() {
console.log("Exiting Netchick.");
}
function viewUsers() {
console.clear();
netchick
.prompt([
{
type: "list",
name: "users",
message: "List of Users in Admin ",
choices: admin.viewUsers,
},
])
.then(answer => {
database.users.forEach(user => {
if (user.username == answer.users) {
admin.userProfile(user);
}
});
});
}
function addTitle() {
netchick
.prompt([
{
type: "input",
name: "title",
message: "Enter title: ",
},
])
.then(answer => {
if (answer.title != "" && !database.movies.includes(answer.title)) {
movieObj.title = answer.title;
return addGenre();
} else {
console.log("invalid entry");
addTitle();
}
});
}
function addGenre() {
netchick
.prompt([
{
type: "input",
name: "genre",
message: "Enter genre: ",
},
])
.then(answer => {
if (answer.genre != "") {
newGenre = answer.genre.split(",");
movieObj.genre = newGenre;
return addSynopsis();
} else {
console.log("invalid entry");
addGenre();
}
console.log(movieObj);
});
}
function addSynopsis() {
netchick
.prompt([
{
type: "input",
name: "synopsis",
message: "",
},
])
.then(answer => {
if (answer.synopsis != "") {
movieObj.synopsis = answer.synopsis;
addDuration();
} else {
console.log("invalid entry");
addSynopsis();
}
});
}
function addDuration() {
netchick
.prompt([
{
type: "input",
name: "duration",
message: "Enter duration: ",
},
])
.then(answer => {
if (answer.duration != "") {
movieObj.duration = answer.duration;
addSize();
} else {
console.log("invalid entry");
addDuration();
}
});
}
function addSize() {
netchick
.prompt([
{
type: "input",
name: "size",
message: "Enter Size: ",
},
])
.then(answer => {
if (answer.size != "") {
movieObj.size = answer.size;
addPrice();
} else {
console.log("invalid entry");
addSize();
}
});
}
function addPrice() {
netchick
.prompt([
{
type: "number",
name: "price",
message: "Enter Price: ",
},
])
.then(answer => {
if (answer.price != "") {
movieObj.price = answer.price;
//console.log("Thank you!!");
commitMovie();
} else {
console.log("invalid entry");
addPrice();
}
});
}
function commitMovie(movie) {
netchick
.prompt([
{
type: "list",
name: "commit",
message: "Do you want to commit movie?",
choices: ["Yes", "No"],
},
])
.then(answer => {
if (answer.commit == "Yes") {
console.log(movieObj);
database.movies.push(movieObj);
updateJsonfile();
console.log("Movie successfully uploaded!");
}
});
}
//=================================REMOVE MOVIE========================================================
function removeMovie() {
netchick
.prompt([
{
type: "list",
name: "movieId",
message: "Please select movie to remove",
choices: admin.listMovies,
},
])
.then(answer => {
movieid = answer.movieId;
//checked inside MOVIES to make sure it exists and prints movie details
database.movies.forEach(movie => {
let moviecont = movie.title;
//index = user.rentedMovies.indexOf(movieId);
index = database.movies.indexOf(movie);
if (answer.movieId == movie.title) {
console.log(index);
viewMovie(movie);
confirmAction(movieid, index);
}
});
});
}
function confirmAction(movieid, index) {
netchick
.prompt([
{
type: "list",
name: "remove",
message: "Do you want to remove movie?",
choices: ["Yes", "No"],
},
])
.then(answer => {
if (answer.remove == "Yes") {
database.users.forEach(user => {
if (user.rentedMovies.includes(movieid)) {
return console.log(
`Movie Unavailable! \nReason: ${
user.username
} Rented ${movieid}. `
);
} else {
database.movies.splice(index, 1);
updateJsonfile();
return console.log(`${movieid} movie removed!!!`);
}
});
}
});
}
function viewAllMovies() {
netchick
.prompt([
{
type: "list",
name: "movielist",
message: "NetChick Movies:",
choices: admin.listMovies,
},
])
.then(answer => {
database.movies.forEach(movie => {
if (answer.movielist == movie.title) {
viewMovie(movie);
}
});
});
}
function updateJsonfile() {
fs.writeFileSync("./database.json", JSON.stringify(database, null, 1));
}