@izara_project/izara-market-library-service-schemas 1.0.54 → 1.0.56

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 (71) hide show
  1. package/package.json +1 -1
  2. package/src/GenerateCodeLibs/src/GenerateCodeLibs.js +1 -0
  3. package/src/reStructure/TemplateData/Auth/generateAuthYml/template.ejs +15 -16
  4. package/src/reStructure/TemplateData/EndpointPerService/handler/api/template.ejs +13 -14
  5. package/src/reStructure/TemplateData/EndpointPerService/handler/dsq/template.ejs +14 -11
  6. package/src/reStructure/TemplateData/EndpointPerService/handler/inv/template.ejs +9 -11
  7. package/src/reStructure/TemplateData/EndpointPerService/handler/sqs/template.ejs +16 -13
  8. package/src/reStructure/TemplateData/EndpointPerService/mainFunction/create/template.ejs +41 -17
  9. package/src/reStructure/TemplateData/EndpointPerService/mainFunction/delete/template.ejs +39 -21
  10. package/src/reStructure/TemplateData/EndpointPerService/mainFunction/get/template.ejs +43 -8
  11. package/src/reStructure/TemplateData/EndpointPerService/mainFunction/update/template.ejs +29 -9
  12. package/src/reStructure/TemplateData/EndpointPerService/yaml/data.js +21 -15
  13. package/src/reStructure/TemplateData/EndpointPerService/yaml/template.ejs +2 -2
  14. package/src/reStructure/TemplateData/flowSchema/components/upload/confirmReserved/handler/template.ejs +2 -0
  15. package/src/reStructure/TemplateData/flowSchema/components/upload/confirmReserved/mainFunction/template.ejs +14 -10
  16. package/src/reStructure/TemplateData/flowSchema/components/upload/createObject/mainFunction/data.js +21 -4
  17. package/src/reStructure/TemplateData/flowSchema/components/upload/createObject/mainFunction/template.ejs +8 -14
  18. package/src/reStructure/TemplateData/flowSchema/components/upload/preSignUrl/createPresignUrl/handler/template.ejs +2 -0
  19. package/src/reStructure/TemplateData/flowSchema/components/upload/preSignUrl/createPresignUrl/mainFunction/template.ejs +45 -55
  20. package/src/reStructure/TemplateData/flowSchema/components/upload/preSignUrl/reservedLimit/handler/template.ejs +5 -3
  21. package/src/reStructure/TemplateData/flowSchema/components/upload/preSignUrl/reservedLimit/mainFunction/data.js +4 -3
  22. package/src/reStructure/TemplateData/flowSchema/components/upload/preSignUrl/reservedLimit/mainFunction/template.ejs +35 -40
  23. package/src/reStructure/TemplateData/flowSchema/components/upload/processFile/handler/handlerS3/template.ejs +3 -17
  24. package/src/reStructure/TemplateData/flowSchema/components/upload/processFile/mainFunction/ProcessCsvtemplate.ejs +391 -0
  25. package/src/reStructure/TemplateData/flowSchema/components/upload/processFile/mainFunction/template.ejs +9 -317
  26. package/src/reStructure/TemplateData/flowSchema/components/upload/relate/libs/template.ejs +5 -5
  27. package/src/reStructure/TemplateData/flowSchema/externalTopic/Process/functionYaml/data.js +1 -1
  28. package/src/reStructure/TemplateData/flowSchema/externalTopic/Process/handler/data.js +1 -1
  29. package/src/reStructure/TemplateData/flowSchema/externalTopic/sns-in-sqs/data.js +8 -8
  30. package/src/reStructure/TemplateData/flowSchema/flowSchemaMainFunction/data.js +1 -1
  31. package/src/reStructure/TemplateData/flowSchema/flowSchemaMainFunction/template.ejs +5 -3
  32. package/src/reStructure/TemplateData/flowSchema/flowSchemaOwnTopic/EndpointComponent/functionYaml/data.js +1 -1
  33. package/src/reStructure/TemplateData/flowSchema/flowSchemaOwnTopic/FlowSchemaComponent/handler/template.ejs +28 -5
  34. package/src/reStructure/TemplateData/flowSchema/flowSchemaOwnTopic/sns-in/data.js +11 -11
  35. package/src/reStructure/TemplateData/flowSchema/flowStep/handler/template.ejs +1 -0
  36. package/src/reStructure/TemplateData/flowSchema/generateTemplateData.js +28 -26
  37. package/src/reStructure/TemplateData/flowSchema/lambdaSync/Api/functionYaml/data.js +10 -1
  38. package/src/reStructure/TemplateData/flowSchema/lambdaSync/Api/functionYaml/template.ejs +2 -2
  39. package/src/reStructure/TemplateData/flowSchema/register/complete/handler/template.ejs +3 -0
  40. package/src/reStructure/TemplateData/flowSchema/register/complete/mainFunction/template.ejs +1 -1
  41. package/src/reStructure/TemplateData/flowSchema/templateByStatusType/storedCacheTemplate.ejs +3 -0
  42. package/src/reStructure/TemplateData/flowSchema/templateByStatusType/triggerCacheTemplate.ejs +3 -0
  43. package/src/reStructure/TemplateData/flowSchema/webSocketComplete/handler/template.ejs +4 -0
  44. package/src/reStructure/TemplateData/flowSchema/webSocketComplete/mainFunction/template.ejs +36 -7
  45. package/src/reStructure/TemplateData/perActionEndpoint/handler/api/template.ejs +2 -2
  46. package/src/reStructure/TemplateData/perActionEndpoint/handler/dsq/template.ejs +2 -2
  47. package/src/reStructure/TemplateData/perActionEndpoint/handler/sqs/template.ejs +2 -2
  48. package/src/reStructure/TemplateData/perActionEndpoint/yaml/api/data.js +10 -2
  49. package/src/reStructure/TemplateData/perActionEndpoint/yaml/api/template.ejs +1 -1
  50. package/src/reStructure/TemplateData/propertyValueSchema/relationshipPropertyValueSchema/data.js +1 -2
  51. package/src/reStructure/TemplateData/relationshipPerAction/create/action/handler/api/template.ejs +2 -2
  52. package/src/reStructure/TemplateData/relationshipPerAction/create/action/handler/dsq/template.ejs +2 -2
  53. package/src/reStructure/TemplateData/relationshipPerAction/create/action/handler/sqs/template.ejs +2 -2
  54. package/src/reStructure/TemplateData/relationshipPerAction/create/action/mainFunction/template.ejs +9 -1
  55. package/src/reStructure/TemplateData/relationshipPerAction/create/complete/handler/sqs/template.ejs +2 -1
  56. package/src/reStructure/TemplateData/relationshipPerAction/create/complete/mainFunction/template.ejs +6 -0
  57. package/src/reStructure/TemplateData/relationshipPerAction/delete/action/handler/api/template.ejs +2 -2
  58. package/src/reStructure/TemplateData/relationshipPerAction/delete/action/handler/dsq/template.ejs +2 -2
  59. package/src/reStructure/TemplateData/relationshipPerAction/delete/action/handler/sqs/template.ejs +2 -2
  60. package/src/reStructure/TemplateData/relationshipPerAction/get/action/handler/api/template.ejs +23 -23
  61. package/src/reStructure/TemplateData/relationshipPerAction/get/action/handler/dsq/template.ejs +18 -16
  62. package/src/reStructure/TemplateData/relationshipPerAction/get/action/handler/inv/template.ejs +14 -12
  63. package/src/reStructure/TemplateData/relationshipPerAction/get/action/handler/sqs/template.ejs +21 -19
  64. package/src/reStructure/TemplateData/relationshipPerAction/get/action/mainFunction/template.ejs +2 -0
  65. package/src/reStructure/TemplateData/relationshipPerAction/get/complete/handler/sqs/template.ejs +3 -1
  66. package/src/reStructure/TemplateData/relationshipPerAction/update/action/handler/api/template.ejs +24 -22
  67. package/src/reStructure/TemplateData/relationshipPerAction/update/action/handler/dsq/template.ejs +21 -17
  68. package/src/reStructure/TemplateData/relationshipPerAction/update/action/handler/inv/template.ejs +21 -19
  69. package/src/reStructure/TemplateData/relationshipPerAction/update/action/handler/sqs/template.ejs +20 -20
  70. package/src/reStructure/TemplateData/relationshipPerAction/update/action/mainFunction/template.ejs +1 -1
  71. package/src/reStructure/TemplateData/relationshipPerAction/update/complete/handler/sqs/template.ejs +1 -1
