n8n-nodes-evolution-message-search 2.0.2 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,71 @@ exports.SearchMessages = void 0;
5
5
 
6
6
  const n8n_workflow_1 = require('n8n-workflow');
7
7
 
8
+ async function performSearch(ctx, itemIndex) {
9
+ const credentials = await ctx.getCredentials('evolutionApi');
10
+ const serverUrl = credentials['server-url'];
11
+ const apiKey = credentials.apikey;
12
+
13
+ const instanceName = ctx.getNodeParameter('instanceName', itemIndex);
14
+ const userConversationId = ctx.getNodeParameter('conversationId', itemIndex) || '';
15
+ const keywords = ctx.getNodeParameter('keywords', itemIndex) || '';
16
+ const dateFrom = ctx.getNodeParameter('dateFrom', itemIndex) || '';
17
+ const dateTo = ctx.getNodeParameter('dateTo', itemIndex) || '';
18
+ const pageSize = ctx.getNodeParameter('pageSize', itemIndex) || 10;
19
+ const page = ctx.getNodeParameter('page', itemIndex) || 1;
20
+
21
+ const skip = (page - 1) * pageSize;
22
+
23
+ const whereClause = {};
24
+ if (userConversationId) {
25
+ whereClause['key.remote'] = userConversationId;
26
+ }
27
+ if (keywords) {
28
+ whereClause['message.conversationMessage.text'] = { contains: keywords };
29
+ }
30
+ if (dateFrom || dateTo) {
31
+ whereClause['timestamp'] = {};
32
+ if (dateFrom) {
33
+ whereClause['timestamp']['gte'] = new Date(dateFrom).getTime() / 1000;
34
+ }
35
+ if (dateTo) {
36
+ whereClause['timestamp']['lte'] = new Date(dateTo).getTime() / 1000;
37
+ }
38
+ }
39
+
40
+ const body = {
41
+ where: whereClause,
42
+ take: pageSize,
43
+ skip: skip,
44
+ orderBy: { timestamp: 'desc' },
45
+ };
46
+
47
+ const cleanUrl = serverUrl.replace(/\/$/, '');
48
+ const response = await ctx.helpers.request({
49
+ method: 'POST',
50
+ uri: `${cleanUrl}/chat/findMessages/${instanceName}`,
51
+ body,
52
+ headers: {
53
+ 'Content-Type': 'application/json',
54
+ Accept: 'application/json',
55
+ apikey: apiKey,
56
+ },
57
+ json: true,
58
+ });
59
+
60
+ const messages = response.messages || response;
61
+ const total = messages.total || messages.length || 0;
62
+ const totalPages = Math.ceil(total / pageSize);
63
+
64
+ return {
65
+ total,
66
+ pages: totalPages,
67
+ currentPage: page,
68
+ pageSize,
69
+ records: Array.isArray(messages.records) ? messages.records : messages,
70
+ };
71
+ }
72
+
8
73
  class SearchMessages {
9
74
  constructor() {
10
75
  this.description = {
@@ -104,95 +169,27 @@ class SearchMessages {
104
169
  }
105
170
 
106
171
  async execute() {
107
- const items = this.getInputData ? this.getInputData() : [];
172
+ const items = typeof this.getInputData === 'function' ? this.getInputData() : [];
108
173
  const returnData = [];
174
+ const max = items.length > 0 ? items.length : 1;
109
175
 
110
- for (let itemIndex = 0; itemIndex < (items.length || 1); itemIndex++) {
176
+ for (let itemIndex = 0; itemIndex < max; itemIndex++) {
111
177
  try {
112
- const result = await this.executeSingle.call(this, itemIndex);
178
+ const result = await performSearch(this, itemIndex);
113
179
  returnData.push({
114
180
  json: result,
115
181
  pairedItem: { item: itemIndex },
116
182
  });
117
183
  } catch (err) {
118
- if (this.continueOnFail && this.continueOnFail()) {
119
- returnData.push({
120
- json: { error: err.message },
121
- pairedItem: { item: itemIndex },
122
- });
123
- } else {
124
- throw err;
125
- }
184
+ returnData.push({
185
+ json: { error: err.message },
186
+ pairedItem: { item: itemIndex },
187
+ });
126
188
  }
127
189
  }
128
190
 
129
191
  return [returnData];
130
192
  }
131
-
132
- async executeSingle(itemIndex) {
133
- const credentials = await this.getCredentials('evolutionApi');
134
- const serverUrl = credentials['server-url'];
135
- const apiKey = credentials.apikey;
136
-
137
- const instanceName = this.getNodeParameter('instanceName', itemIndex);
138
- const userConversationId = this.getNodeParameter('conversationId', itemIndex) || '';
139
- const keywords = this.getNodeParameter('keywords', itemIndex) || '';
140
- const dateFrom = this.getNodeParameter('dateFrom', itemIndex) || '';
141
- const dateTo = this.getNodeParameter('dateTo', itemIndex) || '';
142
- const pageSize = this.getNodeParameter('pageSize', itemIndex) || 10;
143
- const page = this.getNodeParameter('page', itemIndex) || 1;
144
-
145
- const skip = (page - 1) * pageSize;
146
-
147
- const whereClause = {};
148
- if (userConversationId) {
149
- whereClause['key.remote'] = userConversationId;
150
- }
151
- if (keywords) {
152
- whereClause['message.conversationMessage.text'] = { contains: keywords };
153
- }
154
- if (dateFrom || dateTo) {
155
- whereClause['timestamp'] = {};
156
- if (dateFrom) {
157
- whereClause['timestamp']['gte'] = new Date(dateFrom).getTime() / 1000;
158
- }
159
- if (dateTo) {
160
- whereClause['timestamp']['lte'] = new Date(dateTo).getTime() / 1000;
161
- }
162
- }
163
-
164
- const body = {
165
- where: whereClause,
166
- take: pageSize,
167
- skip: skip,
168
- orderBy: { timestamp: 'desc' },
169
- };
170
-
171
- const cleanUrl = serverUrl.replace(/\/$/, '');
172
- const response = await this.helpers.request({
173
- method: 'POST',
174
- uri: `${cleanUrl}/chat/findMessages/${instanceName}`,
175
- body,
176
- headers: {
177
- 'Content-Type': 'application/json',
178
- Accept: 'application/json',
179
- apikey: apiKey,
180
- },
181
- json: true,
182
- });
183
-
184
- const messages = response.messages || response;
185
- const total = messages.total || messages.length || 0;
186
- const totalPages = Math.ceil(total / pageSize);
187
-
188
- return {
189
- total,
190
- pages: totalPages,
191
- currentPage: page,
192
- pageSize,
193
- records: Array.isArray(messages.records) ? messages.records : messages,
194
- };
195
- }
196
193
  }
197
194
 
198
195
  exports.SearchMessages = SearchMessages;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n8n-nodes-evolution-message-search",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "Evolution API - Search Messages node for n8n. Search WhatsApp message history by keywords, dates, and conversation.",
5
5
  "keywords": [
6
6
  "n8n-community-node-package",