1 | 'use strict';
|
2 |
|
3 | const util = require('util');
|
4 | const moment = require('moment-timezone');
|
5 | const content = require('./content/utils');
|
6 | const routes = require('./routes/utils');
|
7 | const _ = require('lodash');
|
8 | const Promise = require('bluebird');
|
9 |
|
10 | moment.tz.setDefault('UTC');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | const singleItem = (key, value, arr) => {
|
22 | const filtered = arr.filter(type => {
|
23 | if (type[key] === value) {
|
24 | return true;
|
25 | }
|
26 |
|
27 | return false;
|
28 | });
|
29 |
|
30 | if (filtered.length === 0) {
|
31 | return false;
|
32 | }
|
33 |
|
34 | return filtered[0];
|
35 | };
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | const log = (object) => {
|
44 |
|
45 | console.log(util.inspect(object, false, null));
|
46 | };
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | const isoTime = (date, time, zone) => {
|
58 | if (date === '' || date === undefined || time === '' || time === undefined || zone === '' || zone === undefined) {
|
59 | return null;
|
60 | }
|
61 |
|
62 | const converted = moment.tz(`${date} ${time}`, zone);
|
63 |
|
64 | return converted.toISOString();
|
65 | };
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | const inputTime = (date, zone) => {
|
85 | let tz = zone;
|
86 |
|
87 | if (date === '' || date === undefined || date === null) {
|
88 | return null;
|
89 | }
|
90 |
|
91 | if (tz === undefined || tz === null) {
|
92 | tz = 'UTC';
|
93 | }
|
94 |
|
95 |
|
96 | return {
|
97 | date: moment(date).tz('America/New_York').format('YYYY-MM-DD'),
|
98 | time: moment(date).tz('America/New_York').format('HH:mm'),
|
99 | zone: 'America/New_York',
|
100 | };
|
101 | };
|
102 |
|
103 | const time = {
|
104 | iso: isoTime,
|
105 | input: inputTime,
|
106 | };
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | const config = values => {
|
113 | const result = {};
|
114 |
|
115 | Object.keys(values).map(value => {
|
116 | const split = value.split('--');
|
117 | const plugin = split[0];
|
118 | const input = split[1];
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | if (!result.hasOwnProperty(plugin)) {
|
124 | result[plugin] = {};
|
125 | }
|
126 |
|
127 | result[plugin][input] = {
|
128 | value: values[value],
|
129 | };
|
130 | });
|
131 |
|
132 | return result;
|
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 | const format = body => {
|
160 | const data = {};
|
161 | const single = Object.keys(body).filter(input => {
|
162 | if ((input === 'sunset-date') || (input === 'sunset-time') || (input === 'sunrise-date') || (input === 'sunrise-time')) {
|
163 | data[input] = { 'value': body[input] };
|
164 |
|
165 | return false;
|
166 | }
|
167 | if (input.split('--').length < 3) {
|
168 | return true;
|
169 | }
|
170 |
|
171 | return false;
|
172 | });
|
173 | const multiple = Object.keys(body).filter(input => {
|
174 | if (input.split('--').length >= 3) {
|
175 | return true;
|
176 | }
|
177 |
|
178 | return false;
|
179 | });
|
180 |
|
181 | single.forEach(input => {
|
182 | const inputs = input.split('--');
|
183 | const inputName = inputs[0];
|
184 | const inputType = inputs[1];
|
185 | if (!data.hasOwnProperty(inputName)) {
|
186 |
|
187 | data[inputName] = {};
|
188 | }
|
189 | data[inputName][inputType] = { 'value': body[input] };
|
190 | });
|
191 |
|
192 | multiple.forEach(input => {
|
193 | const inputs = input.split('--');
|
194 | const inputName = inputs[0];
|
195 | const inputType = inputs[1];
|
196 | const index = inputs[2];
|
197 | if (!data.hasOwnProperty(inputName)) {
|
198 |
|
199 | data[inputName] = [];
|
200 | }
|
201 | if (!data[inputName][index]) {
|
202 |
|
203 | data[inputName][index] = {};
|
204 | }
|
205 |
|
206 |
|
207 | data[inputName][index][inputType] = { 'value': body[input] };
|
208 | });
|
209 |
|
210 |
|
211 | Object.keys(data).forEach(key => {
|
212 | if (Array.isArray(data[key])) {
|
213 | data[key] = data[key].filter(input => {
|
214 | return Object.keys(input).filter(type => {
|
215 | if (input[type].value !== null && input[type].value !== '' && input[type].value !== []) {
|
216 | return true;
|
217 | }
|
218 |
|
219 | return false;
|
220 | }).length > 0;
|
221 | });
|
222 |
|
223 |
|
224 | if (data[key].length === 0) {
|
225 | delete data[key];
|
226 | }
|
227 | }
|
228 | });
|
229 |
|
230 | return data;
|
231 | };
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 | const references = (types) => {
|
254 | const cts = _.cloneDeep(types);
|
255 | const refs = [];
|
256 | const position = {};
|
257 |
|
258 |
|
259 | cts.forEach((type, index) => {
|
260 | if (!position.hasOwnProperty(type.id)) {
|
261 | position[type.id] = index;
|
262 | }
|
263 | });
|
264 |
|
265 |
|
266 | cts.forEach(ct => {
|
267 | ct.attributes.forEach((attr, index) => {
|
268 | let inputs;
|
269 | let length;
|
270 |
|
271 |
|
272 | if (Array.isArray(attr.inputs)) {
|
273 | inputs = attr.inputs[0];
|
274 | length = attr.inputs.length;
|
275 | }
|
276 | else {
|
277 | inputs = attr.inputs;
|
278 | }
|
279 |
|
280 |
|
281 | Object.keys(inputs).forEach(input => {
|
282 |
|
283 | if (inputs[input].hasOwnProperty('reference') && inputs[input].reference === true) {
|
284 |
|
285 | if (!_.get(inputs[input], 'settings.contentType')) {
|
286 | throw new Error('Reference must have a content type');
|
287 | }
|
288 |
|
289 | if (!position.hasOwnProperty(inputs[input].settings.contentType)) {
|
290 | throw new Error(`Content Type ${inputs[input].settings.contentType} is not valid`);
|
291 | }
|
292 |
|
293 | if (_.get(inputs[input], 'settings.view') === 'radio') {
|
294 | inputs[input].type = 'radio';
|
295 | }
|
296 |
|
297 |
|
298 | refs.push({
|
299 | type: ct.id,
|
300 | attr: index,
|
301 | input,
|
302 | length,
|
303 | ct: inputs[input].settings.contentType,
|
304 | });
|
305 | }
|
306 | });
|
307 | });
|
308 | });
|
309 |
|
310 | return {
|
311 | cts,
|
312 | references: refs,
|
313 | };
|
314 | };
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 | const fill = (types, type, refs, database) => {
|
329 | const ct = _.cloneDeep(type);
|
330 |
|
331 | if (!ct) {
|
332 | return new Promise(resolve => {
|
333 | resolve(ct);
|
334 | });
|
335 | }
|
336 | const ref = refs.filter(reference => {
|
337 | return ct.id === reference.type;
|
338 | });
|
339 |
|
340 | return Promise.map(ref, input => {
|
341 |
|
342 | const where = database
|
343 | .max('revision')
|
344 | .from(`content-type--${input.ct}`)
|
345 | .groupBy('id');
|
346 |
|
347 |
|
348 | return database
|
349 | .select('id', 'revision', 'value')
|
350 | .from(`content-type--${input.ct}`)
|
351 | .whereIn('revision', where).then(rws => {
|
352 |
|
353 | const refCT = types.find((val) => {
|
354 | return val.id === input.ct;
|
355 | });
|
356 | const rows = routes.identifier(rws, refCT);
|
357 | const options = rows.map(row => {
|
358 | return {
|
359 | value: row.id,
|
360 | label: row.identifier,
|
361 | };
|
362 | });
|
363 |
|
364 |
|
365 | if (input.length) {
|
366 | for (let i = 0; i < input.length; i++) {
|
367 | const inp = ct.attributes[input.attr].inputs[i][input.input];
|
368 |
|
369 | if (inp.type === 'select') {
|
370 | options.unshift({
|
371 | value: '',
|
372 | label: inp.settings.placeholder,
|
373 | });
|
374 | }
|
375 | inp.options = options;
|
376 | }
|
377 | }
|
378 | else {
|
379 | const inp = ct.attributes[input.attr].inputs[input.input];
|
380 |
|
381 | if (inp.type === 'select') {
|
382 | options.unshift({
|
383 | value: '',
|
384 | label: inp.settings.placeholder,
|
385 | });
|
386 | }
|
387 | inp.options = options;
|
388 | }
|
389 | });
|
390 | }).then(() => {
|
391 | return ct;
|
392 | });
|
393 | };
|
394 |
|
395 | module.exports = {
|
396 | content,
|
397 | routes,
|
398 | singleItem,
|
399 | log,
|
400 | time,
|
401 | config,
|
402 | format,
|
403 | fill,
|
404 | references,
|
405 | };
|