Rewrite user conversion function
This commit is contained in:
@ -6500,7 +6500,7 @@
|
|||||||
"type": "function",
|
"type": "function",
|
||||||
"z": "ea629141206d5343",
|
"z": "ea629141206d5343",
|
||||||
"name": "user conversion",
|
"name": "user conversion",
|
||||||
"func": "// Get the user ID list from global context\n\nconst users = global.get(\"userIDList\",\"diskCon\")\n\n// Initialize variables\n\nlet userTrigger = {}\nlet userFirstName = {}\nlet userLastName = {}\nlet hassUserID = {}\nlet statusTxt = {}\n\n// Check if the user ID is in the payload or data\n\nif (msg.payload && msg.payload.context && msg.payload.context.user_id) {\n userTrigger = msg.payload.context.user_id\n} else if (msg.data && msg.data.context && msg.data.context.user_id) {\n userTrigger = msg.data.context.user_id\n} else {\n userTrigger = \"none\"\n}\n\n// Set the status message based on the user ID\n\nlet successTxt = `User: ${users[userTrigger].firstName}`\nlet errorTxt = \"User not found\"\n\nif (userTrigger === \"none\") {\n statusTxt = errorTxt\n} else {\n statusTxt = successTxt\n}\n\n// Set the user's information in the message\n\nmsg.user = users[userTrigger]\n\n// Create the status message\n\nlet statusMsg = {\n \"status\": {\n \"fill\": \"green\",\n \"shape\": \"dot\",\n \"text\": `${statusTxt} at ${new Date().toLocaleString()}`\n }\n}\n\n// Send the message and status message to the output nodes\n\nif (userTrigger === \"none\") {\n node.error(\"User not found\")\n} else {\n node.send([msg,statusMsg])\n}",
|
"func": "// Get the user ID list from global context\nconst users = global.get(\"userIDList\", \"diskCon\") || {}\n\n// Initialize variables\nlet userTrigger = \"none\"\nlet statusTxt = \"\"\n\n// Extract user ID from the message\nif (msg.payload?.context?.user_id) {\n userTrigger = msg.payload.context.user_id\n} else if (msg.data?.context?.user_id) {\n userTrigger = msg.data.context.user_id\n}\n\n// Validate userTrigger and set status message\nif (userTrigger === \"none\" || !users[userTrigger]) {\n statusTxt = \"User not found\"\n node.error(`User ID '${userTrigger}' not found in user list.`)\n return null // Stop execution if user is not found\n} else {\n const user = users[userTrigger]\n statusTxt = `User: ${user.firstName}`\n msg.user = { ...user } // Avoid mutating the original msg\n}\n\n// Create the status message\nconst statusMsg = {\n status: {\n fill: \"green\",\n shape: \"dot\",\n text: `${statusTxt} at ${new Date().toLocaleString()}`\n }\n}\n\n// Send the message and status message to the output nodes\nnode.send([msg, statusMsg])",
|
||||||
"outputs": 2,
|
"outputs": 2,
|
||||||
"timeout": "",
|
"timeout": "",
|
||||||
"noerr": 0,
|
"noerr": 0,
|
||||||
|
Reference in New Issue
Block a user