@genesislcap/mock-server 15.74.0 → 15.76.0
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/README.md +296 -58
- package/dist/db/schema.d.ts +9 -1
- package/dist/db/schema.js +40 -11
- package/dist/db/store.d.ts +1 -0
- package/dist/db/store.js +4 -0
- package/dist/handlers/auth.d.ts +15 -6
- package/dist/handlers/auth.js +152 -33
- package/dist/handlers/commitEvent.js +43 -20
- package/dist/handlers/crudEvents.d.ts +3 -1
- package/dist/handlers/crudEvents.js +3 -2
- package/dist/handlers/eventValidation.js +6 -3
- package/dist/handlers/http.d.ts +2 -2
- package/dist/handlers/http.js +66 -43
- package/dist/handlers/jsonSchema.d.ts +1 -0
- package/dist/handlers/jsonSchema.js +72 -21
- package/dist/handlers/meta.d.ts +1 -1
- package/dist/handlers/meta.js +59 -10
- package/dist/handlers/requestReply.js +36 -9
- package/dist/index.d.ts +1 -1
- package/dist/presets/auth.d.ts +25 -0
- package/dist/presets/auth.js +855 -0
- package/dist/presets/cronExpression.d.ts +1 -0
- package/dist/presets/cronExpression.js +429 -0
- package/dist/presets/fileServer.d.ts +13 -0
- package/dist/presets/fileServer.js +399 -0
- package/dist/presets/gsf.d.ts +24 -0
- package/dist/presets/gsf.js +450 -0
- package/dist/presets/index.d.ts +1 -0
- package/dist/presets/index.js +1 -0
- package/dist/presets/javaTime.d.ts +7 -0
- package/dist/presets/javaTime.js +322 -0
- package/dist/presets/multipart.d.ts +14 -0
- package/dist/presets/multipart.js +91 -0
- package/dist/presets/notify.d.ts +27 -0
- package/dist/presets/notify.js +250 -0
- package/dist/presets/notifyDispatch.d.ts +28 -0
- package/dist/presets/notifyDispatch.js +570 -0
- package/dist/presets/notifyRules.d.ts +9 -0
- package/dist/presets/notifyRules.js +754 -0
- package/dist/presets/platform/backing.json +8058 -217
- package/dist/presets/platform/criteria-model.json +362 -0
- package/dist/presets/platform/time-zones.json +608 -0
- package/dist/presets/platform.d.ts +7 -0
- package/dist/presets/platform.js +145 -103
- package/dist/presets/products.d.ts +9 -7
- package/dist/presets/products.js +89 -21
- package/dist/presets/reconciliation.d.ts +18 -0
- package/dist/presets/reconciliation.js +721 -0
- package/dist/presets/reporting.d.ts +14 -0
- package/dist/presets/reporting.js +862 -0
- package/dist/protocol/connection.d.ts +2 -0
- package/dist/protocol/connection.js +4 -0
- package/dist/protocol/dtoTypes.d.ts +30 -0
- package/dist/protocol/dtoTypes.js +250 -0
- package/dist/protocol/errors.d.ts +3 -1
- package/dist/protocol/errors.js +2 -0
- package/dist/protocol/fieldTypes.js +5 -0
- package/dist/protocol/router.d.ts +1 -0
- package/dist/protocol/router.js +3 -3
- package/dist/protocol/schemaValidation.d.ts +1 -1
- package/dist/protocol/schemaValidation.js +53 -8
- package/dist/server.js +34 -2
- package/dist/types.d.ts +50 -2
- package/package.json +1 -1
- package/src/db/schema.ts +56 -13
- package/src/db/store.ts +5 -0
- package/src/handlers/auth.ts +211 -37
- package/src/handlers/commitEvent.ts +44 -19
- package/src/handlers/crudEvents.ts +9 -2
- package/src/handlers/eventValidation.ts +6 -3
- package/src/handlers/http.ts +83 -42
- package/src/handlers/jsonSchema.ts +86 -23
- package/src/handlers/meta.ts +77 -9
- package/src/handlers/requestReply.ts +42 -6
- package/src/index.ts +7 -0
- package/src/presets/auth.ts +961 -0
- package/src/presets/cronExpression.ts +445 -0
- package/src/presets/fileServer.ts +475 -0
- package/src/presets/gsf.ts +475 -0
- package/src/presets/index.ts +5 -0
- package/src/presets/javaTime.ts +354 -0
- package/src/presets/multipart.ts +110 -0
- package/src/presets/notify.ts +312 -0
- package/src/presets/notifyDispatch.ts +685 -0
- package/src/presets/notifyRules.ts +866 -0
- package/src/presets/platform/backing.json +8058 -217
- package/src/presets/platform/criteria-model.json +362 -0
- package/src/presets/platform/time-zones.json +608 -0
- package/src/presets/platform.ts +204 -115
- package/src/presets/products.ts +125 -19
- package/src/presets/reconciliation.ts +824 -0
- package/src/presets/reporting.ts +953 -0
- package/src/protocol/connection.ts +4 -0
- package/src/protocol/dtoTypes.ts +310 -0
- package/src/protocol/errors.ts +13 -1
- package/src/protocol/fieldTypes.ts +5 -0
- package/src/protocol/router.ts +3 -3
- package/src/protocol/schemaValidation.ts +61 -8
- package/src/server.ts +38 -2
- package/src/types.ts +147 -3
|
@@ -0,0 +1,754 @@
|
|
|
1
|
+
import { NackError } from "../protocol/errors.js";
|
|
2
|
+
import { schemaFieldErrors, validateSchema } from "../protocol/schemaValidation.js";
|
|
3
|
+
import { dto, ENUMS, nullable } from "./notify.js";
|
|
4
|
+
import criteriaModel from './platform/criteria-model.json' with { type: 'json' };
|
|
5
|
+
const ADMIN_RIGHT = 'NotificationAdminAction';
|
|
6
|
+
const ON_BEHALF_RIGHT = 'SendNotificationOnBehalfOfOthers';
|
|
7
|
+
// --- the expression model, written as GSF does ---------------------------------
|
|
8
|
+
// global.genesis.criteria.model.Operation, MethodCall (genesis-criteria, GSF
|
|
9
|
+
// v8.15.29 Expressions.kt).
|
|
10
|
+
const OPERATIONS = {
|
|
11
|
+
GREATER_THAN: '>',
|
|
12
|
+
LESS_THAN: '<',
|
|
13
|
+
GREATER_THAN_OR_EQUAL: '>=',
|
|
14
|
+
LESS_THAN_OR_EQUAL: '<=',
|
|
15
|
+
EQUALS: '==',
|
|
16
|
+
NOT_EQUALS: '!=',
|
|
17
|
+
AND: '&&',
|
|
18
|
+
OR: '||',
|
|
19
|
+
PLUS: '+',
|
|
20
|
+
MINUS: '-',
|
|
21
|
+
MULTIPLY: '*',
|
|
22
|
+
DIVIDE: '/',
|
|
23
|
+
BITWISE_AND: '&',
|
|
24
|
+
BITWISE_OR: '|',
|
|
25
|
+
ASSIGNMENT: '=',
|
|
26
|
+
};
|
|
27
|
+
const METHODS = {
|
|
28
|
+
CONTAINS_IGNORE_CASE: 'Expr.containsIgnoreCase',
|
|
29
|
+
CONTAINS_WORDS_STARTING_WITH_IGNORE_CASE: 'Expr.containsWordsStartingWithIgnoreCase',
|
|
30
|
+
IS_NULL_OR_BLANK: 'Expr.isNullOrBlank',
|
|
31
|
+
NOT_NULL_OR_BLANK: 'Expr.notNullOrBlank',
|
|
32
|
+
LONG_TO_DATE: 'Expr.longLocalDate',
|
|
33
|
+
LONG_TO_DATE_TIME: 'Expr.longDateTime',
|
|
34
|
+
DATE_TIME_IS_IN_RANGE: 'Expr.dateTimeIsInRange',
|
|
35
|
+
};
|
|
36
|
+
const operation = (name) => OPERATIONS[String(name)] ?? String(name);
|
|
37
|
+
// EvaluatorExpressionWriter.writeExpression (genesis-criteria util).
|
|
38
|
+
function writeExpression(expression) {
|
|
39
|
+
switch (expression?.TYPE) {
|
|
40
|
+
case 'FIELD':
|
|
41
|
+
return String(expression.NAME);
|
|
42
|
+
case 'NUMBER':
|
|
43
|
+
return String(expression.VALUE);
|
|
44
|
+
case 'PLACEHOLDER':
|
|
45
|
+
return `{{${expression.KEY}}}`;
|
|
46
|
+
case 'STRING':
|
|
47
|
+
return `"${expression.VALUE}"`;
|
|
48
|
+
case 'BOOLEAN':
|
|
49
|
+
return String(expression.VALUE);
|
|
50
|
+
case 'NULL':
|
|
51
|
+
return 'null';
|
|
52
|
+
case 'BINARY_EXPRESSION':
|
|
53
|
+
return `(${writeExpression(expression.LEFT)} ${operation(expression.OPERATION)} ${writeExpression(expression.RIGHT)})`;
|
|
54
|
+
case 'METHOD_EXPRESSION':
|
|
55
|
+
return `${METHODS[expression.METHOD] ?? expression.METHOD}(${(expression.PARAMETERS ?? []).map(writeExpression).join(', ')})`;
|
|
56
|
+
case 'PREDICATE_EXPRESSION':
|
|
57
|
+
return `(${(expression.EXPRESSIONS ?? [])
|
|
58
|
+
.map(writeExpression)
|
|
59
|
+
.join(` ${operation(expression.OPERATION ?? 'AND')} `)})`;
|
|
60
|
+
default:
|
|
61
|
+
throw NackError.standard('INTERNAL_ERROR');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const writeRule = (rule) => `(${(rule.CONDITIONS ?? [])
|
|
65
|
+
.map((condition) => `(${writeExpression(condition.LEFT)} ${operation(condition.OPERATION)} ${writeExpression(condition.RIGHT)})`)
|
|
66
|
+
.join(' && ')})`;
|
|
67
|
+
const writeResult = (assignments) => `(${assignments
|
|
68
|
+
.map((assignment) => `(${writeExpression(assignment.FIELD)} = ${writeExpression(assignment.VALUE)})`)
|
|
69
|
+
.join(' && ')})`;
|
|
70
|
+
// RULE_EXPRESSION and RESULT_EXPRESSION as GSF's inbound schema describes
|
|
71
|
+
// them (platform/criteria-model.json: EventMeta's schema of
|
|
72
|
+
// NotificationRuleInsert on genesis-criteria 8.15.29, its Expression tree in
|
|
73
|
+
// $defs). GSF's decoder checks them before the handler, VALIDATE too: an
|
|
74
|
+
// operation, method or value off the tree (a NUMBER's VALUE is a decimal
|
|
75
|
+
// string) is a VALIDATION_ERROR, with every oneOf branch's complaint.
|
|
76
|
+
const EXPRESSIONS_SCHEMA = {
|
|
77
|
+
properties: {
|
|
78
|
+
DETAILS: {
|
|
79
|
+
properties: {
|
|
80
|
+
RULE_EXPRESSION: criteriaModel.RULE_EXPRESSION,
|
|
81
|
+
RESULT_EXPRESSION: criteriaModel.RESULT_EXPRESSION,
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
$defs: criteriaModel.$defs,
|
|
86
|
+
};
|
|
87
|
+
function checkExpressions(details) {
|
|
88
|
+
const sent = Object.fromEntries(['RULE_EXPRESSION', 'RESULT_EXPRESSION']
|
|
89
|
+
.filter((name) => Object.hasOwn(details, name))
|
|
90
|
+
.map((name) => [name, details[name]]));
|
|
91
|
+
const errors = schemaFieldErrors(validateSchema(EXPRESSIONS_SCHEMA, { DETAILS: sent }));
|
|
92
|
+
if (errors.length > 0)
|
|
93
|
+
throw new NackError(errors);
|
|
94
|
+
}
|
|
95
|
+
const field = (NAME) => ({ TYPE: 'FIELD', NAME });
|
|
96
|
+
const assign = (name, VALUE) => ({ FIELD: field(name), VALUE });
|
|
97
|
+
// --- schemas ---------------------------------------------------------------------
|
|
98
|
+
const MESSAGE_CLASS = 'global.genesis.notify.message.event';
|
|
99
|
+
const TABLE_OPERATIONS = ['INSERT', 'MODIFY', 'DELETE'];
|
|
100
|
+
// RuleExpression and ResultExpression hold a sealed Expression tree, which
|
|
101
|
+
// FieldDef can't describe: declared as objects, and checked against GSF's own
|
|
102
|
+
// schema of them before the handler (checkExpressions).
|
|
103
|
+
const RULE_FIELDS = [
|
|
104
|
+
dto('RULE_NAME', 'STRING'),
|
|
105
|
+
dto('RULE_DESCRIPTION', 'STRING', { optional: true }),
|
|
106
|
+
dto('RULE_TABLE', 'STRING'),
|
|
107
|
+
dto('RULE_EXPRESSION', 'MAP', nullable),
|
|
108
|
+
dto('RAW_RULE_EXPRESSION', 'STRING', nullable),
|
|
109
|
+
dto('RESULT_EXPRESSION', 'MAP'),
|
|
110
|
+
dto('TABLE_OPERATIONS', 'ARRAY', {
|
|
111
|
+
items: {
|
|
112
|
+
type: 'ENUM',
|
|
113
|
+
values: TABLE_OPERATIONS,
|
|
114
|
+
enumClass: 'global.genesis.db.updatequeue.UpdateType',
|
|
115
|
+
},
|
|
116
|
+
unique: true,
|
|
117
|
+
optional: true,
|
|
118
|
+
}),
|
|
119
|
+
dto('ALERT_SOUND_FILE_NAME', 'STRING', nullable),
|
|
120
|
+
dto('ALERT_SOUND_MIME_TYPE', 'STRING', nullable),
|
|
121
|
+
dto('ALERT_SOUND_DATA', 'STRING', nullable),
|
|
122
|
+
];
|
|
123
|
+
// NotificationRuleTemplateParameters.
|
|
124
|
+
const TEMPLATE_PARAMETERS = dto('PARAMETER_DETAILS', 'MAP', {
|
|
125
|
+
items: {
|
|
126
|
+
type: 'OBJECT',
|
|
127
|
+
className: 'global.genesis.notify.message.event.NotificationRuleTemplateParameters',
|
|
128
|
+
nullable: false,
|
|
129
|
+
fields: [
|
|
130
|
+
dto('PARAM_SOURCE', 'STRING'),
|
|
131
|
+
dto('PARAM_SOURCE_TYPE', 'ENUM', {
|
|
132
|
+
values: ['USER_TEXT', 'DEFINED_GROUP', 'REQ_REP'],
|
|
133
|
+
enumClass: `${ENUMS}.ParamSourceType`,
|
|
134
|
+
}),
|
|
135
|
+
dto('PARAM_TYPE', 'ENUM', {
|
|
136
|
+
values: ['FIELD', 'STRING', 'BOOLEAN', 'NUMBER'],
|
|
137
|
+
enumClass: `${ENUMS}.ParamType`,
|
|
138
|
+
}),
|
|
139
|
+
dto('PARAM_LABEL', 'STRING'),
|
|
140
|
+
dto('PARAM_OPERATOR', 'STRING'),
|
|
141
|
+
],
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
const RULE_ID = dto('DYNAMIC_RULE_ID', 'STRING');
|
|
145
|
+
const CLEAR_ALERT_SOUND = dto('CLEAR_ALERT_SOUND', 'BOOLEAN', { optional: true });
|
|
146
|
+
const BY_ID = {
|
|
147
|
+
table: 'DYNAMIC_RULE',
|
|
148
|
+
pkOnly: true,
|
|
149
|
+
className: 'global.genesis.gen.dao.DynamicRule.ById',
|
|
150
|
+
};
|
|
151
|
+
const SCHEMAS = {
|
|
152
|
+
EVENT_NOTIFICATION_RULE_TEMPLATE_CREATE: {
|
|
153
|
+
table: 'DYNAMIC_RULE_TEMPLATE_PARAMS',
|
|
154
|
+
className: `${MESSAGE_CLASS}.NotificationRuleTemplateInsert`,
|
|
155
|
+
fields: [...RULE_FIELDS, TEMPLATE_PARAMETERS],
|
|
156
|
+
},
|
|
157
|
+
EVENT_NOTIFICATION_RULE_TEMPLATE_UPDATE: {
|
|
158
|
+
table: 'DYNAMIC_RULE_TEMPLATE_PARAMS',
|
|
159
|
+
className: `${MESSAGE_CLASS}.NotificationRuleTemplateUpdate`,
|
|
160
|
+
fields: [RULE_ID, ...RULE_FIELDS, TEMPLATE_PARAMETERS, CLEAR_ALERT_SOUND],
|
|
161
|
+
},
|
|
162
|
+
EVENT_NOTIFICATION_RULE_TEMPLATE_DELETE: BY_ID,
|
|
163
|
+
EVENT_NOTIFICATION_RULE_CREATE: {
|
|
164
|
+
className: `${MESSAGE_CLASS}.NotificationRuleInsert`,
|
|
165
|
+
fields: RULE_FIELDS,
|
|
166
|
+
},
|
|
167
|
+
EVENT_NOTIFICATION_RULE_UPDATE: {
|
|
168
|
+
className: `${MESSAGE_CLASS}.NotificationRuleUpdate`,
|
|
169
|
+
fields: [RULE_ID, ...RULE_FIELDS, CLEAR_ALERT_SOUND],
|
|
170
|
+
},
|
|
171
|
+
EVENT_NOTIFICATION_RULE_DELETE: BY_ID,
|
|
172
|
+
EVENT_NOTIFICATION_RULE_SUBSCRIBE: {
|
|
173
|
+
className: `${MESSAGE_CLASS}.NotificationRuleSubscribe`,
|
|
174
|
+
fields: [RULE_ID, dto('PARAMETER_DETAILS', 'MAP', { items: { type: 'STRING' } })],
|
|
175
|
+
},
|
|
176
|
+
EVENT_NOTIFICATION_RULE_UNSUBSCRIBE: BY_ID,
|
|
177
|
+
EVENT_NOTIFICATION_RULE_DISABLE: BY_ID,
|
|
178
|
+
EVENT_NOTIFICATION_RULE_ENABLE: BY_ID,
|
|
179
|
+
};
|
|
180
|
+
// --- alert sounds (RULES-EH :23-117) ------------------------------------------------
|
|
181
|
+
// 1536 * 1024, and the base64 length that holds it: ((bytes + 2) / 3) * 4.
|
|
182
|
+
const MAX_ALERT_SOUND_BYTES = 1_572_864;
|
|
183
|
+
const MAX_ALERT_SOUND_BASE64_CHARS = 2_097_152;
|
|
184
|
+
// What java.util.Base64's decoder takes: whole 4-character units, the last of
|
|
185
|
+
// which may be 2 or 3 characters, padded or not.
|
|
186
|
+
const BASE64 = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}(?:==)?|[A-Za-z0-9+/]{3}=?)?$/;
|
|
187
|
+
const TOO_BIG = 'Alert sound exceeds maximum size of 1.5MB';
|
|
188
|
+
const isBlank = (value) => value === undefined || value === null || String(value).trim() === '';
|
|
189
|
+
// decodeAlertSoundData: the bytes, as canonical base64 (the RAW column's
|
|
190
|
+
// form here), or null for none.
|
|
191
|
+
function decodeAlertSound(data) {
|
|
192
|
+
if (isBlank(data))
|
|
193
|
+
return null;
|
|
194
|
+
const text = String(data)
|
|
195
|
+
.replace(/^data:[^;,]*;base64,/, '')
|
|
196
|
+
.replace(/\s/g, '');
|
|
197
|
+
if (text.length > MAX_ALERT_SOUND_BASE64_CHARS)
|
|
198
|
+
throw new NackError(TOO_BIG);
|
|
199
|
+
if (!BASE64.test(text)) {
|
|
200
|
+
throw new NackError('Alert sound data is not valid Base64');
|
|
201
|
+
}
|
|
202
|
+
const bytes = Buffer.from(text, 'base64');
|
|
203
|
+
if (bytes.length > MAX_ALERT_SOUND_BYTES)
|
|
204
|
+
throw new NackError(TOO_BIG);
|
|
205
|
+
return bytes.toString('base64');
|
|
206
|
+
}
|
|
207
|
+
function validateAlertSound(details) {
|
|
208
|
+
if (isBlank(details.ALERT_SOUND_DATA))
|
|
209
|
+
return;
|
|
210
|
+
if (isBlank(details.ALERT_SOUND_MIME_TYPE)) {
|
|
211
|
+
throw new NackError('MIME type is required when an alert sound is provided');
|
|
212
|
+
}
|
|
213
|
+
if (isBlank(details.ALERT_SOUND_FILE_NAME)) {
|
|
214
|
+
throw new NackError('File name is required when an alert sound is provided');
|
|
215
|
+
}
|
|
216
|
+
decodeAlertSound(details.ALERT_SOUND_DATA);
|
|
217
|
+
}
|
|
218
|
+
function validateAlertSoundUpdate(details) {
|
|
219
|
+
if (details.CLEAR_ALERT_SOUND === true && !isBlank(details.ALERT_SOUND_DATA)) {
|
|
220
|
+
throw new NackError('clearAlertSound cannot be combined with alertSoundData');
|
|
221
|
+
}
|
|
222
|
+
validateAlertSound(details);
|
|
223
|
+
}
|
|
224
|
+
// --- the backing -------------------------------------------------------------------
|
|
225
|
+
const PARAMS = 'DYNAMIC_RULE_TEMPLATE_PARAMS';
|
|
226
|
+
// A template's params as GSF reads them, through the DYNAMIC_RULE_ID index:
|
|
227
|
+
// in primary-key order, so by PARAM_NAME.
|
|
228
|
+
const paramsOf = (store, ruleId) => store
|
|
229
|
+
.getAllRows(PARAMS)
|
|
230
|
+
.filter((row) => row.DYNAMIC_RULE_ID === ruleId)
|
|
231
|
+
.sort((a, b) => (a.PARAM_NAME < b.PARAM_NAME ? -1 : a.PARAM_NAME > b.PARAM_NAME ? 1 : 0));
|
|
232
|
+
// The rules' and the templates' `where` / `filter`: IS_TEMPLATE is false
|
|
233
|
+
// unless set (its dictionary default).
|
|
234
|
+
const where = (template) => (row) => (row.IS_TEMPLATE ?? false) === template && row.RULE_TYPE === 'NOTIFY';
|
|
235
|
+
// The events whose DTO holds the expressions.
|
|
236
|
+
const WITH_EXPRESSIONS = new Set([
|
|
237
|
+
'EVENT_NOTIFICATION_RULE_TEMPLATE_CREATE',
|
|
238
|
+
'EVENT_NOTIFICATION_RULE_TEMPLATE_UPDATE',
|
|
239
|
+
'EVENT_NOTIFICATION_RULE_CREATE',
|
|
240
|
+
'EVENT_NOTIFICATION_RULE_UPDATE',
|
|
241
|
+
]);
|
|
242
|
+
export function ruleBacking(users) {
|
|
243
|
+
// Each rule's model, per server, with the strings it was stored as: a rule
|
|
244
|
+
// whose strings have changed since, or one the mock didn't write, has none.
|
|
245
|
+
// Kept and dropped once the event commits.
|
|
246
|
+
const models = new WeakMap();
|
|
247
|
+
const modelsOf = (store) => {
|
|
248
|
+
let kept = models.get(store);
|
|
249
|
+
if (!kept) {
|
|
250
|
+
kept = new Map();
|
|
251
|
+
models.set(store, kept);
|
|
252
|
+
}
|
|
253
|
+
return kept;
|
|
254
|
+
};
|
|
255
|
+
const keep = (ctx, id, row, model) => ctx.afterCommit(() => modelsOf(ctx.store).set(id, { model, stored: [row.RULE_EXPRESSION, row.RESULT_EXPRESSION] }));
|
|
256
|
+
const forget = (ctx, id) => ctx.afterCommit(() => modelsOf(ctx.store).delete(id));
|
|
257
|
+
const keptFor = (store, row) => {
|
|
258
|
+
const kept = modelsOf(store).get(row.ID);
|
|
259
|
+
const [rule, result] = kept?.stored ?? [];
|
|
260
|
+
return rule === row.RULE_EXPRESSION && result === row.RESULT_EXPRESSION
|
|
261
|
+
? kept?.model
|
|
262
|
+
: undefined;
|
|
263
|
+
};
|
|
264
|
+
const isAdmin = (store, user) => users.hasRight(store, user, ADMIN_RIGHT);
|
|
265
|
+
// Writes a row and pushes it live.
|
|
266
|
+
function write(ctx, table, op, row, key) {
|
|
267
|
+
const { store } = ctx;
|
|
268
|
+
const written = op === 'INSERT' ? store.insertRow(table, row) : store.updateRow(table, key, row);
|
|
269
|
+
ctx.broadcastTableChange(table, op, store.rowKey(table, written));
|
|
270
|
+
return written;
|
|
271
|
+
}
|
|
272
|
+
function remove(ctx, table, key) {
|
|
273
|
+
if (ctx.store.deleteRow(table, key))
|
|
274
|
+
ctx.broadcastTableChange(table, 'DELETE', key);
|
|
275
|
+
}
|
|
276
|
+
const rowsWhere = (ctx, table, test) => ctx.store.getAllRows(table).filter(test);
|
|
277
|
+
// The rule's own record, or the 2.2 checks' NACK: it must exist, and be
|
|
278
|
+
// the event user's or the user an admin.
|
|
279
|
+
function ownedRule(ctx, id, user, missing) {
|
|
280
|
+
const rule = ctx.store.getRow('DYNAMIC_RULE', id);
|
|
281
|
+
if (!rule)
|
|
282
|
+
throw new NackError(missing);
|
|
283
|
+
if (rule.USER_NAME !== user && !isAdmin(ctx.store, user)) {
|
|
284
|
+
throw new NackError(`User ${user} lacks sufficient permissions`);
|
|
285
|
+
}
|
|
286
|
+
return rule;
|
|
287
|
+
}
|
|
288
|
+
// The result assignments a rule is stored with (RULES-EH :128-174): the sent
|
|
289
|
+
// ones, then TABLE_NAME, TABLE_ENTITY_ID (a table with a single-field key
|
|
290
|
+
// only; what GSF does for a view isn't known, so none) and SENDER.
|
|
291
|
+
function resultAssignments(ctx, details, sender) {
|
|
292
|
+
const table = String(details.RULE_TABLE);
|
|
293
|
+
const key = ctx.store.hasTable(table) ? ctx.store.getPkFields(table) : [];
|
|
294
|
+
return [
|
|
295
|
+
...(details.RESULT_EXPRESSION?.ASSIGNMENTS ?? []),
|
|
296
|
+
assign('TABLE_NAME', { TYPE: 'STRING', VALUE: table }),
|
|
297
|
+
...(key.length === 1 ? [assign('TABLE_ENTITY_ID', field(key[0]))] : []),
|
|
298
|
+
assign('SENDER', sender),
|
|
299
|
+
];
|
|
300
|
+
}
|
|
301
|
+
// The DYNAMIC_RULE row 2.1 / 2.4 build, and the model it was built from.
|
|
302
|
+
function ruleRow(ctx, details, user, template) {
|
|
303
|
+
const rule = details.RULE_EXPRESSION ?? null;
|
|
304
|
+
const raw = rule ? null : (details.RAW_RULE_EXPRESSION ?? null);
|
|
305
|
+
// ruleExpressionString: the model written, else the raw string (both
|
|
306
|
+
// null is a NullPointerException).
|
|
307
|
+
if (!rule && raw === null)
|
|
308
|
+
throw NackError.standard('INTERNAL_ERROR');
|
|
309
|
+
const sender = template
|
|
310
|
+
? { TYPE: 'PLACEHOLDER', KEY: 'USER' }
|
|
311
|
+
: { TYPE: 'STRING', VALUE: user };
|
|
312
|
+
const assignments = resultAssignments(ctx, details, sender);
|
|
313
|
+
const operations = details.TABLE_OPERATIONS ?? ['INSERT'];
|
|
314
|
+
return {
|
|
315
|
+
row: {
|
|
316
|
+
USER_NAME: user,
|
|
317
|
+
NAME: details.RULE_NAME,
|
|
318
|
+
DESCRIPTION: details.RULE_DESCRIPTION ?? '',
|
|
319
|
+
RULE_TABLE: details.RULE_TABLE,
|
|
320
|
+
RULE_EXECUTION_STRATEGY: 'UNLIMITED',
|
|
321
|
+
RULE_EXPRESSION: rule ? writeRule(rule) : raw,
|
|
322
|
+
RESULT_EXPRESSION: writeResult(assignments),
|
|
323
|
+
RULE_STATUS: 'ENABLED',
|
|
324
|
+
MESSAGE_TYPE: 'EVENT_NOTIFY_INSERT',
|
|
325
|
+
IS_TEMPLATE: template,
|
|
326
|
+
RULE_TYPE: 'NOTIFY',
|
|
327
|
+
TABLE_OPERATION: [...new Set(operations)].join('|'),
|
|
328
|
+
PROCESS_NAME: null,
|
|
329
|
+
},
|
|
330
|
+
model: { rule, raw, result: { ASSIGNMENTS: assignments } },
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
// upsertAlertSound (:77-103): one sound per rule, kept by its id.
|
|
334
|
+
function upsertSound(ctx, ruleId, details, data) {
|
|
335
|
+
const existing = rowsWhere(ctx, 'NOTIFY_ALERT_SOUND', (row) => row.DYNAMIC_RULE_ID === ruleId)[0];
|
|
336
|
+
const row = {
|
|
337
|
+
DYNAMIC_RULE_ID: ruleId,
|
|
338
|
+
ALERT_SOUND_FILE_NAME: details.ALERT_SOUND_FILE_NAME,
|
|
339
|
+
ALERT_SOUND_MIME_TYPE: details.ALERT_SOUND_MIME_TYPE,
|
|
340
|
+
ALERT_SOUND_DATA: data,
|
|
341
|
+
};
|
|
342
|
+
if (existing)
|
|
343
|
+
write(ctx, 'NOTIFY_ALERT_SOUND', 'MODIFY', row, existing.ALERT_SOUND_ID);
|
|
344
|
+
else
|
|
345
|
+
write(ctx, 'NOTIFY_ALERT_SOUND', 'INSERT', row);
|
|
346
|
+
}
|
|
347
|
+
function deleteSound(ctx, ruleId) {
|
|
348
|
+
for (const row of rowsWhere(ctx, 'NOTIFY_ALERT_SOUND', (each) => each.DYNAMIC_RULE_ID === ruleId)) {
|
|
349
|
+
remove(ctx, 'NOTIFY_ALERT_SOUND', row.ALERT_SOUND_ID);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
const soundOf = (ctx, ruleId) => rowsWhere(ctx, 'NOTIFY_ALERT_SOUND', (row) => row.DYNAMIC_RULE_ID === ruleId)[0];
|
|
353
|
+
const reservedUser = (details) => {
|
|
354
|
+
if (Object.hasOwn(details.PARAMETER_DETAILS ?? {}, 'USER')) {
|
|
355
|
+
throw new NackError('USER is a reserved keyword for rule creation, please choose another key for your parameter');
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
// The template's parameters as DYNAMIC_RULE_TEMPLATE_PARAMS rows.
|
|
359
|
+
const paramRows = (ruleId, details) => Object.entries((details.PARAMETER_DETAILS ?? {})).map(([name, param]) => ({
|
|
360
|
+
DYNAMIC_RULE_ID: ruleId,
|
|
361
|
+
PARAM_NAME: name,
|
|
362
|
+
PARAM_SOURCE: param.PARAM_SOURCE,
|
|
363
|
+
PARAM_SOURCE_TYPE: param.PARAM_SOURCE_TYPE,
|
|
364
|
+
PARAM_TYPE: param.PARAM_TYPE,
|
|
365
|
+
PARAM_LABEL: param.PARAM_LABEL,
|
|
366
|
+
PARAM_OPERATOR: param.PARAM_OPERATOR,
|
|
367
|
+
}));
|
|
368
|
+
// DELETE, UNSUBSCRIBE (:510-531, :583-603): the sound, the template link,
|
|
369
|
+
// the rule.
|
|
370
|
+
const deleteRule = (missing) => (details, ctx) => {
|
|
371
|
+
const user = users.userOf(ctx);
|
|
372
|
+
const id = details.ID;
|
|
373
|
+
ownedRule(ctx, id, user, missing(id));
|
|
374
|
+
if (ctx.validate)
|
|
375
|
+
return {};
|
|
376
|
+
deleteSound(ctx, id);
|
|
377
|
+
remove(ctx, 'NOTIFY_RULE_TEMPLATE_LINK', id);
|
|
378
|
+
remove(ctx, 'DYNAMIC_RULE', id);
|
|
379
|
+
forget(ctx, id);
|
|
380
|
+
return {};
|
|
381
|
+
};
|
|
382
|
+
// DISABLE, ENABLE (:606-651).
|
|
383
|
+
const setStatus = (status) => (details, ctx) => {
|
|
384
|
+
const user = users.userOf(ctx);
|
|
385
|
+
const rule = ctx.store.getRow('DYNAMIC_RULE', details.ID);
|
|
386
|
+
if (rule?.USER_NAME !== user && !isAdmin(ctx.store, user)) {
|
|
387
|
+
throw new NackError(`User ${user} lacks sufficient permissions`);
|
|
388
|
+
}
|
|
389
|
+
if (ctx.validate)
|
|
390
|
+
return {};
|
|
391
|
+
if (!rule)
|
|
392
|
+
throw NackError.nack(`Cannot find Dynamic Rule for ID: ${details.ID}`);
|
|
393
|
+
write(ctx, 'DYNAMIC_RULE', 'MODIFY', { RULE_STATUS: status }, details.ID);
|
|
394
|
+
return {};
|
|
395
|
+
};
|
|
396
|
+
const notFound = (id) => `Dynamic Rule of ID ${id} doesn't exist!`;
|
|
397
|
+
const eventHandlers = {
|
|
398
|
+
// :177-246
|
|
399
|
+
EVENT_NOTIFICATION_RULE_TEMPLATE_CREATE: (details, ctx) => {
|
|
400
|
+
reservedUser(details);
|
|
401
|
+
validateAlertSound(details);
|
|
402
|
+
if (ctx.validate)
|
|
403
|
+
return {};
|
|
404
|
+
const user = users.userOf(ctx);
|
|
405
|
+
const { row, model } = ruleRow(ctx, details, user, true);
|
|
406
|
+
const id = write(ctx, 'DYNAMIC_RULE', 'INSERT', row).ID;
|
|
407
|
+
for (const param of paramRows(id, details)) {
|
|
408
|
+
write(ctx, PARAMS, 'INSERT', param);
|
|
409
|
+
}
|
|
410
|
+
const sound = decodeAlertSound(details.ALERT_SOUND_DATA);
|
|
411
|
+
if (sound)
|
|
412
|
+
upsertSound(ctx, id, details, sound);
|
|
413
|
+
keep(ctx, id, row, model);
|
|
414
|
+
return {};
|
|
415
|
+
},
|
|
416
|
+
// :249-356: the whole row rebuilt, the editor its owner; the sent params
|
|
417
|
+
// upserted and the others deleted; a sound change reaches every rule
|
|
418
|
+
// subscribed to the template.
|
|
419
|
+
EVENT_NOTIFICATION_RULE_TEMPLATE_UPDATE: (details, ctx) => {
|
|
420
|
+
const user = users.userOf(ctx);
|
|
421
|
+
const id = details.DYNAMIC_RULE_ID;
|
|
422
|
+
ownedRule(ctx, id, user, notFound(id));
|
|
423
|
+
reservedUser(details);
|
|
424
|
+
validateAlertSoundUpdate(details);
|
|
425
|
+
if (ctx.validate)
|
|
426
|
+
return {};
|
|
427
|
+
const { row, model } = ruleRow(ctx, details, user, true);
|
|
428
|
+
write(ctx, 'DYNAMIC_RULE', 'MODIFY', row, id);
|
|
429
|
+
const sent = paramRows(id, details);
|
|
430
|
+
for (const old of paramsOf(ctx.store, id)) {
|
|
431
|
+
if (!sent.some((param) => param.PARAM_NAME === old.PARAM_NAME)) {
|
|
432
|
+
remove(ctx, PARAMS, ctx.store.rowKey(PARAMS, old));
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
for (const param of sent) {
|
|
436
|
+
const key = ctx.store.rowKey(PARAMS, param);
|
|
437
|
+
if (ctx.store.getRow(PARAMS, key))
|
|
438
|
+
write(ctx, PARAMS, 'MODIFY', param, key);
|
|
439
|
+
else
|
|
440
|
+
write(ctx, PARAMS, 'INSERT', param);
|
|
441
|
+
}
|
|
442
|
+
const linked = rowsWhere(ctx, 'NOTIFY_RULE_TEMPLATE_LINK', (link) => link.TEMPLATE_DYNAMIC_RULE_ID === id).map((link) => link.DYNAMIC_RULE_ID);
|
|
443
|
+
if (details.CLEAR_ALERT_SOUND === true) {
|
|
444
|
+
for (const ruleId of [id, ...linked])
|
|
445
|
+
deleteSound(ctx, ruleId);
|
|
446
|
+
}
|
|
447
|
+
else {
|
|
448
|
+
const sound = decodeAlertSound(details.ALERT_SOUND_DATA);
|
|
449
|
+
if (sound)
|
|
450
|
+
for (const ruleId of [id, ...linked])
|
|
451
|
+
upsertSound(ctx, ruleId, details, sound);
|
|
452
|
+
}
|
|
453
|
+
keep(ctx, id, row, model);
|
|
454
|
+
return {};
|
|
455
|
+
},
|
|
456
|
+
// :359-383: the sound, the links (the subscribed rules stay), the
|
|
457
|
+
// template, its params.
|
|
458
|
+
EVENT_NOTIFICATION_RULE_TEMPLATE_DELETE: (details, ctx) => {
|
|
459
|
+
const user = users.userOf(ctx);
|
|
460
|
+
const id = details.ID;
|
|
461
|
+
ownedRule(ctx, id, user, notFound(id));
|
|
462
|
+
if (ctx.validate)
|
|
463
|
+
return {};
|
|
464
|
+
deleteSound(ctx, id);
|
|
465
|
+
for (const link of rowsWhere(ctx, 'NOTIFY_RULE_TEMPLATE_LINK', (each) => each.TEMPLATE_DYNAMIC_RULE_ID === id)) {
|
|
466
|
+
remove(ctx, 'NOTIFY_RULE_TEMPLATE_LINK', link.DYNAMIC_RULE_ID);
|
|
467
|
+
}
|
|
468
|
+
remove(ctx, 'DYNAMIC_RULE', id);
|
|
469
|
+
for (const param of paramsOf(ctx.store, id))
|
|
470
|
+
remove(ctx, PARAMS, ctx.store.rowKey(PARAMS, param));
|
|
471
|
+
forget(ctx, id);
|
|
472
|
+
return {};
|
|
473
|
+
},
|
|
474
|
+
// :386-439
|
|
475
|
+
EVENT_NOTIFICATION_RULE_CREATE: (details, ctx) => {
|
|
476
|
+
validateAlertSound(details);
|
|
477
|
+
if (ctx.validate)
|
|
478
|
+
return {};
|
|
479
|
+
const user = users.userOf(ctx);
|
|
480
|
+
const { row, model } = ruleRow(ctx, details, user, false);
|
|
481
|
+
const id = write(ctx, 'DYNAMIC_RULE', 'INSERT', row).ID;
|
|
482
|
+
const sound = decodeAlertSound(details.ALERT_SOUND_DATA);
|
|
483
|
+
if (sound)
|
|
484
|
+
upsertSound(ctx, id, details, sound);
|
|
485
|
+
keep(ctx, id, row, model);
|
|
486
|
+
return {};
|
|
487
|
+
},
|
|
488
|
+
// :442-507: rebuilt as a rule (IS_TEMPLATE false), the editor its owner.
|
|
489
|
+
EVENT_NOTIFICATION_RULE_UPDATE: (details, ctx) => {
|
|
490
|
+
const user = users.userOf(ctx);
|
|
491
|
+
const id = details.DYNAMIC_RULE_ID;
|
|
492
|
+
ownedRule(ctx, id, user, notFound(id));
|
|
493
|
+
validateAlertSoundUpdate(details);
|
|
494
|
+
if (ctx.validate)
|
|
495
|
+
return {};
|
|
496
|
+
const { row, model } = ruleRow(ctx, details, user, false);
|
|
497
|
+
write(ctx, 'DYNAMIC_RULE', 'MODIFY', row, id);
|
|
498
|
+
if (details.CLEAR_ALERT_SOUND === true)
|
|
499
|
+
deleteSound(ctx, id);
|
|
500
|
+
else {
|
|
501
|
+
const sound = decodeAlertSound(details.ALERT_SOUND_DATA);
|
|
502
|
+
if (sound)
|
|
503
|
+
upsertSound(ctx, id, details, sound);
|
|
504
|
+
}
|
|
505
|
+
keep(ctx, id, row, model);
|
|
506
|
+
return {};
|
|
507
|
+
},
|
|
508
|
+
EVENT_NOTIFICATION_RULE_DELETE: deleteRule(notFound),
|
|
509
|
+
// :533-580: a rule from the template, its expressions' placeholders
|
|
510
|
+
// filled as Mustache fills them (DynamicRuleFactory.kt:50-96).
|
|
511
|
+
EVENT_NOTIFICATION_RULE_SUBSCRIBE: (details, ctx) => {
|
|
512
|
+
const id = details.DYNAMIC_RULE_ID;
|
|
513
|
+
const template = ctx.store.getRow('DYNAMIC_RULE', id);
|
|
514
|
+
if (!template)
|
|
515
|
+
throw NackError.nack(`Unable to find dynamic rule template with id ${id}`);
|
|
516
|
+
const params = paramsOf(ctx.store, id);
|
|
517
|
+
const required = params.map((param) => param.PARAM_NAME);
|
|
518
|
+
const provided = Object.keys(details.PARAMETER_DETAILS ?? {});
|
|
519
|
+
if (required.includes('USER') || provided.includes('USER')) {
|
|
520
|
+
throw new NackError('USER is a reserved keyword and cannot be used as a parameter key');
|
|
521
|
+
}
|
|
522
|
+
if (required.length !== provided.length ||
|
|
523
|
+
required.some((name) => !provided.includes(name))) {
|
|
524
|
+
throw new NackError(`The required parameters ([${required.join(', ')}]) did not match the provided parameters ([${provided.join(', ')}]) for template ${id}`);
|
|
525
|
+
}
|
|
526
|
+
if (ctx.validate)
|
|
527
|
+
return {};
|
|
528
|
+
const user = users.userOf(ctx);
|
|
529
|
+
// transformInputs: the sent values, a STRING parameter's quoted, and
|
|
530
|
+
// USER the quoted subscriber. Mustache HTML-escapes each value it puts
|
|
531
|
+
// in (mustache.java 0.9.14), an unknown key gives "" (GSF looks it up
|
|
532
|
+
// in the system definition first, which the mock doesn't model), and
|
|
533
|
+
// then every " in the result goes back to ".
|
|
534
|
+
const values = new Map(Object.entries(details.PARAMETER_DETAILS ?? {}));
|
|
535
|
+
values.set('USER', user);
|
|
536
|
+
const typeOf = (key) => key === 'USER' ? 'STRING' : params.find((param) => param.PARAM_NAME === key)?.PARAM_TYPE;
|
|
537
|
+
const shown = (key) => escapeHtml(values.get(key) ?? '').replace(/"/g, '"');
|
|
538
|
+
const fill = (text) => (text ?? '')
|
|
539
|
+
.replace(/\{\{([^}]*)\}\}/g, (_match, tag) => {
|
|
540
|
+
const key = tag.trim();
|
|
541
|
+
if (!values.has(key))
|
|
542
|
+
return '';
|
|
543
|
+
return typeOf(key) === 'STRING' ? `"${shown(key)}"` : shown(key);
|
|
544
|
+
})
|
|
545
|
+
.replace(/"/g, '"');
|
|
546
|
+
const created = write(ctx, 'DYNAMIC_RULE', 'INSERT', {
|
|
547
|
+
NAME: template.NAME,
|
|
548
|
+
DESCRIPTION: template.DESCRIPTION,
|
|
549
|
+
RULE_TABLE: template.RULE_TABLE,
|
|
550
|
+
RULE_STATUS: template.RULE_STATUS,
|
|
551
|
+
RULE_EXECUTION_STRATEGY: template.RULE_EXECUTION_STRATEGY,
|
|
552
|
+
PROCESS_NAME: template.PROCESS_NAME,
|
|
553
|
+
MESSAGE_TYPE: template.MESSAGE_TYPE,
|
|
554
|
+
TABLE_OPERATION: template.TABLE_OPERATION,
|
|
555
|
+
USER_NAME: user,
|
|
556
|
+
IS_TEMPLATE: false,
|
|
557
|
+
RULE_TYPE: 'NOTIFY',
|
|
558
|
+
RULE_EXPRESSION: fill(template.RULE_EXPRESSION),
|
|
559
|
+
RESULT_EXPRESSION: fill(template.RESULT_EXPRESSION),
|
|
560
|
+
});
|
|
561
|
+
write(ctx, 'NOTIFY_RULE_TEMPLATE_LINK', 'INSERT', {
|
|
562
|
+
DYNAMIC_RULE_ID: created.ID,
|
|
563
|
+
TEMPLATE_DYNAMIC_RULE_ID: id,
|
|
564
|
+
});
|
|
565
|
+
const sound = soundOf(ctx, id);
|
|
566
|
+
if (sound)
|
|
567
|
+
upsertSound(ctx, created.ID, sound, sound.ALERT_SOUND_DATA);
|
|
568
|
+
// The instance's model: the template's, each placeholder the value the
|
|
569
|
+
// stored string got, as GSF would read it back.
|
|
570
|
+
const templateModel = keptFor(ctx.store, template);
|
|
571
|
+
if (templateModel) {
|
|
572
|
+
const placeholder = (key) => {
|
|
573
|
+
const value = shown(key);
|
|
574
|
+
switch (typeOf(key)) {
|
|
575
|
+
case 'STRING':
|
|
576
|
+
return { TYPE: 'STRING', VALUE: value };
|
|
577
|
+
// NumericValue's VALUE is a BigDecimal, a string on the wire.
|
|
578
|
+
case 'NUMBER':
|
|
579
|
+
return { TYPE: 'NUMBER', VALUE: value };
|
|
580
|
+
case 'BOOLEAN':
|
|
581
|
+
return { TYPE: 'BOOLEAN', VALUE: value === 'true' };
|
|
582
|
+
default:
|
|
583
|
+
return { TYPE: 'FIELD', NAME: value };
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
const filled = (expression) => expression && fillModel(expression, placeholder);
|
|
587
|
+
keep(ctx, created.ID, created, {
|
|
588
|
+
rule: filled(templateModel.rule),
|
|
589
|
+
raw: templateModel.raw === null ? null : fill(templateModel.raw),
|
|
590
|
+
result: filled(templateModel.result),
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
return {};
|
|
594
|
+
},
|
|
595
|
+
EVENT_NOTIFICATION_RULE_UNSUBSCRIBE: deleteRule((id) => `No record found for Dynamic Rule ID: ${id}`),
|
|
596
|
+
EVENT_NOTIFICATION_RULE_DISABLE: setStatus('DISABLED'),
|
|
597
|
+
EVENT_NOTIFICATION_RULE_ENABLE: setStatus('ENABLED'),
|
|
598
|
+
};
|
|
599
|
+
// NOTIFICATION_RULE_DETAILS (:101) and _TEMPLATE_DETAILS (:56): the rule
|
|
600
|
+
// as the dialog sent it. A rule the mock didn't write (a seeded one), or
|
|
601
|
+
// whose strings have changed since, answers its stored string as the raw
|
|
602
|
+
// expression, as GSF does for one too complex for the dialog's model, and
|
|
603
|
+
// no result expression.
|
|
604
|
+
const details = (template) => ({
|
|
605
|
+
requestFields: ['ID'],
|
|
606
|
+
resolver: ({ request, store }) => {
|
|
607
|
+
const rule = store.getRow('DYNAMIC_RULE', request.ID);
|
|
608
|
+
// A failed `require`: GENERIC_ERROR with its text; the request is a
|
|
609
|
+
// data class, so it prints as ById(id=…).
|
|
610
|
+
if (!rule)
|
|
611
|
+
throw new Error(`Could not find dynamic rule record for id: ById(id=${request.ID})`);
|
|
612
|
+
const model = keptFor(store, rule) ?? {
|
|
613
|
+
rule: null,
|
|
614
|
+
raw: rule.RULE_EXPRESSION ?? null,
|
|
615
|
+
result: null,
|
|
616
|
+
};
|
|
617
|
+
return [
|
|
618
|
+
{
|
|
619
|
+
DYNAMIC_RULE_ID: rule.ID,
|
|
620
|
+
RULE_NAME: rule.NAME,
|
|
621
|
+
RULE_DESCRIPTION: rule.DESCRIPTION ?? null,
|
|
622
|
+
RULE_TABLE: rule.RULE_TABLE ?? '',
|
|
623
|
+
RULE_EXECUTION_STRATEGY: rule.RULE_EXECUTION_STRATEGY,
|
|
624
|
+
RULE_EXPRESSION: model.rule,
|
|
625
|
+
RAW_RULE_EXPRESSION: model.raw,
|
|
626
|
+
RESULT_EXPRESSION: model.result,
|
|
627
|
+
...(template
|
|
628
|
+
? {
|
|
629
|
+
PARAMETERS: Object.fromEntries(paramsOf(store, rule.ID).map((param) => [param.PARAM_NAME, param])),
|
|
630
|
+
}
|
|
631
|
+
: {}),
|
|
632
|
+
TABLE_OPERATIONS: tableOperations(rule),
|
|
633
|
+
},
|
|
634
|
+
];
|
|
635
|
+
},
|
|
636
|
+
metadataRows: [
|
|
637
|
+
{
|
|
638
|
+
DYNAMIC_RULE_ID: '',
|
|
639
|
+
RULE_NAME: '',
|
|
640
|
+
RULE_DESCRIPTION: '',
|
|
641
|
+
RULE_TABLE: '',
|
|
642
|
+
RULE_EXECUTION_STRATEGY: 'UNLIMITED',
|
|
643
|
+
},
|
|
644
|
+
],
|
|
645
|
+
});
|
|
646
|
+
// NOTIFY_ALERT_SOUND (:172-203): the rule's sound, to whoever may hear it.
|
|
647
|
+
const alertSound = {
|
|
648
|
+
requestFields: ['DYNAMIC_RULE_ID'],
|
|
649
|
+
resolver: ({ request, store, user }) => {
|
|
650
|
+
const sound = store
|
|
651
|
+
.getAllRows('NOTIFY_ALERT_SOUND')
|
|
652
|
+
.find((row) => row.DYNAMIC_RULE_ID === request.DYNAMIC_RULE_ID);
|
|
653
|
+
const rule = sound && store.getRow('DYNAMIC_RULE', sound.DYNAMIC_RULE_ID);
|
|
654
|
+
if (!sound || !rule || !mayHear(store, rule, user))
|
|
655
|
+
return [];
|
|
656
|
+
return [
|
|
657
|
+
{
|
|
658
|
+
ALERT_SOUND_ID: sound.ALERT_SOUND_ID,
|
|
659
|
+
DYNAMIC_RULE_ID: sound.DYNAMIC_RULE_ID,
|
|
660
|
+
ALERT_SOUND_FILE_NAME: sound.ALERT_SOUND_FILE_NAME,
|
|
661
|
+
ALERT_SOUND_MIME_TYPE: sound.ALERT_SOUND_MIME_TYPE,
|
|
662
|
+
SOUND_DATA: sound.ALERT_SOUND_DATA,
|
|
663
|
+
},
|
|
664
|
+
];
|
|
665
|
+
},
|
|
666
|
+
metadataRows: [
|
|
667
|
+
{
|
|
668
|
+
ALERT_SOUND_ID: '',
|
|
669
|
+
DYNAMIC_RULE_ID: '',
|
|
670
|
+
ALERT_SOUND_FILE_NAME: '',
|
|
671
|
+
ALERT_SOUND_MIME_TYPE: '',
|
|
672
|
+
SOUND_DATA: '',
|
|
673
|
+
},
|
|
674
|
+
],
|
|
675
|
+
};
|
|
676
|
+
const mayHear = (store, rule, requester) => {
|
|
677
|
+
const name = requester.userName ?? users.fallback;
|
|
678
|
+
return (rule.IS_TEMPLATE === true ||
|
|
679
|
+
rule.USER_NAME === name ||
|
|
680
|
+
users.hasRight(store, name, ADMIN_RIGHT) ||
|
|
681
|
+
rule.USER_NAME === 'SYSTEM' ||
|
|
682
|
+
users.hasRight(store, rule.USER_NAME, ON_BEHALF_RIGHT));
|
|
683
|
+
};
|
|
684
|
+
return {
|
|
685
|
+
eventHandlers: Object.fromEntries(Object.entries(eventHandlers).map(([name, handler]) => [
|
|
686
|
+
name,
|
|
687
|
+
WITH_EXPRESSIONS.has(name)
|
|
688
|
+
? (details, ctx) => {
|
|
689
|
+
checkExpressions(details);
|
|
690
|
+
return handler(details, ctx);
|
|
691
|
+
}
|
|
692
|
+
: handler,
|
|
693
|
+
])),
|
|
694
|
+
eventSchemas: SCHEMAS,
|
|
695
|
+
requestReplies: {
|
|
696
|
+
NOTIFICATION_RULE_DETAILS: details(false),
|
|
697
|
+
NOTIFICATION_RULE_TEMPLATE_DETAILS: details(true),
|
|
698
|
+
NOTIFY_ALERT_SOUND: alertSound,
|
|
699
|
+
// The `filter` and the user filter the generated backing leaves out.
|
|
700
|
+
NOTIFICATION_RULE: {
|
|
701
|
+
source: 'DYNAMIC_RULE_VIEW',
|
|
702
|
+
requestFields: ['ID'],
|
|
703
|
+
filter: where(false),
|
|
704
|
+
auth: { rowFilter: (row, user) => row.USER_NAME === (user.userName ?? users.fallback) },
|
|
705
|
+
},
|
|
706
|
+
NOTIFICATION_RULE_TEMPLATE: {
|
|
707
|
+
source: 'DYNAMIC_RULE_VIEW',
|
|
708
|
+
requestFields: ['ID'],
|
|
709
|
+
filter: where(true),
|
|
710
|
+
},
|
|
711
|
+
},
|
|
712
|
+
queries: {
|
|
713
|
+
ALL_DYNAMIC_NOTIFY_RULES: { source: 'DYNAMIC_RULE', filter: where(false) },
|
|
714
|
+
ALL_RULE_TEMPLATES: { source: 'DYNAMIC_RULE', filter: where(true) },
|
|
715
|
+
},
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
// RULES-RR :80/:121: TABLE_OPERATION split on '|', each UpdateType.valueOf,
|
|
719
|
+
// whose failure (an empty or unknown one) fails the request.
|
|
720
|
+
function tableOperations(rule) {
|
|
721
|
+
return String(rule.TABLE_OPERATION ?? '')
|
|
722
|
+
.split('|')
|
|
723
|
+
.map((name) => {
|
|
724
|
+
if (!TABLE_OPERATIONS.includes(name)) {
|
|
725
|
+
throw new Error(`No enum constant global.genesis.db.updatequeue.UpdateType.${name}`);
|
|
726
|
+
}
|
|
727
|
+
return name;
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
// Mustache.java's HTML escaping of a value.
|
|
731
|
+
function escapeHtml(text) {
|
|
732
|
+
return (text
|
|
733
|
+
.replace(/&/g, '&')
|
|
734
|
+
.replace(/</g, '<')
|
|
735
|
+
.replace(/>/g, '>')
|
|
736
|
+
.replace(/"/g, '"')
|
|
737
|
+
.replace(/'/g, ''')
|
|
738
|
+
.replace(/`/g, '`')
|
|
739
|
+
.replace(/=/g, '=')
|
|
740
|
+
// HtmlEscaper (compiler 0.9.14) writes characters 0-13 as references.
|
|
741
|
+
// eslint-disable-next-line no-control-regex
|
|
742
|
+
.replace(/[\u0000-\u000d]/g, (char) => `&#${char.charCodeAt(0)};`));
|
|
743
|
+
}
|
|
744
|
+
// A model with each PLACEHOLDER replaced.
|
|
745
|
+
function fillModel(expression, placeholder) {
|
|
746
|
+
if (Array.isArray(expression))
|
|
747
|
+
return expression.map((item) => fillModel(item, placeholder));
|
|
748
|
+
if (typeof expression !== 'object' || expression === null)
|
|
749
|
+
return expression;
|
|
750
|
+
const node = expression;
|
|
751
|
+
if (node.TYPE === 'PLACEHOLDER')
|
|
752
|
+
return placeholder(String(node.KEY));
|
|
753
|
+
return Object.fromEntries(Object.entries(node).map(([key, value]) => [key, fillModel(value, placeholder)]));
|
|
754
|
+
}
|