Files
nextcloud-nodered-woocommerce/nodes/woocommerce-order.js
T

84 lines
4.1 KiB
JavaScript

module.exports = function (RED) {
"use strict";
function WooCommerceOrderNode(config) {
RED.nodes.createNode(this, config);
const node = this;
node.configNode = RED.nodes.getNode(config.config);
if (!node.configNode) { node.error("No woocommerce-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" }); done ? done(err) : node.error(err, msg); return; }
msg.payload = result.body || {};
msg.statusCode = result.statusCode;
msg.woocommerce = result;
node.status({ fill: "green", shape: "dot", text: result.statusCode });
done();
});
}
function buildListParams(msg) {
const p = new URLSearchParams();
if (msg.status) p.append("status", msg.status);
if (msg.after) p.append("after", msg.after);
if (msg.before) p.append("before", msg.before);
if (msg.customer) p.append("customer", msg.customer);
if (msg.product) p.append("product", msg.product);
if (msg.limit) p.append("per_page", msg.limit);
if (msg.page) p.append("page", msg.page);
if (msg.order) p.append("order", msg.order);
if (msg.orderby) p.append("orderby", msg.orderby);
return p.toString() ? "?" + p.toString() : "";
}
node.on("input", function (msg, send, done) {
const op = msg.operation || node.operation;
switch (op) {
case "list": {
doRequest("GET", "/orders" + buildListParams(msg), undefined, msg, done);
break;
}
case "get": {
const id = msg.orderId || msg.payload;
if (!id) { node.error("Missing orderId"); done && done(new Error("Missing orderId")); return; }
doRequest("GET", "/orders/" + encodeURIComponent(id), undefined, msg, done);
break;
}
case "updateStatus": {
const id = msg.orderId || (msg.payload && msg.payload.id);
const status = msg.status || msg.newStatus;
if (!id || !status) { node.error("Missing orderId or status"); done && done(new Error("Missing orderId or status")); return; }
doRequest("PUT", "/orders/" + encodeURIComponent(id), { status }, msg, done);
break;
}
case "addNote": {
const id = msg.orderId || (msg.payload && msg.payload.id);
const note = msg.note || msg.noteText || (msg.payload && msg.payload.note);
if (!id || !note) { node.error("Missing orderId or note"); done && done(new Error("Missing orderId or note")); return; }
doRequest("POST", "/orders/" + encodeURIComponent(id) + "/notes", { note, customer_note: msg.customerNote || false }, msg, done);
break;
}
case "getNotes": {
const id = msg.orderId || (msg.payload && msg.payload.id);
if (!id) { node.error("Missing orderId"); done && done(new Error("Missing orderId")); return; }
doRequest("GET", "/orders/" + encodeURIComponent(id) + "/notes", undefined, msg, done);
break;
}
default: {
const err = new Error("Unknown operation: " + op);
node.error(err);
if (done) done(err);
}
}
});
}
RED.nodes.registerType("woocommerce-order", WooCommerceOrderNode);
};