-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
120 lines (91 loc) · 2.68 KB
/
Copy pathhelpers.js
File metadata and controls
120 lines (91 loc) · 2.68 KB
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
const {
userDatabase,
urlDatabase
} = require('./data');
const generateRandomString = () => {
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0213456789';
let result = '';
for (let i = 0; i < 6; i++) {
result += chars.charAt(Math.random() * chars.length);
}
return result;
};
// Function to find user data based on certain value
const getUserByEmail = (email, database = userDatabase) => {
for (const data in database) {
if (database[data].email === email) {
return database[data];
}
}
return undefined;
};
const getCurrentUserByCookie = (req, database = userDatabase) => {
return database[req.session.userID];
};
// Functions to check for data of user
const urlsForUser = (userID, database = urlDatabase) => {
let userURLS = {};
for (const data in database) {
if (database[data].userID === userID) {
userURLS[data] = database[data];
}
}
return userURLS;
};
const userOwnsURL = (userID, urlID, database = urlDatabase) => {
const userURLs = urlsForUser(userID, database);
if (!userURLs[urlID] || userURLs[urlID] === undefined) {
return false;
}
return true;
};
// Functions to build link visit data
const getTime = num => {
const date = new Date();
let timeSuffix = 'AM';
if (num > 12) {
num = num % 12;
timeSuffix = 'PM';
}
if (num === 0) {
num = 12;
}
return `${num}:${date.getMinutes()}:${date.getSeconds()} ${timeSuffix}`;
};
const getTimeStamp = () => {
const date = new Date();
const months = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sep', 'Oct.', 'Nov.', 'Dec.'];
const month = months[date.getMonth()];
return `${month} ${date.getDate()}, ${date.getFullYear()} @ ${getTime(date.getHours())}`;
};
const trackVisit = () => {
const timestamp = getTimeStamp();
const visitorID = generateRandomString();
return {timestamp, visitorID};
};
// Functions to send user errors
const displayErrorMsg = (res, status, errMsg, returnLink) => {
console.log(`Error ${status}: ${errMsg}`);
res.statusCode = status;
return res.send(`<h3>Error ${status}</h3>
<p>${errMsg}.<br/><br/>
<b><a href="${returnLink}">Return</a></b></p>\n`);
};
const display404ErrorMsg = (res, errMsg = 'Page not found', returnLink = '/urls') => {
return displayErrorMsg(res, 404, errMsg, returnLink);
};
const display403ErrorMsg = (res, errMsg = 'Please login to proceed', returnLink = '/login') => {
return displayErrorMsg(res, 403, errMsg, returnLink);
};
module.exports = {
generateRandomString,
getUserByEmail,
getCurrentUserByCookie,
displayErrorMsg,
display404ErrorMsg,
display403ErrorMsg,
urlsForUser,
userOwnsURL,
getTimeStamp,
trackVisit
};