-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkController.ts
More file actions
283 lines (259 loc) · 7.45 KB
/
linkController.ts
File metadata and controls
283 lines (259 loc) · 7.45 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
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
import { Request, Response } from "express";
import { Op } from "sequelize";
import { v4 as uuidv4, validate as isValidUUID } from "uuid";
import db from "../models";
import { getOrgId } from "../utils/apiutils";
import { linkTypes, userRoles } from "../utils/defaults";
export const createLink = (req: Request, res: Response) => {
const DB: any = db;
const { link } = DB;
const requiredFields = ["shortLink", "fullUrl", "type", "private"];
for (let i of requiredFields) {
if (req.body[i] == null) {
return res.status(400).json({
error: `Required field \`${i}\` not provided or null`,
});
}
}
const shortLink: string = req.body["shortLink"];
const fullUrl: string = req.body["fullUrl"];
const description: string = req.body["description"];
const type: linkTypes = req.body["type"];
const isPrivate: boolean = req.body["private"];
if (!Object.values(linkTypes)?.includes(type)) {
return res.status(400).json({
error: `\`type\` can only be \`static\` or \`dynamic\``,
});
}
if (
req.auth.userRole === userRoles.global_admin &&
req.body.orgId &&
req.body.orgId !== req.auth.orgId &&
isPrivate
) {
return res.status(400).json({
error: `\`global_admin\` can only create private links in their own orgs`,
});
}
const createdBy = req.auth.userId;
const orgId = getOrgId(req);
link
.create({
id: uuidv4(),
orgId: orgId,
shortLink: shortLink,
fullUrl: fullUrl,
description: description,
private: isPrivate,
type: type,
createdBy: createdBy,
active: true,
})
.then((data: typeof link) => {
return res.status(200).json({
id: data.id,
orgId: data.orgId,
shortLink: data.shortLink,
fullUrl: data.fullUrl,
description: data.description,
type: data.type,
private: data.private,
createdBy: data.createdBy,
active: data.active,
});
})
.catch(() => {
if (typeof isPrivate !== "undefined" && isPrivate == true) {
return res
.status(400)
.json({ error: "This private shortlink is already defined" });
} else {
return res
.status(400)
.json({ error: "This shortlink is already defined for your org" });
}
});
};
export const getLinkDetails = (req: Request, res: Response) => {
const DB: any = db;
const { link } = DB;
const linkId = req.params.linkId;
if (linkId == null) {
return res.status(400).json({
error: "Required param `linkId` not provided or null",
});
}
if (!isValidUUID(linkId)) {
return res.status(400).json({
error: "Invalid value provided for `linkId`",
});
}
const orgId = getOrgId(req);
link
.findOne({
where: {
id: linkId,
[Op.or]: [
{ orgId: orgId, private: false },
{ orgId: orgId, private: true, createdBy: req.auth.userId },
],
},
})
.then((data: typeof link) => {
if (data) {
return res.json({
id: data.id,
orgId: data.orgId,
shortLink: data.shortLink,
fullUrl: data.fullUrl,
description: data.description,
active: data.active,
private: data.private,
type: data.type,
createdBy: data.createdBy,
});
} else {
res.status(404).json({ error: "Link with this id not found!" });
}
})
.catch(() =>
res.status(500).json({ error: "Could not get details for id" })
);
};
export const getAllLinks = (req: Request, res: Response) => {
const DB: any = db;
const { link } = DB;
const orgId = getOrgId(req);
link
.findAll({
where: {
[Op.or]: [
{ orgId: orgId, private: false },
{ orgId: orgId, private: true, createdBy: req.auth.userId },
],
},
})
.then((data: Array<typeof link>) => {
return res.json(
data.map((data) => {
return {
id: data.id,
shortLink: data.shortLink,
fullUrl: data.fullUrl,
description: data.description,
private: data.private,
createdBy: data.createdBy,
type: data.type,
};
})
);
})
.catch(() => {
return res.status(500).json({ error: `Unable to get links for user` });
});
};
export const updateLink = (req: Request, res: Response) => {
const DB: any = db;
const { link } = DB;
const linkId = req.params.linkId;
if (linkId == null) {
return res.status(400).json({
error: "Required param `linkId` not provided or null",
});
}
if (!isValidUUID(linkId)) {
return res.status(400).json({
error: "Invalid value provided for `linkId`",
});
}
const orgId = getOrgId(req);
if (req.auth.userRole == userRoles.global_admin && req.auth.orgId !== orgId) {
delete req.body.orgId;
}
const updateRule = { orgId: orgId, id: linkId };
// non admins can only update links they created
if (req.auth.userRole !== "admin" && req.auth.userRole !== "global_admin") {
Object.assign(updateRule, { createdBy: req.auth.userId });
}
const allowedFields = [
"description",
"shortLink",
"fullUrl",
"type",
"private",
"active",
];
const invalidFields = [];
for (let i of Object.keys(req.body)) {
if (allowedFields.indexOf(i) === -1) {
invalidFields.push(i);
}
}
if (invalidFields.length > 0) {
return res.status(400).json({
error: `Invalid fields provided for update operation \`${invalidFields}\``,
});
}
link
.update(req.body, {
where: updateRule,
returning: true,
})
.then((data: any) => {
if (data[0] == 1) {
return res.send({
id: data[1][0].dataValues.id,
orgId: data[1][0].dataValues.orgId,
shortLink: data[1][0].dataValues.shortLink,
fullUrl: data[1][0].dataValues.fullUrl,
description: data[1][0].dataValues.description,
private: data[1][0].dataValues.private,
type: data[1][0].dataValues.type,
createdBy: data[1][0].dataValues.createdBy,
});
} else {
return res.status(400).json({
error:
"Unable to update link. Ensure you the creator of this shortlink or an admin",
});
}
})
.catch(() => res.json({ error: "Could not get details for id" }));
};
export const deleteLink = (req: Request, res: Response) => {
const DB: any = db;
const { link } = DB;
const linkId = req.params.linkId;
if (linkId == null) {
return res.status(400).json({
error: "Required param `linkId` not provided or null",
});
}
if (!isValidUUID(linkId)) {
return res.status(400).json({
error: "Invalid value provided for `linkId`",
});
}
const orgId = getOrgId(req);
const deleteRule = { orgId: orgId, id: linkId };
// non admins can only delete links they created
if (req.auth.userRole !== "admin" && req.auth.userRole !== "global_admin") {
Object.assign(deleteRule, { createdBy: req.auth.userId });
}
link
.destroy({
where: deleteRule,
})
.then((data: any) => {
if (data === 1) {
return res.status(200).json({ status: `OK` });
} else {
return res.status(400).json({
error: `Unable to delete shortlink. Ensure you have access to delete this shortlink`,
});
}
})
.catch(() => {
return res.status(500).json({ error: `Error when deleting shortlink` });
});
};