@izara_project/izara-core-generate-service-code 1.0.61 → 1.0.62

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 (39) hide show
  1. package/package.json +5 -6
  2. package/src/codeGenerators/app/initial_setup/InitialSetupGenerator.js +100 -150
  3. package/src/codeGenerators/app/initial_setup/templates/InitialSetup_LambdaRole.ejs +27 -20
  4. package/src/codeGenerators/app/sls_yaml/FunctionYamlGenerator.js +8 -17
  5. package/src/codeGenerators/app/sls_yaml/RoleNameConfigGenerator.js +13 -72
  6. package/src/codeGenerators/app/sls_yaml/_policy/PolicyEmitter.js +3 -2
  7. package/src/codeGenerators/app/sls_yaml/_policy/__tests__/PolicyEmitter.test.js +3 -3
  8. package/src/codeGenerators/app/sls_yaml/_policy/templates/ManagedPolicy_Yaml.ejs +2 -2
  9. package/src/codeGenerators/app/sls_yaml/templates/SharedResource_Yaml.ejs +1 -1
  10. package/src/codeGenerators/app/src/generatedCode/Flow/FlowSchemas/templates/endpoint/FlowEndpoint_HdrWbs.ejs +2 -2
  11. package/src/codeGenerators/app/src/generatedCode/Flow/FlowSchemas/templates/endpoint/FlowEndpoint_Main.ejs +8 -4
  12. package/src/codeGenerators/app/src/generatedCode/Flow/FlowSchemas/templates/endpoint/FlowMain_Wbs.ejs +4 -4
  13. package/src/codeGenerators/app/src/generatedCode/Flow/FlowSchemas/templates/s3/CreatePreSignUrl_Main.ejs +1 -1
  14. package/src/codeGenerators/app/src/generatedCode/Flow/FlowSchemas/templates/s3/ProcessS3File_Main.ejs +1 -1
  15. package/src/codeGenerators/app/src/generatedCode/Flow/FlowSchemas/templates/step/FlowEndpointComplete_Main.ejs +11 -12
  16. package/src/codeGenerators/app/src/generatedCode/Flow/_shared/events/BaseDsqHandler.js +4 -13
  17. package/src/codeGenerators/app/src/generatedCode/Flow/_shared/events/BaseSqsHandler.js +14 -15
  18. package/src/codeGenerators/app/src/generatedCode/Flow/_shared/events/lambdaSyncApi.js +4 -13
  19. package/src/codeGenerators/app/src/generatedCode/Flow/_shared/events/lambdaSyncInv.js +4 -13
  20. package/src/codeGenerators/app/src/generatedCode/Flow/_shared/events/ownTopic.js +16 -4
  21. package/src/codeGenerators/app/src/generatedCode/Flow/_shared/handlers/FlowHandler_HdrDsq.ejs +71 -63
  22. package/src/codeGenerators/app/src/generatedCode/Flow/_shared/handlers/FlowHandler_HdrSqs.ejs +54 -50
  23. package/src/codeGenerators/app/src/generatedCode/Flow/_shared/shared/flowEntryPointBase.js +7 -4
  24. package/src/codeGenerators/app/src/generatedCode/Flow/_shared/shared/flowMainFunctionBase.js +21 -29
  25. package/src/codeGenerators/app/src/generatedCode/Flow/_shared/shared/flowNaming.js +16 -0
  26. package/src/codeGenerators/app/src/generatedCode/Flow/_shared/shared/flowStepBase.js +9 -4
  27. package/src/codeGenerators/app/src/generatedCode/Flow/_shared/shared/flowStepNormalizer.js +21 -20
  28. package/src/codeGenerators/app/src/generatedCode/Flow/_shared/shared/flowValidator.js +1 -13
  29. package/src/codeGenerators/app/src/generatedCode/Flow/_shared/shared/propertyNormalizer.js +20 -0
  30. package/src/codeGenerators/app/src/generatedCode/Flow/_shared/shared/roleNameDetection.js +49 -0
  31. package/src/codeGenerators/resource/sls_yaml/DynamoDBGenerator.js +1 -10
  32. package/src/codeGenerators/resource/sls_yaml/FlowOutGenerator.js +1 -10
  33. package/src/codeGenerators/resource/sls_yaml/FlowResourceYamlGenerator.js +2 -11
  34. package/src/core/validate.js +9 -5
  35. package/src/generate.js +1 -1
  36. package/src/generateCode.js +21 -0
  37. package/src/schemaGenerators/app/src/schemas/FlowSchemas/FlowSchemaGenerator.js +0 -43
  38. package/test_generate.js +0 -7
  39. package/test_normalize.js +0 -17
