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