-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
476 lines (368 loc) · 10.1 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//Includes------------------------------------------------------------------------------------------------------
var _ = require('lodash');
var async = require('async');
var pjson = require('./package.json');
// --------------------------------------------------------------------------------------------------
var glOptions;
const defDefshema = {
// user: {},
parameter: {},
// object: {},
// objectSet: {},
// message: {},
// messageSets: {},
// record: {},
// recordSet: {},
// lookup: {},
// binary: {},
// spreadsheet: {}
};
// Private Example
function fnConditional(parameter, next, exit){
try {
if (!parameter.zipFile){
next('STEP SKIPPED: parameter.zipFile=' + parameter.zipFile);
}
if (!parameter.outputFormat){
next('STEP SKIPPED: parameter.outputFormat=' + parameter.zipFile);
}
next(); // NO MESSAGE PAYLOAD SHOULD BE PROVIDED OR PROCESSED BY HANDLER FOR "TRUE" RESULT
} catch(err){
exit(err);
}
}
// Private Example
function fnTransformer(parameter, next, exit){
try {
parameter.localStartDate = parameter.startDate.substr(0, 6);
next('Transformation done');
} catch(err){
exit(err);
}
return fnConditional;
}
// ------------------------------------------------------------
// Private
// public
// private
function buildValidator(entrypoint, meta, message, fnLogic){
//ian make something to build these functions
var fnSuccess = function(meta, next, exit){
next('somehow in a successful way');
};
var fnFail = function(meta, next, exit){
exit(message);
};
if (fnLogic(returnMetaComponent(entrypoint, meta))){
return fnFail;
} else {
return fnSuccess;
}
}
//private
function buildActor(entrypoint, message){
return function(meta, next, exit){
};
}
// public
function wrapMetaFunction(entrypoint, fnLogic){
return function(meta, next, exit){
var metaComponent = returnMetaComponent(entrypoint, meta) //IW MUST FLESH THIS OUT TO USE A DEFAULT PATH IF NONE IS SUPPLIED
return fnLogic(metaComponent, next, exit);
};
}// public
function wrapFnAssert(entrypoint, fnLogic){
return function(meta, next, exit){
var metaComponent = returnMetaComponent(entrypoint, meta) //IW MUST FLESH THIS OUT TO USE A DEFAULT PATH IF NONE IS SUPPLIED
return fnLogic(metaComponent, next, exit);
};
}
// //Public
// function wrapConditional(entrypoint, fnConditionalLogic){
// return function(meta, next, exit){
// var metaComponent = returnMetaComponent(entrypoint, meta) //IW MUST FLESH THIS OUT TO USE A DEFAULT PATH IF NONE IS SUPPLIED
// return fnConditionalLogic(metaComponent, next, exit);
// };
// }
// // public
// function wrapValidator(entrypoint, fnTransformLogic){
// return function(meta, next, exit){
// var metaComponent = returnMetaComponent(entrypoint, meta) //IW MUST FLESH THIS OUT TO USE A DEFAULT PATH IF NONE IS SUPPLIED
// return fnTransformLogic(metaComponent, next, exit);
// };
// }
// // public
// function wrapTransformer(entrypoint, fnTransformLogic){
// return function(meta, next, exit){
// var metaComponent = returnMetaComponent(entrypoint, meta) //IW MUST FLESH THIS OUT TO USE A DEFAULT PATH IF NONE IS SUPPLIED
// return fnTransformLogic(metaComponent, next, exit);
// };
// }
//public
// Work here tomorrow
//public
//public
function packAction(entrypoint, actionName, fnValidator, fnActor){
var wrappedValidator = wrapMetaFunction(entrypoint, fnValidator);
var wrappedActor = wrapMetaFunction(entrypoint, fnActor);
var action = {name: actionName, validator: wrappedValidator, actor: wrappedActor};
return action;
}
// Public
function buildTransformAction(entrypoint, actionName, arValidationRules, arTransformRules){
var fnValidator = buildValidator(entrypoint, arValidationRules);
var fnActor = buildRulesTransformer(entrypoint, arTransformRules);
return packAction(entrypoint, actionName, fnValidator, fnActor);
}
//public
function buildValidatorWithRules(entryPoint, arValidationRules){
//do logic on arValidationRules to build resulting function
function fnValidator(meta, next, exit){
try {
if (meta.user.userId){
exit('USER NOT AUTHENTICATED');
}
next(); // NO MESSAGE PAYLOAD SHOULD BE PROVIDED OR PROCESSED BY HANDLER
} catch(err){
exit(err);
}
}
return fnValidator;
}
//public
function buildRulesTransformerActor(entrypoint, arTransformRules){
function fnActor(meta, next, exit){
try {
//BUILD FUNCTION BASED ON ENTRYPOINT AND RULES PROVIDED
//it will return a fnTransformer
var fnTransformer = function(meta, next, exit){
next('Transformation done');
}
var fnWrappedTransformer = wrapMetaFunction(entrypoint, fnTransformer);
meta.user = {firstName: 'Ian'};
next('USER DATA SET');
} catch(err){
exit(err);
}
}
return fnActor;
}
function buildTransformTriggerAction(arValidationRules, arTransformRules){
var fnTriggerValidator = function(triggerData, next, exit){
try {
if (triggerData.apikey){
exit('NO API KEY IN TRIGGER DATA'); //GRACEFUL EXIT PAYLOAD
}
next(); // NO MESSAGE PAYLOAD SHOULD BE PROVIDED OR PROCESSED BY HANDLER
} catch(err){
exit(err); //error Exit
}
};
var fnTriggerActor = function(triggerData, meta, next){
try {
meta.input.parameters.ruler = 'Ian the Fucking Great';
next('TRIGGER DATA PROCESSED'); //PAYLOAD SUGGESTED BUT OPTIONAL
} catch(err){
next(err); //ERROR PAYLOAD
}
};
var triggerAction = {validator: fnTriggerValidator, actor: fnTriggerActor};
return triggerAction;
}
module.exports = function(schema = defDefshema, fnTriggerMapper, moduleOptions){
var defOptions = {};
glOptions = _.extend(defOptions, moduleOptions);
glOptions.schema = schema;
glOptions.fnTriggerMapper = fnTriggerMapper;
var module = {
run: fnMiddleware,
version: fnGetVersion
};
return module;
};
// ------------------------ BASE FUNCTIONS ------------------------------------
function fnMiddleware(triggerObject, arActions, arReusables = []){
var ctx = {
meta: glOptions.schema,
reusables: {},
outputs: []
};
//eg.
// arReusables = [['sql', 'winsms4', sqlWinSms4], ['sql', 'winsmsint', sqlWinSmsInt], ['queue', 'sendSMS', tQueueSender]];
arReusables.forEach(function(arReusable){
ctx['reusables'][arReusable[0]][arReusable[1]] = arReusable[2];
});
var arPrmFunctions = [];
arActions.forEach(function(oAction){
arPrmFunctions.push(wrapValidator(oAction.validator));
arPrmFunctions.push(wrapActor(oAction.actor));
});
//private
function addOutput(output){
ctx.outputs.push(output);
};
processTriggerAction(triggerObject, glOptions.triggerAction)
.then(function(meta){
// THIS IS THE META I NEED FOR PRIVATE FUNCTIONS
function getMetaComponent(entryPoint){
//IW - 2021-07-03 - STUB - User JmesPath to return correct bit of meta
return meta.parameter;
}
var pFn = {};
pFn.getMetaComponent = getMetaComponent;
meta.function = pFn;
ctx.meta = meta;
return new Promise(function(fulfill, reject){
function buildDone(message){
if (message){
addOutput('done', message);
}
fulfill();
}
// private
function buildError(err){
reject(err);
}
// private
function buildFailer(message = 'User not populated'){
addOutput('fail', message);
reject();
}
// public
function buildExiter(message = 'parameters.zipFile is not true'){
addOutput('exit', message);
fulfill();
}
function conditionExiter(entrypoint, fnAssert, message = 'User Id Not set - cannot run this process'){
try {
if (fnAssert(pFn.getMetaComponent(entrypoint))){
return buildDone();
} else {
return buildExiter(message);
}
} catch(err){
return buildError(err);
}
}
function conditionFailer(entrypoint, fnAssert, message = 'User Id Not set - cannot run this process'){
try {
if (fnAssert(pFn.getMetaComponent(entrypoint))){
return buildDone();
} else {
return buildFailer(message);
}
} catch(err){
return buildError(err);
}
}
async.reduce(arPrmFunctions, ctx.meta, function(meta, item, callback){
item(meta)
.then(function(meta){
callback(null, meta);
})
.catch(function(err){
callback(err);
});
}, function(err, result){
if (err){
reject(err);
}
fulfill(result);
});
});
});
//private
function processTriggerAction(triggerObject, triggerAction){
var fnPrmTriggerValidator = wrapTriggerValidator(triggerObject, triggerAction.validator);
var fnPrmTriggerActor = wrapTriggerActor(triggerObject, triggerAction.actor);
return new Promise(function(fulfill, reject){
fnPrmTriggerValidator
.then(function(validatorResult){
fnPrmTriggerActor
.then(function(meta){
fulfill(meta);
})
.catch(function(err){
reject(err);
});
})
.catch(function(err){
reject(err);
});
});
}
function wrapTriggerValidator(triggerObject, fnIn){
return function(meta){
return new Promise(function(fulfill, reject){
function fnNext(err){
if (err){
reject(err);
}
fulfill(meta);
}
try {
fnIn(triggerObject, meta, fnNext);
} catch(err){
reject(err);
}
});
};
}
function wrapTriggerActor(triggerObject, fnIn){
return function(meta){
return new Promise(function(fulfill, reject){
function fnNext(err){
if (err){
reject(err);
}
fulfill(meta);
}
try {
fnIn(triggerObject, meta, fnNext);
} catch(err){
reject(err);
}
});
};
}
function wrapValidator(fnIn){
return function(meta){
return new Promise(function(fulfill, reject){
function fnNext(err){
if (err){
reject(err);
}
fulfill(meta);
}
try {
fnIn(meta, fnNext);
} catch(err){
reject(err);
}
});
};
}
function wrapActor(fnIn){
return function(meta){
return new Promise(function(fulfill, reject){
function fnNext(err){
if (err){
reject(err);
}
fulfill(meta);
}
try {
fnIn(meta, fnNext);
} catch(err){
reject(err);
}
});
};
}
}
function fnGetVersion(){
return pjson.version;
}
// function isObject(a){
// return (!!a) && (a.constructor === Object);
// }