@@ -48,7 +48,7 @@ const { postToConnection } = webSocket;
48
48
  * @returns {object} description of return value
49
49
  */
50
50
 
51
- export default async function <%- upperCaseFlowTag %>(
51
+ export default async function <%- typeof functionName !== 'undefined' ? functionName : upperCaseFlowTag %>(
52
52
  _izContext,
53
53
  requestParams,
54
54
  //(<additionalParams>)
@@ -57,9 +57,9 @@ export default async function <%- upperCaseFlowTag %>(
57
57
  ) {
58
58
 
59
59
  try {
60
- _izContext.logger.debug("<%- upperCaseFlowTag %> _izContext", _izContext)
61
- _izContext.logger.debug("<%- upperCaseFlowTag %> requestParams", requestParams)
62
- _izContext.logger.debug("<%- upperCaseFlowTag %> callingFlowConfig", callingFlowConfig)
60
+ _izContext.logger.debug("<%- typeof functionName !== 'undefined' ? functionName : upperCaseFlowTag %> _izContext", _izContext)
61
+ _izContext.logger.debug("<%- typeof functionName !== 'undefined' ? functionName : upperCaseFlowTag %> requestParams", requestParams)
62
+ _izContext.logger.debug("<%- typeof functionName !== 'undefined' ? functionName : upperCaseFlowTag %> callingFlowConfig", callingFlowConfig)
63
63
 
64
64
  let flowSchema = await getObjectSchema.getFlowSchemaS3WithCache(_izContext, {
65
65
  flowTag: "<%- flowTag %>",
@@ -26,7 +26,7 @@ import { lambda, sns } from '@izara_project/izara-core-library-external-request'
26
26
 
27
27
  import { S3Client } from '@aws-sdk/client-s3';
28
28
  import { createPresignedPost } from '@aws-sdk/s3-presigned-post';
29
- const client = new S3Client({ region: 'us-east-2' });
29
+ const client = new S3Client({ region: process.env.AWS_REGION });
30
30
  import { consts } from '@izara_project/izara-middleware';
31
31
 
32
32
  import { postToConnection, getNestObject } from '../../../../libs/source/GenerateCodeLibs.js'
@@ -16,7 +16,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
  */
17
17
 
18
18
  import { S3Client, GetObjectCommand, DeleteObjectCommand, PutObjectCommand, CopyObjectCommand } from '@aws-sdk/client-s3';
19
- const s3 = new S3Client({ region: 'us-east-2' });
19
+ const s3 = new S3Client({ region: process.env.AWS_REGION });
20
20
 
21
21
  //(<optionalRequired>)
22
22
  //(</optionalRequired>)
@@ -39,7 +39,7 @@ const PREFIX = {
39
39
  * @param {*} graphServiceTag
40
40
  * @param {*} errorsFound
41
41
  */
42
- function validateInput(returnValue, status, graphServiceTag, errorsFound) {
42
+ function _validateInput(returnValue, status, graphServiceTag, errorsFound) {
43
43
  if (!Object.prototype.hasOwnProperty.call(returnValue, 'queryResult')) {
44
44
  errorsFound.push(
45
45
  'message is not have property queryResult, should be not happen',
@@ -83,7 +83,7 @@ function validateInput(returnValue, status, graphServiceTag, errorsFound) {
83
83
  /**
84
84
  * @param {*} value
85
85
  */
86
- function isEmpty(value) {
86
+ function _isEmpty(value) {
87
87
  if (value == null) return true;
88
88
 
89
89
  if (
@@ -108,7 +108,7 @@ function isEmpty(value) {
108
108
  * @param {*} returnValue
109
109
  * @returns {object|null}
110
110
  */
111
- function buildExistsNode(returnValue) {
111
+ function _buildExistsNode(returnValue) {
112
112
  const { existsNode } = returnValue.queryResult.return<%- upperCaseFlowTag %>Node;
113
113
  if (!existsNode || !Object.keys(existsNode).length) return null;
114
114
 
@@ -129,7 +129,7 @@ function buildExistsNode(returnValue) {
129
129
  * @param {*} errorsFound
130
130
  * @returns {object}
131
131
  */
132
- function buildMessageObject(returnValue, existsNode, status, errorsFound) {
132
+ function _buildMessageObject(returnValue, existsNode, status, errorsFound) {
133
133
  const { objType } = returnValue.requestParams;
134
134
  const { return<%- upperCaseFlowTag %>Node } = returnValue.queryResult;
135
135
 
@@ -152,15 +152,14 @@ function buildMessageObject(returnValue, existsNode, status, errorsFound) {
152
152
  * @param {*} passBackProperties
153
153
  * @param {*} messageObject
154
154
  */
155
- async function sendSnsMessage(_izContext, passBackProperties, messageObject) {
155
+ async function _sendSnsMessage(_izContext, passBackProperties, messageObject) {
156
156
  const enrichedMessage =
157
157
  callingFlowSharedLib.addParentPassBackPropertiesToSnsResponseMessageObject(
158
158
  passBackProperties,
159
159
  messageObject,
160
160
  );
161
161
  const messageAttributes =
162
- callingFlowSharedLib.addParentPassBackCallingFlowToSnsResponseMessageAttribu
163
- tes(
162
+ callingFlowSharedLib.addParentPassBackCallingFlowToSnsResponseMessageAttributes(
164
163
  passBackProperties,
165
164
  {},
166
165
  );
@@ -211,19 +210,19 @@ export default async function <%- functionName %>(
211
210
  passBackProperties,
212
211
  });
213
212
 
214
- if (isEmpty(returnValue.requestParams)) {
213
+ if (_isEmpty(returnValue.requestParams)) {
215
214
  _izContext.logger.debug('message callingFlowProperties is empty');
216
215
  throw new NoRetryError('message not an object');
217
216
  }
218
217
 
219
- validateInput(returnValue, status, graphServiceTag, errorsFound);
218
+ _validateInput(returnValue, status, graphServiceTag, errorsFound);
220
219
 
221
220
  if (errorsFound.length > 0) {
222
221
  _izContext.logger.debug('Have error form graphServerTag:', errorsFound);
223
222
  errorsFound.push(errorsFound.join(','));
224
223
  }
225
224
 
226
- const existsNode = buildExistsNode(returnValue);
225
+ const existsNode = _buildExistsNode(returnValue);
227
226
 
228
227
  const awaitingStepId = await asyncFlowSharedLib.createAwaitingStepId(
229
228
  parentFlowId,
@@ -269,7 +268,7 @@ export default async function <%- functionName %>(
269
268
  }
270
269
  }))
271
270
 
272
- const messageObject = buildMessageObject(
271
+ const messageObject = _buildMessageObject(
273
272
  returnValue,
274
273
  existsNode,
275
274
  status,
@@ -279,7 +278,7 @@ export default async function <%- functionName %>(
279
278
  //(<beforeSendMessage>)
280
279
  //(</beforeSendMessage>)
281
280
 
282
- await sendSnsMessage(_izContext, passBackProperties, messageObject);
281
+ await _sendSnsMessage(_izContext, passBackProperties, messageObject);
283
282
 
284
283
  //(<afterSendMessage>)
285
284
  //(</afterSendMessage>)
@@ -1,6 +1,7 @@
1
1
  import ejs from 'ejs';
2
2
  import fs from 'fs/promises';
3
3
  import path from 'path';
4
+ import { normalizePropertyType } from '../shared/propertyNormalizer.js';
4
5
 
5
6
  export async function generateDsqHandler(flow, stepName, suffix, flowOutputDir, mainFileName, templates) {
6
7
  const upperCase = (str) => str.charAt(0).toUpperCase() + str.slice(1);
@@ -27,26 +28,16 @@ export async function generateDsqHandler(flow, stepName, suffix, flowOutputDir,
27
28
 
28
29
  let startConfig = null;
29
30
  if (!stepName && flow.flowSteps) {
30
- const startStep = flow.flowSteps.find(s => s.stepName === 'start');
31
+ const startStep = flow.flowSteps && flow.flowSteps.length > 0 ? flow.flowSteps[0] : null;
31
32
  if (startStep) {
32
33
  startConfig = { ...startStep };
33
34
  // Map properties
34
35
  if (startConfig.properties && Array.isArray(startConfig.properties)) {
35
- startConfig.properties = startConfig.properties.map(p => {
36
- if (typeof p === 'string') {
37
- return (flow.stepProperties && flow.stepProperties[p]) ? flow.stepProperties[p] : { propertyName: p, type: 'string' };
38
- }
39
- return p;
40
- });
36
+ startConfig.properties = startConfig.properties.map(p => normalizePropertyType(p, flow.stepProperties));
41
37
  }
42
38
  // Map messageAttributes
43
39
  if (startConfig.messageAttributes && Array.isArray(startConfig.messageAttributes)) {
44
- startConfig.messageAttributes = startConfig.messageAttributes.map(p => {
45
- if (typeof p === 'string') {
46
- return (flow.stepMessageAttributes && flow.stepMessageAttributes[p]) ? flow.stepMessageAttributes[p] : { propertyName: p, type: 'string' };
47
- }
48
- return p;
49
- });
40
+ startConfig.messageAttributes = startConfig.messageAttributes.map(p => normalizePropertyType(p, flow.stepMessageAttributes));
50
41
  }
51
42
  }
52
43
  }
@@ -1,18 +1,27 @@
1
+ import { isCrudFlow, isRbacFlow, isRelationshipFlow } from '../shared/flowClassifier.js';
2
+ import { ownTopicFunctionName, processFunctionName } from '../shared/flowNaming.js';
1
3
  import ejs from 'ejs';
2
4
  import fs from 'fs/promises';
3
5
  import path from 'path';
6
+ import { normalizePropertyType } from '../shared/propertyNormalizer.js';
4
7
 
5
8
  export async function generateSqsHandler(flow, stepName, suffix, flowOutputDir, mainFileName, templates) {
6
9
  const upperCase = (str) => str.charAt(0).toUpperCase() + str.slice(1);
7
10
  const flowTag = flow.flowTag;
8
11
 
12
+ const isOwnTopic = !stepName && !isCrudFlow(flowTag) && !isRbacFlow(flowTag) && !isRelationshipFlow(flowTag) && flow.event && flow.event.includes('ownTopic');
13
+
9
14
  // For entry point stepName is empty
10
- const handlerFileName = stepName ? `${flowTag}${stepName}_${suffix}` : `Process${upperCase(flowTag)}_${suffix}`;
15
+ const handlerFileName = stepName
16
+ ? `${flowTag}${stepName}_${suffix}`
17
+ : (isOwnTopic ? `${upperCase(flowTag)}_${suffix}` : `Process${upperCase(flowTag)}_${suffix}`);
11
18
 
12
19
  // Function name: matches default export in the referenced _Main.js
13
20
  // For entry point: Process${upperCase(flowTag)} (e.g. ProcessOwnTopicFlow)
14
21
  // For steps: ${flowTag}${flowStepName} (e.g. OwnTopicFlowProcess)
15
- const functionName = stepName ? `${flowTag}${stepName}` : `Process${upperCase(flowTag)}`;
22
+ const functionName = stepName
23
+ ? `${flowTag}${stepName}`
24
+ : (isOwnTopic ? ownTopicFunctionName(flowTag) : processFunctionName(flowTag));
16
25
 
17
26
  // Queue name: keep the full handler suffix so handler code matches
18
27
  // the generated queue/resource names.
@@ -30,26 +39,16 @@ export async function generateSqsHandler(flow, stepName, suffix, flowOutputDir,
30
39
 
31
40
  let startConfig = null;
32
41
  if (!stepName && flow.flowSteps) {
33
- const startStep = flow.flowSteps.find(s => s.stepName === 'start');
42
+ const startStep = flow.flowSteps && flow.flowSteps.length > 0 ? flow.flowSteps[0] : null;
34
43
  if (startStep) {
35
44
  startConfig = { ...startStep };
36
45
  // Map properties
37
46
  if (startConfig.properties && Array.isArray(startConfig.properties)) {
38
- startConfig.properties = startConfig.properties.map(p => {
39
- if (typeof p === 'string') {
40
- return (flow.stepProperties && flow.stepProperties[p]) ? flow.stepProperties[p] : { propertyName: p, type: 'string' };
41
- }
42
- return p;
43
- });
47
+ startConfig.properties = startConfig.properties.map(p => normalizePropertyType(p, flow.stepProperties));
44
48
  }
45
49
  // Map messageAttributes
46
50
  if (startConfig.messageAttributes && Array.isArray(startConfig.messageAttributes)) {
47
- startConfig.messageAttributes = startConfig.messageAttributes.map(p => {
48
- if (typeof p === 'string') {
49
- return (flow.stepMessageAttributes && flow.stepMessageAttributes[p]) ? flow.stepMessageAttributes[p] : { propertyName: p, type: 'string' };
50
- }
51
- return p;
52
- });
51
+ startConfig.messageAttributes = startConfig.messageAttributes.map(p => normalizePropertyType(p, flow.stepMessageAttributes));
53
52
  }
54
53
  }
55
54
  }
@@ -1,6 +1,7 @@
1
1
  import ejs from 'ejs';
2
2
  import fs from 'fs/promises';
3
3
  import path from 'path';
4
+ import { normalizePropertyType } from '../shared/propertyNormalizer.js';
4
5
 
5
6
  export default async function generate(flow, stepName, flowOutputDir, mainFileName, templates) {
6
7
  const upperCase = (str) => str.charAt(0).toUpperCase() + str.slice(1);
@@ -15,26 +16,16 @@ export default async function generate(flow, stepName, flowOutputDir, mainFileNa
15
16
 
16
17
  let startConfig = null;
17
18
  if (!stepName && flow.flowSteps) {
18
- const startStep = flow.flowSteps.find(s => s.stepName === 'start');
19
+ const startStep = flow.flowSteps && flow.flowSteps.length > 0 ? flow.flowSteps[0] : null;
19
20
  if (startStep) {
20
21
  startConfig = { ...startStep };
21
22
  // Map properties
22
23
  if (startConfig.properties && Array.isArray(startConfig.properties)) {
23
- startConfig.properties = startConfig.properties.map(p => {
24
- if (typeof p === 'string') {
25
- return (flow.stepProperties && flow.stepProperties[p]) ? flow.stepProperties[p] : { propertyName: p, type: 'string' };
26
- }
27
- return p;
28
- });
24
+ startConfig.properties = startConfig.properties.map(p => normalizePropertyType(p, flow.stepProperties));
29
25
  }
30
26
  // Map messageAttributes
31
27
  if (startConfig.messageAttributes && Array.isArray(startConfig.messageAttributes)) {
32
- startConfig.messageAttributes = startConfig.messageAttributes.map(p => {
33
- if (typeof p === 'string') {
34
- return (flow.stepMessageAttributes && flow.stepMessageAttributes[p]) ? flow.stepMessageAttributes[p] : { propertyName: p, type: 'string' };
35
- }
36
- return p;
37
- });
28
+ startConfig.messageAttributes = startConfig.messageAttributes.map(p => normalizePropertyType(p, flow.stepMessageAttributes));
38
29
  }
39
30
  }
40
31
  }
@@ -1,6 +1,7 @@
1
1
  import ejs from 'ejs';
2
2
  import fs from 'fs/promises';
3
3
  import path from 'path';
4
+ import { normalizePropertyType } from '../shared/propertyNormalizer.js';
4
5
 
5
6
  export default async function generate(flow, stepName, flowOutputDir, mainFileName, templates) {
6
7
  const upperCase = (str) => str.charAt(0).toUpperCase() + str.slice(1);
@@ -15,26 +16,16 @@ export default async function generate(flow, stepName, flowOutputDir, mainFileNa
15
16
 
16
17
  let startConfig = null;
17
18
  if (!stepName && flow.flowSteps) {
18
- const startStep = flow.flowSteps.find(s => s.stepName === 'start');
19
+ const startStep = flow.flowSteps && flow.flowSteps.length > 0 ? flow.flowSteps[0] : null;
19
20
  if (startStep) {
20
21
  startConfig = { ...startStep };
21
22
  // Map properties
22
23
  if (startConfig.properties && Array.isArray(startConfig.properties)) {
23
- startConfig.properties = startConfig.properties.map(p => {
24
- if (typeof p === 'string') {
25
- return (flow.stepProperties && flow.stepProperties[p]) ? flow.stepProperties[p] : { propertyName: p, type: 'string' };
26
- }
27
- return p;
28
- });
24
+ startConfig.properties = startConfig.properties.map(p => normalizePropertyType(p, flow.stepProperties));
29
25
  }
30
26
  // Map messageAttributes
31
27
  if (startConfig.messageAttributes && Array.isArray(startConfig.messageAttributes)) {
32
- startConfig.messageAttributes = startConfig.messageAttributes.map(p => {
33
- if (typeof p === 'string') {
34
- return (flow.stepMessageAttributes && flow.stepMessageAttributes[p]) ? flow.stepMessageAttributes[p] : { propertyName: p, type: 'string' };
35
- }
36
- return p;
37
- });
28
+ startConfig.messageAttributes = startConfig.messageAttributes.map(p => normalizePropertyType(p, flow.stepMessageAttributes));
38
29
  }
39
30
  }
40
31
  }
@@ -1,4 +1,6 @@
1
1
  import { generateSqsHandler } from './BaseSqsHandler.js';
2
+ import { isCrudFlow, isRbacFlow, isRelationshipFlow } from '../shared/flowClassifier.js';
3
+ import { ownTopicWebSocketMainFileName, ownTopicWebSocketFunctionName } from '../shared/flowNaming.js';
2
4
  import ejs from 'ejs';
3
5
  import fs from 'fs/promises';
4
6
  import path from 'path';
@@ -15,9 +17,18 @@ export default async function generate(flow, stepName, flowOutputDir, mainFileNa
15
17
  // ownTopic flows ALWAYS generate the WebSocket entry + SNS-publish main file,
16
18
  // regardless of initiateBy (legacy behavior: WebSocket is the universal entry point).
17
19
  if (!stepName) {
20
+ const isOwnTopicConvention = !isCrudFlow(flow.flowTag) && !isRbacFlow(flow.flowTag) && !isRelationshipFlow(flow.flowTag);
21
+ let entryPointFileName;
22
+ let wbsFunctionName;
23
+
24
+ if (isOwnTopicConvention) {
25
+ entryPointFileName = ownTopicWebSocketMainFileName(flow.flowTag);
26
+ wbsFunctionName = ownTopicWebSocketFunctionName(flow.flowTag);
27
+ } else {
28
+ entryPointFileName = `${upperCase(flow.flowTag)}_Main`;
29
+ wbsFunctionName = upperCase(flow.flowTag);
30
+ }
18
31
  const handlerFileName = `${upperCase(flow.flowTag)}_HdrWbs`;
19
- const functionName = `${flow.flowTag}Main`;
20
- const entryPointFileName = `${upperCase(flow.flowTag)}_Main`;
21
32
 
22
33
  // 1. Render HdrWbs.js
23
34
  const handlerContent = ejs.render(templates.endpointHdrWbs, {
@@ -25,7 +36,7 @@ export default async function generate(flow, stepName, flowOutputDir, mainFileNa
25
36
  upperCaseFlowTag: upperCase(flow.flowTag),
26
37
  flowStepName: '',
27
38
  mainFileName: entryPointFileName,
28
- functionName: functionName,
39
+ functionName: wbsFunctionName,
29
40
  route: flow.flowTag
30
41
  });
31
42
  await fs.writeFile(path.join(flowOutputDir, `${handlerFileName}.js`), handlerContent, 'utf-8');
@@ -35,7 +46,8 @@ export default async function generate(flow, stepName, flowOutputDir, mainFileNa
35
46
  flowTag: flow.flowTag,
36
47
  upperCaseFlowTag: upperCase(flow.flowTag),
37
48
  topicArn: `${upperCase(flow.flowTag)}_In`,
38
- generateCode: generateCode
49
+ generateCode: generateCode,
50
+ functionName: wbsFunctionName
39
51
  });
40
52
  await fs.writeFile(path.join(flowOutputDir, `${entryPointFileName}.js`), mainContent, 'utf-8');
41
53
  }
@@ -15,8 +15,8 @@ You should have received a copy of the GNU Affero General Public License
15
15
  along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
  */
17
17
 
18
- import { middlewareHandler as middleware } from "@izara_project/izara-middleware";
19
- import { recordHandlerSharedLib } from "@izara_project/izara-core-library-record-handler";
18
+ import { middlewareHandler as middleware } from '@izara_project/izara-middleware';
19
+ import { recordHandlerSharedLib } from '@izara_project/izara-core-library-record-handler';
20
20
  import Logger from '@izara_project/izara-core-library-logger';
21
21
  import callingFlowSharedLib from '@izara_project/izara-core-library-calling-flow';
22
22
  import <%= functionName %> from './<%= mainFileName %>.js';
@@ -27,11 +27,9 @@ let perRecordsValidatorSchema = {
27
27
  <% if (typeof startConfig !== 'undefined' && startConfig !== null && startConfig.properties && startConfig.properties.length > 0) { -%>
28
28
  type: 'object',
29
29
  properties: {
30
- <% startConfig.properties.forEach(prop => { -%>
31
- '<%- prop.propertyName %>': { type: '<%- prop.type || 'string' %>' },
32
- <% }) -%>
33
- },
34
- required: [ <%- startConfig.properties.filter(p => p.required !== false).map(p => `'${p.propertyName}'`).join(', ') %> ]
30
+ <% startConfig.properties.forEach((prop, index) => { %> <%- prop.propertyName %>: { type: '<%- prop.type || 'string' %>' }<%= index < startConfig.properties.length - 1 ? ',' : '' %>
31
+ <% }) %> },
32
+ required: [<%- startConfig.properties.filter(p => p.required !== false).map(p => `'${p.propertyName}'`).join(', ') %>]
35
33
  <% } -%>
36
34
  //(</validatorSchema>)
37
35
  };
@@ -40,90 +38,100 @@ let perRecordsValidatorSchema = {
40
38
  let messageAttributeValidatorSchema = {
41
39
  type: 'object',
42
40
  properties: {
43
- <% startConfig.messageAttributes.forEach(prop => { -%>
44
- '<%- prop.propertyName %>': {
41
+ <% startConfig.messageAttributes.forEach((prop, index) => { %> '<%- prop.propertyName %>': {
45
42
  type: 'object',
46
43
  properties: {
47
44
  Type: { type: 'string' },
48
45
  Value: { type: '<%- prop.type || 'string' %>' }
49
46
  },
50
47
  required: ['Type', 'Value']
51
- },
52
- <% }) -%>
53
- },
54
- required: [ <%- startConfig.messageAttributes.filter(p => p.required !== false).map(p => `'${p.propertyName}'`).join(', ') %> ]
48
+ }<%= index < startConfig.messageAttributes.length - 1 ? ',' : '' %>
49
+ <% }) %> },
50
+ required: [<%- startConfig.messageAttributes.filter(p => p.required !== false).map(p => `'${p.propertyName}'`).join(', ') %>]
55
51
  };
56
52
  <% } else { -%>
57
53
  let messageAttributeValidatorSchema = null;
58
54
  <% } -%>
59
55
 
60
- middleware.setValidatorSchema(recordHandlerSharedLib.baseValidatorSchema(
61
- {
62
- type: "object",
63
- required: ['body', 'messageAttributes'],
64
- properties: {
65
- body: {
66
- type: 'object',
67
- },
68
- messageAttributes: {
69
- type: 'object'
56
+ middleware.setValidatorSchema(
57
+ recordHandlerSharedLib.baseValidatorSchema({
58
+ type: 'object',
59
+ required: ['body', 'messageAttributes'],
60
+ properties: {
61
+ body: {
62
+ type: 'object'
63
+ },
64
+ messageAttributes: {
65
+ type: 'object'
66
+ }
70
67
  }
71
- }
72
- }));
68
+ })
69
+ );
73
70
 
74
71
  export const main = middleware.wrap(async (event, context) => {
75
-
76
72
  try {
77
-
78
73
  let recordPromises = [];
79
74
 
80
75
  // loop each record and send to mainFunction
81
- await Promise.all(event.Records.map(async record => { // promise.all for map() function
82
-
83
- let passOnProperties = []
84
- record = recordHandlerSharedLib.reformatDsqMessage(record._izContext, record);
85
- record._izContext.logger.debug('record ReceiveMsgOutHdrSqs', record);
86
-
87
- //validate message (and MessageAttributes)
88
- await recordHandlerSharedLib.validateRecord(
89
- record, // one record will send to mainFunction
90
- "<%= queueName %>", // queue name that need to retry or send to dlq
91
- perRecordsValidatorSchema, // schema for record.Message
92
- messageAttributeValidatorSchema
93
- );
94
-
95
- // add argument (to invoke lambda) to passOnProperties[]
96
- passOnProperties.push(record.body.Message);
97
- passOnProperties.push(callingFlowSharedLib.addCallingFlowToPassOnProperties(record.body.Message));
98
- //(<additionalParams>)
99
- //(</additionalParams>)
100
- record._izContext.logger.debug('passOnProperties in handler', passOnProperties);
101
-
102
- // call recordHandlerSharedLib.recordHandler with 3 parameters and return promise(resolve)
103
- let recordPromise = recordHandlerSharedLib.recordHandler(
104
- record, // one record will send to mainFunction
105
- <%= functionName %>, // mainFunction that need to invoke.
106
- "<%= queueName %>", // queue name that need to retry or send to dlq
107
- passOnProperties, // all parameters that mainFunction needed.
108
- );
109
- record._izContext.logger.debug('after recordPromise in handler');
110
- recordPromises.push(recordPromise); // push promise to recordPromises
111
- }))
76
+ await Promise.all(
77
+ event.Records.map(async record => {
78
+ // promise.all for map() function
79
+
80
+ let passOnProperties = [];
81
+ record = recordHandlerSharedLib.reformatDsqMessage(
82
+ record._izContext,
83
+ record
84
+ );
85
+ record._izContext.logger.debug('record ReceiveMsgOutHdrSqs', record);
86
+
87
+ //validate message (and MessageAttributes)
88
+ await recordHandlerSharedLib.validateRecord(
89
+ record, // one record will send to mainFunction
90
+ '<%= queueName %>', // queue name that need to retry or send to dlq
91
+ perRecordsValidatorSchema, // schema for record.Message
92
+ messageAttributeValidatorSchema
93
+ );
94
+
95
+ // add argument (to invoke lambda) to passOnProperties[]
96
+ passOnProperties.push(record.body.Message);
97
+ passOnProperties.push(
98
+ callingFlowSharedLib.addCallingFlowToPassOnProperties(
99
+ record.body.Message
100
+ )
101
+ );
102
+ //(<additionalParams>)
103
+ //(</additionalParams>)
104
+ record._izContext.logger.debug(
105
+ 'passOnProperties in handler',
106
+ passOnProperties
107
+ );
108
+
109
+ // call recordHandlerSharedLib.recordHandler with 3 parameters and return promise(resolve)
110
+ let recordPromise = recordHandlerSharedLib.recordHandler(
111
+ record, // one record will send to mainFunction
112
+ <%= functionName %>, // mainFunction that need to invoke.
113
+ '<%= queueName %>', // queue name that need to retry or send to dlq
114
+ passOnProperties // all parameters that mainFunction needed.
115
+ );
116
+ record._izContext.logger.debug('after recordPromise in handler');
117
+ recordPromises.push(recordPromise); // push promise to recordPromises
118
+ })
119
+ );
112
120
 
113
121
  Logger.debug('before Promise.all(recordPromises) in handler');
114
122
  try {
115
123
  // --- main await all promises
116
124
  await Promise.all(recordPromises); // await all promises
117
125
 
118
- return event.Records // return all for local testing
119
-
126
+ return event.Records; // return all for local testing
120
127
  } catch {
121
- Logger.debug('Promise.all(recordPromises) in handler threw error (at least one record did no resolve)');
128
+ Logger.debug(
129
+ 'Promise.all(recordPromises) in handler threw error (at least one record did no resolve)'
130
+ );
122
131
  }
123
132
  Logger.debug('after Promise.all(recordPromises) in handler');
124
-
125
133
  } catch (err) {
126
134
  Logger.error('Unhandled Error, LambdaFunctionHdrSqs: ', err);
127
- throw (err);
135
+ throw err;
128
136
  }
129
137
  });