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

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.
@@ -16,120 +16,125 @@ Copyright (C) 2020 Sven Mason <http: //izara.io>
16
16
  <http: //www.gnu.org/licenses />.
17
17
  */
18
18
 
19
- 'use strict';
19
+ 'use strict';
20
20
 
21
- const {
21
+ const {
22
22
  getObjSchemaS3WithHierarchy,
23
- } = require('@izara_project/izara-core-library-service-schemas/src/GetObjectSchema');
23
+ } = require('@izara_project/izara-core-library-service-schemas/src/GetObjectSchema');
24
24
 
25
- const {
25
+ const {
26
26
  getGraphServiceTagWithCache,
27
- getServiceNameWithCache
28
- } = require('@izara_project/izara-core-library-service-schemas/src/ServiceConfig');
27
+ } = require('@izara_project/izara-core-library-service-schemas/src/ServiceConfig');
29
28
 
30
- const consts = require('@izara_project/izara-core-library-service-schemas/src/Consts');
29
+ const consts = require('@izara_project/izara-core-library-service-schemas/src/Consts');
31
30
 
31
+ const {
32
+ objInstanceFromobjInstanceBase
33
+ } = require('@izara_project/izara-core-library-service-schemas/src/IdentifiersObject');
32
34
 
33
- const {
35
+ const {
34
36
  dynamoDbIdentifiersByStorageResource,
35
- collectGetData
36
- } = require('@izara_project/izara-market-library-service-schemas/src/GenerateCodeLibs/src/GenerateCodeLibs');
37
+ collectGetData,
38
+ createGetDataDetails,
39
+ } = require('@izara_project/izara-market-library-service-schemas/src/GenerateCodeLibs/src/GenerateCodeLibs');
37
40
 
38
41
 
39
- const dynamodbSharedLib = require('@izara_project/izara-shared/src/DynamodbSharedLib');
40
- const graphSharedLib = require('@izara_project/izara-shared/src/GraphSharedLib');
42
+ const dynamodbSharedLib = require('@izara_project/izara-shared/src/DynamodbSharedLib');
43
+ const graphSharedLib = require('@izara_project/izara-shared/src/GraphSharedLib');
41
44
 
45
+ const NoRetryError = require('@izara_project/izara-core-library-core/src/NoRetryError');
42
46
 
43
- const NoRetryError = require('@izara_project/izara-core-library-core/src/NoRetryError');
44
- const { createGetDataDetails } = require('@izara_project/izara-market-library-service-schemas/src/GenerateCodeLibs/src/GenerateCodeLibs')
45
47
 
46
48
 
47
49
 
