This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
395 lines (348 loc) · 9.96 KB
/
helpers.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
import { ObjectId } from "mongodb";
import storageFirebase from "./firebase.js";
import { getDownloadURL } from "firebase-admin/storage";
import xss from "xss";
let helper = {
inputValidator(input, inputName) {
if (!input) {
throw `${input} is not a valid ${inputName}`;
}
if (typeof input != "string") {
throw `${input} is not a valid string`;
}
input = input.trim();
if (input === "") {
throw `${inputName} cannot be an empty string`;
}
return input;
},
emailValidator(email) {
if (!email) {
throw `Invalid email`;
}
email = email.toLowerCase().trim();
if (email === "") {
throw `Email address contain be an empty string`;
}
if (typeof email != "string") {
throw `Email address must be a valid string`;
}
let domRegex = /[^-[A-Za-z0-9]/g;
let domRegexTwo = /((?<!\d+|\w+)-)|(-(?!\d+|\w+))/g;
let preRegex = /[^A-Za-z0-9_.-]/g;
let preRegexTwo = /[._-](?!\w+|\d+)/g;
if (!email.includes("@") || !email.includes(".")) {
throw "Invalid email.";
}
if (email.match(/@/g).length != 1) {
throw "Invalid email.";
}
const [prefix, domain] = email.split("@");
if (!prefix | !domain) {
throw "Invalid email.";
}
const [domainName, com] = domain.split(".");
if (prefix.length < 1 || domainName.length < 1) {
throw "Invalid email.";
}
let invalidChars = prefix.match(preRegex);
if (invalidChars != null) {
throw "Invalid email.";
}
invalidChars = prefix.match(preRegexTwo);
if (invalidChars != null) {
throw "Invalid email.";
}
invalidChars = domainName.match(domRegex);
if (invalidChars != null) {
throw "Invalid email.";
}
invalidChars = domainName.match(domRegexTwo);
if (invalidChars != null) {
throw "Invalid email.";
}
if (com.length < 2) {
throw "Invalid email.";
}
return email;
},
dateValidator(date) {
let temp = new Date(date);
if (isNaN(temp)) {
throw "Invalid date";
}
return true;
},
idValidator(id) {
if (!id) {
throw "No id was provided";
}
if (typeof id != "string") {
throw "Id must be a string!";
}
id = id.trim();
if (id.length === 0) {
throw "Id can't be an empty string.";
}
if (!ObjectId.isValid(id)) {
throw "Id is invalid.";
}
return id;
},
passwordValidator(password) {
if (!password) {
throw `You have input an invalid password`;
}
password = password.trim();
if (password === "") {
throw `Password cannot contain be an empty string`;
}
if (typeof password != "string") {
throw `Password must be a valid string`;
}
if (password.length < 8) {
throw "password must be atleast 8 characters.";
}
if (password.match(/\ /g)) {
throw "password cannot contain spaces.";
}
let uppercaseRegex = /[A-Z]+/g;
let numberRegex = /[0-9]+/g;
let symbolRegex = /[~`!@#\$%^&*()_+\-=[\]{}|\\;:'",<.>\/?]+/g;
if (!password.match(uppercaseRegex)) {
throw "Password must have at least 1 UPPERCASE character";
}
if (!password.match(numberRegex)) {
throw "Password must have at least 1 NUMBER";
}
if (!password.match(symbolRegex)) {
throw "Password must have at least 1 SPECIAL character";
}
return password;
},
createUserValidator(
firstName,
lastName,
userName,
emailAddress,
password,
description,
age,
friendsList,
incomingRequests,
outgoingRequests,
role,
unitMeasure,
) {
firstName = this.inputValidator(firstName, "firstName");
lastName = this.inputValidator(lastName, "lastName");
userName = this.inputValidator(userName, "userName");
emailAddress = this.emailValidator(emailAddress);
password = this.passwordValidator(password);
description = description.trim();
if (age && age < 0) throw "Age cannot be a negative number";
if (unitMeasure && unitMeasure !== "lb" && unitMeasure !== "kg")
throw "Invalid unit of measure";
return {
firstName: firstName,
lastName: lastName,
userName: userName,
emailAddress: emailAddress,
password: password,
aboutMe: { description: description, age: age },
friends: {
friendsList: friendsList,
incomingRequests: incomingRequests,
outgoingRequests: outgoingRequests,
},
role: role,
unitMeasure: unitMeasure,
};
},
loginUserValidator(userName, password) {
userName = this.inputValidator(userName, "userName");
password = this.passwordValidator(password);
return {
userName: userName,
password: password,
};
},
registerUserValidator(
firstName,
lastName,
userName,
emailAddress,
password,
confirmPassword,
) {
firstName = this.inputValidator(firstName, "firstName");
lastName = this.inputValidator(lastName, "lastName");
userName = this.inputValidator(userName, "userName");
emailAddress = this.emailValidator(emailAddress);
password = this.passwordValidator(password);
if (password !== confirmPassword) throw "Passwords do not match";
return {
firstName: firstName,
lastName: lastName,
userName: userName,
emailAddress: emailAddress,
password: password,
};
},
exerciseValidator(
exerciseName,
targetMuscles,
exerciseDescription,
instructions,
equipment,
difficulty,
image,
) {
exerciseName = this.inputValidator(exerciseName, "exerciseName");
exerciseDescription = this.inputValidator(
exerciseDescription,
"exerciseDescription",
);
instructions = this.inputValidator(instructions, "instructions");
difficulty = this.inputValidator(difficulty, "difficulty");
image = this.inputValidator(image, "image");
if (!Array.isArray(targetMuscles)) {
throw "TargetMuscle must be an array.";
}
if (!Array.isArray(equipment)) {
throw "Equiment must be an array.";
}
return {
name: exerciseName,
targetMuscles: targetMuscles,
description: exerciseDescription,
instructions: instructions,
equipment: equipment,
difficulty: difficulty,
image: image,
};
},
exerciseComponentValidator(exercise) {
const sets = Number(exercise.sets);
const reps = Number(exercise.reps);
const test_id = exercise.id;
if (isNaN(sets) || sets <= 0 || !Number.isInteger(sets))
throw "Sets is invalid";
if (isNaN(reps) || reps <= 0 || !Number.isInteger(reps))
throw "Reps is invalid";
const id = this.idValidator(test_id);
return {
id: id,
sets: sets,
reps: reps,
};
},
workoutValidator(
name,
workoutTypes,
notes,
exercises,
unitMeasure,
weightGoal,
difficulty,
restTime,
) {
const definedWorkoutTypes = [];
name = helper.inputValidator(name, "name");
if (typeof notes !== "string") {
throw "Notes must be a valid string";
}
notes = notes.trim();
if (
!Array.isArray(workoutTypes) ||
workoutTypes.some(
(workout) =>
workout.trim() === "" && definedWorkoutTypes.includes(workout.trim()),
)
) {
throw "workoutType must be a valid array.";
}
if (!Array.isArray(exercises)) throw "Exercises must be an array.";
exercises = exercises.map((exercise) =>
this.exerciseComponentValidator(exercise),
);
if (typeof weightGoal === "string") {
let weight = parseInt(weightGoal);
if (weight < 1) throw "Weight must be greater than zero";
if (!unitMeasure) throw "No unit measurement specified";
weightGoal = weightGoal + " " + unitMeasure;
}
if (difficulty) {
difficulty = parseInt(difficulty);
if (difficulty < 1 || difficulty > 10)
throw "Difficulty must be between 1 and 10";
}
if (restTime) {
restTime = parseInt(restTime);
if (restTime < 1 || restTime > 300)
throw "Rest time must be between 1 and 300 seconds";
}
return {
newName: name,
newWorkoutTypes: workoutTypes,
newNotes: notes,
newExercises: exercises,
newWeightGoal: weightGoal,
newDifficulty: difficulty,
newRestTime: restTime,
};
},
challengeValidator(title, description, exercises, reward) {
title = helper.inputValidator(title, "title");
description = helper.inputValidator(description, "description");
if (isNaN(reward) || reward <= 0 || !Number.isInteger(reward))
throw "Reward is not a valid number";
if (description.trim() === "") throw "Description cannot be empty";
if (!Array.isArray(exercises)) throw "Exercises must be an array";
if (exercises.length === 0) throw "Exercises cannot be empty";
exercises = exercises.map((exercise) =>
this.exerciseComponentValidator(exercise),
);
return {
title: title,
description: description,
exercises: exercises,
reward: reward,
};
},
async uploadImageToFirebase(firebasePath, imageBuffer) {
const bucket = storageFirebase.bucket();
if (typeof firebasePath !== "string") throw "firebasePath must be a String";
const path = firebasePath.trim();
let link = "";
const file = bucket.file(path, { uploadType: { resumeable: false } });
await new Promise((res) => {
file.save(imageBuffer, async (err) => {
if (err) throw err;
else {
link = await getDownloadURL(file);
res();
}
});
});
return { link: link, relPath: path };
},
};
export const xssSafe = (input) => {
// console.log(
// `DEBUG: xssSafe called with input ${input} and type ${typeof input}`,
// );
if (typeof input === "object") {
if (Array.isArray(input)) {
let parsed = input.map((e) => xssSafe(e));
return parsed;
} else {
let parsed = xss(JSON.stringify(input));
return JSON.parse(parsed);
}
} else {
let parsed = xss(input);
return parsed;
}
};
export default helper;