@izara_project/izara-market-library-service-schemas 1.0.84 → 1.0.85

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.
Files changed (30) hide show
  1. package/index.js +0 -1
  2. package/package.json +1 -2
  3. package/src/GenerateCodeLibs/src/GenerateCodeLibs.js +17 -1938
  4. package/src/MainLibs/src/Consts.js +383 -4
  5. package/src/reStructure/TemplateData/EndpointPerService/handler/dsq/template.ejs +2 -2
  6. package/src/reStructure/TemplateData/EndpointPerService/handler/sqs/data.js +9 -0
  7. package/src/reStructure/TemplateData/EndpointPerService/handler/sqs/template.ejs +2 -2
  8. package/src/reStructure/TemplateData/EndpointPerService/mainFunction/create/template.ejs +16 -3
  9. package/src/reStructure/TemplateData/EndpointPerService/mainFunction/delete/template.ejs +2 -2
  10. package/src/reStructure/TemplateData/EndpointPerService/mainFunction/get/template.ejs +2 -2
  11. package/src/reStructure/TemplateData/EndpointPerService/mainFunction/update/template.ejs +18 -5
  12. package/src/reStructure/TemplateData/IntTest/generateTests/events/data.js +2 -0
  13. package/src/reStructure/TemplateData/IntTest/generateTests/pathIntTest/data.js +10 -1
  14. package/src/reStructure/TemplateData/IntTest/generateTests/pathIntTest/template.ejs +4 -4
  15. package/src/reStructure/TemplateData/IntTest/libs/libs.js +1 -1
  16. package/src/reStructure/TemplateData/externalService/functionNameConfig/templateIntTesting.ejs +1 -1
  17. package/src/reStructure/TemplateData/externalService/lambdaRole/template.ejs +1 -1
  18. package/src/reStructure/TemplateData/externalService/snsTopicSubscriptions/template.ejs +1 -1
  19. package/src/reStructure/TemplateData/flowSchema/triggerCacheComponent/processTriggerCache/handler/template.ejs +1 -0
  20. package/src/reStructure/TemplateData/relationshipPerAction/changeRelationship/action/handler/templateByHandler/dsqTemplate.ejs +1 -1
  21. package/src/reStructure/TemplateData/relationshipPerAction/changeRelationship/action/handler/templateByHandler/sqsTemplate.ejs +2 -3
  22. package/src/reStructure/TemplateData/relationshipPerAction/changeRelationship/action/mainFunction/template.ejs +1 -1
  23. package/src/reStructure/TemplateData/relationshipPerAction/get/action/mainFunction/template.ejs +1 -1
  24. package/src/reStructure/TemplateData/relationshipPerAction/moveRelationship/action/mainFunction/template.ejs +1 -1
  25. package/src/reStructure/TemplateData/relationshipPerAction/update/action/mainFunction/template.ejs +1 -1
  26. package/src/CheckPermission/CheckPermission.js +0 -122
  27. package/src/reStructure/TemplateData/relationshipPerAction/changeRelationship/action/handler/api/request.json +0 -0
  28. package/src/reStructure/TemplateData/relationshipPerAction/changeRelationship/action/handler/dsq/request.json +0 -0
  29. package/src/reStructure/TemplateData/relationshipPerAction/changeRelationship/action/handler/inv/request.json +0 -0
  30. package/src/reStructure/TemplateData/relationshipPerAction/changeRelationship/action/handler/sqs/request.json +0 -0
@@ -17,1190 +17,26 @@ along with this program.If not, see < http://www.gnu.org/licenses/>.
17
17
 
18
18
  import lodash from 'lodash';
19
19
  import { objectHash as hash } from '@izara_project/izara-shared-core';
