-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
242 lines (199 loc) · 5.76 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
234
235
236
237
238
239
240
241
242
const routingConditionConversion = require("../../../../utils/routingConditionConversion");
const {
getMetadataKey,
} = require("../../../../utils/contentUtils/getMetadataKey");
const { getValueSource } = require("../../valueSource");
const { getListFromAll } = require("../../../../utils/functions/listGetters");
const { flatMap, filter, find } = require("lodash");
const { getAnswerById } = require("../../../../utils/functions/answerGetters");
const authorConditions = {
UNANSWERED: "Unanswered",
};
const getOptionsFromQuestionaire = (questionnaire) => {
const pages = flatMap(questionnaire.sections, (section) =>
flatMap(section.folders, "pages")
);
const answers = flatMap(pages, "answers");
return flatMap(filter(answers, "options"), "options");
};
const getOptionValues = (optionIds, questionnaire) => {
const options = getOptionsFromQuestionaire(questionnaire);
const optionResults = optionIds.map((id) => {
const option = find(options, { id });
const optionContent = option.value
? option.value.trim()
: option.label.trim();
const updatedContent = optionContent
.replace(/'/g, `\u2019`)
.replace(/'/g, `\u2019`)
.replace(/‘/g, `\u2019`);
return updatedContent.trim();
});
if (optionResults === undefined || optionResults.length < 0) {
return null;
} else {
return optionResults;
}
};
const checkType = (type) => {
if (!type) {
return null;
}
if (type === "Answer") {
return "answers";
}
if (type === "Metadata") {
return "metadata";
}
if (type === "List") {
return "list";
}
return null;
};
const buildAnswerObject = (
{ left, condition, secondaryCondition, right },
ctx
) => {
let returnVal = [getValueSource(ctx, left.answerId)];
if (right.type === "DateValue") {
returnVal = [
{
date: [returnVal[0]],
},
];
}
if (right === null) {
returnVal.push(null);
const finalVal = { [routingConditionConversion(condition)]: returnVal };
return finalVal;
}
if (condition === "CountOf") {
const countOfObject = [
{
count: [
{
source: checkType(left.type),
identifier: `answer${left.answerId}`,
},
],
},
right.customValue.number,
];
const finalVal = {
[routingConditionConversion(secondaryCondition)]: countOfObject,
};
return finalVal;
}
if (right.type === "SelectedOptions") {
const optionValues = [
condition !== authorConditions.UNANSWERED
? getOptionValues(right.optionIds, ctx.questionnaireJson)
: null,
{
identifier: `answer${left.answerId}`,
source: checkType(left.type),
},
];
if (condition === "NotAnyOf") {
const SelectedOptions = {
[routingConditionConversion(condition)]: [{ "any-in": optionValues }],
};
return SelectedOptions;
}
const leftSideAnswer = getAnswerById(ctx, left.answerId);
if (condition === "OneOf") {
if (
leftSideAnswer &&
leftSideAnswer.type === "MutuallyExclusive" &&
leftSideAnswer.options.length === 1
) {
const SelectedOptions = {
[routingConditionConversion("AllOf")]: optionValues,
};
return SelectedOptions;
} else {
const swapOptionValues = ([optionValues[0], optionValues[1]] = [
optionValues[1],
optionValues[0],
]);
const SelectedOptions = {
[routingConditionConversion(condition)]: swapOptionValues,
};
return SelectedOptions;
}
}
const SelectedOptions = {
[routingConditionConversion(condition)]: optionValues,
};
return SelectedOptions;
} else if (right.type === "DateValue") {
const offsetValue =
right.dateValue.offsetDirection === "Before"
? -1 * right.dateValue.offset
: right.dateValue.offset;
const dateValueRouting = {
date: [
"now",
{
years: offsetValue,
},
],
};
returnVal.push(dateValueRouting);
} else {
if (condition !== authorConditions.UNANSWERED) {
returnVal.push(right.customValue.number);
} else {
returnVal.push(null);
}
}
const finalVal = { [routingConditionConversion(condition)]: returnVal };
return finalVal;
};
const buildMetadataObject = (expression, ctx) => {
const { condition, left, right } = expression;
const returnValue = [
{
source: checkType(left.type),
identifier: getMetadataKey(ctx, left.metadataId),
},
right.customValue.text,
];
return { [routingConditionConversion(condition)]: returnValue };
};
const buildListObject = (expression, ctx) => {
const { condition, secondaryCondition, left, right } = expression;
// Error trap as currently countOf is the only condition for lists, this may be extended later on.
if (condition !== "CountOf") {
throw new Error(
`${condition} is not a valid routing condition for list type`
);
}
const list = getListFromAll(ctx, left.listId);
const countOfObject = [
{
count: [
{
source: checkType(left.type),
identifier: list.listName,
},
],
},
right.customValue.number,
];
return { [routingConditionConversion(secondaryCondition)]: countOfObject };
};
const checkValidRoutingType = (expression, ctx) => {
if (expression.left.type === "Answer") {
return buildAnswerObject(expression, ctx);
} else if (expression.left.type === "Metadata") {
return buildMetadataObject(expression, ctx);
} else if (expression.left.type === "List") {
return buildListObject(expression, ctx);
} else {
throw new Error(
`${expression.left.type} is not a valid routing answer type`
);
}
};
module.exports = checkValidRoutingType;