@@ -20,7 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
20
20
  const callingFlowSharedLib = require('@izara_project/izara-core-library-calling-flow');
21
21
  const lambdaSharedLib = require('@izara_project/izara-core-library-lambda');
22
22
  const snsSharedLib = require('@izara_project/izara-core-library-sns');
23
-
23
+ const asyncFlowSharedLib = require("@izara_project/izara-core-library-asynchronous-flow")
24
24
  const externalRequest = require('@izara_project/izara-core-library-external-request')
25
25
  const sns = externalRequest.sns;
26
26
  const lambda = externalRequest.lambda
@@ -45,6 +45,8 @@ module.exports.reservedLimit = async (
45
45
  _izContext,
46
46
  objType,
47
47
  objInstanceFull,
48
+ //(<additionalProperties>)
49
+ //(</additionalProperties>)
48
50
  callingFlowConfig = {}
49
51
  ) => {
50
52
 
@@ -55,19 +57,9 @@ module.exports.reservedLimit = async (
55
57
  objInstanceFull,
56
58
  callingFlowConfig
57
59
  });
58
- const {
59
- userId,
60
- submittedByUserId,
61
- importConfigId,
62
- importBatchStatus,
63
- importType,
64
- fileName,
65
- fileSize,
66
- startTime
67
- } = objInstanceFull.fields
68
- const {
69
- importBatchId
70
- } = objInstanceFull.identifiers
60
+
61
+ //(<beforeReserved>)
62
+ //(</beforeReserved>)
71
63
 