20
- import {
21
- NoRetryError,
22
- inMemoryCacheLib,
23
- validator as validateObject
24
- } from '@izara_project/izara-core-library-core';
25
- import { sns, sqs } from '@izara_project/izara-core-library-external-request';
26
- import {
27
- getObjectSchema,
28
- validator as validatorSchema,
29
- serviceConfig,
30
- utils,
31
- consts as coreConsts,
32
- explodedReqParams,
33
- deliminatorTree
34
- } from '@izara_project/izara-core-library-service-schemas';
35
- import Logger from '@izara_project/izara-core-library-logger';
36
-
37
- import { validateObjType } from '@izara_project/izara-shared-service-schemas';
38
-
39
- // core libs
40
- import dynamodbSharedLib from '@izara_project/izara-core-library-dynamodb';
41
- import asyncFlowSharedLib from '@izara_project/izara-core-library-asynchronous-flow';
42
- import callingFlowSharedLib from '@izara_project/izara-core-library-calling-flow';
43
- import sqsSharedLib from '@izara_project/izara-core-library-sqs';
44
- import snsSharedLib from '@izara_project/izara-core-library-sns';
45
- import graphSharedLibs from '@izara_project/izara-core-library-graph-service';
46
- import lambdaSharedLib from '@izara_project/izara-core-library-lambda';
47
- import consts from './Consts.js';
48
- const { TOPIC_NAME_GENERATE_CODE, TOPIC_NAME_GRAPH_HANDLER, PREFIX } = consts;
49
-
50
- const nodeLabelRegexPattern = '^[a-zA-Z0-9_-]+(?:\:[a-zA-Z0-9_-]+)?$';
51
-
52
- const schemaFunctionPerAction = {
53
- [coreConsts.ACTIONS.create]: validatorSchema.generateValidatorSchemaForCreate,
54
- [coreConsts.ACTIONS.update]: validatorSchema.generateValidatorSchemaForUpdate,
55
- [coreConsts.ACTIONS.get]:
56
- validatorSchema.generateValidatorSchemaForIdentifier,
57
- [coreConsts.ACTIONS.delete]:
58
- validatorSchema.generateValidatorSchemaForIdentifier
59
- };
60
-
61
- const explodeDataPerAction = {
62
- [coreConsts.ACTIONS.create]: explodedReqParams.explodedDataForCreate,
63
- [coreConsts.ACTIONS.get]: explodedReqParams.explodedDataForIdentifiers,
64
- [coreConsts.ACTIONS.update]: explodedReqParams.explodedDataForUpdate,
65
- [coreConsts.ACTIONS.delete]: explodedReqParams.explodedDataForIdentifiers
66
- };
67
-
68
- /*
69
- ---------- Contain function use for generated code ----------
70
- */
71
-
72
- /**
73
- *
74
- * @param {middleware} middleware
75
- * @param {string} objectType - name of ObjectType
76
- * @param {string} action - action type of Lambda create | update | get | delete
77
- */
78
- function validateSchemaMiddleware(
79
- middleware,
80
- // objectType,
81
- action,
82
- bucketName = process.env.iz_serviceSchemaBucketName
83
- // setting = {}
84
- ) {
85
- if (!schemaFunctionPerAction[action] || !explodeDataPerAction[action]) {
86
- throw new NoRetryError(
87
- `Not found function for generateValidatorSchema or explodeDataPerAction of action:${action}`
88
- );
89
- }
90
- // Logger.debug("setting in generateCodeLibs", setting)
91
- middleware.setServiceSchema(
92
- schemaFunctionPerAction[action],
93
- explodeDataPerAction[action],
94
- {
95
- // objectType: objectType,
96
- // specificFieldNames: setting?.specificFieldNames ? setting.specificFieldNames : [],
97
- action: action,
98
- bucketName: bucketName
99
- }
100
- );
101
- }
102
-
103
- /**
104
- * use to validate record for Lambda handler hdrSqs/hdrDsq
105
- * Note!: cannot throw error when use this function outside recordHandler
106
- *
107
- * @param {object} record - one record from sqs
108
- * @param {string} lambdaFunctionName - name of Lambda used to build SQS/DLQ // QueueName
109
- * @param {string} objectType - name of ObjectType
110
- * @param {string} action - action type of Lambda create | update | get | delete
111
- * @param {object} [setting]
112
- * @param {string[]} [setting.specificFieldNames] - optional - specific fieldNames use as param in generateValidatorFunction
113
- */
114
- async function validateSchemaPerRecord(
115
- record,
116
- lambdaFunctionName,
117
- objType,
118
- action,
119
- setting = {
120
- bucketName: process.env.iz_serviceSchemaBucketName,
121
- specificFieldNames: []
122
- }
123
- ) {
124
- try {
125
- record._izContext.logger.debug(
126
- 'validateSchemaPerRecord: ',
127
- record,
128
- lambdaFunctionName,
129
- objType,
130
- action,
131
- setting
132
- );
133
-
134
- if (!schemaFunctionPerAction[action] || !explodeDataPerAction[action]) {
135
- throw new Error(
136
- `Not found function for generateValidatorSchema or function for explodeData of action:${action}`
137
- );
138
- }
139
-
140
- const objectSchema = await getObjectSchema.getObjSchemaS3WithHierarchy(
141
- record._izContext,
142
- objType
143
- );
144
-
145
- if (!objectSchema) {
146
- throw new Error('not have objectSchema in service');
147
- }
148
-
149
- const generateValidatorFunction = schemaFunctionPerAction[action];
150
- const explodeDataFunction = explodeDataPerAction[action];
151
-
152
- const generatedSchema = await generateValidatorFunction(
153
- record._izContext,
154
- objType,
155
- setting
156
- );
157
- record._izContext.logger.debug(
158
- `generatedSchema in validateSchemaPerRecord : `,
159
- generatedSchema
160
- );
161
-
162
- const explodedDataRequestParams = await explodeDataFunction(
163
- record._izContext,
164
- record.body.Message,
165
- objectSchema,
166
- setting
167
- );
168
-
169
- record._izContext.logger.debug(
170
- 'explodedDataRequestParams in validateSchemaPerRecord : ',
171
- JSON.stringify(explodedDataRequestParams)
172
- );
173
-
174
- let validateStatus = validateObject(
175
- generatedSchema,
176
- explodedDataRequestParams
177
- );
178
- record._izContext.logger.debug('validateStatus : ', validateStatus);
179
-
180
- // if not pass validate will sent message to dlq and throw NoRetryError
181
- if (!validateStatus.pass) {
182
- await messageToDlq(
183
- record,
184
- `Invalid: ${validateStatus.error}`,
185
- await sqsSharedLib.sqsQueueUrlDLQ(record._izContext, lambdaFunctionName)
186
- );
187
- record._izError = new Error(validateStatus.error);
188
- }
189
- } catch (error) {
190
- record._izContext.logger.debug('validate PerSchema Record error', error);
191
- await messageToDlq(
192
- record,
193
- error.message,
194
- await sqsSharedLib.sqsQueueUrlDLQ(record._izContext, lambdaFunctionName)
195
- );
196
- record._izError = new Error(error);
197
- }
198
- }
199
-
200
- /**
201
- * use to create validation for initialize request
202
- *
203
- * @param {object} _izContext
204
- * @param {object} flowType
205
- * @param {object} flowType.flowTag
206
- * @param {object} flowType.serviceTag
207
- * @returns {object}
208
- */
209
-
210
- async function validationForWebSocket(_izContext, flowType) {
211
- _izContext.logger.debug('validator schema for flowTag', {
212
- flowType
213
- });
214
- let flowSchema = await getObjectSchema.getFlowSchemaS3(_izContext, flowType);
215
-
216
- let validatorSchemaForFlowSchema =
217
- flowSchema.initializeRequest.initializeRequestProperties;
218
- _izContext.logger.debug(
219
- 'validatorSchemaForFlowSchema:',
220
- validatorSchemaForFlowSchema
221
- );
222
- }
223
-
224
- /**
225
- *
226
- * @param {middleware} middleware
227
- * @param {string} objectType - name of ObjectType
228
- * @param {string} action - action type of Lambda create | update | get | delete
229
- */
230
- function validatorSchemaMiddlewareByAction(
231
- middleware,
232
- // objectType,
233
- action,
234
- setting = {}
235
- ) {
236
- // if (!schemaFunctionPerAction[action] || !explodeDataPerAction[action]) {
237
- // throw new NoRetryError(`Not found function for generateValidatorSchema or explodeDataPerAction of action:${action}`);
238
- // }
239
- console.log('before send to middleware', {
240
- middleware,
241
- // objectType,
242
- action,
243
- setting
244
- });
245
- Logger.debug('setting in generateCodeLibs', setting);
246
- middleware.setServiceSchema(
247
- // schemaFunctionPerAction[action],
248
- // explodeDataPerAction[action],
249
- {
250
- // objectType: objectType,
251
- specificFieldNames: setting?.specificFieldNames
252
- ? setting.specificFieldNames
253
- : [],
254
- action: action
255
- }
256
- );
257
- }
258
- // /**
259
- // * use to validate record for Lambda handler hdrSqs/hdrDsq
260
- // * Note!: cannot throw error when use this function outside recordHandler
261
-
262
- // * @param {string} objectType - name of ObjectType
263
- // * @param {string} action - action type of Lambda create | update | get | delete
264
- // * @param {object} [setting]
265
- // */
266
- // async function validateSchemaPerWebSocket(
267
- // _izContext,
268
- // requestParams,
269
- // objectType,
270
- // action,
271
- // setting = {}
272
- // ) {
273
- // _izContext.logger.debug("validateSchema Per WebSocketFunction", {
274
- // objectType,
275
- // requestParams,
276
- // action,
277
- // setting
278
- // })
279
- // try {
280
- // if (action === "update") {
281
- // setting.updateScenario = "versionedData"
282
- // }
283
-
284
- // const objType = utils.createObjType(objectType)
285
- // _izContext.logger.debug("objType in validatorSchema per WebSocket", objType)
286
-
287
- // const objectSchema = await getObjectSchema.getObjSchemaS3WithHierarchy(_izContext, objType);
288
- // _izContext.logger.debug("objectSchema", objectSchema)
289
- // const generateValidatorSchema = schemaFunctionPerAction[action];
290
- // const explodeDataFunction = explodeDataPerAction[action];
291
-
292
- // const generateValidatorSchemaPerWebSocket = await generateValidatorSchema(_izContext, objType, setting);
293
- // _izContext.logger.debug("Generate Validator Schema For WebSocket :", generateValidatorSchemaPerWebSocket);
294
-
295
- // const explodedDataPerWebSocket = await explodeDataFunction(_izContext, requestParams, objectSchema, setting);
296
-
297
- // _izContext.logger.debug("exploded RequestParams Data in GeneratorValidatorSchema :", explodedDataPerWebSocket);
298
-
299
- // let validateStatus = validateObject(_izContext, generateValidatorSchemaPerWebSocket, explodedDataPerWebSocket);
300
- // _izContext.logger.debug('validateStatus : ', validateStatus);
301
-
302
- // if (!validateStatus.pass) {
303
- // const validatorError = new Error(`validation failed: ${validateStatus.error}`)
304
- // throw validatorError;
305
- // }
306
- // } catch (err) {
307
- // _izContext.logger.error("Error in validateSchemaPerWebSocket:", err);
308
- // throw err;
309
- // }
310
- // }
311
-
312
- async function sendMsgOutComplete(
313
- _izContext,
314
- topicName,
315
- messageObj,
316
- callingFlowConfig
317
- ) {
318
- try {
319
- // add callingFlow to message
320
- messageObj =
321
- callingFlowSharedLib.addPassBackPropertiesToSnsResponseMessageObject(
322
- callingFlowConfig,
323
- messageObj
324
- );
325
- let messageAttributes =
326
- callingFlowSharedLib.addCallingFlowToSnsResponseMessageAttributes(
327
- callingFlowConfig,
328
- {}
329
- );
330
-
331
- let sendMessageOutComplete = {
332
- Message: JSON.stringify(messageObj),
333
- MessageAttributes: sns.createStringMessageAttributes(
334
- _izContext,
335
- messageAttributes
336
- ),
337
- TopicArn: await snsSharedLib.snsTopicArn(_izContext, topicName)
338
- };
339
-
340
- _izContext.logger.debug(
341
- `Send message to ${topicName} :`,
342
- sendMessageOutComplete
343
- );
344
- await sns.publishAsync(_izContext, sendMessageOutComplete);
345
- return;
346
- } catch (error) {
347
- throw error;
348
- }
349
- }
350
-
351
- /**
352
- * @param {object} record - One record from sqs.
353
- * @param {string} messageFailError - The message error.
354
- * @param {object} lambdaFunctionName - Queue anme what to send to dlq./name of Lambda being invoked, used to build SQS/DLQ
355
- *
356
- */
357
- async function messageToDlq(record, messageFailError, queueUrl) {
358
- let messageBody = record.body;
359
-
360
- let params = {
361
- QueueUrl: queueUrl,
362
- MessageAttributes: {
363
- messageFailError: {
364
- DataType: 'String',
365
- StringValue: messageFailError
366
- }
367
- },
368
- MessageBody: JSON.stringify(messageBody)
369
- };
370
-
371
- record._izContext.logger.debug(
372
- 'messageToDlq, params before sending DLQ',
373
- params
374
- );
375
-
376
- await sqs.sendMessage(record._izContext, params);
377
- record._izContext.logger.debug(
378
- '----- messageToDlq sendMessage success -----'
379
- );
380
- }
381
-
382
- // {
383
- // [storageTag]: {
384
- // storageType: 'graph',
385
- // graphServiceTag: 'GraphHandler',
386
- // fieldNames: [ 'companyName', 'companyId' ],
387
- // versionedDatas: {
388
- // ceo: [ 'name', 'ceoAddress', 'salary' ],
389
- // branch: [ 'branchAddress', 'branchId' ]
390
- // }
391
- // versionedDataLabels:[],
392
- // }
393
- // }
394
-
395
- /**
396
- * helper function for get endpoint
397
- * create get data detail for each storageTag
398
- *
399
- * @param {Object} _izContext
400
- * @param {Object} objectSchema
401
- */
402
- async function createGetDataDetails(
403
- _izContext,
404
- objectSchema,
405
- settings = { bucketName: process.env.iz_serviceSchemaBucketName }
406
- ) {
407
- // group versionedData per storageTag
408
- // or should group versionedData per graph serviceTag
409
- let versionedDataPerStorageTag = {};
410
- if (objectSchema?.addOnDataStructure?.length) {
411
- for (let addOn of objectSchema.addOnDataStructure) {
412
- if (addOn.type !== 'versionedData') {
413
- continue;
414
- }
415
-
416
- if (
417
- !versionedDataPerStorageTag.hasOwnProperty(addOn.storageResourceTag)
418
- ) {
419
- versionedDataPerStorageTag[addOn.storageResourceTag] = [];
420
- }
421
-
422
- versionedDataPerStorageTag[addOn.storageResourceTag].push(addOn);
423
- }
424
- }
425
-
426
- _izContext.logger.debug(
427
- 'versionedDataPerStorageTag: ',
428
- versionedDataPerStorageTag
429
- );
430
-
431
- let getGraphDataDetails = {};
432
- let getDynamoDbDataDetails = {};
433
-
434
- // reference to graph serviceTag that already used
435
- let graphStorageTagPerGraphServiceTag = {}; // {serviceTag:storageTag,... }
436
-
437
- for (let [storageTag, storageData] of Object.entries(
438
- objectSchema.storageResources
439
- )) {
440
- // collect fieldNames per storageTag
441
- let storageTagFieldNames = [];
442
- for (let [fieldName, fieldNameData] of Object.entries(
443
- objectSchema.fieldNames
444
- )) {
445
- // if (fieldName.startsWith("versionedDataField")) {
446
- // continue;
447
- // }
448
-
449
- if (fieldNameData.storageResourceTags.includes(storageTag)) {
450
- storageTagFieldNames.push(fieldName);
451
- }
452
- }
453
-
454
- if (storageData.storageType === coreConsts.STORAGE_TYPES.graph) {
455
- let useStorageTag = storageTag;
456
- let graphServiceTag = await serviceConfig.getGraphServiceTagWithCache(
457
- _izContext,
458
- storageData.graphServerTag,
459
- settings.bucketName
460
- );
461
-
462
- // check duplicate graph serviceTag
463
- if (!graphStorageTagPerGraphServiceTag.hasOwnProperty(graphServiceTag)) {
464
- graphStorageTagPerGraphServiceTag[graphServiceTag] = storageTag;
465
- } else {
466
- // overwrite useStorageTag if graphServiceTag already used
467
- _izContext.logger.debug('current storageTag: ', storageTag);
468
- _izContext.logger.debug('use storageTag: ', useStorageTag);
469
-
470
- useStorageTag = graphStorageTagPerGraphServiceTag[graphServiceTag];
471
- }
472
-
473
- if (!getGraphDataDetails.hasOwnProperty(useStorageTag)) {
474
- getGraphDataDetails[useStorageTag] = {
475
- storageType: storageData.storageType,
476
- graphServiceTag: graphServiceTag,
477
- fieldNames: storageTagFieldNames
478
- };
479
- } else {
480
- getGraphDataDetails[useStorageTag].fieldNames = [
481
- ...new Set(
482
- getGraphDataDetails[useStorageTag].fieldNames.concat(
483
- storageTagFieldNames
484
- )
485
- )
486
- ];
487
- }
488
-
489
- // add versionedData fieldNames
490
- if (versionedDataPerStorageTag.hasOwnProperty(storageTag)) {
491
- if (
492
- !getGraphDataDetails[useStorageTag].hasOwnProperty('versionedDatas')
493
- ) {
494
- getGraphDataDetails[useStorageTag].versionedDatas = {};
495
- }
496
-
497
- for (let versionedData of versionedDataPerStorageTag[storageTag]) {
498
- _izContext.logger.debug(
499
- 'versionedData.fieldNames',
500
- versionedData.fieldNames
501
- );
502
- let versionedDataFieldNames = Object.keys(versionedData.fieldNames);
503
- Object.assign(getGraphDataDetails[useStorageTag].versionedDatas, {
504
- [versionedData.versionedDataLabel]: versionedDataFieldNames
505
- });
506
- }
507
- }
508
- } else if (storageData.storageType === coreConsts.STORAGE_TYPES.dynamoDB) {
509
- let serviceTag = storageData.serviceTag || process.env.iz_serviceTag;
510
- Object.assign(getDynamoDbDataDetails, {
511
- [storageTag]: {
512
- storageType: storageData.storageType,
513
- tableName: storageData.tableName,
514
- serviceTag: serviceTag,
515
- fieldNames: storageTagFieldNames
516
- }
517
- });
518
-
519
- if (storageData.groupByPartitionKeyField) {
520
- Object.assign(getDynamoDbDataDetails[storageTag], {
521
- groupByPartitionKeyField: storageData.groupByPartitionKeyField
522
- });
523
- }
524
- } else {
525
- throw new NoRetryError('Unhanled storageType');
526
- }
527
- }
528
-
529
- _izContext.logger.debug('getGraphDataDetail: ', getGraphDataDetails);
530
- _izContext.logger.debug('getDynamoDbData: ', getDynamoDbDataDetails);
531
-
532
- //-------- sort descending order of fieldNames.length --------
533
- let sortedGetGraphWithVersionedData = Object.entries(getGraphDataDetails)
534
- .filter(getGraphDetail =>
535
- getGraphDetail[1].hasOwnProperty('versionedDatas')
536
- )
537
- .sort(
538
- (storageDataA, storageDataB) =>
539
- storageDataB[1].fieldNames.length - storageDataA[1].fieldNames.length
540
- );
541
-
542
- _izContext.logger.debug(
543
- 'sortedGetGraphWithVersionedData: ',
544
- sortedGetGraphWithVersionedData
545
- );
546
-
547
- let sortedGetGraphWithoutVersionedData = Object.entries(getGraphDataDetails)
548
- .filter(
549
- getGraphDetail => !getGraphDetail[1].hasOwnProperty('versionedDatas')
550
- )
551
- .sort(
552
- (storageDataA, storageDataB) =>
553
- storageDataB[1].fieldNames.length - storageDataA[1].fieldNames.length
554
- );
555
-
556
- _izContext.logger.debug(
557
- 'sortedGetGraphWithoutVersionedData: ',
558
- sortedGetGraphWithoutVersionedData
559
- );
560
-
561
- let sortedGetDynamoDbData = Object.entries(getDynamoDbDataDetails).sort(
562
- (storageDataA, storageDataB) =>
563
- storageDataB[1].fieldNames.length - storageDataA[1].fieldNames.length
564
- );
565
-
566
- _izContext.logger.debug('sortedGetDynamoDbData: ', sortedGetDynamoDbData);
567
-
568
- // after sort data then filter duplicate fieldName
569
- let allSortedGetDataDetails = [
570
- ...sortedGetGraphWithVersionedData,
571
- ...sortedGetDynamoDbData,
572
- ...sortedGetGraphWithoutVersionedData
573
- ];
574
- _izContext.logger.debug('allSortedGetDataDetails: ', allSortedGetDataDetails);
575
-
576
- let usedFieldName = [];
577
- for (let [storageTag, getDataDetail] of allSortedGetDataDetails) {
578
- let remainsFieldNames = [];
579
- while (getDataDetail.fieldNames.length) {
580
- let checkedFiedName = getDataDetail.fieldNames.pop();
581
- if (!usedFieldName.includes(checkedFiedName)) {
582
- usedFieldName.push(checkedFiedName);
583
- remainsFieldNames.push(checkedFiedName);
584
- }
585
- }
586
-
587
- getDataDetail.fieldNames = remainsFieldNames;
588
- }
589
-
590
- _izContext.logger.debug('newSortedArray after: ', allSortedGetDataDetails);
591
-
592
- let getDataDetailResults = Object.fromEntries(
593
- allSortedGetDataDetails.filter(([storageTag, getDataDetail]) => {
594
- return (
595
- getDataDetail.fieldNames.length ||
596
- getDataDetail.hasOwnProperty('versionedDatas')
597
- );
598
- })
599
- );
600
-
601
- _izContext.logger.debug('getDataDetailResults: ', getDataDetailResults);
602
-
603
- for (const getDataDetail of Object.values(getDataDetailResults)) {
604
- if (getDataDetail.hasOwnProperty('versionedDatas')) {
605
- getDataDetail.versionedDataLabels = [];
606
- for (let [versionedDataLabel, fieldNames] of Object.entries(
607
- getDataDetail.versionedDatas
608
- )) {
609
- getDataDetail.versionedDataLabels.push(versionedDataLabel);
610
- getDataDetail.fieldNames.push(...fieldNames);
611
- }
612
- }
613
- }
614
-
615
- return getDataDetailResults;
616
- }
617
-
618
- /**
619
- * create dynamoDb identifiers using request param and objectSchema
620
- *
621
- * @param {Object} _izContext
622
- * @param {Object} objectSchema
623
- * @param {Object} reqIdentifiers - input identifier from calling function
624
- */
625
- async function generateDynamoDbIdentifiers(
626
- _izContext,
627
- objectSchema,
628
- reqIdentifiers,
629
- bucketName = process.env.iz_serviceSchemaBucketName
630
- ) {
631
- // start create identifiers for dynamo
632
- let dynamoIdentifiers = {};
633
-
634
- for (let identifier of objectSchema.identifiers) {
635
- if (identifier.name) {
636
- let deliminator =
637
- identifier.deliminator || coreConsts.DEFAULT_IDENTIFIER_DELIMINATOR;
638
- let currentDeliminatorAmount = 0;
639
-
640
- let concatenateValue = [];
641
- for (let fieldName of identifier.fieldNames) {
642
- concatenateValue.push(reqIdentifiers[fieldName]);
643
-
644
- let deliminatorTreeFieldName =
645
- await deliminatorTree.generateDeliminatorTreePerFieldName(
646
- _izContext,
647
- fieldName,
648
- objectSchema.fieldNames[fieldName],
649
- {},
650
- bucketName
651
- );
652
- _izContext.logger.debug(
653
- 'deliminatorTreeFieldName: ',
654
- deliminatorTreeFieldName
655
- );
656
-
657
- if (
658
- deliminatorTreeFieldName[fieldName]?.deliminatorList?.[deliminator]
659
- ) {
660
- currentDeliminatorAmount = Math.max(
661
- currentDeliminatorAmount,
662
- deliminatorTreeFieldName[fieldName].deliminatorList[deliminator]
663
- );
664
- }
665
- }
666
-
667
- Object.assign(dynamoIdentifiers, {
668
- [identifier.name]: concatenateValue.join(
669
- deliminator.repeat(currentDeliminatorAmount + 1)
670
- )
671
- });
672
- } else {
673
- Object.assign(dynamoIdentifiers, {
674
- [identifier.fieldName]: reqIdentifiers[identifier.fieldName]
675
- });
676
- }
677
- }
678
-
679
- _izContext.logger.debug('dynamoIdentifiers: ', dynamoIdentifiers);
680
-
681
- return dynamoIdentifiers;
682
- }
683
-
684
- /**
685
- * create dynamoDb identifiers using request param and objectSchema
686
- *
687
- * @param {Object} _izContext
688
- * @param {Object} objectSchema
689
- * @param {Object} reqIdentifiers - input identifier from calling function
690
- */
691
- const generateDynamoDbIdentifiersWithCache = inMemoryCacheLib(
692
- generateDynamoDbIdentifiers, // fn
693
- {
694
- // setting
695
- max: 20,
696
- maxAge: 86400000,
697
- promise: true,
698
- profileName: 'generateDynamoDbIdentifiers',
699
- normalizer: function (args) {
700
- return hash([args[1], args[2]]);
701
- }
702
- }
703
- );
704
-
705
- /**
706
- *
707
- * @param {Object} _izContext
708
- * @param {Object} objectSchema
709
- * @param {Object} dynamoStorageResource
710
- * @param {Object} reqIdentifiers
711
- */
712
- async function dynamoDbIdentifiersByStorageResource(
713
- _izContext,
714
- objectSchema,
715
- dynamoStorageResource,
716
- reqIdentifiers,
717
- bucketName = process.env.iz_serviceSchemaBucketName
718
- ) {
719
- let dynamoIdentifiers = await generateDynamoDbIdentifiersWithCache(
720
- _izContext,
721
- objectSchema,
722
- reqIdentifiers,
723
- bucketName
724
- );
725
-
726
- let identifiersByStorageResource = {};
727
-
728
- if (dynamoStorageResource.groupByPartitionKeyField) {
729
- let deliminatorTreeIdentifiers =
730
- await deliminatorTree.generateDeliminatorTreeIdentifier(
731
- _izContext,
732
- objectSchema,
733
- bucketName
734
- );
735
- _izContext.logger.debug('deliminatorTree: ', deliminatorTreeIdentifiers);
736
-
737
- let compositeKeyDeliminator =
738
- objectSchema.compositeKeyDeliminator ||
739
- coreConsts.DEFAULT_IDENTIFIER_DELIMINATOR;
740
-
741
- let currentDeliminatorAmount = 0;
742
-
743
- for (let currentDeliminator of Object.values(deliminatorTreeIdentifiers)) {
744
- if (currentDeliminator.deliminator === compositeKeyDeliminator) {
745
- if (
746
- currentDeliminatorAmount <
747
- currentDeliminator.deliminatorList[compositeKeyDeliminator]
748
- ) {
749
- currentDeliminatorAmount =
750
- currentDeliminator.deliminatorList[compositeKeyDeliminator];
751
- }
752
- }
753
- }
754
-
755
- // sort dynamoDbIdentifiers data by objectSchema
756
- let sortedIdentifiersValue = [];
757
- for (let identifier of objectSchema.identifiers) {
758
- let identifierName = identifier.fieldName || identifier.name;
759
- sortedIdentifiersValue.push(dynamoIdentifiers[identifierName]);
760
- }
761
-
762
- let newIdentifierValue = sortedIdentifiersValue.join(
763
- compositeKeyDeliminator.repeat(currentDeliminatorAmount + 1)
764
- );
765
-
766
- identifiersByStorageResource = {
767
- [dynamoStorageResource.groupByPartitionKeyField]: newIdentifierValue
768
- };
769
- } else {
770
- identifiersByStorageResource = dynamoIdentifiers;
771
- }
772
-
773
- return identifiersByStorageResource;
774
- }
775
-
776
- /**
777
- * receive result from multiple database and create data format depend on returnFormat setting
778
- *
779
- * @param {object} _izContext
780
- * @param {object[]} getResults
781
- * @param {object} objectSchema
782
- * @returns
783
- */
784
- function collectGetData(
785
- _izContext,
786
- getResults,
787
- objectSchema,
788
- returnSystemFieldsName
789
- ) {
790
- _izContext.logger.debug('collectGetData: ', {
791
- getResults,
792
- objectSchema,
793
- returnSystemFieldsName
794
- });
795
-
796
- let collectedData = {
797
- identifiers: {},
798
- fields: {}
799
- };
800
-
801
- if (returnSystemFieldsName) {
802
- Object.assign(collectedData, { versionDataSystemFields: {} });
803
- }
804
- const identifierFieldNames = new Set(
805
- utils.getUsedFieldNamesOfIdentifiers(_izContext, objectSchema.identifiers)
806
- );
807
-
808
- let addedFieldNames = new Set();
809
-
810
- // if isFound === true then return null from this function
811
- let isFound = false;
812
-
813
- for (const [getData, getDataDetail] of getResults) {
814
- if (!getData || !Object.keys(getData).length) {
815
- continue;
816
- }
817
-
818
- // this process will add result from graph and dynamo to collectedData object by add identifiers and fieldName from getData
819
- if (getDataDetail.storageType === coreConsts.STORAGE_TYPES.graph) {
820
- if (
821
- getData.returnValue.queryResults.refactoredNodes.objInstanceFull &&
822
- Object.keys(
823
- getData.returnValue.queryResults.refactoredNodes.objInstanceFull
824
- ).length
825
- ) {
826
- for (const getDetailFieldName of getDataDetail.fieldNames) {
827
- if (addedFieldNames.has(getDetailFieldName)) {
828
- continue;
829
- }
830
-
831
- if (
832
- getData.returnValue.queryResults.refactoredNodes.objInstanceFull.identifiers.hasOwnProperty(
833
- getDetailFieldName
834
- )
835
- ) {
836
- Object.assign(collectedData.identifiers, {
837
- [getDetailFieldName]:
838
- getData.returnValue.queryResults.refactoredNodes.objInstanceFull
839
- .identifiers[getDetailFieldName]
840
- });
841
- addedFieldNames.add(getDetailFieldName);
842
- isFound = true;
843
- } else if (
844
- getData.returnValue.queryResults.refactoredNodes.objInstanceFull.fields.hasOwnProperty(
845
- getDetailFieldName
846
- )
847
- ) {
848
- Object.assign(collectedData.fields, {
849
- [getDetailFieldName]:
850
- getData.returnValue.queryResults.refactoredNodes.objInstanceFull
851
- .fields[getDetailFieldName]
852
- });
853
- addedFieldNames.add(getDetailFieldName);
854
- isFound = true;
855
- }
856
- }
857
- }
858
- if (
859
- getData.returnValue.queryResults.refactoredNodes
860
- .versionDataSystemFields ||
861
- returnSystemFieldsName == true
862
- ) {
863
- Object.assign(
864
- collectedData.versionDataSystemFields,
865
- getData.returnValue.queryResults.refactoredNodes
866
- .versionDataSystemFields
867
- );
868
- }
869
- } else if (
870
- getDataDetail.storageType === coreConsts.STORAGE_TYPES.dynamoDB
871
- ) {
872
- for (const getDetailFieldName of getDataDetail.fieldNames) {
873
- if (addedFieldNames.has(getDetailFieldName)) {
874
- continue;
875
- }
876
-
877
- if (getData.hasOwnProperty(getDetailFieldName)) {
878
- if (identifierFieldNames.has(getDetailFieldName)) {
879
- Object.assign(collectedData.identifiers, {
880
- [getDetailFieldName]: getData[getDetailFieldName]
881
- });
882
- addedFieldNames.add(getDetailFieldName);
883
- isFound = true;
884
- } else {
885
- Object.assign(collectedData.fields, {
886
- [getDetailFieldName]: getData[getDetailFieldName]
887
- });
888
- addedFieldNames.add(getDetailFieldName);
889
- isFound = true;
890
- }
891
- }
892
- }
893
- } else {
894
- throw new NoRetryError('collectGetData unhandled storageType');
895
- }
896
- }
897
-
898
- if (isFound) {
899
- return collectedData;
900
- } else {
901
- return null;
902
- }
903
- }
904
-
905
- async function createUpdateDataDetail(
906
- _izContext,
907
- objectSchema,
908
- settings = { bucketName: process.env.iz_serviceSchemaBucketName }
909
- ) {
910
- let getGraphDataDetails = {};
911
- let getDynamoDbDataDetails = {};
912
- let allUpdateDataDetail = {};
913
- for (let [storageTag, storageProperties] of Object.entries(
914
- objectSchema.storageResources
915
- )) {
916
- let storageFieldNames = [];
917
- for (let [fieldName, fieldNameProperties] of Object.entries(
918
- objectSchema.fieldNames
919
- )) {
920
- if (fieldNameProperties.storageResourceTags.includes(storageTag)) {
921
- let versionedDataLabel = fieldNameProperties.versionedDataLabel;
922
- if (!fieldName.includes(versionedDataLabel)) {
923
- // not get fieldNames VersionedData_VersionedDataLabel_VersionedDataFieldNames
924
- storageFieldNames.push(fieldName);
925
- }
926
- }
927
- }
928
- if (storageProperties.storageType === coreConsts.STORAGE_TYPES.graph) {
929
- let graphStorageTagPerGraphServiceTag = {};
930
- let useStorageTag = storageTag;
931
- let graphServiceName = await serviceConfig.getGraphServiceTagWithCache(
932
- _izContext,
933
- storageProperties.graphServerTag,
934
- settings.bucketName
935
- );
936
- if (!graphStorageTagPerGraphServiceTag.hasOwnProperty(graphServiceName)) {
937
- graphStorageTagPerGraphServiceTag[graphServiceName] = storageTag;
938
- _izContext.logger.debug(
939
- 'graphStorageTagPerGraphServiceTag',
940
- graphStorageTagPerGraphServiceTag
941
- );
942
- } else {
943
- _izContext.logger.debug('current storageTag', storageTag);
944
- _izContext.logger.debug('used storageTag', useStorageTag);
945
- useStorageTag = graphStorageTagPerGraphServiceTag[graphServiceName];
946
- }
947
- if (!getGraphDataDetails.hasOwnProperty(useStorageTag)) {
948
- getGraphDataDetails[graphServiceName] = {
949
- storageType: storageProperties.storageType,
950
- fieldNames: storageFieldNames
951
- };
952
- } else {
953
- getGraphDataDetails[graphServiceName].fieldNames = [
954
- ...new Set(
955
- getGraphDataDetails[useStorageTag].fieldNames.concat(
956
- storageFieldNames
957
- )
958
- )
959
- ];
960
- }
961
- } else if (
962
- storageProperties.storageType === coreConsts.STORAGE_TYPES.dynamoDB
963
- ) {
964
- let serviceTag =
965
- storageProperties.serviceTag || process.env.iz_serviceTag;
966
- Object.assign(getDynamoDbDataDetails, {
967
- [storageTag]: {
968
- storageType: storageProperties.storageType,
969
- tableName: storageProperties.tableName,
970
- serviceTag: serviceTag,
971
- fieldNames: storageFieldNames
972
- }
973
- });
974
-
975
- if (storageTag.groupByPartitionKeyField) {
976
- Object.assign(getDynamoDbDataDetails[storageTag], {
977
- groupByPartitionKeyField: storageProperties.groupByPartitionKeyField
978
- });
979
- }
980
- }
981
- }
982
-
983
- _izContext.logger.debug('getGraphDataDetails', getGraphDataDetails);
984
- _izContext.logger.debug('getDynamoDataDetail', getDynamoDbDataDetails);
985
-
986
- Object.assign(
987
- allUpdateDataDetail,
988
- getDynamoDbDataDetails,
989
- getGraphDataDetails
990
- );
991
- return allUpdateDataDetail;
992
- }
993
-
994
- async function createDataDetailsLib(
995
- _izContext,
996
- objectSchemas,
997
- settings = { bucketName: process.env.iz_serviceSchemaBucketName }
998
- ) {
999
- _izContext.logger.debug('Lib: createDataDetailsLib:', {
1000
- objectSchemas: objectSchemas
1001
- });
1002
- /**
1003
- * TODO: crate DataDetail now will get parent objectSchema if have extendObjType
1004
- * will split fieldNames for each serviceTag and storageREsourceTags
1005
- * */
1006
-
1007
- let createDataDetails = {};
1008
-
1009
- let storageResources = objectSchemas.storageResources;
1010
- _izContext.logger.debug('storageResources:', storageResources);
1011
-
1012
- for (let [keyFieldName, settingFieldName] of Object.entries(
1013
- objectSchemas.fieldNames
1014
- )) {
1015
- // _izContext.logger.debug("Loop fieldNamesObjectSchemas", { keyFieldName });
1016
-
1017
- for (let eachStorageResourceTag of settingFieldName.storageResourceTags) {
1018
- // _izContext.logger.debug("Loop eachStorageResourceTags", eachStorageResourceTag);
1019
-
1020
- if (!storageResources.hasOwnProperty(eachStorageResourceTag)) {
1021
- throw new Error("storageResources is'n exist"); // should be validate in step upload in s3
1022
- } else {
1023
- if (
1024
- storageResources[eachStorageResourceTag].storageType ==
1025
- coreConsts.STORAGE_TYPES.dynamoDB
1026
- ) {
1027
- if (createDataDetails.hasOwnProperty(eachStorageResourceTag)) {
1028
- // _izContext.logger.debug("SAME STG DB", eachStorageResourceTag);
1029
- createDataDetails[eachStorageResourceTag].fieldNames.push(
1030
- keyFieldName
1031
- );
1032
- } else {
1033
- Object.assign(createDataDetails, {
1034
- [eachStorageResourceTag]: {
1035
- storageType: coreConsts.STORAGE_TYPES.dynamoDB,
1036
- tableName: storageResources[eachStorageResourceTag].tableName,
1037
- serviceTag:
1038
- storageResources[eachStorageResourceTag].serviceTag ||
1039
- process.env.iz_serviceTag,
1040
- fieldNames: [keyFieldName]
1041
- }
1042
- });
1043
-
1044
- if (
1045
- storageResources[eachStorageResourceTag].hasOwnProperty(
1046
- 'groupByPartitionKeyField'
1047
- )
1048
- ) {
1049
- Object.assign(createDataDetails[eachStorageResourceTag], {
1050
- groupByPartitionKeyField:
1051
- storageResources[eachStorageResourceTag]
1052
- .groupByPartitionKeyField
1053
- });
1054
- }
1055
- }
1056
- } else if (
1057
- storageResources[eachStorageResourceTag].storageType ==
1058
- coreConsts.STORAGE_TYPES.graph
1059
- ) {
1060
- let checkGraphServerTags =
1061
- await serviceConfig.getGraphServiceTagWithCache(
1062
- _izContext,
1063
- storageResources[eachStorageResourceTag].graphServerTag,
1064
- settings.bucketName
1065
- );
1066
- if (checkGraphServerTags) {
1067
- if (createDataDetails.hasOwnProperty(checkGraphServerTags)) {
1068
- // _izContext.logger.debug("SAME STG", checkGraphServerTags);
1069
- createDataDetails[checkGraphServerTags].fieldNames.push(
1070
- keyFieldName
1071
- );
1072
- } else {
1073
- // _izContext.logger.debug("NEW STG", checkGraphServerTags);
1074
- Object.assign(createDataDetails, {
1075
- [checkGraphServerTags]: {
1076
- storageType: coreConsts.STORAGE_TYPES.graph,
1077
- fieldNames: [keyFieldName]
1078
- }
1079
- });
1080
- }
1081
- }
1082
- } else if (
1083
- storageResources[eachStorageResourceTag].storageType ===
1084
- coreConsts.STORAGE_TYPES.externalTopic
1085
- ) {
1086
- let topicName =
1087
- storageResources[eachStorageResourceTag].serviceTag +
1088
- storageResources[eachStorageResourceTag].stage +
1089
- storageResources[eachStorageResourceTag].topicName;
1090
- Object.assign(createDataDetails, {
1091
- [topicName]: {
1092
- storageType: coreConsts.STORAGE_TYPES.externalTopic,
1093
- fieldNames: [keyFieldName],
1094
- accountId: storageResources[eachStorageResourceTag].accountId,
1095
- region: storageResources[eachStorageResourceTag].region,
1096
- serviceTag: storageResources[eachStorageResourceTag].serviceTag,
1097
- stage: storageResources[eachStorageResourceTag].stage
1098
- }
1099
- });
1100
- }
1101
- }
1102
- } // end loop storageResourceTags
1103
- } // end loop
1104
-
1105
- return createDataDetails;
1106
- }
1107
-
1108
- async function createDeleteDataDetail(
1109
- _izContext,
1110
- objectSchema,
1111
- settings = { bucketName: process.env.iz_serviceSchemaBucketName }
1112
- ) {
1113
- let deleteDynamoDataDetail = {};
1114
- let deleteGraphDataDetail = {};
1115
- let allDeleteDataDetail = {};
1116
-
1117
- for (let [storageTag, storageProperties] of Object.entries(
1118
- objectSchema.storageResources
1119
- )) {
1120
- if (storageProperties.storageType === coreConsts.STORAGE_TYPES.dynamoDB) {
1121
- let serviceTag =
1122
- storageProperties.serviceTag || process.env.iz_serviceTag;
1123
- Object.assign(deleteDynamoDataDetail, {
1124
- [storageTag]: {
1125
- storageType: storageProperties.storageType,
1126
- serviceTag: serviceTag,
1127
- tableName: storageProperties.tableName
1128
- }
1129
- });
1130
- if (storageProperties.groupByPartitionKeyField) {
1131
- Object.assign(deleteDynamoDataDetail[storageTag], {
1132
- groupByPartitionKeyField: storageProperties.groupByPartitionKeyField
1133
- });
1134
- }
1135
- } else if (
1136
- storageProperties.storageType === coreConsts.STORAGE_TYPES.graph
1137
- ) {
1138
- let dataDetailPerGraphStorage = {};
1139
- let useStorageTag = storageTag;
1140
- let graphServiceName = await serviceConfig.getGraphServiceTagWithCache(
1141
- _izContext,
1142
- storageProperties.graphServerTag,
1143
- settings.bucketName
1144
- );
1145
- if (!dataDetailPerGraphStorage.hasOwnProperty(graphServiceName)) {
1146
- dataDetailPerGraphStorage[graphServiceName] = storageTag;
1147
- } else {
1148
- useStorageTag = dataDetailPerGraphStorage[graphServiceName];
1149
- }
1150
- if (!deleteGraphDataDetail.hasOwnProperty(useStorageTag)) {
1151
- deleteGraphDataDetail[graphServiceName] = {
1152
- storageType: storageProperties.storageType
1153
- };
1154
- }
1155
- }
1156
- }
1157
- _izContext.logger.debug('deleteGraphDataDetail', deleteGraphDataDetail);
1158
- _izContext.logger.debug('deleteDynamoDataDetail', deleteDynamoDataDetail);
1159
- Object.assign(
1160
- allDeleteDataDetail,
1161
- deleteGraphDataDetail,
1162
- deleteDynamoDataDetail
1163
- );
1164
20
 
1165
- _izContext.logger.debug('allDeleteDataDetail', allDeleteDataDetail);
1166
- return allDeleteDataDetail;
1167
- }
21
+ import { sns, sqs } from '@izara_project/izara-core-library-external-request';
22
+ import {
23
+ getObjectSchema,
24
+ consts as coreConsts,
25
+ } from '@izara_project/izara-core-library-service-schemas';
1168
26
 
