89 lines
3.5 KiB
JavaScript
89 lines
3.5 KiB
JavaScript
|
|
module.exports = function (RED) {
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
function ZammadUserNode(config) {
|
||
|
|
RED.nodes.createNode(this, config);
|
||
|
|
const node = this;
|
||
|
|
|
||
|
|
node.configNode = RED.nodes.getNode(config.config);
|
||
|
|
if (!node.configNode) {
|
||
|
|
node.error("No zammad-config node configured");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
node.operation = config.operation || "list";
|
||
|
|
|
||
|
|
node.status({ fill: "blue", shape: "ring", text: "ready" });
|
||
|
|
|
||
|
|
function doRequest(method, path, body, msg, done) {
|
||
|
|
node.status({ fill: "yellow", shape: "dot", text: method + " " + path });
|
||
|
|
node.configNode.sendRequest(method, path, body, {}, function (err, result) {
|
||
|
|
if (err) {
|
||
|
|
node.status({ fill: "red", shape: "ring", text: "error" });
|
||
|
|
if (done) done(err);
|
||
|
|
else node.error(err, msg);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
msg.payload = result.body;
|
||
|
|
msg.statusCode = result.statusCode;
|
||
|
|
msg.zammad = result;
|
||
|
|
node.status({ fill: "green", shape: "dot", text: result.statusCode });
|
||
|
|
done();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
node.on("input", function (msg, send, done) {
|
||
|
|
const op = msg.operation || node.operation;
|
||
|
|
|
||
|
|
switch (op) {
|
||
|
|
case "me": {
|
||
|
|
doRequest("GET", "/users/me", undefined, msg, done);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
case "list": {
|
||
|
|
const params = new URLSearchParams();
|
||
|
|
if (msg.limit) params.append("per_page", msg.limit);
|
||
|
|
if (msg.page) params.append("page", msg.page);
|
||
|
|
const qs = params.toString();
|
||
|
|
doRequest("GET", "/users" + (qs ? "?" + qs : ""), undefined, msg, done);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
case "search": {
|
||
|
|
const searchParams = new URLSearchParams();
|
||
|
|
if (msg.query) searchParams.append("query", msg.query);
|
||
|
|
if (msg.limit) searchParams.append("per_page", msg.limit);
|
||
|
|
const qs = searchParams.toString();
|
||
|
|
doRequest("GET", "/users/search" + (qs ? "?" + qs : ""), undefined, msg, done);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
case "get": {
|
||
|
|
const id = msg.userId || msg.payload;
|
||
|
|
if (!id) { node.error("Missing userId"); if (done) done(new Error("Missing userId")); return; }
|
||
|
|
doRequest("GET", "/users/" + encodeURIComponent(id), undefined, msg, done);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
case "create": {
|
||
|
|
const body = msg.payload && typeof msg.payload === "object" ? msg.payload : {
|
||
|
|
firstname: msg.firstname || "",
|
||
|
|
lastname: msg.lastname || "",
|
||
|
|
email: msg.email || "",
|
||
|
|
phone: msg.phone || "",
|
||
|
|
customer: msg.customer || "",
|
||
|
|
group: msg.group || "",
|
||
|
|
note: msg.note || ""
|
||
|
|
};
|
||
|
|
doRequest("POST", "/users", body, msg, done);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
default: {
|
||
|
|
const err = new Error("Unknown operation: " + op);
|
||
|
|
node.error(err);
|
||
|
|
if (done) done(err);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
RED.nodes.registerType("zammad-user", ZammadUserNode);
|
||
|
|
};
|