forked from aahmadyar123/CookMyFridge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser-services.js
265 lines (239 loc) · 6.33 KB
/
user-services.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
const userModel = require("../models/user");
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");
async function register(email, password) {
/*
This function checks for valid login
:param email: user email
:param password: password for user
:return: user model
*/
try {
//check if duplicate email
duplicate = await userModel.findOne({ email: email });
if (duplicate) {
return undefined;
}
//create new user model and add to database
if (password.length < 6)
throw new Error(
"Invalid password. Length must be at least 6 characters."
);
const hashedPword = await bcrypt.hash(password, 10);
const userToAdd = new userModel({ email: email, password: hashedPword });
const savedUser = await userToAdd.save();
return savedUser;
} catch (error) {
console.log(error);
return undefined;
}
}
async function login(email, password) {
/*
This function checks for valid login
:param email: user email
:param password: password for user
:return: user model
*/
try {
//get user
const user = await userModel.findOne({ email: email });
//invalid email (user does not exist)
if (!user) throw new Error("Invalid email.");
//compare entered password to one retreieved from DB
const validPwd = await bcrypt.compare(password, user.password);
if (validPwd) return user;
else return undefined;
} catch (error) {
console.log(error);
return undefined;
}
}
async function getUsers(name) {
// use mongoose to find all users that match this name
let result = await userModel.find({ name: name });
return result;
}
async function findUserById(id) {
/*
Finds user by id
:param id: user database ID
:return: user model with associated id
*/
try {
return await userModel.findById(id);
} catch (error) {
console.log(error);
return undefined;
}
}
async function addRecipe(userID, recipeID) {
/*
Adds recipe reference to user
:param userID: id of user
:param recipeID: recipe id
:return: boolean if added successfully
*/
try {
const user = await findUserById(userID);
if (user.recipes.includes(recipeID)) {
return true;
} else {
user.recipes.push(recipeID);
await user.save();
return true;
}
} catch (error) {
console.log(error);
return false;
}
}
async function removeRecipe(userID, recipeID) {
/*
Removes recipe reference from user
:param userID: id of user
:param recipeID: recipe id
:return: boolean if removed successfully
*/
try {
// mongoRecipeId = mongoose.Types.ObjectId(recipeID);
//remove recipe reference from user.recipes
//const user = await userModel.find({ _id: userID });
const user = await findUserById(userID);
//check if recipe not favorited
const idx = user.recipes.indexOf(recipeID);
if (idx === -1) {
return false;
}
//remove recipe if favorited
else {
user.recipes.splice(idx, 1);
await user.save();
return true;
}
} catch (error) {
console.log(error);
return false;
}
}
async function getRecipes(userID) {
/*
This function populates a user's list of recipes
:param: userID: id of user to get recipes from
:return: array of recipes
*/
try {
//find user and populate recipes from doucment references
let user = await findUserById(userID);
let populatedUser = await userModel.populate(user, "recipes");
return populatedUser.recipes;
} catch (error) {
console.log(error);
return undefined;
}
}
async function getIngredients(id) {
/*
Gets all ingredients associated with specified user
:param id: id of user generated by MongoDB
:return: list of ingredients associated with user
*/
try {
// populate without using utility functions
const ingredients = await userModel.findById(id).select("ingredients");
return ingredients;
} catch (error) {
console.log(error);
return undefined;
}
}
async function updateIngredients(id, userIngredients) {
/*
Updates user.ingredients field in database
:param id: user id in database
:param ingredients: updated list of ingredients for user
:return: user model
*/
try {
//update ingredients field for user with specified id
const result = await userModel.updateOne(
{ _id: id },
{ $set: { ingredients: userIngredients } }
);
ret = await userModel.findById(id);
return ret;
} catch (error) {
console.log(error);
return undefined;
}
}
module.exports = {
register,
login,
getUsers,
findUserById,
// deleteUser,
addRecipe,
getRecipes,
getIngredients,
updateIngredients,
removeRecipe,
};
// ----------------- UNIMPLEMENTED -----------------
// async function deleteUser(login) {
// /*
// This function deletes a user from the database
// Args:
// login(JSON): login information to confirm deletion of account
// Return:
// boolean: true if deleted, false otherwise
// */
// try {
// //get user
// const user = await userModel.findOne({ email: login.email });
// //invalid email (user does not exist)
// if (!user) return false;
// //compare entered password to one retreieved from DB
// const validPwd = await bcrypt.compare(login.password, user.password);
// if (validPwd) {
// return (await userModel.findByIdAndDelete(user.id)) !== null;
// } else return false;
// } catch (error) {
// console.log(error);
// return false;
// }
// }
// async function addFriend(user, friendId) {
// /*
// This function adds a friend to a user's list of friends
// Args:
// friendId: id of friend to add
// user: user to add friend to
// Return:
// boolean: true if added, false otherwise
// */
// try {
// user.friends.push(friendId);
// await user.save();
// return true;
// } catch (error) {
// console.log(error);
// return false;
// }
// }
// async function getFriends(user) {
// /*
// Populates and returns user friend list
// :param user: user to get recipes from
// :return: users populated friend list
// */
// try {
// // populate without using utility functions
// let populatedUser = await user.populate("friends");
// // const recipes = await populateField(user, "recipes");
// return populatedUser;
// } catch (error) {
// console.log(error);
// return undefined;
// }
// }