1169
- function createFieldForUpdateDynamoDb(
1170
- _izContext,
1171
- objectSchema,
1172
- dynamoDataDetail,
1173
- fields
1174
- ) {
1175
- _izContext.logger.debug('createFieldForUpdateDynamoDb', {
1176
- objectSchema,
1177
- dynamoDataDetail,
1178
- fields
1179
- });
27
+ import { validateObjType } from '@izara_project/izara-shared-service-schemas';
1180
28
 
1181
- let fieldsForUpdateDynamo = {};
29
+ // core libs
30
+ import dynamodbSharedLib from '@izara_project/izara-core-library-dynamodb';
31
+ import asyncFlowSharedLib from '@izara_project/izara-core-library-asynchronous-flow';
32
+ import callingFlowSharedLib from '@izara_project/izara-core-library-calling-flow';
33
+ import snsSharedLib from '@izara_project/izara-core-library-sns';
34
+ import graphSharedLibs from '@izara_project/izara-core-library-graph-service';
35
+ import lambdaSharedLib from '@izara_project/izara-core-library-lambda';
36
+ import consts from './Consts.js';
37
+ const { TOPIC_NAME_GENERATE_CODE, TOPIC_NAME_GRAPH_HANDLER, PREFIX } = consts;
1182
38
 
1183
- for (let [fieldName, fieldSetting] of Object.entries(
1184
- objectSchema.fieldNames
1185
- )) {
1186
- if (
1187
- fieldSetting.canUpdate == true ||
1188
- !fieldSetting.hasOwnProperty('canUpdate')
1189
- ) {
1190
- if (dynamoDataDetail.fieldNames.includes(fieldName)) {
1191
- if (fields[fieldName]) {
1192
- if (!fieldsForUpdateDynamo.hasOwnProperty(fields[fieldName])) {
1193
- _izContext.logger.debug('assign new fieldName', fields[fieldName]);
1194
- Object.assign(fieldsForUpdateDynamo, {
1195
- [fieldName]: fields[fieldName]
1196
- });
1197
- }
1198
- }
1199
- }
1200
- }
1201
- }
1202
- return fieldsForUpdateDynamo;
1203
- }
39
+ const nodeLabelRegexPattern = '^[a-zA-Z0-9_-]+(?:\:[a-zA-Z0-9_-]+)?$';
1204
40
 