48
- /**
49
- *
50
- *
51
- * description of function.
52
- * @param {Object} _izContext
53
- * @param {CorrelationIds} _izContext.correlationIds - property of _izContext
54
- * @param {Logger} _izContext.logger - property of _izContext
55
- * @param {Object} requestParams - request params
56
- * @param {Object} requestParams.identifiers - identifiers for get data
57
- * @param {Object} requestParams.additionalRequest - additionalRequest
58
- *
59
- *
60
- * @returns {object} description of return value
61
- */
62
- module.exports.<%- functionName %>Main = async (
63
- _izContext,
64
- requestParams,
65
- callingFlowConfig = {},
66
- //(<additionalParams>)
67
- //(</additionalParams>)
68
- ) => {
50
+ /**
51
+ *
52
+ *
53
+ * description of function.
54
+ * @param {Object} _izContext
55
+ * @param {CorrelationIds} _izContext.correlationIds - property of _izContext
56
+ * @param {Logger} _izContext.logger - property of _izContext
57
+ * @param {Object} requestParams - request params
58
+ * @param {Object} requestParams.identifiers - identifiers for get data
59
+ * @param {Object} requestParams.additionalRequest - additionalRequest
60
+ *
61
+ *
62
+ * @returns {object} description of return value
63
+ */
64
+ module.exports.<%- functionName %>Main = async (
65
+ _izContext,
66
+ requestParams,
67
+ callingFlowConfig = {},
68
+ //(<additionalParams>)
69
+ //(</additionalParams>)
70
+ ) => {
69
71
 
70
72
 
71
- try {
72
- _izContext.logger.debug('<%- functionName %> _izContext: ', _izContext);
73
- _izContext.logger.debug('<%- functionName %> requestParams: ', requestParams);
74
- _izContext.logger.debug('<%- functionName %> callingFlowConfig: ', callingFlowConfig);
73
+ try {
74
+ _izContext.logger.debug('<%- functionName %> _izContext: ', _izContext);
75
+ _izContext.logger.debug('<%- functionName %> requestParams: ', requestParams);
76
+ _izContext.logger.debug('<%- functionName %> callingFlowConfig: ', callingFlowConfig);
75
77
 
78
+ const returnFormat = requestParams.additionalRequest?.setting?.returnFormat;
79
+ const throwWhenNotFoundSomeRecord = requestParams.additionalRequest?.setting?.throwWhenNotFoundSomeRecord || true;
76
80
 
77
- const OBJECT_TYPE = "<%- objectType %>";
78
- const SERVICE_TAG = process.env.iz_serviceTag;
79
81
 
80
- //(<beforeQuery>)
81
- //(</beforeQuery>)
82
+ const OBJECT_TYPE = "<%- objectType %>";
83
+ const SERVICE_TAG = process.env.iz_serviceTag;
82
84
 
83
- let objectSchema = await getObjSchemaS3WithHierarchy(
84
- _izContext,
85
- {
85
+ //(<beforeQuery>)
86
+ //(</beforeQuery>)
87
+
88
+ let objectSchema = await getObjSchemaS3WithHierarchy(
89
+ _izContext,
90
+ {
86
91
  objectType: OBJECT_TYPE,
87
92
  serviceTag: SERVICE_TAG
88
- }
89
- );
93
+ }
94
+ );
90
95
 
91
- _izContext.logger.debug("objectSchema: ", objectSchema);
96
+ _izContext.logger.debug("objectSchema: ", objectSchema);
92
97
 
93
- let getDataDetails = await createGetDataDetails(_izContext, objectSchema);
94
- _izContext.logger.debug("getDataDetails:", getDataDetails)
98
+ let getDataDetails = await createGetDataDetails(_izContext, objectSchema);
99
+ _izContext.logger.debug("getDataDetails:", getDataDetails)
95
100
 
96
- // start to get data
97
- let getResults = {}; // for collect result data from
98
- let getRecordStatus = {
99
- getAtLeastOneRecord: false,
100
- notFoundSomeRecord: false
101
- };
101
+ // start to get data
102
+ let getResults = []; // for collect result data from
103
+ let getRecordStatus = {
104
+ getAtLeastOneRecord: false,
105
+ notFoundSomeRecord: false
106
+ };
102
107
 
103
- await Promise.all(
104
- Object.entries(getDataDetails).map(
108
+ await Promise.all(
109
+ Object.entries(getDataDetails).map(
105
110
  async ([storageTag, getDataDetail]) => {
106
- if (getDataDetail.storageType === consts.STORAGE_TYPES.graph) {
107
- // get data from graph
108
- let graphServiceName = await getServiceNameWithCache(_izContext, getDataDetail.graphServiceTag);
109
-
110
-
111
- let versionedDataLabels = [];
112
- if (getDataDetail.versionedDatas && Object.keys(getDataDetail.versionedDatas).length) {
113
- // have versionedData
114
- versionedDataLabels = Object.keys(getDataDetail.versionedDatas);
115
- }
116
-
117
-
118
- //(<beforeGetGraph>)
119
- //(</beforeGetGraph>)
120
-
121
- let dataFromGraph = await graphSharedLib.getNodeV2(
122
- _izContext,
123
- graphServiceName,
124
- {
125
- objectType: OBJECT_TYPE,
126
- serviceTag: SERVICE_TAG
127
- },
128
- {
129
- identifiers: requestParams.identifiers
130
- },
131
- versionedDataLabels
132
- )
111
+ if (getDataDetail.storageType === consts.STORAGE_TYPES.graph) {
112
+ // get data from graph
113
+ let graphServiceName = await getGraphServiceTagWithCache(_izContext, getDataDetail.graphServiceTag);
114
+
115
+
116
+ let versionedDataLabels = [];
117
+ if (getDataDetail.versionedDatas && Object.keys(getDataDetail.versionedDatas).length) {
118
+ // have versionedData
119
+ versionedDataLabels = Object.keys(getDataDetail.versionedDatas);
120
+ }
121
+
122
+
123
+ //(<beforeGetGraph>)
124
+ //(</beforeGetGraph>)
125
+
126
+ let dataFromGraph = await graphSharedLib.getNodeV2(
127
+ _izContext,
128
+ graphServiceName,
129
+ {
130
+ objectType: OBJECT_TYPE,
131
+ serviceTag: SERVICE_TAG
132
+ },
133
+ {
134
+ identifiers: requestParams.identifiers
135
+ },
136
+ versionedDataLabels
137
+ )
133
138
 
134
139
  _izContext.logger.debug("dataFromGraphV2: ", dataFromGraph);
135
140
 
@@ -139,84 +144,88 @@ Copyright (C) 2020 Sven Mason <http: //izara.io>
139
144
  getRecordStatus.notFoundSomeRecord = true
140
145
  }
141
146
 
142
- //(<afterGetGraph>)
143
- //(</afterGetGraph>)
144
-
145
- Object.assign(
146
- getResults,
147
- collectGetData(_izContext, dataFromGraph, getDataDetail)
148
- )
147
+ //(<afterGetGraph>)
148
+ //(</afterGetGraph>)
149
149
 
150
+ getResults.push([dataFromGraph, getDataDetail]);
150
151
 
151
- } else {
152
+ } else {
152
153
 
153
154
  let identifiersForGetData = await dynamoDbIdentifiersByStorageResource(
154
- _izContext,
155
- objectSchema,
156
- getDataDetail,
157
- requestParams.identifiers
155
+ _izContext,
156
+ objectSchema,
157
+ getDataDetail,
158
+ requestParams.identifiers
158
159
  );
159
160
 
160
161
  _izContext.logger.debug("identifiersForGetData: ", identifiersForGetData);
161
162
 
162
163
 
163
164
  //(<beforeGetDynamoDb>)
164
- //(</beforeGetDynamoDb>)
165
+ //(</beforeGetDynamoDb>)
165
166
 
166
167
  let dataFromDynamoDb = await dynamodbSharedLib.getItem(
167
- _izContext,
168
- await dynamodbSharedLib.tableName(_izContext, getDataDetail.tableName),
169
- identifiersForGetData
168
+ _izContext,
169
+ await dynamodbSharedLib.tableName(_izContext, getDataDetail.tableName),
170
+ identifiersForGetData
170
171
  );
171
172
 
172
173
  _izContext.logger.debug("dataFromDynamoDb: ", dataFromDynamoDb);
173
174
 
174
175
 
175
176
  if (dataFromDynamoDb) {
176
- getRecordStatus.getAtLeastOneRecord = true;
177
+ dataFromDynamoDb = await objInstanceFromobjInstanceBase(
178
+ _izContext,
179
+ {
180
+ objectType: OBJECT_TYPE,
181
+ serviceTag: SERVICE_TAG
182
+ },
183
+ dataFromDynamoDb
184
+ );
185
+
186
+ getRecordStatus.getAtLeastOneRecord = true;
177
187
  } else {
178
- getRecordStatus.notFoundSomeRecord = true;
188
+ getRecordStatus.notFoundSomeRecord = true;
179
189
  }
180
190
 
181
191
  //(<afterGetDynamoDb>)
182
- //(</afterGetDynamoDb>)
192
+ //(</afterGetDynamoDb>)
183
193
 
184
194
  // collect return data depend on getDataDetail
185
- Object.assign(
186
- getResults,
187
- collectGetData(_izContext, dataFromDynamoDb, getDataDetail)
188
- );
195
+ getResults.push([dataFromDynamoDb, getDataDetail]);
189
196
 
190
- }
191
- })
192
- );
197
+ }
198
+ })
199
+ );
193
200
 
194
- _izContext.logger.debug("getRecordStatus: ", getRecordStatus);
201
+ _izContext.logger.debug("getRecordStatus: ", getRecordStatus);
195
202
 
196
- if (getRecordStatus.getAtLeastOneRecord === true && getRecordStatus.notFoundSomeRecord === true) {
203
+ if (throwWhenNotFoundSomeRecord && getRecordStatus.getAtLeastOneRecord === true && getRecordStatus.notFoundSomeRecord === true) {
197
204
  throw Error("Not found some record while get data");
198
- }
199
-
200
- if (Object.keys(getResults).length) {
201
- _izContext.logger.debug("getResults", getResults)
202
- return getResults;
203
- } else {
204
- return null
205
- }
206
-
207
- } catch (err) {
208
- _izContext.logger.error('error <%- firstLetterUpperCase(functionName) %>Main: ', err)
209
- throw (err)
210
- }
211
- }
212
-
213
- <%_ function firstLetterUpperCase(text){ return text.charAt(0).toUpperCase() + text.slice(1) } _%>
214
- <%_ function firstLetterLowerCase(str) { return str.charAt(0).toLowerCase() + str.slice(1) } _%>
215
-
216
- // request input
217
-
218
- // {
219
- // "identifiers": {
220
- // "personId" : "basTest"
221
- // }
222
- // }
205
+ }
206
+
207
+ let getResult = collectGetData(_izContext, getResults, objectSchema, returnFormat);
208
+
209
+ return getResult;
210
+ } catch (err) {
211
+ _izContext.logger.error('error <%- firstLetterUpperCase(functionName) %>Main: ', err)
212
+ throw (err)
213
+ }
214
+ }
215
+
216
+ <%_ function firstLetterUpperCase(text){ return text.charAt(0).toUpperCase() + text.slice(1) } _%>
217
+ <%_ function firstLetterLowerCase(str) { return str.charAt(0).toLowerCase() + str.slice(1) } _%>
218
+
219
+ // request input
220
+ // {
221
+ // "identifiers": {
222
+ // "companyId": "seagame001GenerateCode2",
223
+ // "city": "cnxCity",
224
+ // "companyName": "RoyalSeafood"
225
+ // },
226
+ // "additionalRequest": {
227
+ // "setting": {
228
+ // "returnFormat": "STD" || "FULL"
229
+ // }
230
+ // }
231
+ // }
@@ -44,7 +44,7 @@ const sqsTemplatePath = path.join(__dirname, './sqsTemplate.ejs')
44
44
  function dataForCreateSnsAndSqsQueueFromObjectSchema(_izContext, objectSchema, srcPath) {
45
45
  const snsAndSqsDataArray = [];
46
46
  for (const action of Object.values(ACTIONS)) {
47
- if (objectSchema.overWriteHandlers[action]) {
47
+ if (objectSchema.overWriteHandlers?.[action]) {
48
48
  for (const handler of objectSchema.overWriteHandlers[action]) {
49
49
  const snsAndSqsData = {
50
50
  queueName: createQueueNameForSnsAndSqsQueue(_izContext, objectSchema, action, handler, srcPath)
@@ -35,7 +35,7 @@ const templatePath = path.join(__dirname, "./template.ejs")
35
35
  function createDataForCreateSourceSnsOut(_izContext, objectSchema, srcPath) {
36
36
  const createSourceArray = [];
37
37
  for (const action of Object.values(ACTIONS)) {
38
- if (objectSchema.overWriteHandlers[action]) {
38
+ if (objectSchema.overWriteHandlers?.[action]) {
39
39
  for (const handler of objectSchema.overWriteHandlers[action]) {
40
40
  const snsOut = {
41
41
  queueName: createQueueName(_izContext, objectSchema, action, handler, srcPath)
@@ -0,0 +1,57 @@
1
+ /*
2
+ Copyright (C) 2020 Sven Mason <http://izara.io>
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Affero General Public License as
6
+ published by the Free Software Foundation, either version 3 of the
7
+ License, or (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Affero General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Affero General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ 'use strict';
19
+
20
+ const hash = require('object-hash');
21
+
22
+
23
+ function checkValidTableYaml(_izContext, currentTableSetting, existsTableSetting) {
24
+ /*
25
+ return table yml setting that already filter duplicate tableName that have same table setting
26
+ throw error when found duplicate table name but not have same table setting
27
+ */
28
+
29
+ let filteredDynamoDbYamlSetting = [];
30
+
31
+ for (let i = 0, length = currentTableSetting.length; i < length; i++) {
32
+ let dynamoDbYamlSetting = currentTableSetting[i];
33
+ if (existsTableSetting.hasOwnProperty(dynamoDbYamlSetting.templateData.tableName)) {
34
+
35
+ // exists table
36
+ if (hash(existsTableSetting[dynamoDbYamlSetting.templateData.tableName]) === hash(dynamoDbYamlSetting.templateData.attributes.sort())) {
37
+ continue;
38
+ } else {
39
+ throw Error(`tableName:${dynamoDbYamlSetting.templateData.tableName} have duplicate setting but not have same setting`)
40
+ }
41
+
42
+ } else {
43
+ filteredDynamoDbYamlSetting.push(dynamoDbYamlSetting);
44
+
45
+ // not exists table
46
+ Object.assign(
47
+ existsTableSetting,
48
+ {
49
+ [dynamoDbYamlSetting.templateData.tableName]: dynamoDbYamlSetting.templateData.attributes.sort()
50
+ }
51
+ );
52
+ }
53
+ }
54
+ return [filteredDynamoDbYamlSetting, existsTableSetting];
55
+ }
56
+
57
+ module.exports = { checkValidTableYaml };