@iapps/d2-web-sdk 1.1.7 → 1.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/lib/modules/engine/program-rule/program-rule.engine.d.ts +33 -5
- package/src/lib/modules/engine/program-rule/program-rule.engine.js +183 -47
- package/src/lib/modules/engine/program-rule/program-rule.engine.js.map +1 -1
- package/src/lib/modules/engine/program-rule/utils/d2-functions.util.d.ts +6 -2
- package/src/lib/modules/engine/program-rule/utils/d2-functions.util.js +750 -293
- package/src/lib/modules/engine/program-rule/utils/d2-functions.util.js.map +1 -1
- package/src/lib/modules/engine/program-rule/utils/run-d2-expression.util.d.ts +12 -4
- package/src/lib/modules/engine/program-rule/utils/run-d2-expression.util.js +363 -67
- package/src/lib/modules/engine/program-rule/utils/run-d2-expression.util.js.map +1 -1
- package/src/lib/modules/event/models/data-value.model.d.ts +1 -0
- package/src/lib/modules/event/models/event.model.d.ts +11 -0
- package/src/lib/modules/event/models/event.model.js +32 -0
- package/src/lib/modules/event/models/event.model.js.map +1 -1
- package/src/lib/modules/program/models/program-section.model.d.ts +2 -1
- package/src/lib/modules/program/models/program-section.model.js +1 -0
- package/src/lib/modules/program/models/program-section.model.js.map +1 -1
- package/src/lib/modules/program/models/program-stage-section.model.d.ts +2 -1
- package/src/lib/modules/program/models/program-stage-section.model.js +1 -0
- package/src/lib/modules/program/models/program-stage-section.model.js.map +1 -1
- package/src/lib/modules/tracker/helpers/form.helper.d.ts +17 -0
- package/src/lib/modules/tracker/helpers/form.helper.js +64 -0
- package/src/lib/modules/tracker/helpers/form.helper.js.map +1 -0
- package/src/lib/modules/tracker/models/enrollment.model.d.ts +1 -0
- package/src/lib/modules/tracker/models/enrollment.model.js +1 -0
- package/src/lib/modules/tracker/models/enrollment.model.js.map +1 -1
- package/src/lib/modules/tracker/models/tracked-entity-instance.model.d.ts +3 -1
- package/src/lib/modules/tracker/models/tracked-entity-instance.model.js +166 -31
- package/src/lib/modules/tracker/models/tracked-entity-instance.model.js.map +1 -1
- package/src/lib/modules/user/user.module.js +1 -1
- package/src/lib/modules/user/user.module.js.map +1 -1
- package/src/lib/shared/models/pager.model.d.ts +5 -0
- package/src/lib/shared/models/pager.model.js +48 -8
- package/src/lib/shared/models/pager.model.js.map +1 -1
|
@@ -1,8 +1,519 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
// // @flow
|
|
3
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
5
|
exports.d2FunctionsEval = exports.d2FuctionsVariables = void 0;
|
|
5
6
|
const tslib_1 = require("tslib");
|
|
7
|
+
// import dateUtils from './date.utils';
|
|
8
|
+
// import getZScoreWFA from './z-score-wfa.util';
|
|
9
|
+
// import trimQuotes from './trim-quotes.util';
|
|
10
|
+
// import { ValueConverter } from './value-converter.util';
|
|
11
|
+
// export const d2FuctionsVariables: Array<{
|
|
12
|
+
// name: string;
|
|
13
|
+
// parameters?: number;
|
|
14
|
+
// }> = [
|
|
15
|
+
// { name: 'd2:daysBetween', parameters: 2 },
|
|
16
|
+
// { name: 'd2:weeksBetween', parameters: 2 },
|
|
17
|
+
// { name: 'd2:monthsBetween', parameters: 2 },
|
|
18
|
+
// { name: 'd2:yearsBetween', parameters: 2 },
|
|
19
|
+
// { name: 'd2:floor', parameters: 1 },
|
|
20
|
+
// { name: 'd2:modulus', parameters: 2 },
|
|
21
|
+
// { name: 'd2:concatenate' },
|
|
22
|
+
// { name: 'd2:addDays', parameters: 2 },
|
|
23
|
+
// { name: 'd2:zing', parameters: 1 },
|
|
24
|
+
// { name: 'd2:oizp', parameters: 1 },
|
|
25
|
+
// { name: 'd2:count', parameters: 1 },
|
|
26
|
+
// { name: 'd2:countIfZeroPos', parameters: 1 },
|
|
27
|
+
// { name: 'd2:countIfValue', parameters: 2 },
|
|
28
|
+
// { name: 'd2:ceil', parameters: 1 },
|
|
29
|
+
// { name: 'd2:round', parameters: 1 },
|
|
30
|
+
// { name: 'd2:hasValue', parameters: 1 },
|
|
31
|
+
// { name: 'd2:lastEventDate', parameters: 1 },
|
|
32
|
+
// { name: 'd2:validatePattern', parameters: 2 },
|
|
33
|
+
// { name: 'd2:addControlDigits', parameters: 1 },
|
|
34
|
+
// { name: 'd2:checkControlDigits', parameters: 1 },
|
|
35
|
+
// { name: 'd2:left', parameters: 2 },
|
|
36
|
+
// { name: 'd2:right', parameters: 2 },
|
|
37
|
+
// { name: 'd2:substring', parameters: 3 },
|
|
38
|
+
// { name: 'd2:split', parameters: 3 },
|
|
39
|
+
// { name: 'd2:zScoreWFA', parameters: 3 },
|
|
40
|
+
// { name: 'd2:length', parameters: 1 },
|
|
41
|
+
// ];
|
|
42
|
+
// export const d2FunctionsEval: { [x: string]: Function } = {
|
|
43
|
+
// 'd2:hasValue': (
|
|
44
|
+
// expression: any,
|
|
45
|
+
// parameters: Array<string>,
|
|
46
|
+
// variableHash: any,
|
|
47
|
+
// regexFunct: string
|
|
48
|
+
// ) => {
|
|
49
|
+
// const [value] = parameters || [];
|
|
50
|
+
// const valueFound =
|
|
51
|
+
// !!value ||
|
|
52
|
+
// Number(value) === 0 ||
|
|
53
|
+
// ValueConverter.toBoolean(value) === false;
|
|
54
|
+
// //Replace the end evaluation of the dhis function:
|
|
55
|
+
// const newExpression = expression.replace(regexFunct, valueFound);
|
|
56
|
+
// const expressionUpdated = true;
|
|
57
|
+
// return { expression: newExpression, expressionUpdated };
|
|
58
|
+
// },
|
|
59
|
+
// 'd2:daysBetween': (
|
|
60
|
+
// expression: any,
|
|
61
|
+
// parameters: Array<string>,
|
|
62
|
+
// variableHash: any,
|
|
63
|
+
// regexFunct: string
|
|
64
|
+
// ) => {
|
|
65
|
+
// const [date1, date2] = parameters;
|
|
66
|
+
// const daysBetween = dateUtils.daysBetween(date1, date2);
|
|
67
|
+
// //Replace the end evaluation of the dhis function:
|
|
68
|
+
// const newExpression = expression.replace(regexFunct, daysBetween);
|
|
69
|
+
// const expressionUpdated = true;
|
|
70
|
+
// return { expression: newExpression, expressionUpdated };
|
|
71
|
+
// },
|
|
72
|
+
// 'd2:weeksBetween': (
|
|
73
|
+
// expression: any,
|
|
74
|
+
// parameters: Array<string>,
|
|
75
|
+
// variableHash: any,
|
|
76
|
+
// regexFunct: string
|
|
77
|
+
// ) => {
|
|
78
|
+
// const [date1, date2] = parameters;
|
|
79
|
+
// const weeksBetween = dateUtils.weeksBetween(date1, date2);
|
|
80
|
+
// //Replace the end evaluation of the dhis function:
|
|
81
|
+
// const newExpression = expression.replace(regexFunct, weeksBetween);
|
|
82
|
+
// const expressionUpdated = true;
|
|
83
|
+
// return { expression: newExpression, expressionUpdated };
|
|
84
|
+
// },
|
|
85
|
+
// 'd2:monthsBetween': (
|
|
86
|
+
// expression: any,
|
|
87
|
+
// parameters: Array<string>,
|
|
88
|
+
// variableHash: any,
|
|
89
|
+
// regexFunct: string
|
|
90
|
+
// ) => {
|
|
91
|
+
// const [date1, date2] = parameters;
|
|
92
|
+
// const monthsBetween = dateUtils.monthsBetween(date1, date2);
|
|
93
|
+
// //Replace the end evaluation of the dhis function:
|
|
94
|
+
// const newExpression = expression.replace(regexFunct, monthsBetween);
|
|
95
|
+
// const expressionUpdated = true;
|
|
96
|
+
// return { expression: newExpression, expressionUpdated };
|
|
97
|
+
// },
|
|
98
|
+
// 'd2:yearsBetween': (
|
|
99
|
+
// expression: any,
|
|
100
|
+
// parameters: Array<string>,
|
|
101
|
+
// variableHash: any,
|
|
102
|
+
// regexFunct: string
|
|
103
|
+
// ) => {
|
|
104
|
+
// const [date1, date2] = parameters;
|
|
105
|
+
// const yearsBetween = dateUtils.yearsBetween(date1, date2);
|
|
106
|
+
// //Replace the end evaluation of the dhis function:
|
|
107
|
+
// const newExpression = expression.replace(regexFunct, yearsBetween);
|
|
108
|
+
// const expressionUpdated = true;
|
|
109
|
+
// return { expression: newExpression, expressionUpdated };
|
|
110
|
+
// },
|
|
111
|
+
// 'd2:floor': (
|
|
112
|
+
// expression: any,
|
|
113
|
+
// parameters: Array<number>,
|
|
114
|
+
// variableHash: any,
|
|
115
|
+
// regexFunct: string
|
|
116
|
+
// ) => {
|
|
117
|
+
// const [date1, ...rest] = parameters;
|
|
118
|
+
// const floored = Math.floor(date1);
|
|
119
|
+
// //Replace the end evaluation of the dhis function:
|
|
120
|
+
// const newExpression = expression.replace(regexFunct, floored);
|
|
121
|
+
// const expressionUpdated = true;
|
|
122
|
+
// return { expression: newExpression, expressionUpdated };
|
|
123
|
+
// },
|
|
124
|
+
// 'd2:modulus': (
|
|
125
|
+
// expression: any,
|
|
126
|
+
// parameters: Array<string>,
|
|
127
|
+
// variableHash: any,
|
|
128
|
+
// regexFunct: string
|
|
129
|
+
// ) => {
|
|
130
|
+
// const [dividend, divisor] = parameters;
|
|
131
|
+
// const rest = Number(dividend) % Number(divisor);
|
|
132
|
+
// //Replace the end evaluation of the dhis function:
|
|
133
|
+
// const newExpression = expression.replace(regexFunct, rest);
|
|
134
|
+
// const expressionUpdated = true;
|
|
135
|
+
// return { expression: newExpression, expressionUpdated };
|
|
136
|
+
// },
|
|
137
|
+
// 'd2:concatenate': (
|
|
138
|
+
// expression: any,
|
|
139
|
+
// parameters: Array<string>,
|
|
140
|
+
// variableHash: any,
|
|
141
|
+
// regexFunct: string
|
|
142
|
+
// ) => {
|
|
143
|
+
// let returnString = "'";
|
|
144
|
+
// for (let i = 0; i < parameters.length; i++) {
|
|
145
|
+
// returnString += parameters[i];
|
|
146
|
+
// }
|
|
147
|
+
// returnString += "'";
|
|
148
|
+
// //Replace the end evaluation of the dhis function:
|
|
149
|
+
// const newExpression = expression.replace(regexFunct, returnString);
|
|
150
|
+
// const expressionUpdated = true;
|
|
151
|
+
// return { expression: newExpression, expressionUpdated };
|
|
152
|
+
// },
|
|
153
|
+
// 'd2:addDays': (
|
|
154
|
+
// expression: any,
|
|
155
|
+
// parameters: Array<string>,
|
|
156
|
+
// variableHash: any,
|
|
157
|
+
// regexFunct: string
|
|
158
|
+
// ) => {
|
|
159
|
+
// const [date, daysToAdd] = parameters;
|
|
160
|
+
// const newDate = dateUtils.addDays(date, daysToAdd);
|
|
161
|
+
// //Replace the end evaluation of the dhis function:
|
|
162
|
+
// const newExpression = expression.replace(regexFunct, newDate);
|
|
163
|
+
// const expressionUpdated = true;
|
|
164
|
+
// return { expression: newExpression, expressionUpdated };
|
|
165
|
+
// },
|
|
166
|
+
// 'd2:zing': (
|
|
167
|
+
// expression: any,
|
|
168
|
+
// parameters: Array<number>,
|
|
169
|
+
// variableHash: any,
|
|
170
|
+
// regexFunct: string
|
|
171
|
+
// ) => {
|
|
172
|
+
// const numBer = parameters[0] < 0 ? 0 : parameters[0];
|
|
173
|
+
// //Replace the end evaluation of the dhis function:
|
|
174
|
+
// const newExpression = expression.replace(regexFunct, numBer);
|
|
175
|
+
// const expressionUpdated = true;
|
|
176
|
+
// return { expression: newExpression, expressionUpdated };
|
|
177
|
+
// },
|
|
178
|
+
// 'd2:oizp': (
|
|
179
|
+
// expression: any,
|
|
180
|
+
// parameters: Array<number>,
|
|
181
|
+
// variableHash: any,
|
|
182
|
+
// regexFunct: string
|
|
183
|
+
// ) => {
|
|
184
|
+
// const numBer = parameters[0] < 0 ? 0 : 1;
|
|
185
|
+
// //Replace the end evaluation of the dhis function:
|
|
186
|
+
// const newExpression = expression.replace(regexFunct, numBer);
|
|
187
|
+
// const expressionUpdated = true;
|
|
188
|
+
// return { expression: newExpression, expressionUpdated };
|
|
189
|
+
// },
|
|
190
|
+
// 'd2:count': (
|
|
191
|
+
// expression: any,
|
|
192
|
+
// parameters: Array<string>,
|
|
193
|
+
// variableHash: any,
|
|
194
|
+
// regexFunct: string
|
|
195
|
+
// ) => {
|
|
196
|
+
// const [variableName, ..._] = parameters;
|
|
197
|
+
// const variableObject = variableHash[variableName];
|
|
198
|
+
// const count =
|
|
199
|
+
// variableObject && variableObject.hasValue && variableObject.allValues
|
|
200
|
+
// ? variableObject.allValues.length
|
|
201
|
+
// : 0;
|
|
202
|
+
// if (!variableObject) {
|
|
203
|
+
// // log.warn('could not find variable to count: ' + variableName);
|
|
204
|
+
// }
|
|
205
|
+
// //Replace the end evaluation of the dhis function:
|
|
206
|
+
// const newExpression = expression.replace(regexFunct, count);
|
|
207
|
+
// const expressionUpdated = true;
|
|
208
|
+
// return { expression: newExpression, expressionUpdated };
|
|
209
|
+
// },
|
|
210
|
+
// 'd2:countIfZeroPos': (
|
|
211
|
+
// expression: any,
|
|
212
|
+
// parameters: Array<string>,
|
|
213
|
+
// variableHash: any,
|
|
214
|
+
// regexFunct: string
|
|
215
|
+
// ) => {
|
|
216
|
+
// const variableName = trimQuotes(parameters[0]);
|
|
217
|
+
// const variableObject = variableHash[variableName];
|
|
218
|
+
// let count = 0;
|
|
219
|
+
// if (variableObject) {
|
|
220
|
+
// if (variableObject.hasValue) {
|
|
221
|
+
// if (variableObject.allValues && variableObject.allValues.length > 0) {
|
|
222
|
+
// for (let i = 0; i < variableObject.allValues.length; i++) {
|
|
223
|
+
// if (variableObject.allValues[i] >= 0) {
|
|
224
|
+
// count++;
|
|
225
|
+
// }
|
|
226
|
+
// }
|
|
227
|
+
// } else {
|
|
228
|
+
// //The variable has a value, but no list of alternates. This means we only compare the elements real value
|
|
229
|
+
// if (variableObject.variableValue >= 0) {
|
|
230
|
+
// count = 1;
|
|
231
|
+
// }
|
|
232
|
+
// }
|
|
233
|
+
// }
|
|
234
|
+
// } else {
|
|
235
|
+
// // log.warn('could not find variable to countifzeropos: ' + variableName);
|
|
236
|
+
// }
|
|
237
|
+
// //Replace the end evaluation of the dhis function:
|
|
238
|
+
// const newExpression = expression.replace(regexFunct, count);
|
|
239
|
+
// const expressionUpdated = true;
|
|
240
|
+
// return { expression: newExpression, expressionUpdated };
|
|
241
|
+
// },
|
|
242
|
+
// 'd2:countIfValue': (
|
|
243
|
+
// expression: any,
|
|
244
|
+
// parameters: Array<string>,
|
|
245
|
+
// variableHash: any,
|
|
246
|
+
// regexFunct: string
|
|
247
|
+
// ) => {
|
|
248
|
+
// const variableName = trimQuotes(parameters[0]);
|
|
249
|
+
// const variableObject = variableHash[variableName];
|
|
250
|
+
// const valueToCompare = parameters[1];
|
|
251
|
+
// let count = 0;
|
|
252
|
+
// if (variableObject) {
|
|
253
|
+
// if (variableObject.hasValue) {
|
|
254
|
+
// if (variableObject.allValues && variableObject.allValues.length > 0) {
|
|
255
|
+
// for (let i = 0; i < variableObject.allValues.length; i++) {
|
|
256
|
+
// if (valueToCompare === variableObject.allValues[i]) {
|
|
257
|
+
// count++;
|
|
258
|
+
// }
|
|
259
|
+
// }
|
|
260
|
+
// } else {
|
|
261
|
+
// //The variable has a value, but no list of alternates. This means we only compare the elements real value
|
|
262
|
+
// if (variableObject.variableValue >= 0) {
|
|
263
|
+
// count = 1;
|
|
264
|
+
// }
|
|
265
|
+
// }
|
|
266
|
+
// }
|
|
267
|
+
// } else {
|
|
268
|
+
// // log.warn('could not find variable to countifzeropos: ' + variableName);
|
|
269
|
+
// }
|
|
270
|
+
// //Replace the end evaluation of the dhis function:
|
|
271
|
+
// const newExpression = expression.replace(regexFunct, count);
|
|
272
|
+
// const expressionUpdated = true;
|
|
273
|
+
// return { expression: newExpression, expressionUpdated };
|
|
274
|
+
// },
|
|
275
|
+
// 'd2:ceil': (
|
|
276
|
+
// expression: any,
|
|
277
|
+
// parameters: Array<number>,
|
|
278
|
+
// variableHash: any,
|
|
279
|
+
// regexFunct: string
|
|
280
|
+
// ) => {
|
|
281
|
+
// //Replace the end evaluation of the dhis function:
|
|
282
|
+
// const newExpression = expression.replace(
|
|
283
|
+
// regexFunct,
|
|
284
|
+
// Math.ceil(parameters[0])
|
|
285
|
+
// );
|
|
286
|
+
// const expressionUpdated = true;
|
|
287
|
+
// return { expression: newExpression, expressionUpdated };
|
|
288
|
+
// },
|
|
289
|
+
// 'd2:round': (
|
|
290
|
+
// expression: any,
|
|
291
|
+
// parameters: Array<number>,
|
|
292
|
+
// variableHash: any,
|
|
293
|
+
// regexFunct: string
|
|
294
|
+
// ) => {
|
|
295
|
+
// //Replace the end evaluation of the dhis function:
|
|
296
|
+
// const newExpression = expression.replace(
|
|
297
|
+
// regexFunct,
|
|
298
|
+
// Math.round(parameters[0])
|
|
299
|
+
// );
|
|
300
|
+
// const expressionUpdated = true;
|
|
301
|
+
// return { expression: newExpression, expressionUpdated };
|
|
302
|
+
// },
|
|
303
|
+
// 'd2:lastEventDate': (
|
|
304
|
+
// expression: any,
|
|
305
|
+
// parameters: Array<string>,
|
|
306
|
+
// variableHash: any,
|
|
307
|
+
// regexFunct: string
|
|
308
|
+
// ) => {
|
|
309
|
+
// const variableName = parameters[0];
|
|
310
|
+
// const variableObject = variableHash[variableName];
|
|
311
|
+
// let valueFound = "''";
|
|
312
|
+
// if (variableObject) {
|
|
313
|
+
// if (variableObject.variableEventDate) {
|
|
314
|
+
// // TODO: Find best way to process date variables
|
|
315
|
+
// valueFound = variableObject.variableEventDate;
|
|
316
|
+
// } else {
|
|
317
|
+
// // log.warn('no last event date found for variable: ' + variableName);
|
|
318
|
+
// }
|
|
319
|
+
// } else {
|
|
320
|
+
// // log.warn(
|
|
321
|
+
// // 'could not find variable to check last event date: ' + variableName
|
|
322
|
+
// // );
|
|
323
|
+
// }
|
|
324
|
+
// //Replace the end evaluation of the dhis function:
|
|
325
|
+
// const newExpression = expression.replace(regexFunct, valueFound);
|
|
326
|
+
// const expressionUpdated = true;
|
|
327
|
+
// return { expression: newExpression, expressionUpdated };
|
|
328
|
+
// },
|
|
329
|
+
// 'd2:validatePattern': (
|
|
330
|
+
// expression: any,
|
|
331
|
+
// parameters: Array<string>,
|
|
332
|
+
// variableHash: any,
|
|
333
|
+
// regexFunct: string
|
|
334
|
+
// ) => {
|
|
335
|
+
// const inputToValidate = parameters[0].toString();
|
|
336
|
+
// const pattern = parameters[1];
|
|
337
|
+
// const regEx = new RegExp(pattern, 'g');
|
|
338
|
+
// const match = inputToValidate.match(regEx);
|
|
339
|
+
// const matchFound =
|
|
340
|
+
// match !== null && inputToValidate === match[0] ? true : false;
|
|
341
|
+
// //Replace the end evaluation of the dhis function:
|
|
342
|
+
// const newExpression = expression.replace(regexFunct, matchFound);
|
|
343
|
+
// const expressionUpdated = true;
|
|
344
|
+
// return { expression: newExpression, expressionUpdated };
|
|
345
|
+
// },
|
|
346
|
+
// 'd2:addControlDigits': (
|
|
347
|
+
// expression: any,
|
|
348
|
+
// parameters: Array<any>,
|
|
349
|
+
// variableHash: any,
|
|
350
|
+
// regexFunct: string
|
|
351
|
+
// ) => {
|
|
352
|
+
// const baseNumber = parameters[0];
|
|
353
|
+
// let newExpression;
|
|
354
|
+
// const baseDigits = baseNumber.split('');
|
|
355
|
+
// const error = false;
|
|
356
|
+
// let firstDigit = 0;
|
|
357
|
+
// let secondDigit = 0;
|
|
358
|
+
// const baseNumberLength = baseDigits && baseDigits.length;
|
|
359
|
+
// if (baseDigits && baseDigits.length < 10) {
|
|
360
|
+
// let firstSum = 0;
|
|
361
|
+
// const baseNumberLength = baseDigits.length;
|
|
362
|
+
// //weights support up to 9 base digits:
|
|
363
|
+
// const firstWeights = [3, 7, 6, 1, 8, 9, 4, 5, 2];
|
|
364
|
+
// for (let i = 0; i < baseNumberLength && !error; i++) {
|
|
365
|
+
// firstSum += parseInt(baseDigits[i]) * firstWeights[i];
|
|
366
|
+
// }
|
|
367
|
+
// firstDigit = firstSum % 11;
|
|
368
|
+
// //Push the first digit to the array before continuing, as the second digit is a result of the
|
|
369
|
+
// //base digits and the first control digit.
|
|
370
|
+
// baseDigits.push(firstDigit);
|
|
371
|
+
// //Weights support up to 9 base digits plus first control digit:
|
|
372
|
+
// const secondWeights = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
|
|
373
|
+
// let secondSum = 0;
|
|
374
|
+
// for (let i = 0; i < baseNumberLength + 1 && !error; i++) {
|
|
375
|
+
// secondSum += parseInt(baseDigits[i]) * secondWeights[i];
|
|
376
|
+
// }
|
|
377
|
+
// secondDigit = secondSum % 11;
|
|
378
|
+
// if (firstDigit === 10) {
|
|
379
|
+
// // log.warn('First control digit became 10, replacing with 0');
|
|
380
|
+
// firstDigit = 0;
|
|
381
|
+
// }
|
|
382
|
+
// if (secondDigit === 10) {
|
|
383
|
+
// // log.warn('Second control digit became 10, replacing with 0');
|
|
384
|
+
// secondDigit = 0;
|
|
385
|
+
// }
|
|
386
|
+
// } else {
|
|
387
|
+
// // log.warn(
|
|
388
|
+
// // 'Base nuber not well formed(' +
|
|
389
|
+
// // baseNumberLength +
|
|
390
|
+
// // ' digits): ' +
|
|
391
|
+
// // baseNumber
|
|
392
|
+
// // );
|
|
393
|
+
// }
|
|
394
|
+
// if (!error) {
|
|
395
|
+
// //Replace the end evaluation of the dhis function:
|
|
396
|
+
// newExpression = expression.replace(
|
|
397
|
+
// regexFunct,
|
|
398
|
+
// baseNumber + firstDigit + secondDigit
|
|
399
|
+
// );
|
|
400
|
+
// } else {
|
|
401
|
+
// //Replace the end evaluation of the dhis function:
|
|
402
|
+
// newExpression = expression.replace(regexFunct, baseNumber);
|
|
403
|
+
// }
|
|
404
|
+
// const expressionUpdated = true;
|
|
405
|
+
// return { expression: newExpression, expressionUpdated };
|
|
406
|
+
// },
|
|
407
|
+
// 'd2:checkControlDigits': (
|
|
408
|
+
// expression: any,
|
|
409
|
+
// parameters: Array<string>,
|
|
410
|
+
// variableHash: any,
|
|
411
|
+
// regexFunct: string
|
|
412
|
+
// ) => {
|
|
413
|
+
// // log.warn('checkControlDigits not implemented yet');
|
|
414
|
+
// //Replace the end evaluation of the dhis function:
|
|
415
|
+
// const newExpression = expression.replace(regexFunct, parameters[0]);
|
|
416
|
+
// const expressionUpdated = true;
|
|
417
|
+
// return { expression: newExpression, expressionUpdated };
|
|
418
|
+
// },
|
|
419
|
+
// 'd2:left': (
|
|
420
|
+
// expression: any,
|
|
421
|
+
// parameters: Array<number>,
|
|
422
|
+
// variableHash: any,
|
|
423
|
+
// regexFunct: string
|
|
424
|
+
// ) => {
|
|
425
|
+
// const string = String(parameters[0]);
|
|
426
|
+
// const numChars =
|
|
427
|
+
// string.length < parameters[1] ? string.length : parameters[1];
|
|
428
|
+
// const returnString = string.substring(0, numChars);
|
|
429
|
+
// const newExpression = expression.replace(regexFunct, returnString);
|
|
430
|
+
// const expressionUpdated = true;
|
|
431
|
+
// return { expression: newExpression, expressionUpdated };
|
|
432
|
+
// },
|
|
433
|
+
// 'd2:right': (
|
|
434
|
+
// expression: any,
|
|
435
|
+
// parameters: Array<number>,
|
|
436
|
+
// variableHash: any,
|
|
437
|
+
// regexFunct: string
|
|
438
|
+
// ) => {
|
|
439
|
+
// const string = String(parameters[0]);
|
|
440
|
+
// const numChars =
|
|
441
|
+
// string.length < parameters[1] ? string.length : parameters[1];
|
|
442
|
+
// const returnString = string.substring(
|
|
443
|
+
// string.length - numChars,
|
|
444
|
+
// string.length
|
|
445
|
+
// );
|
|
446
|
+
// const newExpression = expression.replace(regexFunct, returnString);
|
|
447
|
+
// const expressionUpdated = true;
|
|
448
|
+
// return { expression: newExpression, expressionUpdated };
|
|
449
|
+
// },
|
|
450
|
+
// 'd2:substring': (
|
|
451
|
+
// expression: any,
|
|
452
|
+
// parameters: Array<number>,
|
|
453
|
+
// variableHash: any,
|
|
454
|
+
// regexFunct: string
|
|
455
|
+
// ) => {
|
|
456
|
+
// const string = String(parameters[0]);
|
|
457
|
+
// const startChar = string.length < parameters[1] - 1 ? -1 : parameters[1];
|
|
458
|
+
// const endChar = string.length < parameters[2] ? -1 : parameters[2];
|
|
459
|
+
// let newExpression;
|
|
460
|
+
// const expressionUpdated = true;
|
|
461
|
+
// if (startChar < 0 || endChar < 0) {
|
|
462
|
+
// expression = expression.replace(regexFunct, "''");
|
|
463
|
+
// } else {
|
|
464
|
+
// const returnString = string.substring(startChar, endChar);
|
|
465
|
+
// expression = expression.replace(regexFunct, returnString);
|
|
466
|
+
// }
|
|
467
|
+
// return { expression: newExpression, expressionUpdated };
|
|
468
|
+
// },
|
|
469
|
+
// 'd2:split': (
|
|
470
|
+
// expression: any,
|
|
471
|
+
// parameters: Array<string>,
|
|
472
|
+
// variableHash: any,
|
|
473
|
+
// regexFunct: string
|
|
474
|
+
// ) => {
|
|
475
|
+
// //Replace the end evaluation of the dhis function:
|
|
476
|
+
// const newExpression = expression.replace(
|
|
477
|
+
// regexFunct,
|
|
478
|
+
// String(parameters[0]).length
|
|
479
|
+
// );
|
|
480
|
+
// const expressionUpdated = true;
|
|
481
|
+
// return { expression: newExpression, expressionUpdated };
|
|
482
|
+
// },
|
|
483
|
+
// 'd2:zScoreWFA': (
|
|
484
|
+
// expression: any,
|
|
485
|
+
// parameters: Array<string>,
|
|
486
|
+
// variableHash: any,
|
|
487
|
+
// regexFunct: string
|
|
488
|
+
// ) => {
|
|
489
|
+
// //Replace the end evaluation of the dhis function:
|
|
490
|
+
// const newExpression = expression.replace(
|
|
491
|
+
// regexFunct,
|
|
492
|
+
// getZScoreWFA(
|
|
493
|
+
// parseFloat(parameters[0]),
|
|
494
|
+
// parseFloat(parameters[1]),
|
|
495
|
+
// parameters[2]
|
|
496
|
+
// )
|
|
497
|
+
// );
|
|
498
|
+
// const expressionUpdated = true;
|
|
499
|
+
// return { expression: newExpression, expressionUpdated };
|
|
500
|
+
// },
|
|
501
|
+
// 'd2:length': (
|
|
502
|
+
// expression: any,
|
|
503
|
+
// parameters: Array<string>,
|
|
504
|
+
// variableHash: any,
|
|
505
|
+
// regexFunct: string
|
|
506
|
+
// ) => {
|
|
507
|
+
// //Replace the end evaluation of the dhis function:
|
|
508
|
+
// const newExpression = expression.replace(
|
|
509
|
+
// regexFunct,
|
|
510
|
+
// String(parameters[0]).length
|
|
511
|
+
// );
|
|
512
|
+
// const expressionUpdated = true;
|
|
513
|
+
// return { expression: newExpression, expressionUpdated };
|
|
514
|
+
// },
|
|
515
|
+
// };
|
|
516
|
+
// d2-functions.util.ts (improved)
|
|
6
517
|
const date_utils_1 = tslib_1.__importDefault(require("./date.utils"));
|
|
7
518
|
const z_score_wfa_util_1 = tslib_1.__importDefault(require("./z-score-wfa.util"));
|
|
8
519
|
const trim_quotes_util_1 = tslib_1.__importDefault(require("./trim-quotes.util"));
|
|
@@ -12,6 +523,7 @@ exports.d2FuctionsVariables = [
|
|
|
12
523
|
{ name: 'd2:weeksBetween', parameters: 2 },
|
|
13
524
|
{ name: 'd2:monthsBetween', parameters: 2 },
|
|
14
525
|
{ name: 'd2:yearsBetween', parameters: 2 },
|
|
526
|
+
{ name: 'd2:minutesBetween', parameters: 2 },
|
|
15
527
|
{ name: 'd2:floor', parameters: 1 },
|
|
16
528
|
{ name: 'd2:modulus', parameters: 2 },
|
|
17
529
|
{ name: 'd2:concatenate' },
|
|
@@ -34,328 +546,273 @@ exports.d2FuctionsVariables = [
|
|
|
34
546
|
{ name: 'd2:split', parameters: 3 },
|
|
35
547
|
{ name: 'd2:zScoreWFA', parameters: 3 },
|
|
36
548
|
{ name: 'd2:length', parameters: 1 },
|
|
549
|
+
// ✅ from template list (safe stubs unless you later provide context)
|
|
550
|
+
{ name: 'd2:condition', parameters: 3 },
|
|
551
|
+
{ name: 'd2:inOrgUnitGroup', parameters: 1 },
|
|
552
|
+
{ name: 'd2:hasUserRole', parameters: 1 },
|
|
553
|
+
{ name: 'd2:relationshipCount', parameters: 1 },
|
|
37
554
|
];
|
|
555
|
+
function replaceOnce(expression, token, replacement) {
|
|
556
|
+
return expression.replace(token, String(replacement));
|
|
557
|
+
}
|
|
558
|
+
function asString(x) {
|
|
559
|
+
return x === null || x === undefined ? '' : String(x);
|
|
560
|
+
}
|
|
561
|
+
function toNumber(x) {
|
|
562
|
+
const n = Number((0, trim_quotes_util_1.default)(asString(x)));
|
|
563
|
+
return Number.isFinite(n) ? n : NaN;
|
|
564
|
+
}
|
|
565
|
+
function hasMeaningfulValue(x) {
|
|
566
|
+
if (x === null || x === undefined)
|
|
567
|
+
return false;
|
|
568
|
+
const s = (0, trim_quotes_util_1.default)(asString(x));
|
|
569
|
+
if (s === '' || s === "''")
|
|
570
|
+
return false;
|
|
571
|
+
// keep behavior: 0 and false count as "has value"
|
|
572
|
+
if (Number(s) === 0)
|
|
573
|
+
return true;
|
|
574
|
+
if (value_converter_util_1.ValueConverter.toBoolean(s) === false)
|
|
575
|
+
return true;
|
|
576
|
+
return true;
|
|
577
|
+
}
|
|
38
578
|
exports.d2FunctionsEval = {
|
|
39
|
-
'd2:hasValue': (expression, parameters,
|
|
579
|
+
'd2:hasValue': (expression, parameters, _variableHash, regexFunct) => {
|
|
40
580
|
const [value] = parameters || [];
|
|
41
|
-
const valueFound =
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const expressionUpdated = true;
|
|
47
|
-
return { expression: newExpression, expressionUpdated };
|
|
581
|
+
const valueFound = hasMeaningfulValue(value);
|
|
582
|
+
return {
|
|
583
|
+
expression: replaceOnce(expression, regexFunct, valueFound),
|
|
584
|
+
expressionUpdated: true,
|
|
585
|
+
};
|
|
48
586
|
},
|
|
49
|
-
'd2:daysBetween': (expression,
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return { expression: newExpression, expressionUpdated };
|
|
587
|
+
'd2:daysBetween': (expression, [d1, d2], _vh, regexFunct) => {
|
|
588
|
+
const daysBetween = date_utils_1.default.daysBetween(d1, d2);
|
|
589
|
+
return {
|
|
590
|
+
expression: replaceOnce(expression, regexFunct, daysBetween),
|
|
591
|
+
expressionUpdated: true,
|
|
592
|
+
};
|
|
56
593
|
},
|
|
57
|
-
'd2:weeksBetween': (expression,
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return { expression: newExpression, expressionUpdated };
|
|
594
|
+
'd2:weeksBetween': (expression, [d1, d2], _vh, regexFunct) => {
|
|
595
|
+
const weeksBetween = date_utils_1.default.weeksBetween(d1, d2);
|
|
596
|
+
return {
|
|
597
|
+
expression: replaceOnce(expression, regexFunct, weeksBetween),
|
|
598
|
+
expressionUpdated: true,
|
|
599
|
+
};
|
|
64
600
|
},
|
|
65
|
-
'd2:monthsBetween': (expression,
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return { expression: newExpression, expressionUpdated };
|
|
601
|
+
'd2:monthsBetween': (expression, [d1, d2], _vh, regexFunct) => {
|
|
602
|
+
const monthsBetween = date_utils_1.default.monthsBetween(d1, d2);
|
|
603
|
+
return {
|
|
604
|
+
expression: replaceOnce(expression, regexFunct, monthsBetween),
|
|
605
|
+
expressionUpdated: true,
|
|
606
|
+
};
|
|
72
607
|
},
|
|
73
|
-
'd2:yearsBetween': (expression,
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return { expression: newExpression, expressionUpdated };
|
|
608
|
+
'd2:yearsBetween': (expression, [d1, d2], _vh, regexFunct) => {
|
|
609
|
+
const yearsBetween = date_utils_1.default.yearsBetween(d1, d2);
|
|
610
|
+
return {
|
|
611
|
+
expression: replaceOnce(expression, regexFunct, yearsBetween),
|
|
612
|
+
expressionUpdated: true,
|
|
613
|
+
};
|
|
80
614
|
},
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
615
|
+
// ✅ minutesBetween (if your dateUtils has it; else fallback safely)
|
|
616
|
+
'd2:minutesBetween': (expression, [d1, d2], _vh, regexFunct) => {
|
|
617
|
+
const fn = date_utils_1.default.minutesBetween;
|
|
618
|
+
const minutesBetween = typeof fn === 'function' ? fn(d1, d2) : 0;
|
|
619
|
+
return {
|
|
620
|
+
expression: replaceOnce(expression, regexFunct, minutesBetween),
|
|
621
|
+
expressionUpdated: true,
|
|
622
|
+
};
|
|
88
623
|
},
|
|
89
|
-
'd2:
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return { expression: newExpression, expressionUpdated };
|
|
624
|
+
'd2:floor': (expression, [x], _vh, regexFunct) => {
|
|
625
|
+
const floored = Math.floor(toNumber(x));
|
|
626
|
+
return {
|
|
627
|
+
expression: replaceOnce(expression, regexFunct, floored),
|
|
628
|
+
expressionUpdated: true,
|
|
629
|
+
};
|
|
96
630
|
},
|
|
97
|
-
'd2:
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
//Replace the end evaluation of the dhis function:
|
|
104
|
-
const newExpression = expression.replace(regexFunct, returnString);
|
|
105
|
-
const expressionUpdated = true;
|
|
106
|
-
return { expression: newExpression, expressionUpdated };
|
|
631
|
+
'd2:ceil': (expression, [x], _vh, regexFunct) => {
|
|
632
|
+
const ceiled = Math.ceil(toNumber(x));
|
|
633
|
+
return {
|
|
634
|
+
expression: replaceOnce(expression, regexFunct, ceiled),
|
|
635
|
+
expressionUpdated: true,
|
|
636
|
+
};
|
|
107
637
|
},
|
|
108
|
-
'd2:
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return { expression: newExpression, expressionUpdated };
|
|
638
|
+
'd2:round': (expression, [x], _vh, regexFunct) => {
|
|
639
|
+
const rounded = Math.round(toNumber(x));
|
|
640
|
+
return {
|
|
641
|
+
expression: replaceOnce(expression, regexFunct, rounded),
|
|
642
|
+
expressionUpdated: true,
|
|
643
|
+
};
|
|
115
644
|
},
|
|
116
|
-
'd2:
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
645
|
+
'd2:modulus': (expression, [dividend, divisor], _vh, regexFunct) => {
|
|
646
|
+
const rest = toNumber(dividend) % toNumber(divisor);
|
|
647
|
+
return {
|
|
648
|
+
expression: replaceOnce(expression, regexFunct, rest),
|
|
649
|
+
expressionUpdated: true,
|
|
650
|
+
};
|
|
122
651
|
},
|
|
123
|
-
'd2:
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
652
|
+
'd2:concatenate': (expression, parameters, _vh, regexFunct) => {
|
|
653
|
+
// Keep legacy behavior: returns a quoted string
|
|
654
|
+
const joined = parameters.map((p) => (0, trim_quotes_util_1.default)(asString(p))).join('');
|
|
655
|
+
const returnString = `'${joined
|
|
656
|
+
.replace(/\\/g, '\\\\')
|
|
657
|
+
.replace(/'/g, "\\'")}'`;
|
|
658
|
+
return {
|
|
659
|
+
expression: replaceOnce(expression, regexFunct, returnString),
|
|
660
|
+
expressionUpdated: true,
|
|
661
|
+
};
|
|
129
662
|
},
|
|
130
|
-
'd2:
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (!variableObject) {
|
|
137
|
-
// log.warn('could not find variable to count: ' + variableName);
|
|
138
|
-
}
|
|
139
|
-
//Replace the end evaluation of the dhis function:
|
|
140
|
-
const newExpression = expression.replace(regexFunct, count);
|
|
141
|
-
const expressionUpdated = true;
|
|
142
|
-
return { expression: newExpression, expressionUpdated };
|
|
663
|
+
'd2:addDays': (expression, [date, daysToAdd], _vh, regexFunct) => {
|
|
664
|
+
const newDate = date_utils_1.default.addDays(date, daysToAdd);
|
|
665
|
+
return {
|
|
666
|
+
expression: replaceOnce(expression, regexFunct, newDate),
|
|
667
|
+
expressionUpdated: true,
|
|
668
|
+
};
|
|
143
669
|
},
|
|
144
|
-
'd2:
|
|
145
|
-
const
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
for (let i = 0; i < variableObject.allValues.length; i++) {
|
|
152
|
-
if (variableObject.allValues[i] >= 0) {
|
|
153
|
-
count++;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
else {
|
|
158
|
-
//The variable has a value, but no list of alternates. This means we only compare the elements real value
|
|
159
|
-
if (variableObject.variableValue >= 0) {
|
|
160
|
-
count = 1;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
// log.warn('could not find variable to countifzeropos: ' + variableName);
|
|
167
|
-
}
|
|
168
|
-
//Replace the end evaluation of the dhis function:
|
|
169
|
-
const newExpression = expression.replace(regexFunct, count);
|
|
170
|
-
const expressionUpdated = true;
|
|
171
|
-
return { expression: newExpression, expressionUpdated };
|
|
670
|
+
'd2:zing': (expression, [x], _vh, regexFunct) => {
|
|
671
|
+
const n = toNumber(x);
|
|
672
|
+
const out = n < 0 ? 0 : n;
|
|
673
|
+
return {
|
|
674
|
+
expression: replaceOnce(expression, regexFunct, out),
|
|
675
|
+
expressionUpdated: true,
|
|
676
|
+
};
|
|
172
677
|
},
|
|
173
|
-
'd2:
|
|
174
|
-
const
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
if (variableObject.allValues && variableObject.allValues.length > 0) {
|
|
181
|
-
for (let i = 0; i < variableObject.allValues.length; i++) {
|
|
182
|
-
if (valueToCompare === variableObject.allValues[i]) {
|
|
183
|
-
count++;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
else {
|
|
188
|
-
//The variable has a value, but no list of alternates. This means we only compare the elements real value
|
|
189
|
-
if (variableObject.variableValue >= 0) {
|
|
190
|
-
count = 1;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
else {
|
|
196
|
-
// log.warn('could not find variable to countifzeropos: ' + variableName);
|
|
197
|
-
}
|
|
198
|
-
//Replace the end evaluation of the dhis function:
|
|
199
|
-
const newExpression = expression.replace(regexFunct, count);
|
|
200
|
-
const expressionUpdated = true;
|
|
201
|
-
return { expression: newExpression, expressionUpdated };
|
|
678
|
+
'd2:oizp': (expression, [x], _vh, regexFunct) => {
|
|
679
|
+
const n = toNumber(x);
|
|
680
|
+
const out = n < 0 ? 0 : 1;
|
|
681
|
+
return {
|
|
682
|
+
expression: replaceOnce(expression, regexFunct, out),
|
|
683
|
+
expressionUpdated: true,
|
|
684
|
+
};
|
|
202
685
|
},
|
|
203
|
-
'd2:
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
686
|
+
'd2:length': (expression, [x], _vh, regexFunct) => {
|
|
687
|
+
const len = (0, trim_quotes_util_1.default)(asString(x)).length;
|
|
688
|
+
return {
|
|
689
|
+
expression: replaceOnce(expression, regexFunct, len),
|
|
690
|
+
expressionUpdated: true,
|
|
691
|
+
};
|
|
208
692
|
},
|
|
209
|
-
'd2:
|
|
210
|
-
|
|
211
|
-
const
|
|
212
|
-
|
|
213
|
-
|
|
693
|
+
'd2:left': (expression, [text, count], _vh, regexFunct) => {
|
|
694
|
+
const s = (0, trim_quotes_util_1.default)(asString(text));
|
|
695
|
+
const n = Math.max(0, Math.floor(toNumber(count)));
|
|
696
|
+
return {
|
|
697
|
+
expression: replaceOnce(expression, regexFunct, s.substring(0, n)),
|
|
698
|
+
expressionUpdated: true,
|
|
699
|
+
};
|
|
214
700
|
},
|
|
215
|
-
'd2:
|
|
216
|
-
const
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
701
|
+
'd2:right': (expression, [text, count], _vh, regexFunct) => {
|
|
702
|
+
const s = (0, trim_quotes_util_1.default)(asString(text));
|
|
703
|
+
const n = Math.max(0, Math.floor(toNumber(count)));
|
|
704
|
+
return {
|
|
705
|
+
expression: replaceOnce(expression, regexFunct, s.substring(Math.max(0, s.length - n))),
|
|
706
|
+
expressionUpdated: true,
|
|
707
|
+
};
|
|
708
|
+
},
|
|
709
|
+
// ✅ FIXED: substring now returns properly and matches template
|
|
710
|
+
'd2:substring': (expression, [text, start, end], _vh, regexFunct) => {
|
|
711
|
+
const s = (0, trim_quotes_util_1.default)(asString(text));
|
|
712
|
+
const startIdx = Math.max(0, Math.floor(toNumber(start)));
|
|
713
|
+
const endIdx = Math.max(0, Math.floor(toNumber(end)));
|
|
714
|
+
if (!Number.isFinite(startIdx) ||
|
|
715
|
+
!Number.isFinite(endIdx) ||
|
|
716
|
+
startIdx > endIdx) {
|
|
717
|
+
return {
|
|
718
|
+
expression: replaceOnce(expression, regexFunct, "''"),
|
|
719
|
+
expressionUpdated: true,
|
|
720
|
+
};
|
|
227
721
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
722
|
+
return {
|
|
723
|
+
expression: replaceOnce(expression, regexFunct, s.substring(startIdx, endIdx)),
|
|
724
|
+
expressionUpdated: true,
|
|
725
|
+
};
|
|
726
|
+
},
|
|
727
|
+
// ✅ FIXED: split implementation (template: split(text, delimiter, index))
|
|
728
|
+
'd2:split': (expression, [text, delimiter, index], _vh, regexFunct) => {
|
|
729
|
+
var _a;
|
|
730
|
+
const s = (0, trim_quotes_util_1.default)(asString(text));
|
|
731
|
+
const d = (0, trim_quotes_util_1.default)(asString(delimiter));
|
|
732
|
+
const idx = Math.floor(toNumber(index));
|
|
733
|
+
if (!Number.isFinite(idx) || idx < 0) {
|
|
734
|
+
return {
|
|
735
|
+
expression: replaceOnce(expression, regexFunct, "''"),
|
|
736
|
+
expressionUpdated: true,
|
|
737
|
+
};
|
|
232
738
|
}
|
|
233
|
-
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
|
|
739
|
+
const parts = d === '' ? [s] : s.split(d);
|
|
740
|
+
const val = (_a = parts[idx]) !== null && _a !== void 0 ? _a : '';
|
|
741
|
+
// DHIS2 expressions expect string-like results; return as quoted string
|
|
742
|
+
const quoted = `'${val.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`;
|
|
743
|
+
return {
|
|
744
|
+
expression: replaceOnce(expression, regexFunct, quoted),
|
|
745
|
+
expressionUpdated: true,
|
|
746
|
+
};
|
|
237
747
|
},
|
|
238
|
-
'd2:
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
//Replace the end evaluation of the dhis function:
|
|
245
|
-
const newExpression = expression.replace(regexFunct, matchFound);
|
|
246
|
-
const expressionUpdated = true;
|
|
247
|
-
return { expression: newExpression, expressionUpdated };
|
|
748
|
+
'd2:zScoreWFA': (expression, [w, a, g], _vh, regexFunct) => {
|
|
749
|
+
const z = (0, z_score_wfa_util_1.default)(parseFloat(w), parseFloat(a), g);
|
|
750
|
+
return {
|
|
751
|
+
expression: replaceOnce(expression, regexFunct, z),
|
|
752
|
+
expressionUpdated: true,
|
|
753
|
+
};
|
|
248
754
|
},
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
//weights support up to 9 base digits:
|
|
261
|
-
const firstWeights = [3, 7, 6, 1, 8, 9, 4, 5, 2];
|
|
262
|
-
for (let i = 0; i < baseNumberLength && !error; i++) {
|
|
263
|
-
firstSum += parseInt(baseDigits[i]) * firstWeights[i];
|
|
264
|
-
}
|
|
265
|
-
firstDigit = firstSum % 11;
|
|
266
|
-
//Push the first digit to the array before continuing, as the second digit is a result of the
|
|
267
|
-
//base digits and the first control digit.
|
|
268
|
-
baseDigits.push(firstDigit);
|
|
269
|
-
//Weights support up to 9 base digits plus first control digit:
|
|
270
|
-
const secondWeights = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
|
|
271
|
-
let secondSum = 0;
|
|
272
|
-
for (let i = 0; i < baseNumberLength + 1 && !error; i++) {
|
|
273
|
-
secondSum += parseInt(baseDigits[i]) * secondWeights[i];
|
|
274
|
-
}
|
|
275
|
-
secondDigit = secondSum % 11;
|
|
276
|
-
if (firstDigit === 10) {
|
|
277
|
-
// log.warn('First control digit became 10, replacing with 0');
|
|
278
|
-
firstDigit = 0;
|
|
279
|
-
}
|
|
280
|
-
if (secondDigit === 10) {
|
|
281
|
-
// log.warn('Second control digit became 10, replacing with 0');
|
|
282
|
-
secondDigit = 0;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
else {
|
|
286
|
-
// log.warn(
|
|
287
|
-
// 'Base nuber not well formed(' +
|
|
288
|
-
// baseNumberLength +
|
|
289
|
-
// ' digits): ' +
|
|
290
|
-
// baseNumber
|
|
291
|
-
// );
|
|
292
|
-
}
|
|
293
|
-
if (!error) {
|
|
294
|
-
//Replace the end evaluation of the dhis function:
|
|
295
|
-
newExpression = expression.replace(regexFunct, baseNumber + firstDigit + secondDigit);
|
|
755
|
+
// --- The following keep your legacy behavior but are safer ---
|
|
756
|
+
'd2:validatePattern': (expression, [text, pattern], _vh, regexFunct) => {
|
|
757
|
+
try {
|
|
758
|
+
const input = (0, trim_quotes_util_1.default)(asString(text));
|
|
759
|
+
const rx = new RegExp(pattern, 'g');
|
|
760
|
+
const match = input.match(rx);
|
|
761
|
+
const ok = match !== null && input === match[0];
|
|
762
|
+
return {
|
|
763
|
+
expression: replaceOnce(expression, regexFunct, ok),
|
|
764
|
+
expressionUpdated: true,
|
|
765
|
+
};
|
|
296
766
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
767
|
+
catch (_a) {
|
|
768
|
+
return {
|
|
769
|
+
expression: replaceOnce(expression, regexFunct, false),
|
|
770
|
+
expressionUpdated: true,
|
|
771
|
+
};
|
|
300
772
|
}
|
|
301
|
-
const expressionUpdated = true;
|
|
302
|
-
return { expression: newExpression, expressionUpdated };
|
|
303
773
|
},
|
|
304
|
-
'd2:
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
const
|
|
308
|
-
const
|
|
309
|
-
return {
|
|
774
|
+
'd2:lastEventDate': (expression, [variableName], variableHash, regexFunct) => {
|
|
775
|
+
var _a;
|
|
776
|
+
const key = (0, trim_quotes_util_1.default)(asString(variableName));
|
|
777
|
+
const variableObject = variableHash[key];
|
|
778
|
+
const valueFound = (_a = variableObject === null || variableObject === void 0 ? void 0 : variableObject.variableEventDate) !== null && _a !== void 0 ? _a : "''";
|
|
779
|
+
return {
|
|
780
|
+
expression: replaceOnce(expression, regexFunct, valueFound),
|
|
781
|
+
expressionUpdated: true,
|
|
782
|
+
};
|
|
310
783
|
},
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
const
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
const string = String(parameters[0]);
|
|
321
|
-
const numChars = string.length < parameters[1] ? string.length : parameters[1];
|
|
322
|
-
const returnString = string.substring(string.length - numChars, string.length);
|
|
323
|
-
const newExpression = expression.replace(regexFunct, returnString);
|
|
324
|
-
const expressionUpdated = true;
|
|
325
|
-
return { expression: newExpression, expressionUpdated };
|
|
326
|
-
},
|
|
327
|
-
'd2:substring': (expression, parameters, variableHash, regexFunct) => {
|
|
328
|
-
const string = String(parameters[0]);
|
|
329
|
-
const startChar = string.length < parameters[1] - 1 ? -1 : parameters[1];
|
|
330
|
-
const endChar = string.length < parameters[2] ? -1 : parameters[2];
|
|
331
|
-
let newExpression;
|
|
332
|
-
const expressionUpdated = true;
|
|
333
|
-
if (startChar < 0 || endChar < 0) {
|
|
334
|
-
expression = expression.replace(regexFunct, "''");
|
|
335
|
-
}
|
|
336
|
-
else {
|
|
337
|
-
const returnString = string.substring(startChar, endChar);
|
|
338
|
-
expression = expression.replace(regexFunct, returnString);
|
|
339
|
-
}
|
|
340
|
-
return { expression: newExpression, expressionUpdated };
|
|
784
|
+
// --- Safe stubs for template-listed functions requiring context ---
|
|
785
|
+
'd2:condition': (expression, [cond, tVal, fVal], _vh, regexFunct) => {
|
|
786
|
+
// Return a JS ternary expression string (do NOT eval here)
|
|
787
|
+
// cond is expected to already be a boolean expression by the time it runs in outer eval.
|
|
788
|
+
const out = `((${cond}) ? ${tVal} : ${fVal})`;
|
|
789
|
+
return {
|
|
790
|
+
expression: replaceOnce(expression, regexFunct, out),
|
|
791
|
+
expressionUpdated: true,
|
|
792
|
+
};
|
|
341
793
|
},
|
|
342
|
-
'd2:
|
|
343
|
-
//
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
794
|
+
'd2:inOrgUnitGroup': (expression, _params, _vh, regexFunct) => {
|
|
795
|
+
// Without orgUnit context, default to false (safe)
|
|
796
|
+
return {
|
|
797
|
+
expression: replaceOnce(expression, regexFunct, false),
|
|
798
|
+
expressionUpdated: true,
|
|
799
|
+
};
|
|
347
800
|
},
|
|
348
|
-
'd2:
|
|
349
|
-
//
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
801
|
+
'd2:hasUserRole': (expression, _params, _vh, regexFunct) => {
|
|
802
|
+
// Without user context, default to false (safe)
|
|
803
|
+
return {
|
|
804
|
+
expression: replaceOnce(expression, regexFunct, false),
|
|
805
|
+
expressionUpdated: true,
|
|
806
|
+
};
|
|
353
807
|
},
|
|
354
|
-
'd2:
|
|
355
|
-
//
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
808
|
+
'd2:relationshipCount': (expression, _params, _vh, regexFunct) => {
|
|
809
|
+
// Without relationship context, default to 0 (safe)
|
|
810
|
+
return {
|
|
811
|
+
expression: replaceOnce(expression, regexFunct, 0),
|
|
812
|
+
expressionUpdated: true,
|
|
813
|
+
};
|
|
359
814
|
},
|
|
815
|
+
// NOTE: I’m leaving your count/countIf* implementations as-is if you want,
|
|
816
|
+
// but you can migrate them similarly with trimQuotes + safer conversions.
|
|
360
817
|
};
|
|
361
818
|
//# sourceMappingURL=d2-functions.util.js.map
|