/
method.js
341 lines (311 loc) · 9.23 KB
/
method.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
"use strict";
const express = require("express");
const cache = require("apicache");
const fs = require("fs");
const {
db,
as,
CREATE_METHOD,
METHOD_BY_ID,
INSERT_AUTHOR,
INSERT_LOCALIZED_TEXT,
UPDATE_METHOD,
UPDATE_AUTHOR_FIRST,
UPDATE_AUTHOR_LAST,
listUsers,
refreshSearch,
ErrorReporter
} = require("../helpers/db");
const {
setConditional,
maybeUpdateUserText,
parseGetParams,
validateUrl,
returnByType,
fixUpURLs
} = require("../helpers/things");
const logError = require("../helpers/log-error.js");
const requireAuthenticatedUser = require("../middleware/requireAuthenticatedUser.js");
const METHOD_STRUCTURE = JSON.parse(
fs.readFileSync("api/helpers/data/method-structure.json", "utf8")
);
const sharedFieldOptions = require("../helpers/shared-field-options.js");
async function getEditStaticText(params) {
let staticText = {};
try {
staticText.authors = listUsers();
} catch (e) {
logError("Error reading users in controllers/method.js getEditStaticText");
}
staticText = Object.assign({}, staticText, sharedFieldOptions);
return staticText;
}
/**
* @api {post} /method/new Create new method
* @apiGroup Methods
* @apiVersion 0.1.0
* @apiName newMethod
*
* @apiSuccess {Boolean} OK true if call was successful
* @apiSuccess {String[]} errors List of error strings (when `OK` is false)
* @apiSuccess {Object} data method data
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "OK": true,
* "data": {
* "ID": 3,
* "Description": 'foo'
* }
* }
*
* @apiError NotAuthenticated The user is not authenticated
* @apiError NotAuthorized The user doesn't have permission to perform this operation.
*
*/
async function postMethodNewHttp(req, res) {
// create new `method` in db
try {
cache.clear();
let title = req.body.title;
let body = req.body.body || req.body.summary || "";
let description = req.body.description;
let original_language = req.body.original_language || "en";
if (!title) {
return res.status(400).json({
OK: false,
errors: ["Cannot create a method without at least a title."]
});
}
const user_id = req.user.id;
const thing = await db.one(CREATE_METHOD, {
title,
body,
description,
original_language
});
req.params.thingid = thing.thingid;
await postMethodUpdateHttp(req, res);
} catch (error) {
logError(error);
res.status(400).json({ OK: false, error: error });
}
}
/**
* @api {put} /method/:id Submit a new version of a method
* @apiGroup Methods
* @apiVersion 0.1.0
* @apiName editMethodById
* @apiParam {Number} thingid Method ID
*
* @apiSuccess {Boolean} OK true if call was successful
* @apiSuccess {String[]} errors List of error strings (when `OK` is false)
* @apiSuccess {Object} data method data
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "OK": true,
* "data": {
* "ID": 3,
* "Description": 'foo'
* }
* }
*
* @apiError NotAuthenticated The user is not authenticated
* @apiError NotAuthorized The user doesn't have permission to perform this operation.
*
*/
async function getMethod(params, res) {
try {
if (Number.isNaN(params.articleid)) {
return null;
}
const articleRow = await db.one(METHOD_BY_ID, params);
const article = articleRow.results;
fixUpURLs(article);
return article;
} catch (error) {
// only log actual excaptional results, not just data not found
if (error.message !== "No data returned from the query.") {
logError(error);
}
// if no entry is found, render the 404 page
return null;
}
}
async function postMethodUpdateHttp(req, res) {
cache.clear();
const params = parseGetParams(req, "method");
const user = req.user;
const { articleid, type, view, userid, lang, returns } = params;
const newMethod = req.body;
//check if url is valid, if http or https is not detected append http
newMethod.links = newMethod.links.map((link) => {
if (!/\b(http|https)/.test(link.url)){
link.url = `http://${link.url}`;
}
return link;
});
//validate url params
const isUrlValid = validateUrl(newMethod);
if (!isUrlValid){
return res.status(400).json({
OK: false,
errors: ["Invalid link url."]
});
}
// if this is a new method, we don't have a post_date yet, so we set it here
if (!newMethod.post_date) {
newMethod.post_date = Date.now();
}
// if this is a new method, we don't have a updated_date yet, so we set it here
if (!newMethod.updated_date) {
newMethod.updated_date = Date.now();
}
// save any changes to the user-submitted text
const {
updatedText,
author,
oldArticle: oldMethod
} = await maybeUpdateUserText(req, res, "method");
const [updatedMethod, er] = getUpdatedMethod(
user,
params,
newMethod,
oldMethod
);
//get current date when user.isAdmin is false;
updatedMethod.updated_date = !user.isadmin ? 'now' : updatedMethod.updated_date;
if (!er.hasErrors()) {
if (updatedText) {
await db.tx("update-method", async t => {
await t.none(INSERT_AUTHOR, author);
await t.none(INSERT_LOCALIZED_TEXT, updatedText);
await t.none(UPDATE_METHOD, updatedMethod);
});
//if this is a new method, set creator id to userid and isAdmin
if (user.isadmin){
const creator = {
user_id: newMethod.creator ? newMethod.creator : params.userid,
thingid: params.articleid
};
const updatedBy = {
user_id : newMethod.last_updated_by ? newMethod.last_updated_by : params.userid,
thingid: params.articleid,
updated_date: newMethod.updated_date || 'now'
};
await db.tx("update-method", async t => {
await t.none(UPDATE_AUTHOR_FIRST, creator);
await t.none(UPDATE_AUTHOR_LAST, updatedBy);
});
}
} else {
await db.tx("update-method", async t => {
await t.none(INSERT_AUTHOR, author);
await t.none(UPDATE_METHOD, updatedMethod);
});
}
// the client expects this request to respond with json
// save successful response
const freshArticle = await getMethod(params, res);
res.status(200).json({
OK: true,
article: freshArticle
});
refreshSearch();
} else {
logError(er);
res.status(400).json({
OK: false,
errors: er.errors
});
}
}
function getUpdatedMethod(user, params, newMethod, oldMethod) {
const updatedMethod = Object.assign({}, oldMethod);
const er = new ErrorReporter();
const cond = (key, fn) =>
setConditional(updatedMethod, newMethod, er, fn, key);
// admin-only
if (user.isadmin) {
cond("featured", as.boolean);
cond("hidden", as.boolean);
cond("original_language", as.text);
cond("post_date", as.date);
cond("completeness", as.text);
cond("updated_date", as.date);
}
// media lists
["links", "videos", "audio"].map(key => cond(key, as.media));
// photos and files are slightly different from other media as they have a source url too
["photos", "files"].map(key => cond(key, as.sourcedMedia));
// key
[
"facilitators",
"facetoface_online_or_both",
"public_spectrum",
"open_limited",
"recruitment_method",
"level_polarization",
"level_complexity"
].map(key => cond(key, as.methodkey));
// list of keys
[
"method_types",
"scope_of_influence",
"participants_interactions",
"number_of_participants",
"decision_methods",
"if_voting",
"number_of_participants",
"purpose_method"
].map(key => cond(key, as.methodkeys));
// TODO save bookmarked on user
return [updatedMethod, er];
}
async function getMethodHttp(req, res) {
/* This is the entry point for getting an article */
const params = parseGetParams(req, "method");
const article = await getMethod(params, res);
if (!article) {
res.status(404).render("404");
return null;
}
const staticText = {};
returnByType(res, params, article, staticText, req.user);
}
async function getMethodEditHttp(req, res) {
const params = parseGetParams(req, "method");
params.view = "edit";
const article = await getMethod(params, res);
if (!article) {
res.status(404).render("404");
return null;
}
const staticText = await getEditStaticText(params);
returnByType(res, params, article, staticText, req.user);
}
async function getMethodNewHttp(req, res) {
const params = parseGetParams(req, "method");
params.view = "edit";
const article = METHOD_STRUCTURE;
const staticText = await getEditStaticText(params);
returnByType(res, params, article, staticText, req.user);
}
const router = express.Router(); // eslint-disable-line new-cap
router.get("/:thingid/edit", requireAuthenticatedUser(), getMethodEditHttp);
router.get("/new", requireAuthenticatedUser(), getMethodNewHttp);
router.post("/new", requireAuthenticatedUser(), postMethodNewHttp);
// these have to come *after* /new or BAD THINGS HAPPEN
router.get("/:thingid/", getMethodHttp);
router.post("/:thingid", requireAuthenticatedUser(), postMethodUpdateHttp);
module.exports = {
method: router,
getMethodHttp,
getMethodEditHttp,
getMethodNewHttp,
postMethodNewHttp,
postMethodUpdateHttp
};