forked from konecty/node-red-contrib-konecty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkonecty-search.js
99 lines (92 loc) · 3.33 KB
/
konecty-search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const axios_instance = require('axios');
const moment = require('moment');
function parseValue(msg, value, type) {
if (type == "msg") value = value.split('.').reduce((o,k) => {return o[k]}, msg);
else if (type == "flow") value = flow.get(value);
else if (type == "global") value = global.get(value);
else if (type == "num") value = Number(value);
else if (type == "bool" && value == "true") value = true;
else if (type == "bool" && value == "false") value = false;
return value;
}
module.exports = function (RED) {
function KonectySearchNode(config) {
RED.nodes.createNode(this, config);
let node = this;
let flow = node.context().flow;
let global = node.context().global;
node.on('input', function (msg) {
// FILTER EVALUATION
let filter = JSON.parse(config.filter);
for (let i=0, j=filter.conditions.length; i<j; i++) {
let c = filter.conditions[i];
c.value1 = parseValue(msg, c.value1, c.value1Type);
c.value2 = parseValue(msg, c.value2, c.value2Type);
if (c.operator === "lookup") {
c.term += "._id";
c.operator = "equals";
c.value = c.lookupId;
} else if (c.operator === "between") {
c.value = {};
if (c.value1) {
if (c.fieldType == "dateTime") c.value["greater_or_equals"] = {"$date": moment(c.value1, "YYYY-MM-DD HH:mm").format() };
else if (c.fieldType == "date") c.value["greater_or_equals"] = {"$date": moment(c.value1, "YYYY-MM-DD").format() };
else if (c.fieldType == "money" || c.fieldType == "number") c.value["greater_or_equals"] = Number(c.value1);
}
if (c.value2) {
if (c.fieldType == "dateTime") c.value["less_or_equals"] = {"$date": moment(c.value2, "YYYY-MM-DD HH:mm").format() };
else if (c.fieldType == "date") c.value["less_or_equals"] = {"$date": moment(c.value2, "YYYY-MM-DD").format() };
else if (c.fieldType == "money" || c.fieldType == "number") c.value["less_or_equals"] = Number(c.value2);
}
} else {
c.value = c.value1;
}
}
let root_url = node.credentials.url.trim();
if (root_url.endsWith('/')) {
root_url = root_url.slice(0, root_url.length-1);
}
node.status({fill:"red",shape:"ring",text:"searching..."});
const axios = axios_instance.create({
baseURL: root_url,
headers: {
'Authorization': node.credentials.token
}
});
axios.get('/rest/data/'+ (config.doc.split(':')[1]) +'/find', {
params: {
filter: JSON.stringify(filter),
limit: 0
}
})
.then(function (response) {
node.status({});
msg.success = response.data.success;
msg.payload = response.data.data;
node.send(msg);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// always executed
// console.log('Done.')
});
});
node.on('close', function (removed, done) {
if (removed) {
// This node has been deleted
} else {
// This node is being restarted
}
done();
});
}
RED.nodes.registerType("konecty-search", KonectySearchNode, {
credentials: {
url: {type:"text"},
namespace: {type:"text"},
token: {type:"text"}
}
});
}