1205
41
  /**
1206
42
  *
@@ -1819,765 +655,8 @@ async function sharedCreateCompleteEndpoint(
1819
655
  }
1820
656
 
1821
657
  export default {
1822
- sendMsgOutComplete,
1823
- messageToDlq,
1824
-
1825
- validateSchemaMiddleware,
1826
- validateSchemaPerRecord,
1827
- validatorSchemaMiddlewareByAction,
1828
-
1829
- createGetDataDetails,
1830
- collectGetData,
1831
-
1832
- generateDynamoDbIdentifiers,
1833
- generateDynamoDbIdentifiersWithCache,
1834
- dynamoDbIdentifiersByStorageResource,
1835
- createUpdateDataDetail,
1836
- createDataDetailsLib,
1837
- createDeleteDataDetail,
1838
-
1839
- createFieldForUpdateDynamoDb,
1840
658
  createBasicRelationshipsProperties,
1841
659
  createFieldNamesFromRequestParamsByObjectSchema,
1842
660
  sharedCreateEndpoint,
1843
- sharedCreateCompleteEndpoint
661
+ sharedCreateCompleteEndpoint,
1844
662
  };
1845
-
1846
- // /**
1847
- // * filter request param depend on generatedValidatorSchema function
1848
- // * @param {object} _izContext
1849
- // * @param {object} requestParams - requestParams that need to filter
1850
- // * @param {string} action - use to match generateValidatorSchemaFunction
1851
- // * @param {string} objectType - objectType for search objectSchema
1852
- // * @param {string[]} specificFieldNames - setting of generateValidatorSchemaFunction
1853
- // */
1854
- // async function filterRequestParam(_izContext, requestParams, action, objectType, specificFieldNames) {
1855
-
1856
- // if (!validatorSchema.schemaFunctionPerAction[action]) {
1857
- // throw new NoRetryError(`Not found function for generateValidatorSchema of action:${action}`);
1858
- // }
1859
-
1860
- // // prepare variable
1861
- // const generateValidatorFunction = validatorSchema.schemaFunctionPerAction[action];
1862
-
1863
- // const generatedValidatorSchema = await generateValidatorFunction(_izContext, objectType, specificFieldNames);
1864
-
1865
- // // prepare new requestParams depend on properties of generatedValidatorSchema
1866
- // let newRequestParams = {}
1867
- // for (let validatorFieldName in generatedValidatorSchema.properties) {
1868
- // if (requestParams.hasOwnProperty(validatorFieldName)) {
1869
- // Object.assign(newRequestParams, { [validatorFieldName]: requestParams[validatorFieldName] })
1870
- // }
1871
- // }
1872
-
1873
- // // add additionalRequest to requestParams
1874
- // if (requestParams.hasOwnProperty("additionalRequest") && Object.keys(requestParams.additionalRequest).length) {
1875
- // Object.assign(newRequestParams, { additionalRequest: requestParams.additionalRequest });
1876
- // }
1877
-
1878
- // return newRequestParams
1879
- // }
1880
-
1881
- // /**
1882
- // * reformat request for main function of each action
1883
- // * @param {Object} _izContext
1884
- // * @param {String} objectType
1885
- // * @param {String} action
1886
- // * @param {Object} requestParams
1887
- // * @returns
1888
- // */
1889
- // async function reformattedRequestParams(_izContext, objectType, action, requestParams) {
1890
- // _izContext.logger.debug("------ reformattedRequestParams ------");
1891
- // _izContext.logger.debug("objectType: ", objectType);
1892
- // _izContext.logger.debug("action: ", action);
1893
- // _izContext.logger.debug("requestParams: ", requestParams);
1894
-
1895
- // const objectSchema = await getObjectSchema.getLocalObjectSchemasWithHierarchy(_izContext, objectType).then(data => data.records[0]);
1896
- // _izContext.logger.debug("objectSchema in reformatted: ", objectSchema);
1897
-
1898
- // const identifierFieldNames = utils.getUsedFieldNamesOfIdentifiers(_izContext, objectSchema.identifiers);
1899
- // _izContext.logger.debug("identifierFieldNames: ", identifierFieldNames);
1900
-
1901
- // let versionedDatas = [];
1902
- // if (objectSchema?.addOnDataStructure?.length) {
1903
- // for (let eachAddOn of objectSchema.addOnDataStructure) {
1904
- // if (eachAddOn.type === "versionedData") {
1905
- // versionedDatas.push(eachAddOn);
1906
- // }
1907
- // }
1908
- // }
1909
-
1910
- // let reformattedReq
1911
-
1912
- // // reformat request depend on action
1913
- // switch (action) {
1914
- // case coreConsts.ACTIONS.create:
1915
- // _izContext.logger.debug("reformatted case create");
1916
-
1917
- // // initial format
1918
- // reformattedReq = {
1919
- // identifiers: {},
1920
- // createFieldNames: {},
1921
- // relationships: []
1922
- // };
1923
-
1924
- // let createFieldNames = Object.entries(objectSchema.fieldNames).reduce((returnFieldNames, [fieldName, fieldNameData]) => {
1925
- // if (identifierFieldNames.includes(fieldName)) {
1926
- // return returnFieldNames;
1927
- // }
1928
-
1929
- // if (fieldNameData.requiredOnCreate === true || fieldNameData.optionalOnCreate === true) {
1930
- // returnFieldNames.push(fieldName);
1931
- // }
1932
- // return returnFieldNames;
1933
- // }, [])
1934
-
1935
- // _izContext.logger.debug("createFieldNames: ", createFieldNames);
1936
-
1937
- // for (const [reqFieldName, reqFieldData] of Object.entries(requestParams.fieldNames)) {
1938
- // if (identifierFieldNames.includes(reqFieldName)) {
1939
- // Object.assign(
1940
- // reformattedReq.identifiers,
1941
- // { [reqFieldName]: reqFieldData }
1942
- // );
1943
- // } else {
1944
- // if (createFieldNames.includes(reqFieldName)) {
1945
- // Object.assign(
1946
- // reformattedReq.createFieldNames,
1947
- // { [reqFieldName]: reqFieldData }
1948
- // );
1949
- // }
1950
- // }
1951
- // }
1952
-
1953
- // if (requestParams?.relationships?.length) {
1954
- // reformattedReq.relationships = reformattedReq.relationships.concat(requestParams.relationships)
1955
- // }
1956
-
1957
- // break;
1958
- // case coreConsts.ACTIONS.update:
1959
- // // code block
1960
- // break;
1961
- // case coreConsts.ACTIONS.get:
1962
- // // code block
1963
- // break;
1964
- // case coreConsts.ACTIONS.delete:
1965
- // // code block
1966
- // break;
1967
- // default:
1968
- // throw new NoRetryError(`reformattedRequestParams:unhandled action:${action}`)
1969
- // }
1970
-
1971
- // return reformattedReq;
1972
- // }
1973
-
1974
- // const reformattedRequestParamsWithCache = inMemoryCacheLib.inMemoryCacheLib(
1975
- // reformattedRequestParams, // fn
1976
- // {
1977
- // max: 100, maxAge: 86400000, promise: true, profileName: 'reformattedRequestParams',
1978
- // normalizer: function (args) {
1979
- // return hash([args[1], args[2], args[3]]);
1980
- // }
1981
- // }
1982
- // )
1983
-
1984
- // /**
1985
- // * filter identifiers from requestParams depend on objectSchema.identifiers
1986
- // * @param {object} _izContext
1987
- // * @param {object} objectSchema - objectSchema of objectType
1988
- // * @param {object} requestParams - requestParams from handler
1989
- // * @returns
1990
- // */
1991
- // function saperateIdentifierRequestParams(_izContext, objectSchema, requestParams) {
1992
-
1993
- // let usedIdentifierTypes = utils.selectIdentifierTypeByPriority(_izContext, [], objectSchema);
1994
-
1995
- // let identifierFieldNameList = utils.getUsedFieldNamesOfIdentifiers(_izContext, objectSchema.identifiers, usedIdentifierTypes);
1996
-
1997
- // let identifiersObj = {};
1998
-
1999
- // for (let identifierFieldName of identifierFieldNameList) {
2000
- // if (requestParams.hasOwnProperty(identifierFieldName)) {
2001
- // Object.assign(identifiersObj, { [identifierFieldName]: requestParams[identifierFieldName] });
2002
- // } else {
2003
- // throw new NoRetryError(`missing ${identifierFieldName} in requestParams`);
2004
- // }
2005
- // }
2006
-
2007
- // return identifiersObj;
2008
- // }
2009
-
2010
- // /*
2011
- // Q?
2012
- // - when perform action in dynamodb some table need to concatenate
2013
- // - need to generate new identifier of dynamodb depend on storageResource
2014
-
2015
- // returnValue = {
2016
- // graph:{ // storageType
2017
- // graphHandler:{ // graph service name
2018
- // nodeLabel:"string" // generate node label here
2019
- // identifier: {
2020
- // prop1: value1,
2021
- // prop2: value2,
2022
- // },
2023
- // properties:{
2024
- // prop1: value1,
2025
- // prop2: value2,
2026
- // },
2027
- // versionedDataLabel:[
2028
- // // ...
2029
- // ],
2030
- // versionedDatas:[ // for [create | update] action
2031
- // {
2032
- // nodeLabel:"xxx",
2033
- // nodeProperties:{
2034
- // // ...versionedData node properties
2035
- // }
2036
- // hookProperties:["xxx", yyy]
2037
- // }
2038
- // ]
2039
- // nodePropertyLabel:{ // addOnDataStructure: type = nodePropertyLabel | attributeTree | , not implement yet
2040
- // // ...setting
2041
- // }
2042
- // },
2043
- // mediaGraph:{ // graph service name
2044
- // prop1:value1,
2045
- // prop2:value2,
2046
- // }
2047
- // },
2048
- // dynamoDb:{ // storageType
2049
- // table1:{ // tableName
2050
- // identifier: {
2051
- // prop1: value1,
2052
- // prop2: value2,
2053
- // },
2054
- // properties{
2055
- // prop1: value1,
2056
- // prop2: value2,
2057
- // }
2058
- // },
2059
- // table2:{ // tableName
2060
- // identifier: {
2061
- // prop1: value1,
2062
- // prop2: value2,
2063
- // },
2064
- // properties{
2065
- // prop1: value1,
2066
- // prop2: value2,
2067
- // }
2068
- // }
2069
- // }
2070
- // }
2071
- // */
2072
- // async function saperateRequestByStorageResources(_izContext, objectSchema, requestParams, action) {
2073
- // _izContext.logger.debug("Function: saperateRequestByStorageResources :", { requestParams, action })
2074
-
2075
- // // -------------- initial return value --------------
2076
-
2077
- // let returnValue = {}
2078
-
2079
- // for (let storageType of coreConsts.STORAGE_TYPE_LIST) {
2080
- // if (!returnValue.hasOwnProperty(storageType)) {
2081
- // returnValue[storageType] = {}
2082
- // }
2083
- // }
2084
-
2085
- // _izContext.logger.debug("initial returnValue: ", returnValue);
2086
-
2087
- // // -------------- initial return value --------------
2088
-
2089
- // // create identifier object from requestParams
2090
- // let identifierObj = saperateIdentifierRequestParams(_izContext, objectSchema, requestParams);
2091
-
2092
- // // separate requestParams by storageType
2093
- // for (let [storageTag, storageData] of Object.entries(objectSchema.storageResources)) {
2094
-
2095
- // if (storageData.storageType === coreConsts.STORAGE_TYPES.dynamoDB) {
2096
-
2097
- // // separate by tableName
2098
- // let dynamoRequestParams = await separateRequestByDynamoDb(
2099
- // _izContext,
2100
- // objectSchema,
2101
- // requestParams,
2102
- // identifierObj,
2103
- // storageTag,
2104
- // storageData
2105
- // );
2106
- // _izContext.logger.debug("dynamoRequestParams: ", dynamoRequestParams);
2107
-
2108
- // let tableName = storageData.tableName;
2109
- // if (returnValue[coreConsts.STORAGE_TYPES.dynamoDB][tableName]) {
2110
- // throw new NoRetryError(`Have duplicate tableName in storageTag:${storageTag}`);
2111
- // }
2112
-
2113
- // Object.assign(returnValue[coreConsts.STORAGE_TYPES.dynamoDB], { [tableName]: dynamoRequestParams });
2114
-
2115
- // } else if (storageData.storageType === coreConsts.STORAGE_TYPES.graph) {
2116
-
2117
- // // separate by GraphServiceName
2118
- // let graphRequestParams = await separateRequestByGraph(
2119
- // _izContext,
2120
- // objectSchema,
2121
- // requestParams,
2122
- // identifierObj,
2123
- // storageTag,
2124
- // storageData,
2125
- // action
2126
- // );
2127
- // _izContext.logger.debug("graphRequestParams: ", graphRequestParams);
2128
-
2129
- // let graphServiceName = await serviceConfig.getGraphServiceNameFromGraphServerTagWithCache(_izContext, storageData.graphServerTag);
2130
-
2131
- // if (returnValue[coreConsts.STORAGE_TYPES.graph][graphServiceName]) {
2132
- // // have exists graphServiceName,
2133
- // // need to merge properties for multiple graphServerTag to one serviceName
2134
- // _izContext.logger.debug('exists serviceName');
2135
- // _izContext.logger.debug('graphRequestParams : ', graphRequestParams);
2136
- // _izContext.logger.debug('returnValue[coreConsts.STORAGE_TYPES.graph][graphServiceName]: ',
2137
- // returnValue[coreConsts.STORAGE_TYPES.graph][graphServiceName]
2138
- // );
2139
-
2140
- // // merge node properties
2141
- // for (let graphProperty in graphRequestParams.properties) {
2142
- // if (!returnValue[coreConsts.STORAGE_TYPES.graph][graphServiceName].properties.hasOwnProperty(graphProperty)) {
2143
- // Object.assign(
2144
- // returnValue[coreConsts.STORAGE_TYPES.graph][graphServiceName].properties,
2145
- // { [graphProperty]: graphRequestParams.properties[graphProperty] }
2146
- // );
2147
- // }
2148
- // }
2149
-
2150
- // /*
2151
- // if versionedDatas exists in graphRequestParams
2152
- // - check versionedDatas exists in returnValue[coreConsts.STORAGE_TYPES.graph][graphServiceName]
2153
- // - if exists add versionedData in graphRequestParams to returnValue
2154
- // - if not exists create and add versionedDatas
2155
- // */
2156
-
2157
- // // merge graph versionedData
2158
- // if (graphRequestParams.versionedDatas?.length) {
2159
- // if (Array.isArray(returnValue[coreConsts.STORAGE_TYPES.graph][graphServiceName].versionedDatas)) {
2160
- // returnValue[coreConsts.STORAGE_TYPES.graph][graphServiceName].versionedDatas =
2161
- // returnValue[coreConsts.STORAGE_TYPES.graph][graphServiceName].versionedDatas.concat(graphRequestParams.versionedDatas)
2162
- // } else {
2163
- // returnValue[coreConsts.STORAGE_TYPES.graph][graphServiceName].versionedDatas = graphRequestParams.versionedDatas
2164
- // }
2165
- // }
2166
-
2167
- // } else {
2168
- // // not exists graphServiceName
2169
- // Object.assign(returnValue[coreConsts.STORAGE_TYPES.graph], { [graphServiceName]: graphRequestParams });
2170
- // }
2171
-
2172
- // } else {
2173
- // throw new NoRetryError(`Don't know storageType:${storageData.type} in storageTag:${storageTag}`);
2174
- // }
2175
- // }
2176
-
2177
- // return returnValue
2178
-
2179
- // }
2180
-
2181
- // /**
2182
- // * helper function for separate requestParams depend on storageTag
2183
- // *
2184
- // * @param {Object} _izContext
2185
- // * @param {Object} objectSchema -
2186
- // * @param {Object} requestParams - requestParams from lambda
2187
- // * @param {Object} identifierObj - identifiers
2188
- // * @param {String} storageTag - storageTag of storageResource
2189
- // * @param {Object} storageData - data of storageResource
2190
- // * @returns {Object} - object of identifier and property for dynamodb
2191
- // */
2192
- // async function separateRequestByDynamoDb(_izContext, objectSchema, requestParams, identifierObj, storageTag, storageData) {
2193
- // _izContext.logger.debug('separateRequestByDynamoDb: ', {
2194
- // requestParams,
2195
- // identifierObj,
2196
- // storageTag,
2197
- // storageData
2198
- // });
2199
-
2200
- // //----------------------- Filter requestParams ------------------------
2201
-
2202
- // // filter requestParams that includes in storageTag
2203
- // let filteredRequestParams = {}
2204
- // for (let [fieldName, fieldNameData] of Object.entries(objectSchema.fieldNames)) {
2205
- // if (fieldNameData.storageResourceTags.includes(storageTag) && requestParams.hasOwnProperty(fieldName)) {
2206
- // Object.assign(filteredRequestParams, { [fieldName]: requestParams[fieldName] });
2207
- // }
2208
- // }
2209
-
2210
- // //----------------------- End Filter requestParams ------------------------
2211
-
2212
- // //----------------------- Create dynamoDb identifier object ------------------------
2213
-
2214
- // // use identifierObj to prepare dynamodb identifiers
2215
- // let usedIdentifierTypes = utils.selectIdentifierTypeByPriority(_izContext, [], objectSchema);
2216
-
2217
- // let deliminatorTree = await serviceSchemas.generateDeliminatorTree(_izContext, objectSchema, { objectType: objectSchema.objectType, serviceTag: process.env.iz_serviceTag }, usedIdentifierTypes);
2218
- // _izContext.logger.debug(`deliminatorTree objectType: ${objectSchema.objectType} : `, deliminatorTree)
2219
-
2220
- // let dynamoDbIdentifier = {};
2221
- // for (let identifier of objectSchema.identifiers) {
2222
-
2223
- // if (!usedIdentifierTypes.includes(identifier.type)) {
2224
- // continue;
2225
- // }
2226
-
2227
- // if (identifier.hasOwnProperty("fieldName")) {
2228
-
2229
- // Object.assign(dynamoDbIdentifier, { [identifier.fieldName]: identifierObj[identifier.fieldName] });
2230
-
2231
- // } else if (identifier.hasOwnProperty("name") && identifier.hasOwnProperty("fieldNames")) {
2232
-
2233
- // let deliminator = identifier.deliminator ? identifier.deliminator : "_";
2234
-
2235
- // // if deliminatorList exists overwrite deliminator
2236
- // if (deliminatorTree.identifiersToProcess?.[identifier.name]?.deliminatorList?.[deliminator]) {
2237
- // let deliminatorAmount = deliminatorTree[identifier.name].deliminatorList[deliminator];
2238
- // deliminator = deliminator.repeat(deliminatorAmount)
2239
- // }
2240
-
2241
- // let fieldNameDataList = [];
2242
- // for (let identifierFieldName of identifier.fieldNames) {
2243
- // fieldNameDataList.push(identifierObj[identifierFieldName]);
2244
- // }
2245
-
2246
- // Object.assign(dynamoDbIdentifier, { [identifier.name]: fieldNameDataList.join(deliminator) });
2247
-
2248
- // } else {
2249
- // throw new NoRetryError('separateRequestByDynamoDb: invalid identifier');
2250
- // }
2251
- // }
2252
-
2253
- // //----------------------- End Create dynamoDb identifier object ------------------------
2254
-
2255
- // //----------------------- calculate composite deliminator amount ------------------------
2256
-
2257
- // let compositeDeliminator = objectSchema.compositeKeyDeliminator ? objectSchema.compositeKeyDeliminator : "_";
2258
-
2259
- // let compositeDeliminatorAmount = 0;
2260
- // for (let deliminatorDetail of Object.values(deliminatorTree.identifiersToProcess)) {
2261
- // if (deliminatorDetail?.deliminatorList?.[compositeDeliminator]) {
2262
- // compositeDeliminatorAmount = Math.max(compositeDeliminatorAmount, deliminatorDetail.deliminatorList[compositeDeliminator]);
2263
- // }
2264
- // }
2265
-
2266
- // compositeDeliminatorAmount + 1;
2267
-
2268
- // //----------------------- End calculate composite deliminator amount ------------------------
2269
-
2270
- // //----------------------- create return value ------------------------
2271
-
2272
- // // initial returnValue
2273
- // let returnValue = {
2274
- // identifiers: {},
2275
- // properties: {}
2276
- // }
2277
-
2278
- // if (storageData.hasOwnProperty("groupByPartitionKeyField")) {
2279
- // // concatenate identifierObj
2280
- // let newIdentifier = Object.values(identifierObj).join(compositeDeliminator.repeat(compositeDeliminatorAmount));
2281
-
2282
- // Object.assign(returnValue.identifiers, { [storageData.groupByPartitionKeyField]: newIdentifier });
2283
- // Object.assign(returnValue.properties, filteredRequestParams);
2284
-
2285
- // } else {
2286
- // returnValue.identifiers = identifierObj;
2287
-
2288
- // for (let requestParamProperty in filteredRequestParams) {
2289
- // if (!identifierObj.hasOwnProperty(requestParamProperty)) {
2290
- // Object.assign(returnValue.properties, { [requestParamProperty]: filteredRequestParams[requestParamProperty] });
2291
- // }
2292
- // }
2293
-
2294
- // }
2295
-
2296
- // //----------------------- End create return value ------------------------
2297
-
2298
- // return returnValue;
2299
- // }
2300
-
2301
- // // helper function for separate requestParams fieldName per storageTag
2302
- // async function separateRequestByGraph(_izContext, objectSchema, requestParams, identifierObj, storageTag, storageData, action) {
2303
- // _izContext.logger.debug("separateRequestByGraph: ", {
2304
- // requestParams,
2305
- // identifierObj,
2306
- // storageTag,
2307
- // storageData,
2308
- // action
2309
- // });
2310
-
2311
- // //----------------------- Filter requestParams ------------------------
2312
-
2313
- // // filter requestParams that includes in storageTag
2314
- // let filteredRequestParams = {}
2315
- // for (let [fieldName, fieldNameData] of Object.entries(objectSchema.fieldNames)) {
2316
- // if (fieldNameData.storageResourceTags.includes(storageTag) && requestParams.hasOwnProperty(fieldName)) {
2317
- // Object.assign(filteredRequestParams, { [fieldName]: requestParams[fieldName] });
2318
- // }
2319
- // }
2320
-
2321
- // //----------------------- End Filter requestParams ------------------------
2322
-
2323
- // //----------------------- standard return value ------------------------
2324
-
2325
- // // initial returnValue
2326
- // let returnValue = {
2327
- // identifiers: {},
2328
- // properties: {}
2329
- // }
2330
-
2331
- // // add nodeLabel
2332
- // if (objectSchema.hasOwnProperty("extendObjType")) {
2333
- // let nodeLabel = `${objectSchema.objectType}:${objectSchema.extendObjType.objectType}`;
2334
- // Object.assign(returnValue, { nodeLabel: nodeLabel });
2335
- // } else {
2336
- // Object.assign(returnValue, { nodeLabel: objectSchema.objectType });
2337
- // }
2338
-
2339
- // // add identifiers
2340
- // returnValue.identifiers = identifierObj;
2341
-
2342
- // // add properties from requestParams if not exists in identifierObj
2343
- // for (let requestParamProperty in filteredRequestParams) {
2344
- // if (!identifierObj.hasOwnProperty(requestParamProperty)) {
2345
- // Object.assign(returnValue.properties, { [requestParamProperty]: filteredRequestParams[requestParamProperty] });
2346
- // }
2347
- // }
2348
-
2349
- // //----------------------- End standard return value ------------------------
2350
-
2351
- // //----------------------- prepare additional return data per action ------------------------
2352
-
2353
- // if (action === coreConsts.ACTIONS.get) {
2354
-
2355
- // if (
2356
- // requestParams?.additionalRequest?.versionedDataLabel
2357
- // && Array.isArray(requestParams.additionalRequest.versionedDataLabel)
2358
- // && requestParams.additionalRequest.versionedDataLabel.length
2359
- // && objectSchema.addOnDataStructure
2360
- // && Array.isArray(objectSchema.addOnDataStructure)
2361
- // ) {
2362
- // // if versionedDataLabel exists in requestParams
2363
- // // add versionedDataLabel to returnValue if exists in storageResourceTag
2364
-
2365
- // // get addOnDataStructure versionedData depend on storageTag
2366
- // let allAddOnVersionedDataPerStorageTag = objectSchema.addOnDataStructure.filter(
2367
- // addOnVersionedData => (addOnVersionedData.type === "versionedData" && addOnVersionedData.storageResourceTag === storageTag)
2368
- // );
2369
-
2370
- // for (let addOnVersionedData of allAddOnVersionedDataPerStorageTag) {
2371
- // if (requestParams.additionalRequest.versionedDataLabel.includes(addOnVersionedData.versionedDataLabel)) {
2372
-
2373
- // if (!returnValue.hasOwnProperty("versionedDataLabel")) {
2374
- // returnValue.versionedDataLabel = []
2375
- // }
2376
-
2377
- // returnValue.versionedDataLabel.push(addOnVersionedData.versionedDataLabel)
2378
- // }
2379
- // }
2380
- // }
2381
-
2382
- // } else if (action === coreConsts.ACTIONS.create) {
2383
-
2384
- // } else if (action === coreConsts.ACTIONS.update) {
2385
- // _izContext.logger.debug("filter versionedData update")
2386
- // _izContext.logger.debug("additionalReq: ", requestParams.additionalRequest.versionedDatas)
2387
- // _izContext.logger.debug("addOnDataStructure: ", objectSchema.addOnDataStructure)
2388
- // // {
2389
- // // // ...properties
2390
- // // additionalRequest: {
2391
- // // versionedDatas: [
2392
- // // {
2393
- // // nodeLabel: "nodeLabelName",
2394
- // // nodeProperties: {
2395
- // // // ...properties of versionedData
2396
- // // }
2397
- // // }
2398
- // // ]
2399
- // // // ...other additional request
2400
- // // }
2401
- // // }
2402
-
2403
- // // separate create versionedData request
2404
- // if (
2405
- // requestParams?.additionalRequest?.versionedDatas
2406
- // && Array.isArray(requestParams.additionalRequest.versionedDatas)
2407
- // && requestParams.additionalRequest.versionedDatas.length
2408
- // && objectSchema.addOnDataStructure
2409
- // && Array.isArray(objectSchema.addOnDataStructure)
2410
- // ) {
2411
-
2412
- // let versionedDataReqList = requestParams.additionalRequest.versionedDatas;
2413
-
2414
- // // get addOnDataStructure versionedData depend on storageTag from objectSchema
2415
- // let allAddOnVersionedDataPerStorageTag = objectSchema.addOnDataStructure.filter(
2416
- // addOnVersionedData => (addOnVersionedData.type === "versionedData" && addOnVersionedData.storageResourceTag === storageTag)
2417
- // );
2418
-
2419
- // _izContext.logger.debug('allAddOnVersionedDataPerStorageTag update: ', allAddOnVersionedDataPerStorageTag);
2420
-
2421
- // // check versionedData label in requestParams exists in allAddOnVersionedDataPerStorageTag
2422
- // // if exists validate and add versionedData to return label
2423
- // if (allAddOnVersionedDataPerStorageTag.length) {
2424
- // for (let versionedDataReq of versionedDataReqList) {
2425
- // // find exists versionedData Schema
2426
- // let versionedDataSchema = allAddOnVersionedDataPerStorageTag.find(
2427
- // addOnVersionedData => addOnVersionedData.versionedDataLabel === versionedDataReq.nodeLabel
2428
- // );
2429
-
2430
- // if (versionedDataSchema) {
2431
- // _izContext.logger.debug('exists versionedDataSchema: ', versionedDataSchema);
2432
-
2433
- // // validate versionedData
2434
-
2435
- // let versionedDataValidatorSchema = generateValidatorSchemaForVersionedData(_izContext, versionedDataSchema, action);
2436
- // _izContext.logger.debug("versionedData validator schema: ", versionedDataValidatorSchema);
2437
-
2438
- // _izContext.logger.debug("versionedDataReq before validate: ", versionedDataReq)
2439
- // let validateVersionedDataStatus = validateObject(_izContext, versionedDataValidatorSchema, versionedDataReq);
2440
- // _izContext.logger.debug("versionedData validate status: ", validateVersionedDataStatus);
2441
-
2442
- // if (validateVersionedDataStatus.pass === false) {
2443
- // throw new NoRetryError("Function:separateRequestByGraph: ", validateVersionedDataStatus.error);
2444
- // }
2445
-
2446
- // if (!returnValue.hasOwnProperty("versionedDatas")) {
2447
- // returnValue.versionedDatas = [];
2448
- // }
2449
- // // end validate versionedData
2450
-
2451
- // let hookProperties = [];
2452
- // for (let fieldNameSetting of versionedDataSchema.fieldNames) {
2453
- // hookProperties.push(fieldNameSetting.fieldName);
2454
- // }
2455
-
2456
- // Object.assign(versionedDataReq, { hookProperties });
2457
-
2458
- // returnValue.versionedDatas.push(versionedDataReq);
2459
- // }
2460
- // }
2461
- // }
2462
-
2463
- // }
2464
-
2465
- // } else if (action === coreConsts.ACTIONS.delete) {
2466
-
2467
- // } else {
2468
- // throw new NoRetryError("Function:separateRequestByGraph: invalid action")
2469
- // }
2470
-
2471
- // //----------------------- End prepare additional return data per action ------------------------
2472
-
2473
- // _izContext.logger.debug("Function:separateRequestByGraph: return value: ", returnValue);
2474
- // return returnValue;
2475
- // }
2476
-
2477
- // function mergeReturnGetData(_izContext, dynamoDbData, graphData) {
2478
- // let mergedData = {
2479
- // properties: {}
2480
- // };
2481
-
2482
- // for (let dynamoDbDataDetail of dynamoDbData) {
2483
- // for (let [dynamoDataProp, dynamoDataValue] of Object.entries(dynamoDbDataDetail)) {
2484
- // if (!mergedData.properties.hasOwnProperty(dynamoDataProp)) {
2485
- // Object.assign(mergedData.properties, { [dynamoDataProp]: dynamoDataValue })
2486
- // }
2487
- // }
2488
-
2489
- // }
2490
-
2491
- // for (let graphDataDetail of graphData) {
2492
- // // add nodeProperties to mergedData
2493
- // for (let [graphDataProp, graphDataValue] of Object.entries(graphDataDetail.nodeProperties)) {
2494
- // if (!mergedData.properties.hasOwnProperty(graphDataProp)) {
2495
- // Object.assign(mergedData.properties, { [graphDataProp]: graphDataValue })
2496
- // }
2497
- // }
2498
-
2499
- // // add versionedData to mergedData
2500
- // for (let [graphDataProp, graphDataValue] of Object.entries(graphDataDetail)) {
2501
- // console.log({ graphDataProp, graphDataValue })
2502
- // if (graphDataProp === "nodeLabels" || graphDataProp === "nodeProperties") {
2503
- // continue;
2504
- // }
2505
-
2506
- // if (Object.keys(graphDataValue.nodeProperties).length) {
2507
-
2508
- // if (!mergedData.versionedDataLabel) {
2509
- // mergedData.versionedDataLabel = {}
2510
- // }
2511
-
2512
- // Object.assign(mergedData.versionedDataLabel, { [graphDataValue.nodeLabels.join(":")]: graphDataValue.nodeProperties })
2513
- // }
2514
- // }
2515
- // }
2516
-
2517
- // return mergedData
2518
- // }
2519
-
2520
- // // this function not use for new version because versionedData will not saperate from request params
2521
- // function generateValidatorSchemaForVersionedData(_izContext, versionedObjDataSchema, action) {
2522
- // // input form of versionedData will be the same for create and update action
2523
-
2524
- // // initial versionedData validatorSchema
2525
- // let versionedDataValidatorSchema = {
2526
- // type: "object",
2527
- // required: ['nodeLabel', 'nodeProperties'],
2528
- // properties: {
2529
- // nodeLabel: {
2530
- // type: 'string',
2531
- // pattern: nodeLabelRegexPattern
2532
- // },
2533
- // nodeProperties: {
2534
- // type: 'object',
2535
- // required: [], // require fieldName
2536
- // additionalProperties: false,
2537
- // properties: {}
2538
- // }
2539
- // }
2540
- // }
2541
-
2542
- // // maybe not need to validate action get and delete because
2543
- // if (action === coreConsts.ACTIONS.create) {
2544
- // for (let versionedFieldName of versionedObjDataSchema.fieldNames) {
2545
- // if (versionedFieldName.requireOnCreate === true || versionedFieldName.optionalOnCreate === true) {
2546
- // // add require fieldName to require array in validatorSchema
2547
- // if (versionedFieldName.requireOnCreate === true) {
2548
- // versionedDataValidatorSchema.properties.nodeProperties.required.push(versionedFieldName.fieldName)
2549
- // }
2550
-
2551
- // // add validation to properties
2552
- // let versionedDataProperties = versionedDataValidatorSchema.properties.nodeProperties.properties;
2553
- // Object.assign(versionedDataProperties, {
2554
- // [versionedFieldName.fieldName]: {
2555
- // type: versionedFieldName.type,
2556
- // ...versionedFieldName.validation
2557
- // }
2558
- // })
2559
-
2560
- // }
2561
- // }
2562
- // } else if (action === coreConsts.ACTIONS.update) {
2563
- // for (let versionedFieldName of versionedObjDataSchema.fieldNames) {
2564
- // if (versionedFieldName.canUpdate === true || versionedFieldName.canUpdate === undefined) {
2565
-
2566
- // // add validation to properties
2567
- // let versionedDataProperties = versionedDataValidatorSchema.properties.nodeProperties.properties;
2568
- // Object.assign(versionedDataProperties, {
2569
- // [versionedFieldName.fieldName]: {
2570
- // type: versionedFieldName.type,
2571
- // ...versionedFieldName.validation
2572
- // }
2573
- // })
2574
-
2575
- // }
2576
- // }
2577
- // } else {
2578
- // throw new NoRetryError("Function:generateValidatorSchemaForVersionedData: invalid action")
2579
- // }
2580
-
2581
- // // return validator schema of versionedData
2582
- // return versionedDataValidatorSchema;
2583
- // }