-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
164 lines (136 loc) · 3.78 KB
/
utils.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
const { validationResult } = require("express-validator")
const { User, Story, Comment, Like, Topic, Tag } = require("./db/models");
function asyncHandler(handler) {
return (req, res, next) => {
return handler(req, res, next).catch(next)
}
}
function getCookies(cookies) {
if (cookies) {
const rawCookies = cookies.split("; ");
const bakedCookies = {}
rawCookies.forEach(cookie => {
const bakedCookie = cookie.split("=")
bakedCookies[bakedCookie[0]] = bakedCookie[1]
})
return bakedCookies
}
return {}
}
function deleteCookies() {
const rawCookies = document.cookie.split("; ")
rawCookies.forEach(cookie => {
const localHostName = window.location.hostname.split(".")
while (localHostName.length > 0) {
const cookieBase = encodeURIComponent(rawCookies[cookie]
.split(";")[0].split("=")[0]) + "=; expires=Thu, 01-Jan-1970 00:00:01 GMT; domain=" + localHostName.join(".") + " ;path="
const path = location.pathname.split('/')
document.cookie = cookieBase + "/"
while (path.length > 0) {
document.cookie = cookieBase + path.join("/")
path.pop()
}
localHostName.shift()
}
})
}
function handleValidationErrors(req, res, next) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
const errors = validationErrors.array().map(error => error.msg);
const err = Error("Bad request.");
err.errors = errors;
err.title = "400 Bad request.";
err.status = 400;
return next(err);
} else next();
}
function contentNotFound(id, contentType) {
const err = new Error(`${contentType} id ${id} could not be found.`);
err.title = `404 ${contentType} not found`;
err.status = 404;
return err;
}
async function deleteForStory(id, Model) {
const records = await Model.findAll({ where: { storyId: id } })
console.log(records)
records.forEach(async record => await record.destroy())
}
async function checkForStory(req, res, next) {
const story = await Story.findByPk(req.params.id)
if (!story) next(contentNotFound(req.params.id, "Story"))
else {
req.story = story
next()
}
}
async function checkForUser(req, res, next) {
const user = await User.findByPk(req.params.id)
if (!user) next(contentNotFound(req.params.id, "User"))
else {
req.user = user
next()
}
}
async function checkForComment(req, res, next) {
const comment = await Comment.findByPk(req.params.id)
if (!comment) next(contentNotFound(req.params.id, "Comment"))
else {
req.comment = comment
next()
}
}
// Provide a 'createdAt' value and receive a string in form 'Jan 01 2020'
function getDate(createdAt) {
let parsedDate = new Date(createdAt)
return parsedDate.toDateString().slice(4)
}
// Provide list of objects with 'createdAt' property to update to form 'Jan 01 2020'.
function getDates(content) {
return content.map(item => {
item.createdAt = getDate(item.createdAt)
return item
})
}
function rankStories(stories) {
let count = 1
stories = stories.slice(0, 6)
return stories.map(story => {
story.rank = count
count++
return story
})
}
const userAttributes = ["id", "firstName", "lastName", "bio"]
const storyAttributes = ["id", "title", "subtitle", "createdAt", "authorId"]
const storyInclude = [{
model: User,
as: "Author",
attributes: userAttributes
}, {
model: Comment,
attributes: ["id"],
}, {
model: Like,
attributes: ["id"]
}, {
model: Tag,
attributes: ["topicId"],
include: { model: Topic, attributes: ["id", "topic"] }
}]
module.exports = {
asyncHandler,
getCookies,
deleteCookies,
handleValidationErrors,
contentNotFound,
deleteForStory,
checkForStory,
checkForUser,
checkForComment,
storyInclude,
userAttributes,
storyAttributes,
getDate, getDates,
rankStories
}