1 | const isArray = require('lodash/isArray');
|
2 |
|
3 | module.exports = ({
|
4 | Db,
|
5 | Entity,
|
6 | router,
|
7 | authMiddleware,
|
8 | permissionMiddleware,
|
9 | cacheMiddleware,
|
10 | asyncMiddleware,
|
11 | getConfig,
|
12 | handleResponse,
|
13 | handleError,
|
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 | router.get(
|
61 | '/entities/index.:ext?',
|
62 | asyncMiddleware(async (req, res) => {
|
63 | try {
|
64 | handleResponse(req, res, await Db(await getConfig(req.session.slug)).indexAsync());
|
65 | } catch (error) {
|
66 | handleError(req, res, error);
|
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 | router.all(
|
153 | '/entities/search?.:ext?',
|
154 | cacheMiddleware,
|
155 | asyncMiddleware(async (req, res) => {
|
156 | const include_docs = (req.query.include_docs || req.body.include_docs) !== undefined
|
157 | ? JSON.parse(req.query.include_docs || req.body.include_docs) : false;
|
158 |
|
159 | let children = (req.query.children || req.body.children) !== undefined
|
160 | ? JSON.parse((req.query.children || req.body.children)) : false;
|
161 | let parents = (req.query.parents || req.body.parents) !== undefined
|
162 | ? JSON.parse(req.query.parents || req.body.parents) : false;
|
163 |
|
164 | const trashed = (req.query.trashed || req.body.trashed) !== undefined
|
165 | ? JSON.parse(req.query.trashed || req.body.trashed) : false;
|
166 |
|
167 | const sort = (req.query.sort || req.body.sort) !== undefined
|
168 | ? req.query.sort || req.body.sort : null;
|
169 | const limit = (req.query.limit || req.body.limit) !== undefined
|
170 | ? parseInt(req.query.limit || req.body.limit, 10) : null;
|
171 |
|
172 | const bookmark = (req.query.bookmark || req.body.bookmark) !== undefined
|
173 | ? req.query.bookmark || req.body.bookmark : null;
|
174 |
|
175 | const group_field = (req.query.group_field || req.body.group_field) !== undefined
|
176 | ? req.query.group_field || req.body.group_field : null;
|
177 |
|
178 | const index = (req.query.index || req.body.index) !== undefined
|
179 | ? req.query.index || req.body.index : null;
|
180 |
|
181 | const q = req.query.query || req.query.q || req.body.query || req.body.q;
|
182 |
|
183 | if (children === true) {
|
184 | children = 1;
|
185 | }
|
186 | if (parents === true) {
|
187 | parents = 1;
|
188 | }
|
189 |
|
190 | let query = [];
|
191 |
|
192 | query.push(trashed ? 'trashed:true' : '!trashed:true');
|
193 |
|
194 | if (req.session.role === 'guest') {
|
195 | query.push('published:true');
|
196 | }
|
197 |
|
198 | if (q) {
|
199 | query.push(`(${q})`);
|
200 | }
|
201 |
|
202 | query = query.join(' AND ');
|
203 |
|
204 | const entity = Entity(await getConfig(req.session.slug));
|
205 |
|
206 | try {
|
207 | handleResponse(req, res, await entity.entitySearch({
|
208 | query,
|
209 | include_docs,
|
210 | sort,
|
211 | limit,
|
212 | bookmark,
|
213 | group_field,
|
214 | index,
|
215 | }, children, parents, req.session.role), true);
|
216 | } catch (error) {
|
217 | handleError(req, res, error);
|
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 | router.all(
|
264 | '/entities/find.:ext?',
|
265 | cacheMiddleware,
|
266 | asyncMiddleware(async (req, res) => {
|
267 | let children = (req.query.children || req.body.children) !== undefined
|
268 | ? JSON.parse((req.query.children || req.body.children)) : false;
|
269 | let parents = (req.query.parents || req.body.parents) !== undefined
|
270 | ? JSON.parse(req.query.parents || req.body.parents) : false;
|
271 | const trashed = (req.query.trashed || req.body.trashed) !== undefined
|
272 | ? JSON.parse(req.query.trashed || req.body.trashed) : false;
|
273 | const query = req.query.query || req.body.query
|
274 | ? JSON.parse(req.query.query || req.body.query) : { selector: {} };
|
275 |
|
276 | query.use_index = ['entityIndex', 'entity'];
|
277 |
|
278 | if (children === true) {
|
279 | children = 1;
|
280 | }
|
281 | if (parents === true) {
|
282 | parents = 1;
|
283 | }
|
284 |
|
285 | if (!query.selector.$and) {
|
286 | query.selector = {
|
287 | $and: [
|
288 | query.selector,
|
289 | ],
|
290 | };
|
291 | }
|
292 |
|
293 | if (trashed) {
|
294 | query.selector.$and.push({ trashed: true });
|
295 | } else {
|
296 | query.selector.$and.push({
|
297 | $or: [
|
298 | {
|
299 | trashed: {
|
300 | $exists: false,
|
301 | },
|
302 | },
|
303 | {
|
304 | trashed: false,
|
305 | },
|
306 | ],
|
307 | });
|
308 | }
|
309 |
|
310 | if (req.session.role === 'guest') {
|
311 | query.selector.$and.push({ published: true });
|
312 | }
|
313 |
|
314 | if (req.query.limit) {
|
315 | query.limit = parseInt(req.query.limit, 10);
|
316 | }
|
317 |
|
318 | const entity = Entity(await getConfig(req.session.slug));
|
319 |
|
320 | try {
|
321 | handleResponse(req, res, await entity.entityFind(query, children, parents, req.session.role), true);
|
322 | } catch (error) {
|
323 | handleError(req, res, error);
|
324 | }
|
325 | })
|
326 | );
|
327 |
|
328 | router.get(
|
329 | '/entities/field.:ext?',
|
330 | cacheMiddleware,
|
331 | asyncMiddleware(async (req, res) => {
|
332 | const entity = Entity(await getConfig(req.session.slug));
|
333 |
|
334 | try {
|
335 | handleResponse(req, res, await entity.fieldValues(req.query.slug || req.query.fieldSlug, req.query.searchTerm), true);
|
336 | } catch (error) {
|
337 | handleError(res, error);
|
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 | router.all(
|
386 | '/entities.:ext?',
|
387 | cacheMiddleware,
|
388 | asyncMiddleware(async (req, res) => {
|
389 | let children = (req.query.children || req.body.children) !== undefined
|
390 | ? JSON.parse(req.query.children || req.body.children) : false;
|
391 | let parents = (req.query.parents || req.body.parents) !== undefined
|
392 | ? JSON.parse(req.query.parents || req.body.parents) : false;
|
393 |
|
394 | if (children === true) {
|
395 | children = 1;
|
396 | }
|
397 | if (parents === true) {
|
398 | parents = 1;
|
399 | }
|
400 |
|
401 | let ids = req.query.ids || req.query.id || req.body.ids || req.body.id;
|
402 |
|
403 | if (ids) {
|
404 | ids = isArray(ids) ? ids : [ids];
|
405 | }
|
406 |
|
407 | const entity = Entity(await getConfig(req.session.slug));
|
408 |
|
409 | try {
|
410 | handleResponse(req, res, await entity.entityList(ids, children, parents, req.session.role), true);
|
411 | } catch (error) {
|
412 | handleError(req, res, error);
|
413 | }
|
414 | })
|
415 | );
|
416 |
|
417 | router.get(
|
418 | '/entity/revisions.:ext?',
|
419 | authMiddleware,
|
420 | permissionMiddleware.bind(null, 'entityRead'),
|
421 | asyncMiddleware(async (req, res) => {
|
422 | const entity = Entity(await getConfig(req.session.slug));
|
423 |
|
424 | try {
|
425 | handleResponse(req, res, await entity.entityRevisions(req.query.id));
|
426 | } catch (error) {
|
427 | handleError(req, res, error);
|
428 | }
|
429 | })
|
430 | );
|
431 |
|
432 | router.post(
|
433 | '/entity.:ext?',
|
434 | authMiddleware,
|
435 | permissionMiddleware.bind(null, 'entityCreate'),
|
436 | asyncMiddleware(async (req, res) => {
|
437 | const entity = Entity(await getConfig(req.session.slug));
|
438 |
|
439 | try {
|
440 | handleResponse(req, res, await entity.entityCreate(req.body.entity));
|
441 | } catch (error) {
|
442 | handleError(req, res, error);
|
443 | }
|
444 | })
|
445 | );
|
446 |
|
447 | router.get(
|
448 | '/entity.:ext?',
|
449 | authMiddleware,
|
450 | permissionMiddleware.bind(null, 'entityRead'),
|
451 | asyncMiddleware(async (req, res) => {
|
452 | const entity = Entity(await getConfig(req.session.slug));
|
453 |
|
454 | try {
|
455 | handleResponse(req, res, await entity.entityRead(req.query.id));
|
456 | } catch (error) {
|
457 | handleError(req, res, error);
|
458 | }
|
459 | })
|
460 | );
|
461 |
|
462 | router.put(
|
463 | '/entity.:ext?',
|
464 | authMiddleware,
|
465 | permissionMiddleware.bind(null, 'entityUpdate'),
|
466 | asyncMiddleware(async (req, res) => {
|
467 | const entity = Entity(await getConfig(req.session.slug));
|
468 |
|
469 | try {
|
470 | handleResponse(req, res, await entity.entityUpdate(req.body.entity || req.body.entities, req.body.restore || false));
|
471 | } catch (error) {
|
472 | handleError(req, res, error);
|
473 | }
|
474 | })
|
475 | );
|
476 |
|
477 | router.delete(
|
478 | '/entity.:ext?',
|
479 | authMiddleware,
|
480 | permissionMiddleware.bind(null, 'entityDelete'),
|
481 | asyncMiddleware(async (req, res) => {
|
482 | const entity = Entity(await getConfig(req.session.slug));
|
483 |
|
484 | try {
|
485 | handleResponse(req, res, await entity.entityDelete(req.body.entity || req.body.entities, req.body.forever || false));
|
486 | } catch (error) {
|
487 | handleError(req, res, error);
|
488 | }
|
489 | })
|
490 | );
|
491 |
|
492 | router.delete(
|
493 | '/entity/trashed.:ext?',
|
494 | authMiddleware,
|
495 | permissionMiddleware.bind(null, 'entityDelete'),
|
496 | asyncMiddleware(async (req, res) => {
|
497 | const entity = Entity(await getConfig(req.session.slug));
|
498 |
|
499 | try {
|
500 | handleResponse(req, res, await entity.entityDelete('trashed'));
|
501 | } catch (error) {
|
502 | handleError(req, res, error);
|
503 | }
|
504 | })
|
505 | );
|
506 |
|
507 | };
|