-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
233 lines (202 loc) · 8.66 KB
/
index.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
"use strict";
// @IMPORTS
const Application = require("neat-base").Application;
const Module = require("neat-base").Module;
const Tools = require("neat-base").Tools;
const fs = require("fs");
const path = require("path");
const Promise = require("bluebird");
module.exports = class StructuredData extends Module {
static defaultConfig() {
return {
dbModuleName: "database",
configpath: "config/structured-data"
}
}
/**
*
*/
init() {
return new Promise((resolve, reject) => {
this.log.debug("Initializing...");
resolve(this);
});
}
loadConfig(configName) {
return require(path.resolve(path.join(Application.config.root_path, this.config.configpath, configName + ".js")));
}
getData(configName, doc) {
return new Promise((resolve, reject) => {
let config = null;
try {
config = this.loadConfig(configName);
} catch (e) {
return reject(e);
}
let populateProm = Promise.resolve();
if (config.populate) {
if (!config.populate instanceof Array) {
config.populate = [config.populate];
}
populateProm = Promise.map(config.populate, (field) => {
return doc.populate(field).execPopulate();
});
}
return populateProm.then(() => {
return this.getDataFromDocument(doc, config, config);
}, reject).then(resolve, reject);
});
}
getDataFromDocument(doc, config, mainConfig) {
if (config.groups) {
return Promise.map(config.groups, (group) => {
this.log.debug("Processing group " + group.label);
return this.getDataFromDocument(doc, group, mainConfig).then((data) => {
if ((!data || !data.length) && !group.keepEmpty) {
this.log.debug("Dropping group " + group.label + " got empty data and !keepEmpty");
return;
}
return this.checkUseOfField(group, doc).then((val) => {
if (!val) {
this.log.debug("Not keeping group " + group.label + " condition failed");
return;
}
return {
type: "group",
label: group.label,
key: group.key,
data: data
}
});
});
}).then((data) => {
return data.filter(v => !!v);
});
} else if (config.fields) {
return Promise.map(config.fields, (field) => {
this.log.debug("Processing field " + field.label);
if (field.groups) {
// field is a new array of groups
return this.getDataFromDocument(doc, field, mainConfig);
} else if (field.fields) {
// field is a group itself
return this.getDataFromDocument(doc, field, mainConfig).then((subgroupFields) => {
if ((!subgroupFields || !subgroupFields.length) && !field.keepEmpty) {
this.log.debug("Dropping subgroup " + field.label + " got empty data and !keepEmpty");
return;
}
return this.checkUseOfField(field, doc).then((val) => {
if (!val) {
this.log.debug("Not keeping subgroup " + field.label + " condition failed");
return;
}
return {
type: "group",
label: field.label,
key: field.key,
data: subgroupFields
}
});
});
}
let valPromise = Promise.resolve(null);
if (field.get) {
this.log.debug("Found get function on field " + field.label);
let getResult = field.get(doc);
if (getResult instanceof Promise) {
this.log.debug("Found Promise on field " + field.label);
valPromise = getResult;
} else {
this.log.debug("Found regular return on field " + field.label + " of type " + typeof getResult);
valPromise = Promise.resolve(getResult);
}
} else if (field.path) {
this.log.debug("Found path on field " + field.label);
valPromise = Promise.resolve(doc.get(field.path));
}
return valPromise.then((realVal) => {
if ((realVal === null || realVal === undefined || realVal === "") && !field.keepEmpty) {
this.log.debug("Not keeping field " + field.label + " empty value and !keepEmpty");
return;
}
return this.checkUseOfField(field, doc).then((use) => {
if (!use) {
this.log.debug("Not keeping field " + field.label + " condition failed");
return;
}
let displayValue = realVal;
if (field.map) {
let tempVal = realVal;
if (typeof realVal !== "string") {
tempVal = String(realVal);
}
displayValue = field.map[tempVal] || realVal;
}
if (field.unit) {
if (typeof field.unit === "string") {
displayValue += field.unit;
} else if (field.unit instanceof Array) {
if (field.unit.length === 2) {
if (realVal === 1) {
displayValue += field.unit[0];
} else {
displayValue += field.unit[1];
}
}
}
}
return {
type: "field",
label: field.label,
value: realVal,
display: mainConfig.forceStringDisplay ? displayValue + "" : displayValue,
path: field.path,
}
});
});
}).then((data) => {
return data.filter(v => !!v);
});
}
}
checkUseOfField(field, doc) {
let usePromise = Promise.resolve(true);
if (field.if) {
usePromise = usePromise.then(() => {
return Promise.map(Object.keys(field.if), (key) => {
let ifCondition = field.if[key];
if (typeof ifCondition !== "function") {
if (doc.get(key) == ifCondition) {
return true;
} else {
return false;
}
} else {
return ifCondition(doc);
}
}).then((ifConditionresults) => {
return ifConditionresults.indexOf(false) === -1;
});
});
}
if (field.unless) {
usePromise = usePromise.then(() => {
return Promise.map(Object.keys(field.unless), (key) => {
let unlessCondition = field.unless[key];
if (typeof unlessCondition !== "function") {
if (doc.get(key) != unlessCondition) {
return true;
} else {
return false;
}
} else {
return unlessCondition(doc);
}
}).then((unlessConditionresults) => {
return unlessConditionresults.indexOf(false) === -1;
});
});
}
return usePromise;
}
}