-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
145 lines (142 loc) · 5.09 KB
/
main.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
let fs = require('fs');
let Order = require('./Order');
let db = require("./db");
function Users (username, email, password, access) {
this.username = username;
this.email = email;
this.password = password;
this.access = access;
}
Users.prototype = {
constructor : Users,
createUser : function(id) {
let response = "";
if(this.access === "users" || this.access === "user") {
this.id = id;
if (db.users.length === 0) {
this.id = 1;
db.users.push({
id: this.id,
username: this.username,
email: this.email,
password: this.password,
access: this.access
});
response = "Your user account has been successfully created";
console.log(db.users[0]);
} else {
this.id = (db.users[db.users.length - 1].id) + 1;
db.users.push({
id: this.id,
username: this.username,
email: this.email,
password: this.password,
access: this.access
});
response = "Your user account has been successfully created";
console.log(db.users[0]);
}
}
else if (this.access === "admin") {
this.id = id;
if (db.admin.length === 0) {
this.id = 1;
db.admin.push({
id: this.id,
username: this.username,
email: this.email,
password: this.password,
access: this.access
});
response = "Your administrator account has been successfully created";
console.log(db.admin[0]);
} else {
this.id = (db.admin[db.admin.length - 1].id) + 1;
db.admin.push({
id: this.id,
username: this.username,
email: this.email,
password: this.password,
access: this.access
});
response = "Your administrator account has been successfully created";
console.log(db.admin[0]);
}
}
else {
response = "A user can not be created with this access level. Kindly use either admin or user";
}
console.log(response);
return response;
},
searchSingleUserById : function(id, accountType) {
this.id = id;
this.searchType = accountType;
let result = [];
let response = "";
if (this.searchType === "admin") {
for(let i in db.admin) {
if(db.admin[i].id === this.id) {
result.pop();
result.push(db.admin[i]);
}
}
}
else if(this.searchType === "user") {
for (let i in db.users) {
if(db.users[i].id === this.id) {
result.pop();
result.push(db.users[i]);
}
}
}
else {
console.log("Kindly search using either users or an admin as account type");
return "Kindly search using either users or an admin as account type";
}
if(result.length === 0) {
console.log("User not found");
response = "User not found";
}
else {
console.log(result);
response = "Haha!.. Here you go.";
}
console.log(response);
return response;
},
updateUser : function(username, password, newUsername, newEmail, newPassword, access) {
this.username = username;
this.newUsername = newUsername;
this.password = password;
this.newPassword = newPassword;
this.access = access;
this.newEmail = newEmail;
let response = "";
if(this.access === "user") {
for(let i in db.users) {
if(this.username === db.users[i].username && this.password === db.users[i].password) {
db.users[i].username = this.newUsername;
db.users[i].password = this.newPassword;
db.users[i].email = this.newEmail;
response = "Your user account has been successfully updated";
console.log(db.users[0]);
break;
}
else {
response = "Incorrect username or password";
}
console.log(db.users[i]);
}
}
console.log(response);
return response;
},
makeOrder : function(id, userid, ...products) {
this.id = id;
this.userid = userid;
this.products = products;
return Order.createOrder(this.id, this.userid, this.products);
}
};
module.exports = Users;