@izara_project/izara-market-library-service-schemas 1.0.11 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@izara_project/izara-market-library-service-schemas",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "description": "Schemas for Izara Market project",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@izara_project/izara-core-library-core": "^1.0.13",
24
- "@izara_project/izara-core-library-logger": "^1.0.5",
24
+ "@izara_project/izara-core-library-external-request": "^1.0.14",
25
25
  "@izara_project/izara-core-library-service-schemas": "^1.0.25",
26
26
  "@izara_project/izara-shared": "^1.0.96",
27
27
  "ejs": "^3.1.10",
@@ -47,6 +47,10 @@ const {
47
47
 
48
48
  const nodeLabelRegexPattern = "^[a-zA-Z0-9_-]+(?:\:[a-zA-Z0-9_-]+)?$"
49
49
 
50
+ const RETURN_GET_DATA_FORMAT = {
51
+ std: "STD", // { identifiers:{...}, fields:{...}} , data depend on getDataDetails
52
+ full: "FULL", // {storageTag:{...getResult from database}}
53
+ }
50
54
 
51
55
  const schemaFunctionPerAction = {
52
56
  [consts.ACTIONS.create]: validatorSchema.generateValidatorSchemaForCreate,
@@ -215,6 +219,20 @@ async function messageToDlq(record, messageFailError, queueUrl) {
215
219
  };
216
220
 
217
221
 
222
+ // {
223
+ // [storageTag]: {
224
+ // storageType: 'graph',
225
+ // graphServiceTag: 'GraphHandler',
226
+ // fieldNames: [ 'companyName', 'companyId' ],
227
+ // versionedDatas: {
228
+ // ceo: [ 'name', 'ceoAddress', 'salary' ],
229
+ // branch: [ 'branchAddress', 'branchId' ]
230
+ // }
231
+ // versionedDataLabels:[],
232
+ // }
233
+ // }
234
+
235
+
218
236
  /**
219
237
  * helper function for get endpoint
220
238
  * create get data detail for each storageTag
@@ -257,6 +275,10 @@ async function createGetDataDetails(_izContext, objectSchema) {
257
275
  // collect fieldNames per storageTag
258
276
  let storageTagFieldNames = [];
259
277
  for (let [fieldName, fieldNameData] of Object.entries(objectSchema.fieldNames)) {
278
+ // if (fieldName.startsWith("versionedDataField")) {
279
+ // continue;
280
+ // }
281
+
260
282
  if (fieldNameData.storageResourceTags.includes(storageTag)) {
261
283
  storageTagFieldNames.push(fieldName);
262
284
  }
@@ -391,11 +413,20 @@ async function createGetDataDetails(_izContext, objectSchema) {
391
413
 
392
414
  _izContext.logger.debug("getDataDetailResults: ", getDataDetailResults);
393
415
 
416
+ for (const getDataDetail of Object.values(getDataDetailResults)) {
417
+ if (getDataDetail.hasOwnProperty("versionedDatas")) {
418
+ getDataDetail.versionedDataLabels = [];
419
+ for (let [versionedDataLabel, fieldNames] of Object.entries(getDataDetail.versionedDatas)) {
420
+ getDataDetail.versionedDataLabels.push(versionedDataLabel)
421
+ getDataDetail.fieldNames.push(...fieldNames);
422
+ }
423
+ }
424
+ }
425
+
394
426
  return getDataDetailResults;
395
427
  }
396
428
 
397
429
 
398
-
399
430
  /**
400
431
  * create dynamoDb identifiers using request param and objectSchema
401
432
  *
@@ -518,144 +549,165 @@ async function dynamoDbIdentifiersByStorageResource(_izContext, objectSchema, ge
518
549
  }
519
550
 
520
551
 
521
-
522
- function collectGetData(_izContext, getDataResult, getDataDetail) {
523
-
524
- if (!getDataResult) {
525
- return {};
552
+ /**
553
+ * receive result from multiple database and create data format depend on returnFormat setting
554
+ *
555
+ * @param {object} _izContext
556
+ * @param {object[]} getResults
557
+ * @param {object} objectSchema
558
+ * @param {string} returnFormat
559
+ * @returns
560
+ */
561
+ function collectGetData(_izContext, getResults, objectSchema, returnFormat = RETURN_GET_DATA_FORMAT.std) {
562
+ _izContext.logger.debug("collectGetData: ", {
563
+ getResults,
564
+ objectSchema,
565
+ returnFormat
566
+ });
567
+
568
+ if (returnFormat === "STD") {
569
+ return collectGetDataStdFormat(_izContext, getResults, objectSchema);
570
+ } else if (returnFormat === "FULL") {
571
+ return collectGetDataFullFormat(_izContext, getResults,)
572
+ } else {
573
+ throw new NoRetryError("invalid format");
526
574
  }
575
+ }
527
576
 
528
- let collectedData = {};
529
-
530
- // if (getDataDetail.storageType === consts.STORAGE_TYPES.graph) {
531
- // // collect return data form main node only form properties
532
- // if (getDataResult.properties) {
533
- // for (let [mainFieldName, mainFieldNameValue] of Object.entries(getDataResult.properties)) {
534
- // if (getDataDetail.fieldNames.includes(mainFieldName)) {
535
- // Object.assign(collectedData, { [mainFieldName]: mainFieldNameValue });
536
- // }
537
- // }
538
- // }
539
- if (getDataDetail.storageType === consts.STORAGE_TYPES.graph) {
540
- if (getDataResult.objInstance) {
541
- for (let [identifiersField, identifiersValues] of Object.entries(getDataResult.objInstance)) {
542
- for (let [mainFieldName, mainFieldNameValue] of Object.entries(identifiersValues)) {
543
- Object.assign(collectedData, {
544
- [getDataDetail.graphServiceTag]: {
545
- objType: {
546
-
547
- },
548
- objInstance: {
549
- [mainFieldName]: mainFieldNameValue
550
- }
551
- }
552
- })
553
- }
554
- }
555
- }
556
577
 
578
+ /**
579
+ * helper function for collectGetData
580
+ *
581
+ * @param {object} _izContext
582
+ * @param {object[]} getResults
583
+ * @param {set} identifierFieldNames
584
+ * @returns
585
+ */
586
+ function collectGetDataStdFormat(_izContext, getResults, objectSchema) {
557
587
 
558
- // collect data from versionedData
559
- if (getDataDetail.hasOwnProperty("versionedDatas") && Object.keys(getDataDetail.versionedDatas).length) {
560
- _izContext.logger.debug("getDataDetail has versionedData")
588
+ let collectedData = {
589
+ identifiers: {},
590
+ fields: {}
591
+ };
561
592
 
562
- let versionedDataLabels = Object.keys(getDataDetail.versionedDatas);
593
+ const identifierFieldNames = new Set(utils.getUsedFieldNamesOfIdentifiers(_izContext, objectSchema.identifiers));
563
594
 
564
- for (let versionedDataLabel of versionedDataLabels) {
595
+ let addedFieldNames = new Set();
565
596
 
566
- _izContext.logger.debug(`per versionedData:${versionedDataLabel}`);
597
+ // if isFound === true then return null from this function
598
+ let isFound = false;
567
599
 
568
- if (getDataResult.versionedDatas[versionedDataLabel]?.properties
569
- && Object.keys(getDataResult.versionedDatas[versionedDataLabel].properties).length
570
- ) {
600
+ for (const [getData, getDataDetail] of getResults) {
601
+ if (!getData || !Object.keys(getData).length) {
602
+ continue;
603
+ }
571
604
 
572
- _izContext.logger.debug(`has versionedData result:${versionedDataLabel}`);
605
+ // this process will add result from graph and dynamo to collectedData object by add identifiers and fieldName from getData
606
+ if (getDataDetail.storageType === consts.STORAGE_TYPES.graph) {
607
+ if (getData.objInstanceFull && Object.keys(getData.objInstanceFull).length) {
573
608
 
574
- // let neededVersionedDataFieldNames = Object.keys(getDataDetail.versionedDatas[versionedDataLabel]);
609
+ for (const getDetailFieldName of getDataDetail.fieldNames) {
610
+ if (addedFieldNames.has(getDetailFieldName)) {
611
+ continue;
612
+ }
575
613
 
576
- for (let neededVersionedDataFieldName of getDataDetail.versionedDatas[versionedDataLabel]) {
577
- _izContext.logger.debug(`per needed versionedData fieldName:${versionedDataLabel}`);
578
- if (getDataResult.versionedDatas[versionedDataLabel].properties.hasOwnProperty(neededVersionedDataFieldName)) {
579
- Object.assign(collectedData, { [neededVersionedDataFieldName]: getDataResult.versionedDatas[versionedDataLabel].properties[neededVersionedDataFieldName] });
580
- }
614
+ if (getData.objInstanceFull.identifiers.hasOwnProperty(getDetailFieldName)) {
615
+ Object.assign(
616
+ collectedData.identifiers,
617
+ { [getDetailFieldName]: getData.objInstanceFull.identifiers[getDetailFieldName] }
618
+ );
619
+ addedFieldNames.add(getDetailFieldName);
620
+ isFound = true;
621
+ } else if (getData.objInstanceFull.fields.hasOwnProperty(getDetailFieldName)) {
622
+ Object.assign(
623
+ collectedData.fields,
624
+ { [getDetailFieldName]: getData.objInstanceFull.fields[getDetailFieldName] }
625
+ );
626
+ addedFieldNames.add(getDetailFieldName);
627
+ isFound = true;
581
628
  }
582
629
  }
583
630
  }
584
- }
585
-
586
- } else if (getDataDetail.storageType === consts.STORAGE_TYPES.dynamoDB) {
587
-
588
- // for (let [dynamoDbKey, dynamoDbValue] of Object.entries(getDataResult)) {
589
- // if (getDataDetail.fieldNames.includes(dynamoDbKey)) {
590
- // Object.assign(collectedData, { [dynamoDbKey]: dynamoDbValue });
591
- // }
592
- // }
631
+ } else if (getDataDetail.storageType === consts.STORAGE_TYPES.dynamoDB) {
632
+ for (const getDetailFieldName of getDataDetail.fieldNames) {
633
+ if (addedFieldNames.has(getDetailFieldName)) {
634
+ continue;
635
+ }
593
636
 
594
- for (let [dynamoDbKey, dynamoDbValue] of Object.entries(getDataResult)) {
595
- // _izContext.logger.debug("dynamoDbKey::", dynamoDbKey);
596
- // _izContext.logger.debug("dynamoDbValue::", dynamoDbValue);
597
- if (getDataDetail.fieldNames.includes(dynamoDbKey)) {
598
- Object.assign(collectedData, { [getDataDetail.tableName]: { [dynamoDbKey]: dynamoDbValue } });
637
+ if (getData.hasOwnProperty(getDetailFieldName)) {
638
+ if (identifierFieldNames.has(getDetailFieldName)) {
639
+ Object.assign(
640
+ collectedData.identifiers,
641
+ { [getDetailFieldName]: getData[getDetailFieldName] }
642
+ );
643
+ addedFieldNames.add(getDetailFieldName);
644
+ isFound = true;
645
+ } else {
646
+ Object.assign(
647
+ collectedData.fields,
648
+ { [getDetailFieldName]: getData[getDetailFieldName] }
649
+ );
650
+ addedFieldNames.add(getDetailFieldName);
651
+ isFound = true;
652
+ }
653
+ }
599
654
  }
655
+ } else {
656
+ throw new NoRetryError("collectGetData unhandled storageType");
600
657
  }
658
+ }
601
659
 
660
+ if (isFound) {
661
+ return collectedData;
602
662
  } else {
603
- throw new NoRetryError(`Unhandled storageType:${getDataDetail.storageType}`);
663
+ return null;
604
664
  }
605
-
606
- return collectedData
607
665
  }
608
666
 
609
- // old
610
- // function collectGetData(_izContext, getDataResult, getDataDetail) {
611
-
612
- // if (!getDataResult) {
613
- // return {};
614
- // }
615
-
616
- // let collectedData = {};
617
-
618
- // if (getDataDetail.storageType === consts.STORAGE_TYPES.graph) {
619
- // // collect return data form main node
620
- // if (getDataResult.nodeProperties) {
621
- // for (let [mainFieldName, mainFieldNameValue] of Object.entries(getDataResult.nodeProperties)) {
622
- // if (getDataDetail.fieldNames.includes(mainFieldName)) {
623
- // Object.assign(collectedData, { [mainFieldName]: mainFieldNameValue });
624
- // }
625
- // }
626
- // }
667
+ /**
668
+ *
669
+ * helper function for collectGetData
670
+ *
671
+ * @param {object} _izContext
672
+ * @param {objects[]} getResults
673
+ * @returns
674
+ */
675
+ function collectGetDataFullFormat(_izContext, getResults) {
676
+ let collectedData = {};
627
677
 
628
- // // collect data from versionedData
629
- // if (getDataDetail.hasOwnProperty("versionedDatas") && Object.keys(getDataDetail.versionedDatas).length) {
630
- // let versionedDataLabels = Object.keys(getDataDetail.versionedDatas);
678
+ // if isFound === true then return null from this function
679
+ let isFound = false;
631
680
 
632
- // for (let versionedDataLabel of versionedDataLabels) {
633
- // if (getDataResult[versionedDataLabel]?.nodeProperties && Object.keys(getDataResult[versionedDataLabel].nodeProperties).length) {
634
- // let neededVersionedDataFieldNames = getDataDetail.versionedDatas[versionedDataLabel];
635
681
 
636
- // for (let neededVersionedDataFieldName of neededVersionedDataFieldNames) {
637
- // if (getDataResult[versionedDataLabel].nodeProperties[neededVersionedDataFieldName]) {
638
- // Object.assign(collectedData, { [neededVersionedDataFieldName]: getDataResult[versionedDataLabel].nodeProperties[neededVersionedDataFieldName] });
639
- // }
640
- // }
641
- // }
642
- // }
643
- // }
682
+ for (const [getData, getDataDetail] of getResults) {
683
+ if (!getData || !Object.keys(getData).length) {
684
+ continue;
685
+ }
644
686
 
645
- // } else if (getDataDetail.storageType === consts.STORAGE_TYPES.dynamoDB) {
687
+ // this process will add result from graph and dynamo to collectedData object by add identifiers and fieldName from getData
688
+ if (getDataDetail.storageType === consts.STORAGE_TYPES.graph) {
689
+ if (getData.objInstanceFull?.identifiers && Object.keys(getData.objInstanceFull?.identifiers).length) {
690
+ Object.assign(collectedData, { [getDataDetail.graphServiceTag]: getData });
691
+ isFound = true;
692
+ }
693
+ } else if (getDataDetail.storageType === consts.STORAGE_TYPES.dynamoDB) {
646
694
 
647
- // for (let [dynamoDbKey, dynamoDbValue] of Object.entries(getDataResult)) {
648
- // if (getDataDetail.fieldNames.includes(dynamoDbKey)) {
649
- // Object.assign(collectedData, { [dynamoDbKey]: dynamoDbValue });
650
- // }
651
- // }
695
+ if (getData && Object.keys(getData).length) {
696
+ Object.assign(collectedData, { [getDataDetail.tableName]: getData });
697
+ isFound = true;
698
+ }
699
+ } else {
700
+ throw new NoRetryError("collectGetData unhandled storageType");
701
+ }
702
+ }
652
703
 
653
- // } else {
654
- // throw new NoRetryError(`Unhandled storageType:${getDataDetail.storageType}`);
655
- // }
704
+ if (isFound) {
705
+ return collectedData;
706
+ } else {
707
+ return null;
708
+ }
709
+ }
656
710
 
657
- // return collectedData
658
- // }
659
711
 
660
712
  async function createUpdateDataDetail(_izContext, objectSchema) {
661
713
  let getGraphDataDetails = {};
@@ -899,10 +951,11 @@ module.exports = {
899
951
  validateSchemaPerRecord,
900
952
 
901
953
  createGetDataDetails,
954
+ collectGetData,
955
+
902
956
  generateDynamoDbIdentifiers,
903
957
  generateDynamoDbIdentifiersWithCache,
904
958
  dynamoDbIdentifiersByStorageResource,
905
- collectGetData,
906
959
  createUpdateDataDetail,
907
960
  createDataDetailsLib,
908
961
  createDeleteDataDetail
@@ -217,15 +217,15 @@ async function generateCodeWithTemplate(_izContext, objSchemaPath) {
217
217
 
218
218
  // define path for store yml file
219
219
  const GENERATECODE_SOURCE_PATH = {
220
- appYaml: join(saveFilePath, `../sls_yaml/generatedCode/`),
221
- resourceYaml: join(saveFilePath, `../../resource/sls_yaml/generatedCode/`),
222
- lambdaPerAction: join(saveFilePath, '../src/generatedCode/ObjectTypePerActionEndpoint/'),
220
+ appYaml: join(saveFilePath, `../sls_yaml/generatedCode/source/`),
221
+ resourceYaml: join(saveFilePath, `../../resource/sls_yaml/generatedCode/source/`),
222
+ lambdaPerAction: join(saveFilePath, '../src/generatedCode/ObjectTypePerActionEndpoint/source/'),
223
223
  externalService: join(saveFilePath, '../initial_setup/generatedCode/source/'),
224
224
  resourceLocation: join(saveFilePath, 'src/generatedCode/perAction/source/'),
225
225
  lamdaPerCombindActionPath: join(saveFilePath, `/generatedCode/lamdaPerCombindAction/source/`), // test
226
- lambdaPerObjectTypePath: join(saveFilePath, `/generatedCode/lambdaPerObjectType/`), // test
227
- updateRelationship: join(saveFilePath, `../src/generatedCode/RelationshipPerAction/`),
228
- translateId: join(saveFilePath, `../src/generatedCode/TranslateId/`)
226
+ lambdaPerObjectTypePath: join(saveFilePath, `/generatedCode/lambdaPerObjectType/source/`), // test
227
+ updateRelationship: join(saveFilePath, `../src/generatedCode/RelationshipPerAction/source/`),
228
+ translateId: join(saveFilePath, `../src/generatedCode/TranslateId/source/`)
229
229
  };
230
230
 
231
231
 
@@ -28,7 +28,7 @@ const templatePath = path.join(__dirname, "./template.ejs")
28
28
 
29
29
  /**
30
30
  * receive objectSchema
31
- * create data for hdrInv template
31
+ * create data for hdrApi template
32
32
  *
33
33
  * @param {Object} objectSchema
34
34
  * @return {{ templatePath, templateData, setting } }
@@ -38,10 +38,10 @@ function data(_izContext, objectSchema, srcPath) {
38
38
 
39
39
  for (const action of Object.values(ACTIONS)) {
40
40
  if (objectSchema.overWriteHandlers?.[action]) {
41
- // if have overWriteHander of action check hdrInv inside
41
+ // if have overWriteHander of action check hdrApi inside
42
42
  if (objectSchema.overWriteHandlers[action].includes(HANDLER.hdrApi)) {
43
43
  // create template data
44
- const createSourceParam = createParamForCreateSource(objectSchema, action, HANDLER.hdrInv, srcPath)
44
+ const createSourceParam = createParamForCreateSource(objectSchema, action, HANDLER.hdrApi, srcPath)
45
45
  createSourceArray.push(createSourceParam)
46
46
  }
47
47
  } else {
@@ -49,7 +49,7 @@ function data(_izContext, objectSchema, srcPath) {
49
49
  const defaultHandler = DEFAULT_HANDLER_PER_ACTION[action]
50
50
  if (defaultHandler.includes(HANDLER.hdrApi)) {
51
51
  // create template data and object for create source
52
- const createSourceParam = createParamForCreateSource(objectSchema, action, HANDLER.hdrInv, srcPath)
52
+ const createSourceParam = createParamForCreateSource(objectSchema, action, HANDLER.hdrApi, srcPath)
53
53
  createSourceArray.push(createSourceParam)
54
54
  }
55
55
  }
@@ -95,7 +95,7 @@ module.exports.<%- functionName %>Main = async (
95
95
 
96
96
  let errorsFound = [];
97
97
 
98
- _izContext.correlationIds.set(coreConsts.BASE_USER_ID, "BasDev");
98
+ // _izContext.correlationIds.set(coreConsts.BASE_USER_ID, "BasDev");
99
99
  const userId = _izContext.correlationIds.get(coreConsts.BASE_USER_ID);
100
100
  if (!userId) {
101
101
  errorsFound.push("Not have userId")
@@ -211,7 +211,7 @@ module.exports.<%- functionName %>Main = async (
211
211
  _izContext.logger.debug("filteredRequiredOnCreatedLinks", filteredRequiredOnCreatedLinks);
212
212
 
213
213
  if (filteredRequiredOnCreatedLinks.length) {
214
- if (!requestParams.relationships.length) {
214
+ if (!requestParams.relationships || !requestParams.relationships.length) {
215
215
  // in case not send relationship to create with
216
216
  for (const requireLink of filteredRequiredOnCreatedLinks) {
217
217
  errorsFound.push(`missing required relationship of relType ${JSON.stringify(requireLink.relType)} when create`)
@@ -279,7 +279,7 @@ module.exports.<%- functionName %>Main = async (
279
279
  let allAwaitingStepsId = [];
280
280
 
281
281
  // validate single identifiers
282
- if (objectSchemas.identifiers.length === 1 && !objectSchemas.identifiers.hasOwnProperty("fieldNames")) {
282
+ if (objectSchemas.identifiers.length === 1 && !objectSchemas.identifiers[0].hasOwnProperty("fieldNames")) {
283
283
 
284
284
  const identifier = objectSchemas.identifiers[0];
285
285
  _izContext.logger.debug(":::::case single identifer:::::", identifier);