UNPKG

node-red-contrib-opentext

Version:

node-red-contrib-opentext - An Opentext Core client

218 lines (184 loc) 6.89 kB
/** * MIT License * * Copyright (c) 2019 Marcos Caputo <caputo.marcos@gmail.com> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software.. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * **/ module.exports = function (RED) { "use strict"; // require any external libraries we may need.... let request = require("request"); var querystring = require("querystring"); // The main node definition - most things happen in here function SignNode(signatureNode) { // Create a RED node RED.nodes.createNode(this, signatureNode); // Store local copies of the node configuration (as defined in the .html) this.name = signatureNode.name || ""; this.base_url = signatureNode.base_url || ""; this.from_name = signatureNode.from_name || ""; this.from_email = signatureNode.from_email || ""; this.to_name = signatureNode.to_name || ""; this.to_email = signatureNode.to_email || ""; this.email_subject = signatureNode.email_subject || ""; this.email_message = signatureNode.email_message || ""; // copy "this" object in case we need it in context of callbacks of other functions. let node = this; let msg = {}; let msg1 = {}; //msg.payload = this.payload; // respond to inputs.... this.on("input", function (msg) { // set an empty form node.status({ fill: "yellow", shape: "dot", text: "preparing....", }); // TODO - ??? =) let Method = "Post"; let Authorization = ""; //let url = ""; let Document = ""; let Body = ""; let Headers =""; let Options = ""; //let Form = {}; let Databuffer =""; if (msg.coreSignatureSignRequest){ if (msg.coreSignatureSignRequest.url) node.base_url = msg.coreSignatureSignRequest.base_url; if (msg.coreSignatureSignRequest.email.from_name) node.from_name = msg.coreSignatureSignRequest.email.from_name; if (msg.coreSignatureSignRequest.email.from_email) node.from_email = msg.coreSignatureSignRequest.email.from_email; if (msg.coreSignatureSignRequest.email.to_name) node.to_name = msg.coreSignatureSignRequest.email.to_name; if (msg.coreSignatureSignRequest.email.to_email) node.to_email = msg.coreSignatureSignRequest.email.to_email; if(msg.coreSignatureSignRequest.email.subject) node.email_subject = msg.coreSignatureSignRequest.email.subject; if (msg.coreSignatureSignRequest.email.message) node.email_message = msg.coreSignatureSignRequest.email.message; // get accesstoken & content from input } node.accesstoken = msg.payload.access_token; //set token Authorization = "Bearer " + node.accesstoken; let send_url = node.base_url + "/api/v1/signature-requests/"; //sign Document------------------------------------------------------------------------------------------------------ // signature url Document = msg.payload.url; console.log(Document); //clear Body // Body = ""; Headers =""; Options = ""; Body =JSON.stringify({ "document": Document, "from_email": node.from_email, "from_email_name": node.from_name, "subject": node.email_subject, "message": node.email_message, "signers": [ { "email": node.to_email, "display_name": node.to_name } ] }); console.log("Body "); console.log(Body); //set Headers Headers = { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(Body), Authorization: Authorization, }; console.log("Headers "); console.log(Headers); // set Option Options = { method: Method , url: send_url, headers: Headers, body: Body, json: false, }; console.log("options ok"); // make a post request request(Options, function (err, response, body) { if (msg.coreSignatureRequest) delete msg.coreSignatureRequest; try { let SignBody = JSON.parse(body ? body : JSON.stringify("{}")); if (response && response.statusCode < 299 && response.statusCode > 199) { console.log(response.statusMessage); console.log(response.body); msg.payload = SignBody; msg.payload.statusCode = response.statusCode; node.status({ fill: "green", shape: "dot", text: `HTTP ${response.statusCode}, Document has been sent!!`, }); } else if ( response && response.statusCode && response.statusCode !== 200 ) { console.log(response.statusMessage); console.log(response.body); msg.payload = { statusCode: response.statusCode, statusMessage: response.statusMessage, body: SignBody, }; node.status({ fill: "red", shape: "dot", text: `HTTP ${response.statusCode}, error sending document!`, }); } if (err && err.code) { msg.err = JSON.parse(JSON.stringify(err)); node.status({ fill: "yellow", shape: "dot", text: `ERR ${err.code}` }); } else if (err && err.message && err.stack) { msg.err = { message: err.message, stack: err.stack }; node.status({ fill: "black", shape: "dot", text: `ERR ${err.message}`, }); } node.send(msg); } catch (err) { var d = new Date(); var n = d.toISOString(); console.log(n + ": " + err.message); console.log(n + ": " + body.replace(/ |\r\n|\n|\r/gm, "")); msg.err = JSON.parse(JSON.stringify(err)); node.status({ fill: "blue", shape: "dot", text: `ERR ${err.code}` }); } }); }); } RED.nodes.registerType("sign", SignNode); };