72
64
  let connectionId = _izContext.correlationIds.get(coreConsts.CONNECTION_ID)
73
65
  //* hard code
@@ -75,23 +67,16 @@ module.exports.reservedLimit = async (
75
67
 
76
68
  // get static
77
69
  let payloadBeforeInvokeStaticLimit = {
78
- userId: userId,
79
- limitTag: consts.LIMIT_TAG.uploadData,
80
- value: fileSize,
81
- rbacType: consts.RBAC_TYPE.appLevel,
82
- targetUserId: submittedByUserId,
83
- callingFlow: "BasDev",
84
- callingFlowProperties: {}
70
+ userId: _izContext.correlationIds.get(coreConsts.BASE_USER_ID),
71
+ targetUserId: _izContext.correlationIds.get(coreConsts.TARGET_ID),
72
+ //(<additionalPropertiesStaticLimit>)
73
+ //(</additionalPropertiesStaticLimit>)
85
74
  }
86
75
 
87
- let res = await importDataLibs.getDataFromInvokeFunctionWithCache(_izContext,
76
+ let res = await importDataLibs.processStaticLimit(_izContext,
88
77
  await lambdaSharedLib.lambdaFunctionName(_izContext, "StaticLimitProcessHdrInv", consts.EXTERNAL_SERVICE_NAME.accountLimits),
89
78
  payloadBeforeInvokeStaticLimit
90
79
  )
91
- // let res = await lambda.invokeSync(_izContext,
92
- // await lambdaSharedLib.lambdaFunctionName(_izContext, "StaticLimitProcessHdrInv", consts.EXTERNAL_SERVICE_NAME.accountLimits),
93
- // payloadBeforeInvokeStaticLimit
94
- // )
95
80
 
96
81
  _izContext.logger.debug("res::", res);
97
82
 
@@ -99,27 +84,20 @@ module.exports.reservedLimit = async (
99
84
  importDataLibs.postToConnection({ message: "static limit over" }, connectionId)
100
85
  }
101
86
 
102
-
103
87
  // reservedDynamic
104
88
 
105
89
  let sendMsgReservedDynamic = {
106
- userId: userId,
107
- limitTag: consts.LIMIT_TAG.uploadData,
108
- value: fileSize,
109
- rbacType: consts.RBAC_TYPE.appLevel,
110
- usedTimestamp: startTime,
111
- targetUserId: submittedByUserId
90
+ userId: _izContext.correlationIds.get(coreConsts.BASE_USER_ID),
91
+ targetUserId: _izContext.correlationIds.get(coreConsts.TARGET_ID)
92
+ //(<additionalPropertiesReservedDynamic>)
93
+ //(</additionalPropertiesReservedDynamic>)
112
94
  }
113
95
 
114
96
  const reservedDataId = `${consts.ACCOUNTLIMIT_TAG.reservedDynamic}_${userId}_${consts.LIMIT_TAG.uploadData}`
115
97
 
116
98
  let callingFlowProperties = {
117
- identifier: {
118
- servicePendingId: importBatchId,
119
- reservedDataId: reservedDataId
120
- },
121
- importConfigId: importConfigId,
122
- objType: objType
99
+ //(<baseCallingFlowProperties>)
100
+ //(</baseCallingFlowProperties>)
123
101
  }
124
102
 
125
103
  _izContext.logger.debug("callingFlowProperties", callingFlowProperties);
@@ -130,7 +108,9 @@ module.exports.reservedLimit = async (
130
108
  callingFlowSharedLib.addParentCallingFlowConfig(
131
109
  callingFlowConfig,
132
110
  callingFlowSharedLib.createCallingFlowConfig(
111
+ //(<callingFlowProperties>)
133
112
  await lambdaSharedLib.lambdaFunctionName(_izContext, "ReservedLimitCompleteHdrSqs"),
113
+ //(</callingFlowProperties>)
134
114
  callingFlowProperties
135
115
  )
136
116
  )
@@ -139,15 +119,30 @@ module.exports.reservedLimit = async (
139
119
  sendMsgReservedDynamic = callingFlowSharedLib.addCallingFlowToSnsRequestMessageObject(
140
120
  sendMsgReservedDynamic,
141
121
  callingFlowSharedLib.createCallingFlowConfig(
122
+ //(<callingFlowProperties>)
142
123
  await lambdaSharedLib.lambdaFunctionName(_izContext, "ReservedLimitCompleteHdrSqs"),
124
+ //(</callingFlowProperties>)
143
125
  callingFlowProperties
144
126
  )
145
127
  );
146
128
  }
147
129
 
130
+ // create awaitingSteps
131
+ await asyncFlowSharedLib.createAwaitingStep(_izContext,
132
+ asyncFlowSharedLib.createAwaitingStepId(
133
+ //(<createAwaitStepId>)
134
+ //(</createAwaitStepId>)
135
+ ),
136
+ asyncFlowSharedLib.createPendingStepId(
137
+ //(<createPendingStepId>)
138
+ //(</createPendingStepId>)
139
+ )
140
+ )
141
+
142
+
148
143
  let sendMessageToReservedDynamic = {
149
144
  Message: JSON.stringify(sendMsgReservedDynamic),
150
- TopicArn: await snsSharedLib.snsTopicArn(_izContext, "InReservedDynamicUsage", consts.EXTERNAL_SERVICE_NAME.accountLimits)
145
+ TopicArn: await snsSharedLib.snsTopicArn(_izContext, "ReservedDynamicUsage_In", consts.EXTERNAL_SERVICE_NAME.accountLimits)
151
146
  };
152
147
  _izContext.logger.debug("sendMessageToReservedDynamic", sendMessageToReservedDynamic);
153
148
  await sns.publishAsync(_izContext, sendMessageToReservedDynamic);
@@ -37,6 +37,8 @@ module.exports.main = middleware.wrap(async (event, context, callback) => {
37
37
  event._izContext, // correlationId/logger/integrationTestDetail/uniqueRequestId/userId
38
38
  event.Records[0].s3.bucket,
39
39
  event.Records[0].s3.object,
40
+ //(<additionalProperties>)
41
+ //(</additionalProperties>)
40
42
  );
41
43
 
42
44
  if (processImageResponse instanceof Error) {
@@ -49,20 +51,4 @@ module.exports.main = middleware.wrap(async (event, context, callback) => {
49
51
  event._izContext.logger.error('Error, processAfterUploadHdrS3: ', err);
50
52
  return (izara.response.failure(err));
51
53
  }
52
- });
53
-
54
- //
55
- // s3: {
56
- // s3SchemaVersion: '1.0',
57
- // configurationId: 'ImageStandardTestProcessImageFileHdrS3-68a737133cefb25bff959852b8f04754',
58
- // bucket: {
59
- // name: 'image-test-importdata',
60
- // ownerIdentity: { principalId: 'A3CDE97VJKTOJ0' },
61
- // arn: 'arn:aws:s3:::image-test-importdata'
62
- // },
63
- // object: {
64
- // key: 'image_8733685.png',
65
- // size: 2974746,
66
- // eTag: '93e940d895c26ef90b7542da54d61898',
67
- // sequencer: '00650D4EEAD0F63EB5'
68
- // }
54
+ });
@@ -0,0 +1,391 @@
1
+ /*
2
+ Copyright (C) 2021 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 callingFlowSharedLib = require('@izara_project/izara-core-library-calling-flow');
21
+ const snsSharedLib = require('@izara_project/izara-core-library-sns');
22
+ const dynamodbSharedLib = require('@izara_project/izara-core-library-dynamodb');
23
+ const lambdaSharedLib = require('@izara_project/izara-core-library-lambda');
24
+
25
+ const externalRequest = require('@izara_project/izara-core-library-external-request')
26
+ const lambda = externalRequest.lambda;
27
+ const sns = externalRequest.sns;
28
+ const NoRetryError = require('@izara_project/izara-core-library-core').NoRetryError;
29
+ const utils = require('@izara_project/izara-market-library-service-schemas/src/MainLibs/src/Utils');
30
+
31
+ const { S3Client, GetObjectCommand, DeleteObjectCommand, PutObjectCommand, CopyObjectCommand } = require('@aws-sdk/client-s3');
32
+ const s3 = new S3Client({ region: 'us-east-2' });
33
+
34
+ const hash = require('@izara_project/izara-shared-core').objectHash;
35
+ const Readable = require('stream');
36
+ const { isValidCsv, getNestObject } = require('../../../../libs/source/GenerateCodeLibs');
37
+ const consts = require('@izara_project/izara-market-library-service-schemas/src/GenerateCodeLibs/src/Consts')
38
+ const coreConsts = require('@izara_project/izara-core-library-core/src/Consts');
39
+
40
+
41
+ /**
42
+ * description of function.
43
+ * @param {Object} _izContext
44
+ * @param {CorrelationIds} _izContext.correlationIds - property of _izContext
45
+ * @param {Logger} _izContext.logger - property of _izContext
46
+ * @param {Logger} _izContext.logger.debug - property of _izContext
47
+ * @param {Object} bucket - bucket's data
48
+ * @param {Array} field - image's data
49
+ * @param {Array} importBy - 'webSocket' || 'importData'
50
+ * @param {Array} imageImportId - from importData
51
+ * @param {Array} realImageSize - from importData
52
+ * @param {Array} passBackProperties - from importData
53
+ *
54
+ * @returns {object} returnResponse
55
+ */
56
+
57
+ module.exports.processAfterUpload = async (
58
+ _izContext,
59
+ bucket,
60
+ field,
61
+ importBy = '',
62
+ importBatchId = '',
63
+ realImageSize = 0,
64
+ passBackProperties = {}
65
+ ) => {
66
+
67
+ try {
68
+
69
+ _izContext.logger.debug('----- processImageFile event params ----- ', {
70
+ bucket: bucket,
71
+ field: field,
72
+ importBy: importBy,
73
+ importBatchId: importBatchId,
74
+ realImageSize: realImageSize,
75
+ passBackProperties: passBackProperties
76
+ })
77
+
78
+ let errorsFound = [];
79
+
80
+ const objectS3Param = {
81
+ Bucket: bucket.name,
82
+ Key: field.key
83
+ }
84
+ _izContext.logger.debug("params before getObjectParam::", objectS3Param)
85
+
86
+ const fileObjectS3 = await s3.send(new GetObjectCommand(objectS3Param));
87
+ _izContext.logger.debug("get fileObjectS3", fileObjectS3);
88
+
89
+ importBatchId = fileObjectS3.Metadata.importbatchid
90
+
91
+ let getImportBatchMain = await lambda.invokeSync(_izContext,
92
+ await lambdaSharedLib.lambdaFunctionName(_izContext, "ImportBatchMainGetHdrInv"),
93
+ {
94
+ identifiers: {
95
+ importBatchId: importBatchId
96
+ }
97
+ }
98
+ )
99
+
100
+ _izContext.logger.debug("getImportBatchMain", getImportBatchMain)
101
+ _izContext.correlationIds.set(coreConsts.BASE_USER_ID, getImportBatchMain.fields.userId);
102
+ _izContext.correlationIds.set(coreConsts.TARGET_ID, getImportBatchMain.fields.submittedByUserId);
103
+
104
+ let userId = _izContext.correlationIds.get(coreConsts.BASE_USER_ID);
105
+ let targetId = _izContext.correlationIds.get(coreConsts.TARGET_ID);
106
+
107
+ if (Date.now() > getImportBatchMain.fields.expiryTimestamp) {
108
+ errorsFound.push("url expired can't upload")
109
+ let sendMessageUpdateUrlExpired = {
110
+ objInstanceFull: {
111
+ identifiers: {
112
+ importBatchId: importBatchId
113
+ },
114
+ fields: {
115
+ importBatchStatus: "urlExpired",
116
+ errorsFound: "url expired can't upload",
117
+ processingError: true
118
+ }
119
+ }
120
+ }
121
+
122
+ let sendMessageToInImportBatchUpdate = {
123
+ Message: JSON.stringify(sendMessageUpdateUrlExpired),
124
+ TopicArn: await snsSharedLib.snsTopicArn(_izContext, "InImportBatchMainUpdate")
125
+ };
126
+
127
+ _izContext.logger.debug("message param before send to updateImportBatchMain", sendMessageToInImportBatchUpdate);
128
+ await sns.publishAsync(_izContext, sendMessageToInImportBatchUpdate);
129
+
130
+ let deleteFileObjectS3 = await s3.send(new DeleteObjectCommand(objectS3Param));
131
+ _izContext.logger.debug("Delete object response: ", deleteFileObjectS3)
132
+
133
+ return
134
+ }
135
+
136
+ if (fileObjectS3) {
137
+ if (!importBatchId) {
138
+ errorsFound.push("cant't get importBatchId from S3 uploaded file");
139
+ }
140
+ } else {
141
+ errorsFound.push("cant't get fileObjectS3 from S3 uploaded file");
142
+ };
143
+
144
+ let fileS3Body = fileObjectS3.Body;
145
+ _izContext.logger.debug("fileS3Body", fileS3Body);
146
+
147
+ let fileS3Buffer = null;
148
+
149
+ if (fileS3Body instanceof Readable) {
150
+ fileS3Buffer = Buffer.concat(await fileS3Body.toArray());
151
+ _izContext.logger.debug("fileS3Buffer", fileS3Buffer)
152
+ } else {
153
+ errorsFound.push('unknown object stream type');
154
+ }
155
+
156
+ // check file type
157
+ // can be strict in function.yml that only trigger if it specific type of file
158
+ const srcKey = field.key;
159
+ const importFieldType = srcKey.match(/\.([^.]*)$/);
160
+ _izContext.logger.debug("importFieldType", importFieldType);
161
+ if (!importFieldType) {
162
+ throw new NoRetryError("error fileType")
163
+ };
164
+
165
+ const importKeyType = utils.firstLetterLowerCase(importFieldType[1]);
166
+ _izContext.logger.debug("importKeyType", importKeyType);
167
+
168
+ if (importKeyType === "csv") {
169
+ // check valid CSV file
170
+ let [validCsv, errorCheckCsv] = await isValidCsv(_izContext, fileObjectS3)
171
+ _izContext.logger.debug("validCsv check::", { validCsv, errorsFound })
172
+ if (errorCheckCsv.length) {
173
+ errorsFound.push(errorCheckCsv[0])
174
+ }
175
+
176
+ // check file size
177
+ let reservedFileSize = getImportBatchMain.fields.fileSize;
178
+ _izContext.logger.debug("initialFileSize::", reservedFileSize)
179
+ let s3FileSize = fileObjectS3.ContentLength / 1048576 // convert back to byte unit
180
+ _izContext.logger.debug("s3FileSize in MiB::", s3FileSize)
181
+
182
+ if (s3FileSize < 0.5) { // round up to 1
183
+ s3FileSize = 1
184
+ }
185
+
186
+ let reservedData = await dynamodbSharedLib.getItem(_izContext,
187
+ await dynamodbSharedLib.tableName(_izContext, "ReservedDataMain"),
188
+ {
189
+ servicePendingId: importBatchId,
190
+ reservedDataId: `${consts.ACCOUNTLIMIT_TAG.reservedDynamic}_${getImportBatchMain.fields.userId}_${consts.LIMIT_TAG.uploadData}`
191
+ }
192
+ )
193
+ _izContext.logger.debug("reservedData::", reservedData);
194
+
195
+ let sizeDiff;
196
+
197
+ if (s3FileSize > reservedFileSize) {
198
+ sizeDiff = s3FileSize - reservedFileSize;
199
+
200
+
201
+ if (sizeDiff < 1) {
202
+
203
+ // send message to confirmUsage
204
+ let confirmUsageMessage = {
205
+ userId: userId,
206
+ limitTag: consts.LIMIT_TAG.uploadData,
207
+ uniqueIdentify: reservedData.uniqueIdentify,
208
+ }
209
+
210
+ let sendMessageToConfirmUsage = {
211
+ Message: JSON.stringify(confirmUsageMessage),
212
+ TopicArn: await snsSharedLib.snsTopicArn(_izContext, 'InConfirmUsage', consts.EXTERNAL_SERVICE_NAME.accountLimits)
213
+ };
214
+ _izContext.logger.debug("sendMessageToConfirmUsage", sendMessageToConfirmUsage);
215
+ await sns.publishAsync(_izContext, sendMessageToConfirmUsage);
216
+
217
+ let sendMessageToUpdateImportBatchStatus = {
218
+ objInstanceFull: {
219
+ identifiers: {
220
+ importBatchId: servicePendingId
221
+ },
222
+ fields: {
223
+ importBatchStatus: 'uploadComplete'
224
+ }
225
+ }
226
+ }
227
+
228
+ let sendMessageToUpdateImportBatch = {
229
+ Message: JSON.stringify(sendMessageToUpdateImportBatchStatus),
230
+ TopicArn: await snsSharedLib.snsTopicArn(_izContext, `InImportBatchMainUpdate`)
231
+ };
232
+ _izContext.logger.debug("sendMessageToUpdateImportBatch", sendMessageToUpdateImportBatch);
233
+ await sns.publishAsync(_izContext, sendMessageToUpdateImportBatch);
234
+
235
+ }
236
+ } else if (s3FileSize < reservedFileSize) {
237
+ sizeDiff = reservedFileSize - s3FileSize;
238
+
239
+ if (sizeDiff < 1) {
240
+ let confirmUsageMessage = {
241
+ userId: userId,
242
+ limitTag: consts.LIMIT_TAG.uploadData,
243
+ uniqueIdentify: reservedData.uniqueIdentify,
244
+ }
245
+
246
+ let sendMessageToConfirmUsage = {
247
+ Message: JSON.stringify(confirmUsageMessage),
248
+ TopicArn: await snsSharedLib.snsTopicArn(_izContext, 'InConfirmUsage', consts.EXTERNAL_SERVICE_NAME.accountLimits)
249
+ };
250
+ _izContext.logger.debug("sendMessageToConfirmUsage", sendMessageToConfirmUsage);
251
+ await sns.publishAsync(_izContext, sendMessageToConfirmUsage);
252
+
253
+ let sendMessageToUpdateImportBatchStatus = {
254
+ objInstanceFull: {
255
+ identifiers: {
256
+ importBatchId: servicePendingId
257
+ },
258
+ fields: {
259
+ importBatchStatus: 'uploadComplete'
260
+ }
261
+ }
262
+ }
263
+
264
+ let sendMessageToUpdateImportBatch = {
265
+ Message: JSON.stringify(sendMessageToUpdateImportBatchStatus),
266
+ TopicArn: await snsSharedLib.snsTopicArn(_izContext, `InImportBatchMainUpdate`)
267
+ };
268
+ _izContext.logger.debug("sendMessageToUpdateImportBatch", sendMessageToUpdateImportBatch);
269
+ await sns.publishAsync(_izContext, sendMessageToUpdateImportBatch);
270
+
271
+ } else {
272
+ let realFileSize = Math.round(s3FileSize);
273
+ // cancel reserved -> new reserved -> confirm
274
+ // cancel
275
+ let sendMessageToCancelReserved = {
276
+ Message: JSON.stringify({
277
+ userId: userId,
278
+ limitTag: consts.LIMIT_TAG.uploadData,
279
+ uniqueIdentify: reservedData.uniqueIdentify
280
+ }),
281
+ TopicArn: await snsSharedLib.snsTopicArn(_izContext, 'InCancelUsage', consts.EXTERNAL_SERVICE_NAME.accountLimits)
282
+ };
283
+ _izContext.logger.debug("sendMessageToCancelReserved", sendMessageToCancelReserved);
284
+ await sns.publishAsync(_izContext, sendMessageToCancelReserved);
285
+
286
+ // new reserved
287
+ let originTimestamp = Date.now();
288
+ let messageObjReserved = {
289
+ userId: userId,
290
+ limitTag: consts.LIMIT_TAG.uploadData,
291
+ value: realFileSize,
292
+ rbacType: consts.RBAC_TYPE.appLevel,
293
+ usedTimestamp: originTimestamp,
294
+ targetUserId: targetId
295
+ }
296
+
297
+ let callingFlowProperties = {
298
+ identifier: {
299
+ servicePendingId: importBatchId,
300
+ reservedDataId: `${consts.ACCOUNTLIMIT_TAG.reservedDynamic}_${getImportBatchMain.fields.userId}_${consts.LIMIT_TAG.uploadData}`,
301
+ userId: getImportBatchMain.fields.userId,
302
+ realSize: realFileSize
303
+ }
304
+ }
305
+
306
+ messageObjReserved = callingFlowSharedLib.addCallingFlowToSnsRequestMessageObject(
307
+ messageObjReserved,
308
+ callingFlowSharedLib.createCallingFlowConfig(
309
+ await lambdaSharedLib.lambdaFunctionName(_izContext, "ConfirmNewReservedHdrSqs"),
310
+ callingFlowProperties
311
+ )
312
+ )
313
+ _izContext.logger.debug('after add callingFlow: ', messageObjReserved)
314
+
315
+ let sendMessageToReservedDynamic = {
316
+ Message: JSON.stringify(messageObjReserved),
317
+ TopicArn: await snsSharedLib.snsTopicArn(_izContext, 'InReservedDynamicUsage', consts.EXTERNAL_SERVICE_NAME.accountLimits)
318
+ };
319
+
320
+ _izContext.logger.debug("sendMessageToReservedDynamic", sendMessageToReservedDynamic);
321
+ await sns.publishAsync(_izContext, sendMessageToReservedDynamic);
322
+ };
323
+ } else if (s3FileSize === reservedFileSize) {
324
+ let confirmUsageMessage = {
325
+ userId: getImportBatchMain.fields.userId,
326
+ limitTag: consts.LIMIT_TAG.uploadData,
327
+ uniqueIdentify: reservedData.uniqueIdentify,
328
+ }
329
+
330
+ let sendMessageToConfirmUsage = {
331
+ Message: JSON.stringify(confirmUsageMessage),
332
+ TopicArn: await snsSharedLib.snsTopicArn(_izContext, 'InConfirmUsage', consts.EXTERNAL_SERVICE_NAME.accountLimits)
333
+ };
334
+ _izContext.logger.debug("sendMessageToConfirmUsage", sendMessageToConfirmUsage);
335
+ await sns.publishAsync(_izContext, sendMessageToConfirmUsage);
336
+
337
+ let sendMessageToUpdateImportBatchStatus = {
338
+ objInstanceFull: {
339
+ identifiers: {
340
+ importBatchId: servicePendingId
341
+ },
342
+ fields: {
343
+ importBatchStatus: 'uploadComplete'
344
+ }
345
+ }
346
+ }
347
+
348
+ let sendMessageToUpdateImportBatch = {
349
+ Message: JSON.stringify(sendMessageToUpdateImportBatchStatus),
350
+ TopicArn: await snsSharedLib.snsTopicArn(_izContext, `InImportBatchMainUpdate`)
351
+ };
352
+ _izContext.logger.debug("sendMessageToUpdateImportBatch", sendMessageToUpdateImportBatch);
353
+ await sns.publishAsync(_izContext, sendMessageToUpdateImportBatch);
354
+
355
+ }
356
+
357
+ } else {
358
+ errorsFound.push("error importKeyType")
359
+ }
360
+
361
+ if (errorsFound.length) { // if error send message to update error in importBatchMain
362
+ let updateImportBatchMainError = {
363
+ objInstanceFull: {
364
+ identifiers: {
365
+ importBatchId: importBatchId
366
+ },
367
+ fields: {
368
+ importBatchStatus: "errorToUpload",
369
+ errorsFound: errorsFound,
370
+ processingError: true
371
+ }
372
+ }
373
+ }
374
+
375
+ let sendMessageToUpdateImportBatchMain = {
376
+ Message: JSON.stringify(updateImportBatchMainError),
377
+ TopicArn: await snsSharedLib.snsTopicArn(_izContext, "InImportBatchMainUpdate")
378
+ };
379
+ _izContext.logger.debug('sendMessageToUpdateImportBatchMain::', sendMessageToUpdateImportBatchMain);
380
+ await sns.publishAsync(_izContext, sendMessageToUpdateImportBatchMain);
381
+
382
+ let deleteFileObjectS3 = await s3.send(new DeleteObjectCommand(objectS3Param));
383
+ _izContext.logger.debug("Delete object response: ", deleteFileObjectS3)
384
+
385
+ }
386
+
387
+ } catch (err) {
388
+ _izContext.logger.error('error ProcessImageAfterUploadMain: ', err)
389
+ throw (err)
390
+ }
391
+ }