[
    {
        "id": "6301e063009d9ad9",
        "type": "function",
        "z": "5393de7ec99b351f",
        "name": "Send SQS message",
        "func": "// Import the SendMessageCommand from the AWS SDK\nconst { SendMessageCommand } = awsSdkClientSqs;\n\n// Retrieve the client and config from the node context (varaibles defined in \"On Start\" tab)\nconst client = this.client;\nconst config = this.config;\n// Define the parameters for sending a message\nconst params = {\n    MessageBody: JSON.stringify(msg, this.removeCircular()),\n    QueueUrl: config.queueUrl\n};\n// Create the command for sending a message\nconst command = new SendMessageCommand(params);\n\nasync function sendMessage() {\n    try {\n        // Send the message and wait for the response\n        const data = await client.send(command);\n        node.log(\"Message sent successfully. Message ID: \" + data.MessageId);\n\n        // Attach the response to msg.payload and send the message\n        msg.payload = data;\n        node.send(msg);\n    } catch (error) {\n        node.error(\"Error sending message: \" + error);\n    }\n};\n\n\n// Execute the sendMessage function asynchronously\nsendMessage();\n\n// Return null since output is handled asynchronously via node.send() method\nreturn null;\n\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "// Import required classes from the AWS SDK v3 package\nconst { SQSClient } = awsSdkClientSqs;\n\n// Group configuration values in an object\nthis.config = {\n    queueUrl: global.get(\"SECRETS.AWS_SQS_QUEUE_URL\"),\n    accessKey: global.get(\"SECRETS.AWS_SQS_API_KEY\"),\n    accessSecret: global.get(\"SECRETS.AWS_SQS_API_SECRET\"),\n    region: global.get(\"SECRETS.AWS_SQS_QUEUE_REGION\")\n};\n\n// Create an SQS client and store it as a property of the node (this.client)\nthis.client = new SQSClient({\n    region: this.config.region,\n    apiVersion: '2012-11-05',\n    credentials: {\n        accessKeyId: this.config.accessKey,\n        secretAccessKey: this.config.accessSecret\n    }\n});\n\n// Define a helper function for safely stringifying the msg (avoiding circular references)\nthis.removeCircular = function () {\n    const seen = new WeakSet();\n    return (key, value) => {\n        if (typeof value === 'object' && value !== null) {\n            if (seen.has(value)) {\n                return undefined;\n            }\n            seen.add(value);\n        }\n        return value;\n    };\n};\n",
        "finalize": "",
        "libs": [
            {
                "var": "awsSdkClientSqs",
                "module": "@aws-sdk/client-sqs"
            }
        ],
        "x": 250,
        "y": 120,
        "wires": [
            [
                "950ef4b5ce121ab4"
            ]
        ],
        "icon": "node-red/envelope.svg"
    },
    {
        "id": "402a4536b2a35988",
        "type": "inject",
        "z": "5393de7ec99b351f",
        "name": "",
        "props": [
            {
                "p": "payload.test",
                "v": "",
                "vt": "date"
            },
            {
                "p": "payload.hello",
                "v": "world",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 105,
        "y": 120,
        "wires": [
            [
                "6301e063009d9ad9"
            ]
        ],
        "l": false
    },
    {
        "id": "950ef4b5ce121ab4",
        "type": "debug",
        "z": "5393de7ec99b351f",
        "name": "Send message",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "counter",
        "x": 405,
        "y": 120,
        "wires": [],
        "l": false
    },
    {
        "id": "b91ea294d147b33a",
        "type": "function",
        "z": "5393de7ec99b351f",
        "name": "Reveive SQS message",
        "func": "// Import required commands from the AWS SDK v3 package\nconst { ReceiveMessageCommand } = awsSdkClientSqs;\n\n// Retrieve the client and config from the node context (set in \"On Start\")\nconst client = this.client;\nconst config = this.config;\n\n// Define the parameters for receiving messages\nconst params = {\n    QueueUrl: config.queueUrl,        // Your SQS queue's URL\n    MaxNumberOfMessages: 10,            // Maximum messages to receive (max 10)\n    WaitTimeSeconds: 10                 // Enable long polling (in seconds)\n};\n\nasync function receiveMessages() {\n    try {\n        // Create the command for receiving messages\n        const command = new ReceiveMessageCommand(params);\n\n        // Send the command and wait for the response\n        const data = await client.send(command);\n\n        if (data.Messages && data.Messages.length > 0) {\n            for (const message of data.Messages) {\n                // Safely parse the message body (if JSON, otherwise leave it as a string)\n                message.Body = safeJSONParse(message.Body);\n\n                // Process the message by sending it along the flow\n                node.send(message);\n\n                // Delete the message from the queue to prevent reprocessing\n                await deleteMessageFromQueue(message, client, config);\n            }\n        }\n    } catch (error) {\n        node.error(\"Error receiving messages: \" + error);\n    }\n}\n\n// Helper function to safely parse JSON; returns original string if parsing fails\nfunction safeJSONParse(input) {\n    try {\n        return JSON.parse(input);\n    } catch (err) {\n        return input;\n    }\n}\n\n// Helper function to delete a message from the SQS queue\nasync function deleteMessageFromQueue(message, client, config) {\n    try {\n        const { DeleteMessageCommand } = awsSdkClientSqs;\n        const deleteParams = {\n            QueueUrl: config.queueUrl,\n            ReceiptHandle: message.ReceiptHandle\n        };\n        const deleteCommand = new DeleteMessageCommand(deleteParams);\n        await client.send(deleteCommand);\n    } catch (deleteError) {\n        node.error(\"Error deleting message: \" + deleteError);\n    }\n}\n\n// Execute the receiveMessages function\nreceiveMessages();\n\n// Return null since output is handled asynchronously via node.send()\nreturn null;\n\n\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "// Import required classes from the AWS SDK v3 package\nconst { SQSClient } = awsSdkClientSqs;\n\n// Group configuration values in an object\nthis.config = {\n    queueUrl: global.get(\"SECRETS.AWS_SQS_QUEUE_URL\"),\n    accessKey: global.get(\"SECRETS.AWS_SQS_API_KEY\"),\n    accessSecret: global.get(\"SECRETS.AWS_SQS_API_SECRET\"),\n    region: global.get(\"SECRETS.AWS_SQS_QUEUE_REGION\")\n};\n\n// Create an SQS client and store it as a property of the node (this.client)\nthis.client = new SQSClient({\n    region: this.config.region,\n    apiVersion: '2012-11-05',\n    credentials: {\n        accessKeyId: this.config.accessKey,\n        secretAccessKey: this.config.accessSecret\n    }\n});\n\nnode.log(\"SQS client initialized for receiving messages.\");\n",
        "finalize": "",
        "libs": [
            {
                "var": "awsSdkClientSqs",
                "module": "@aws-sdk/client-sqs"
            }
        ],
        "x": 260,
        "y": 220,
        "wires": [
            [
                "764c9856301e1ca7"
            ]
        ],
        "icon": "font-awesome/fa-envelope-open"
    },
    {
        "id": "764c9856301e1ca7",
        "type": "debug",
        "z": "5393de7ec99b351f",
        "name": "received messages",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "Body",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "counter",
        "x": 405,
        "y": 220,
        "wires": [],
        "l": false
    },
    {
        "id": "c02cf6aae1699486",
        "type": "inject",
        "z": "5393de7ec99b351f",
        "name": "",
        "props": [],
        "repeat": "15",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "x": 105,
        "y": 220,
        "wires": [
            [
                "b91ea294d147b33a"
            ]
        ],
        "icon": "font-awesome/fa-refresh",
        "l": false
    },
    {
        "id": "4d1b4b537afa8320",
        "type": "comment",
        "z": "5393de7ec99b351f",
        "name": "Continuously receives messages",
        "info": "",
        "x": 190,
        "y": 180,
        "wires": []
    },
    {
        "id": "f991ef4638d5c030",
        "type": "comment",
        "z": "5393de7ec99b351f",
        "name": "Send message to sqs queue",
        "info": "",
        "x": 170,
        "y": 60,
        "wires": []
    },
    {
        "id": "5eba11eb0b512a7f",
        "type": "comment",
        "z": "5393de7ec99b351f",
        "name": "This flow expects secrets to be defined in qibb's Secret Manager: \\n AWS_SQS_QUEUE_URL \\n AWS_SQS_API_KEY \\n AWS_SQS_API_SECRET \\n AWS_SQS_QUEUE_REGION",
        "info": "Expected space secrets:\nAWS_SQS_QUEUE_URL\nAWS_SQS_API_KEY\nAWS_SQS_API_SECRET\nAWS_SQS_QUEUE_REGION",
        "x": 720,
        "y": 100,
        "wires": []
    }
]