-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkonecty-search.js
158 lines (143 loc) · 4.62 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const axios_instance = require("axios");
const moment = require("moment");
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.server = RED.nodes.getNode(config.server);
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];
if (c.value1 && c.value1.length) {
c.value1 = RED.util.evaluateNodeProperty(
c.value1,
c.value1Type,
this,
msg
);
}
if (!!c.value2) {
c.value2 = RED.util.evaluateNodeProperty(
c.value2,
c.value2Type,
this,
msg
);
}
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);
else c.value["greater_or_equals"] = 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["less_or_equals"] = c.value2;
}
} else {
c.value = c.value1;
}
}
// Ensure that the condition will have value property
filter.conditions = filter.conditions.filter(
item => ![null, undefined, NaN].includes(item.value)
);
let root_url = node.server.host;
if (root_url.endsWith("/")) {
root_url = root_url.slice(0, root_url.length - 1);
}
let token = node.server.key;
if (config.token && config.tokenType) {
const userToken = RED.util.evaluateNodeProperty(
config.token,
config.tokenType,
this,
msg
);
if (/[^ ]+/.test(userToken)) {
token = userToken;
}
}
let start = 0;
if (config.start && config.startType) {
start = RED.util.evaluateNodeProperty(
config.start,
config.startType,
this,
msg
);
}
node.status({ fill: "blue", shape: "ring", text: "searching..." });
const axios = axios_instance.create({
baseURL: root_url,
headers: {
Authorization: token
}
});
const fields = JSON.parse(config.projections);
const sort = JSON.parse(config.sort || "[]");
const params = {
filter: JSON.stringify(filter),
limit: (config.limit && Number(config.limit)) || 0,
start: Number(start),
sort:
sort.length === 0
? `[{"property":"_id","direction":"ASC"}]`
: JSON.stringify(sort),
fields:
Array.isArray(fields) && fields.length > 0 ? fields.join() : undefined
};
if (config.debugMode) {
node.warn(params);
}
axios
.get(`/rest/data/${config.doc.split(":")[1]}/find`, { params })
.then(function(response) {
node.status({});
msg.success = response.data.success;
msg.payload = response.data.data;
msg.total = response.data.total;
node.send(msg);
})
.catch(function(error) {
node.status({ fill: "red", shape: "ring", text: "Error searching" });
node.error(error);
});
});
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);
};