vtlab-generic-functions 1.0.33 → 1.0.35

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.
@@ -47,7 +47,7 @@ const getAuthToken = async ({
47
47
  } catch (err) {
48
48
  let {res} = utils.dataToString(err);
49
49
  await cloudwatch.putLogEvents(
50
- logId + "Finsish BRINGG LOGIN Process with errors: " + res,
50
+ logId + "Finished BRINGG LOGIN Process with errors: " + res,
51
51
  logGroupName
52
52
  );
53
53
  throw err;
@@ -1,6 +1,10 @@
1
1
 
2
2
  const promiseFTP = require("promise-ftp");
3
3
  const promiseSFTP = require("ssh2-sftp-client");
4
+ // For FTP, we need to create a temporary file and upload it
5
+ const fs = require('fs');
6
+ const os = require('os');
7
+ const path = require('path');
4
8
 
5
9
 
6
10
  /**
@@ -241,8 +245,48 @@ class FTPClientV2 {
241
245
  if (!this._isConnected) {
242
246
  await this.connect(this._ftpCredentials);
243
247
  }
244
- await this._ftpInstance.put(ftpPathOrigin, ftpPathDestination);
248
+
249
+ // Check if source file exists
250
+ const fileExists = await this.isFileExists(ftpPathOrigin);
251
+ if (!fileExists) {
252
+ throw new Error(`Source file does not exist: ${ftpPathOrigin}`);
253
+ }
254
+
255
+ // Download the file from origin
256
+ const fileContent = await this.downloadFile(ftpPathOrigin);
257
+
258
+ // Upload the file to destination
259
+ switch (this._ftpCredentials.type) {
260
+ case 'sftpWithKey':
261
+ // For SFTP, we can use the buffer directly
262
+ await this._ftpInstance.put(Buffer.concat(fileContent), ftpPathDestination);
263
+ break;
264
+ default:
265
+ // Create a temporary file
266
+ const tempFile = path.join(os.tmpdir(), `temp_${Date.now()}_${path.basename(ftpPathOrigin)}`);
267
+
268
+ try {
269
+ // Write the downloaded content to temp file
270
+ fs.writeFileSync(tempFile, Buffer.concat(fileContent));
271
+
272
+ // Upload the temp file to destination
273
+ await this._ftpInstance.put(tempFile, ftpPathDestination);
274
+
275
+ // Clean up temp file
276
+ fs.unlinkSync(tempFile);
277
+ } catch (tempError) {
278
+ // Clean up temp file if it exists
279
+ if (fs.existsSync(tempFile)) {
280
+ fs.unlinkSync(tempFile);
281
+ }
282
+ throw tempError;
283
+ }
284
+ break;
285
+ }
286
+
287
+ // Delete the original file
245
288
  await this.deleteFile(ftpPathOrigin);
289
+
246
290
  return { code: 200, message: "FTP successful moved" };
247
291
  } catch (error) {
248
292
  throw new Error(`Failed to move file inside FTP server: ${error.message}`);
@@ -56,6 +56,15 @@ module.exports = class Lambda {
56
56
  this.DEBUG = DEBUG;
57
57
  }
58
58
 
59
+ recoverFullPayload = async () => {
60
+ if (this.event?.s3PayloadPath) {
61
+ let contentResponse = await this.s3.download(this.event.s3PayloadPath);
62
+ this.jobs = JSON.parse(contentResponse.Body.toString());
63
+ } else {
64
+ this.jobs = this.event?.payload || [];
65
+ }
66
+ };
67
+
59
68
  s3 = {
60
69
  upload: async (data, path, options = {}) => {
61
70
  return await s3bucket.uploadToS3(path, data, {
@@ -225,9 +234,9 @@ module.exports = class Lambda {
225
234
  logFinishProcess = async ({error} = {}) => {
226
235
  if (this.results.totalError || error) {
227
236
  error = error && error.message ? error.message : error;
228
- await this.logMessage("Finsish Process with errors: " + (error || "") + " " + JSON.stringify(this.results));
237
+ await this.logMessage("Finished Process with errors: " + (error || "") + " " + JSON.stringify(this.results));
229
238
  } else {
230
- await this.logMessage("Finsish Process Sucessfully: " + JSON.stringify(this.results));
239
+ await this.logMessage("Finished Process Sucessfully: " + JSON.stringify(this.results));
231
240
  }
232
241
  };
233
242
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vtlab-generic-functions",
3
- "version": "1.0.33",
3
+ "version": "1.0.35",
4
4
  "scripts": {
5
5
  "test": "jest"
6
6
  },
package/utils/index.js CHANGED
@@ -635,6 +635,45 @@ const getMessageAndCodeFromError = (err) => {
635
635
  return returnData;
636
636
  };
637
637
 
638
+ const normalizeString = (_str) => {
639
+ // Handle null/undefined
640
+ if (_str === null || _str === undefined) return _str;
641
+
642
+ // Handle numbers
643
+ if (typeof _str === 'number') return _str;
644
+
645
+ // Handle arrays
646
+ if (Array.isArray(_str)) {
647
+ return _str.map(item => normalizeString(item));
648
+ }
649
+
650
+ // Handle objects
651
+ if (typeof _str === 'object') {
652
+ const normalizedObj = {};
653
+ for (const [key, value] of Object.entries(_str)) {
654
+ normalizedObj[key] = normalizeString(value);
655
+ }
656
+ return normalizedObj;
657
+ }
658
+
659
+ // Handle strings
660
+ if (typeof _str === 'string') {
661
+ if (!_str.length) return _str;
662
+ if (_str.trim() === "-") return "-";
663
+
664
+ let str = _str.trim();
665
+ str = str.replace(/&/g, "&");
666
+ str = str.replace(/</g, "&lt;");
667
+ str = str.replace(/>/g, "&gt;");
668
+ str = str.replace(/"/g, "&quot;");
669
+ str = str.replace(/'/g, "&apos;");
670
+ return str;
671
+ }
672
+
673
+ // Return as is for any other type
674
+ return _str;
675
+ };
676
+
638
677
  module.exports = {
639
678
  dataToString,
640
679
  getMessageFromValidator,
@@ -659,4 +698,5 @@ module.exports = {
659
698
  getValueOrNullControlWithFallback,
660
699
  getMimeTypeFromBase64,
661
700
  getTipoPortes,
701
+ normalizeString,
662
702
  };