vtlab-generic-functions 1.0.38 → 1.0.40
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/lambdaClass/index.js +65 -35
- package/package.json +1 -1
package/lambdaClass/index.js
CHANGED
|
@@ -23,7 +23,7 @@ module.exports = class Lambda {
|
|
|
23
23
|
totalError: 0, totalSuccess: 0, error: [], success: [],
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
constructor({carrier, event, DEBUG = false}) {
|
|
26
|
+
constructor({ carrier, event, DEBUG = false }) {
|
|
27
27
|
this.DEBUG = false;
|
|
28
28
|
this.carrier = carrier;
|
|
29
29
|
if (event.body) {
|
|
@@ -57,18 +57,28 @@ module.exports = class Lambda {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
recoverFullPayload = async () => {
|
|
60
|
-
if (this.event?.s3PayloadPath) {
|
|
61
|
-
let contentResponse = await this.s3.download(this.event.s3PayloadPath);
|
|
60
|
+
if (this.event?.s3PayloadPath || this.event?.payloadS3Key) {
|
|
61
|
+
let contentResponse = await this.s3.download(this.event.s3PayloadPath || this.event.payloadS3Key);
|
|
62
62
|
this.jobs = JSON.parse(contentResponse.Body.toString());
|
|
63
63
|
} else {
|
|
64
64
|
this.jobs = this.event?.payload || [];
|
|
65
65
|
}
|
|
66
66
|
};
|
|
67
67
|
|
|
68
|
+
initialize = async () => {
|
|
69
|
+
try {
|
|
70
|
+
if(this.event?.s3PayloadPath || this.event?.payloadS3Key) {
|
|
71
|
+
await this.recoverFullPayload();
|
|
72
|
+
}
|
|
73
|
+
} catch (error) {
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
68
78
|
s3 = {
|
|
69
79
|
upload: async (data, path, options = {}) => {
|
|
70
80
|
return await s3bucket.uploadToS3(path, data, {
|
|
71
|
-
ContentEncoding: options.contentEncoding || "base64", ContentType: options.ContentType ||
|
|
81
|
+
ContentEncoding: options.contentEncoding || "base64", ContentType: options.ContentType || "image/png",
|
|
72
82
|
});
|
|
73
83
|
}, download: async (path) => {
|
|
74
84
|
return await s3bucket.downloadFileFromS3(path);
|
|
@@ -77,28 +87,37 @@ module.exports = class Lambda {
|
|
|
77
87
|
http = {
|
|
78
88
|
get: async (URL, params, headers = {}) => {
|
|
79
89
|
return await axiosRequest.get(URL, {
|
|
80
|
-
auth: {}, logId: this.logGroupName, headers: {...headers}, ...params
|
|
90
|
+
auth: {}, logId: this.logGroupName, headers: { ...headers }, ...params
|
|
81
91
|
});
|
|
82
|
-
},
|
|
92
|
+
},
|
|
93
|
+
delete: async (URL, params, headers) => {
|
|
83
94
|
return await axiosRequest.deletion(URL, {
|
|
84
|
-
auth: {}, logId: this.logGroupName, headers: {...headers}, ...params
|
|
95
|
+
auth: {}, logId: this.logGroupName, headers: { ...headers }, ...params
|
|
85
96
|
});
|
|
86
|
-
},
|
|
97
|
+
},
|
|
98
|
+
post: async (URL, params, headers) => {
|
|
87
99
|
return await axiosRequest.post(URL, {
|
|
88
|
-
auth: {}, logId: this.logGroupName, headers: {...headers}, ...params,
|
|
100
|
+
auth: {}, logId: this.logGroupName, headers: { ...headers }, ...params,
|
|
89
101
|
});
|
|
90
|
-
},
|
|
102
|
+
},
|
|
103
|
+
put: async (URL, params, headers) => {
|
|
91
104
|
return await axiosRequest.put(URL, {
|
|
92
|
-
auth: {}, logId: this.logGroupName, headers: {...headers}, ...params
|
|
105
|
+
auth: {}, logId: this.logGroupName, headers: { ...headers }, ...params
|
|
93
106
|
});
|
|
94
|
-
},
|
|
107
|
+
},
|
|
108
|
+
patch: async (URL, params, headers) => {
|
|
109
|
+
return await axiosRequest.patch(URL, {
|
|
110
|
+
auth: {}, logId: this.logGroupName, headers: { ...headers }, ...params
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
downloadFile: async (method, URL, params, s3Path) => {
|
|
95
114
|
let response = await axiosRequest[method](URL, {
|
|
96
|
-
auth: {}, logId: this.logGroupName, headers: {}, config: {responseType: 'arraybuffer'}, ...params
|
|
115
|
+
auth: {}, logId: this.logGroupName, headers: {}, config: { responseType: 'arraybuffer' }, ...params
|
|
97
116
|
});
|
|
98
117
|
let buff = Buffer.from(response.data, "binary");
|
|
99
118
|
let s3PathUploaded = await this.s3.upload(buff, s3Path);
|
|
100
119
|
return s3PathUploaded;
|
|
101
|
-
}, postForm: async (URL, {attachment, document}, _headers) => {
|
|
120
|
+
}, postForm: async (URL, { attachment, document }, _headers) => {
|
|
102
121
|
const formData = new FormData();
|
|
103
122
|
formData.append('document', JSON.stringify(document));
|
|
104
123
|
formData.append('attachment', attachment, "document.pdf");
|
|
@@ -125,25 +144,25 @@ module.exports = class Lambda {
|
|
|
125
144
|
await this.ftpConnection.connect(this.configFTP);
|
|
126
145
|
this.logMessage(`Correctly Connected To FTP`, true);
|
|
127
146
|
} catch (error) {
|
|
128
|
-
throw {message: "Error from FTP"};
|
|
147
|
+
throw { message: "Error from FTP" };
|
|
129
148
|
}
|
|
130
149
|
}, listFolder: async (folderAndRegexp) => {
|
|
131
|
-
if (!this.ftpConnection) throw {message: "No connection was stablished"};
|
|
150
|
+
if (!this.ftpConnection) throw { message: "No connection was stablished" };
|
|
132
151
|
let files = await this.ftpConnection.list(folderAndRegexp);
|
|
133
|
-
if (!files || files.length < 1) throw {message: "No files were found"};
|
|
152
|
+
if (!files || files.length < 1) throw { message: "No files were found" };
|
|
134
153
|
this.logMessage(`Files Found: ${files.map((f) => f.name)}`);
|
|
135
154
|
return files;
|
|
136
|
-
}, download: async ({folder, file}) => {
|
|
137
|
-
if (!this.ftpConnection) throw {message: "No connection was stablished"};
|
|
155
|
+
}, download: async ({ folder, file }) => {
|
|
156
|
+
if (!this.ftpConnection) throw { message: "No connection was stablished" };
|
|
138
157
|
let downloadedFile = await promiseFTPWrapper.downloadFromFTP(this.ftpConnection, `${folder}${file}`);
|
|
139
158
|
return downloadedFile;
|
|
140
|
-
}, putFile: async ({path, file}) => {
|
|
159
|
+
}, putFile: async ({ path, file }) => {
|
|
141
160
|
await promiseFTPWrapper.uploadFileToFTP(this.configFTP, file, path);
|
|
142
|
-
}, downloadFiles: async ({folder, _files, removeFiles = false}) => {
|
|
161
|
+
}, downloadFiles: async ({ folder, _files, removeFiles = false }) => {
|
|
143
162
|
const files = [..._files];
|
|
144
|
-
if (!this.ftpConnection) throw {message: "No connection was stablished"};
|
|
163
|
+
if (!this.ftpConnection) throw { message: "No connection was stablished" };
|
|
145
164
|
for (let i = 0; i < files.length; i++) {
|
|
146
|
-
let file = await this.ftp.download({folder, file: files[i].name});
|
|
165
|
+
let file = await this.ftp.download({ folder, file: files[i].name });
|
|
147
166
|
if (file) {
|
|
148
167
|
files[i].file = file;
|
|
149
168
|
if (removeFiles) await this.ftp.deleteFile(folder, files[i]);
|
|
@@ -153,7 +172,7 @@ module.exports = class Lambda {
|
|
|
153
172
|
}, deleteFile: async (folder, file) => {
|
|
154
173
|
await promiseFTPWrapper.deleteFileFromFTP(this.ftpConnection, `${folder}/${file}`);
|
|
155
174
|
}, closeConnection: async () => {
|
|
156
|
-
if (!this.ftpConnection) throw {message: "No connection was stablished"};
|
|
175
|
+
if (!this.ftpConnection) throw { message: "No connection was stablished" };
|
|
157
176
|
this.ftpConnection.end();
|
|
158
177
|
}, sortFiles: (filesList) => {
|
|
159
178
|
return filesList.sort((a, b) => {
|
|
@@ -168,7 +187,7 @@ module.exports = class Lambda {
|
|
|
168
187
|
return 0;
|
|
169
188
|
});
|
|
170
189
|
}, getJobMatchingFile: (file) => {
|
|
171
|
-
if (!file) throw {message: "No file was sent"};
|
|
190
|
+
if (!file) throw { message: "No file was sent" };
|
|
172
191
|
let jobFound = [...this.jobs].find((job) => {
|
|
173
192
|
return String(file.name).includes(utils.getValueOrNull(job, "externalCarrier.trackingId"));
|
|
174
193
|
});
|
|
@@ -189,11 +208,11 @@ module.exports = class Lambda {
|
|
|
189
208
|
};
|
|
190
209
|
|
|
191
210
|
logSendingToCarrier = async (payload) => {
|
|
192
|
-
await this.logMessage(`Sending to ${this.carrier} ${utils.dataToString({...payload}, "data").res}`, true);
|
|
211
|
+
await this.logMessage(`Sending to ${this.carrier} ${utils.dataToString({ ...payload }, "data").res}`, true);
|
|
193
212
|
};
|
|
194
213
|
|
|
195
214
|
logReceivedFromCarrier = async (payload, status) => {
|
|
196
|
-
await this.logMessage(`Received from ${this.carrier} (${status}) ${utils.dataToString({...payload}, "data").res}`, true);
|
|
215
|
+
await this.logMessage(`Received from ${this.carrier} (${status}) ${utils.dataToString({ ...payload }, "data").res}`, true);
|
|
197
216
|
};
|
|
198
217
|
|
|
199
218
|
addSuccessResult = (payload) => {
|
|
@@ -231,7 +250,7 @@ module.exports = class Lambda {
|
|
|
231
250
|
}));
|
|
232
251
|
};
|
|
233
252
|
|
|
234
|
-
logFinishProcess = async ({error} = {}) => {
|
|
253
|
+
logFinishProcess = async ({ error } = {}) => {
|
|
235
254
|
if (this.results.totalError || error) {
|
|
236
255
|
error = error && error.message ? error.message : error;
|
|
237
256
|
await this.logMessage("Finished Process with errors: " + (error || "") + " " + JSON.stringify(this.results));
|
|
@@ -241,36 +260,47 @@ module.exports = class Lambda {
|
|
|
241
260
|
};
|
|
242
261
|
|
|
243
262
|
validatePayload = () => {
|
|
244
|
-
if (this.jobs.length < 1) throw {message: "No Jobs were sent"};
|
|
263
|
+
if (this.jobs.length < 1) throw { message: "No Jobs were sent" };
|
|
245
264
|
};
|
|
246
265
|
|
|
247
266
|
getJobsByTrackingId = () => {
|
|
248
267
|
let objToReturn = {};
|
|
249
268
|
this.jobs.forEach((job) => {
|
|
250
269
|
let valueFound = utils.getValueOrNull(job, "externalCarrier.trackingId");
|
|
251
|
-
if (valueFound) objToReturn[job._id] = {trackingId: valueFound};
|
|
270
|
+
if (valueFound) objToReturn[job._id] = { trackingId: valueFound };
|
|
252
271
|
objToReturn[job._id].jobOwner = {
|
|
253
|
-
account: {_id: job.jobOwner.account._id},
|
|
272
|
+
account: { _id: job.jobOwner.account._id },
|
|
254
273
|
};
|
|
255
274
|
});
|
|
256
275
|
return objToReturn;
|
|
257
276
|
};
|
|
258
277
|
|
|
259
|
-
|
|
278
|
+
uploadResultToS3 = async () => {
|
|
279
|
+
return await this.s3.upload(Buffer.from(utils.dataToString(this.results, "data").res), `tmp/results/${this.carrier}-${new Date().getTime()}.json`);
|
|
280
|
+
};
|
|
281
|
+
endSuccess = async () => {
|
|
282
|
+
//check if the response is bigger than 6291400 bytes
|
|
283
|
+
if(Buffer.byteLength(utils.dataToString(this.results, "data").res) > 6291400) {
|
|
284
|
+
return {
|
|
285
|
+
isBase64Encoded: false, statusCode: 200, body: utils.dataToString({
|
|
286
|
+
pathS3Result: (await this.uploadResultToS3())
|
|
287
|
+
}, "data").res,
|
|
288
|
+
};
|
|
289
|
+
}
|
|
260
290
|
return {
|
|
261
291
|
isBase64Encoded: false, statusCode: 200, body: utils.dataToString(this.results, "data").res,
|
|
262
292
|
};
|
|
263
293
|
};
|
|
264
294
|
endError = (e) => {
|
|
265
|
-
let {code, res} = utils.dataToString(e);
|
|
295
|
+
let { code, res } = utils.dataToString(e);
|
|
266
296
|
let body = {
|
|
267
297
|
code, message: res, carrierCode: e.carrierCode, carrierMessage: e.carrierMessage,
|
|
268
298
|
};
|
|
269
|
-
return {isBase64Encoded: false, statusCode: code ? code : 417, body: res};
|
|
299
|
+
return { isBase64Encoded: false, statusCode: code ? code : 417, body: res };
|
|
270
300
|
};
|
|
271
301
|
getToken = () => {
|
|
272
302
|
const token = `${this.auth.username}:${this.auth.password}`;
|
|
273
303
|
const encodedToken = Buffer.from(token).toString("base64");
|
|
274
304
|
return encodedToken;
|
|
275
305
|
};
|
|
276
|
-
};
|
|
306
|
+
};
|