vtlab-generic-functions 1.0.20 → 1.0.22

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 (2) hide show
  1. package/package.json +1 -1
  2. package/sqs/index.js +41 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vtlab-generic-functions",
3
- "version": "1.0.20",
3
+ "version": "1.0.22",
4
4
  "scripts": {
5
5
  "test": "jest"
6
6
  },
package/sqs/index.js CHANGED
@@ -11,18 +11,23 @@ const validator = require('../validator');
11
11
  * @returns {Promise<Object>} - A promise that resolves to the response from SQS.
12
12
  * @throws {Error} - Throws an error if sending the message fails.
13
13
  */
14
+
15
+ let sqsClient;
16
+
14
17
  const sendSQSMessage = async (message, queueUrl, isFIFOQueue = true) => {
15
18
  try {
16
- const awsCredentials = await config.get('awsCredentials');
17
19
  const environment = process.env.NODE_ENV || 'develop';
18
20
 
19
- const sqsClient = new SQSClient({
20
- region: awsCredentials.region,
21
- credentials: {
22
- accessKeyId: awsCredentials.accessKeyId,
23
- secretAccessKey: awsCredentials.secretAccessKey
24
- }
25
- });
21
+ if (!sqsClient) {
22
+ const awsCredentials = await config.get('awsCredentials');
23
+ sqsClient = new SQSClient({
24
+ region: awsCredentials.region,
25
+ credentials: {
26
+ accessKeyId: awsCredentials.accessKeyId,
27
+ secretAccessKey: awsCredentials.secretAccessKey
28
+ }
29
+ });
30
+ }
26
31
 
27
32
  const params = {
28
33
  MessageBody: JSON.stringify(message),
@@ -78,8 +83,33 @@ const saveIntegrationMessages = async (document = {}) => {
78
83
  throw error;
79
84
  }
80
85
  };
86
+ /**
87
+ * Sends a ZPL message to an SQS queue.
88
+ * @async
89
+ * @function sendZplSqsMessage
90
+ * @param {Object} options - The options object.
91
+ * @param {string} options.s3PathOrigin - The S3 path of the origin.
92
+ * @param {string} options.s3PathDestination - The S3 path of the destination.
93
+ * @param {string} [options.type="pdf"] - The type of the message. Defaults to "pdf".
94
+ * @param {string} [options.size="4x6"] - The size of the message. Defaults to "4x6".
95
+ * @throws {Error} Throws an error if required parameters are missing.
96
+ * @returns {Promise<void>} A Promise that resolves when the message is sent.
97
+ */
98
+ const sendZplSqsMessage = async ({s3PathOrigin, s3PathDestination, type = "pdf", size = "4x6"}) => {
99
+ if (!s3PathOrigin || !s3PathDestination) {
100
+ throw {
101
+ message: 'Missing required parameters'
102
+ }
103
+ }
104
+ const queue = await config.get("microservices.sqs.zplQueue");
105
+ const environment = process.env.NODE_ENV || "develop";
106
+ const payload = {
107
+ s3PathOrigin, s3PathDestination, type, size, environment
108
+ };
109
+ const stageVariables = {environment}
110
+ return await sendSQSMessage({payload, stageVariables}, queue, true);
111
+ }
81
112
 
82
113
  module.exports = {
83
- saveJobEvent,
84
- saveIntegrationMessages
85
- };
114
+ saveJobEvent, saveIntegrationMessages, sendZplSqsMessage
115
+ };