kommo-aiatende-api 1.0.0

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.
package/dist/index.mjs ADDED
@@ -0,0 +1,1413 @@
1
+ // src/client.ts
2
+ import axios2 from "axios";
3
+ import qs17 from "qs";
4
+
5
+ // src/auth.ts
6
+ import axios from "axios";
7
+ var AuthManager = class {
8
+ domain;
9
+ clientId;
10
+ clientSecret;
11
+ redirectUri;
12
+ constructor(domain, clientId, clientSecret, redirectUri) {
13
+ this.domain = domain;
14
+ this.clientId = clientId;
15
+ this.clientSecret = clientSecret;
16
+ this.redirectUri = redirectUri;
17
+ }
18
+ async exchangeCodeForToken(code) {
19
+ const url = `https://${this.domain}.kommo.com/oauth2/access_token`;
20
+ const response = await axios.post(url, {
21
+ client_id: this.clientId,
22
+ client_secret: this.clientSecret,
23
+ grant_type: "authorization_code",
24
+ code,
25
+ redirect_uri: this.redirectUri
26
+ });
27
+ return response.data;
28
+ }
29
+ async refreshToken(refreshToken) {
30
+ const url = `https://${this.domain}.kommo.com/oauth2/access_token`;
31
+ const response = await axios.post(url, {
32
+ client_id: this.clientId,
33
+ client_secret: this.clientSecret,
34
+ grant_type: "refresh_token",
35
+ refresh_token: refreshToken
36
+ });
37
+ return response.data;
38
+ }
39
+ };
40
+
41
+ // src/resources/leads/index.ts
42
+ import qs from "qs";
43
+
44
+ // src/base.ts
45
+ var BaseResource = class {
46
+ client;
47
+ constructor(client) {
48
+ this.client = client;
49
+ }
50
+ };
51
+
52
+ // src/resources/leads/index.ts
53
+ var LeadsResource = class extends BaseResource {
54
+ async list(params) {
55
+ const queryParams = {};
56
+ if (params) {
57
+ if (params.with) {
58
+ queryParams.with = Array.isArray(params.with) ? params.with.join(",") : params.with;
59
+ }
60
+ if (params.page) queryParams.page = params.page;
61
+ if (params.limit) queryParams.limit = params.limit;
62
+ if (params.query) queryParams.query = params.query;
63
+ const filter = {};
64
+ const arrayFilters = ["id", "name", "created_by", "updated_by", "responsible_user_id", "pipeline_id"];
65
+ arrayFilters.forEach((k) => {
66
+ const key = `filter_${k}`;
67
+ if (params[key] !== void 0) filter[k] = params[key];
68
+ });
69
+ if (params.filter_price !== void 0) filter.price = params.filter_price;
70
+ const rangeFilters = ["created_at", "updated_at", "closed_at", "closest_task_at"];
71
+ rangeFilters.forEach((k) => {
72
+ const fromKey = `filter_${k}_from`;
73
+ const toKey = `filter_${k}_to`;
74
+ if (params[fromKey] !== void 0 || params[toKey] !== void 0) {
75
+ filter[k] = {};
76
+ if (params[fromKey] !== void 0) filter[k].from = params[fromKey];
77
+ if (params[toKey] !== void 0) filter[k].to = params[toKey];
78
+ }
79
+ });
80
+ if (params.filter_statuses && params.filter_statuses.length > 0) {
81
+ filter.statuses = params.filter_statuses;
82
+ }
83
+ if (Object.keys(filter).length > 0) {
84
+ queryParams.filter = filter;
85
+ }
86
+ const order = {};
87
+ if (params.order_created_at) order.created_at = params.order_created_at;
88
+ if (params.order_updated_at) order.updated_at = params.order_updated_at;
89
+ if (params.order_id) order.id = params.order_id;
90
+ if (Object.keys(order).length > 0) {
91
+ queryParams.order = order;
92
+ }
93
+ }
94
+ const query = qs.stringify(queryParams, { skipNulls: true, arrayFormat: "brackets" });
95
+ const qsStr = query ? `?${query}` : "";
96
+ const response = await this.client.get(`/api/v4/leads${qsStr}`);
97
+ return response.data;
98
+ }
99
+ async getById(id, params) {
100
+ try {
101
+ const queryParams = {};
102
+ if (params?.with) {
103
+ queryParams.with = Array.isArray(params.with) ? params.with.join(",") : params.with;
104
+ }
105
+ const query = qs.stringify(queryParams, { skipNulls: true });
106
+ const qsStr = query ? `?${query}` : "";
107
+ const response = await this.client.get(`/api/v4/leads/${id}${qsStr}`);
108
+ if (response.status === 204 || !response.data || response.data === "") {
109
+ return null;
110
+ }
111
+ return response.data;
112
+ } catch (error) {
113
+ if (error.response && error.response.status === 204) {
114
+ return null;
115
+ }
116
+ throw error;
117
+ }
118
+ }
119
+ async create(items) {
120
+ const payload = Array.isArray(items) ? items : [items];
121
+ const response = await this.client.post("/api/v4/leads", payload);
122
+ return response.data;
123
+ }
124
+ async update(items) {
125
+ const payload = Array.isArray(items) ? items : [items];
126
+ const response = await this.client.patch("/api/v4/leads", payload);
127
+ return response.data;
128
+ }
129
+ async updateOne(id, payload) {
130
+ const response = await this.client.patch(`/api/v4/leads/${id}`, payload);
131
+ return response.data;
132
+ }
133
+ async createComplex(items) {
134
+ const payload = Array.isArray(items) ? items : [items];
135
+ const response = await this.client.post("/api/v4/leads/complex", payload);
136
+ return response.data;
137
+ }
138
+ async listLossReasons() {
139
+ const response = await this.client.get("/api/v4/leads/loss_reasons");
140
+ return response.data;
141
+ }
142
+ async getLossReasonById(id) {
143
+ const response = await this.client.get(`/api/v4/leads/loss_reasons/${id}`);
144
+ return response.data;
145
+ }
146
+ };
147
+
148
+ // src/resources/contacts/index.ts
149
+ import qs2 from "qs";
150
+ var ContactsResource = class extends BaseResource {
151
+ async list(params) {
152
+ const queryParams = {};
153
+ if (params) {
154
+ if (params.with) {
155
+ queryParams.with = Array.isArray(params.with) ? params.with.join(",") : params.with;
156
+ }
157
+ if (params.page) queryParams.page = params.page;
158
+ if (params.limit) queryParams.limit = params.limit;
159
+ if (params.query) queryParams.query = params.query;
160
+ const filter = {};
161
+ const arrayFilters = ["id", "name", "created_by", "updated_by", "responsible_user_id"];
162
+ arrayFilters.forEach((k) => {
163
+ const key = `filter_${k}`;
164
+ if (params[key] !== void 0) filter[k] = params[key];
165
+ });
166
+ const rangeFilters = ["created_at", "updated_at", "closest_task_at"];
167
+ rangeFilters.forEach((k) => {
168
+ const fromKey = `filter_${k}_from`;
169
+ const toKey = `filter_${k}_to`;
170
+ if (params[fromKey] !== void 0 || params[toKey] !== void 0) {
171
+ filter[k] = {};
172
+ if (params[fromKey] !== void 0) filter[k].from = params[fromKey];
173
+ if (params[toKey] !== void 0) filter[k].to = params[toKey];
174
+ }
175
+ });
176
+ if (Object.keys(filter).length > 0) {
177
+ queryParams.filter = filter;
178
+ }
179
+ const order = {};
180
+ if (params.order_updated_at) order.updated_at = params.order_updated_at;
181
+ if (params.order_id) order.id = params.order_id;
182
+ if (Object.keys(order).length > 0) {
183
+ queryParams.order = order;
184
+ }
185
+ }
186
+ const query = qs2.stringify(queryParams, { skipNulls: true, arrayFormat: "brackets" });
187
+ const qsStr = query ? `?${query}` : "";
188
+ const response = await this.client.get(`/api/v4/contacts${qsStr}`);
189
+ return response.data;
190
+ }
191
+ async getById(id, params) {
192
+ try {
193
+ const queryParams = {};
194
+ if (params?.with) {
195
+ queryParams.with = Array.isArray(params.with) ? params.with.join(",") : params.with;
196
+ }
197
+ const query = qs2.stringify(queryParams, { skipNulls: true });
198
+ const qsStr = query ? `?${query}` : "";
199
+ const response = await this.client.get(`/api/v4/contacts/${id}${qsStr}`);
200
+ if (response.status === 204 || !response.data || response.data === "") {
201
+ return null;
202
+ }
203
+ return response.data;
204
+ } catch (error) {
205
+ if (error.response && error.response.status === 204) {
206
+ return null;
207
+ }
208
+ throw error;
209
+ }
210
+ }
211
+ async create(items) {
212
+ const payload = Array.isArray(items) ? items : [items];
213
+ const response = await this.client.post("/api/v4/contacts", payload);
214
+ return response.data;
215
+ }
216
+ async update(items) {
217
+ const payload = Array.isArray(items) ? items : [items];
218
+ const response = await this.client.patch("/api/v4/contacts", payload);
219
+ return response.data;
220
+ }
221
+ async updateOne(id, payload) {
222
+ const response = await this.client.patch(`/api/v4/contacts/${id}`, payload);
223
+ return response.data;
224
+ }
225
+ };
226
+
227
+ // src/resources/tasks/index.ts
228
+ import qs3 from "qs";
229
+ var TasksResource = class extends BaseResource {
230
+ async list(params) {
231
+ const queryParams = {};
232
+ if (params) {
233
+ if (params.page) queryParams.page = params.page;
234
+ if (params.limit) queryParams.limit = params.limit;
235
+ const filter = {};
236
+ if (params.filter_responsible_user_id !== void 0) filter.responsible_user_id = params.filter_responsible_user_id;
237
+ if (params.filter_is_completed !== void 0) filter.is_completed = params.filter_is_completed;
238
+ if (params.filter_task_type !== void 0) filter.task_type = params.filter_task_type;
239
+ if (params.filter_entity_type !== void 0) filter.entity_type = params.filter_entity_type;
240
+ if (params.filter_entity_id !== void 0) filter.entity_id = params.filter_entity_id;
241
+ if (params.filter_id !== void 0) filter.id = params.filter_id;
242
+ if (params.filter_updated_at_from !== void 0 || params.filter_updated_at_to !== void 0) {
243
+ filter.updated_at = {};
244
+ if (params.filter_updated_at_from !== void 0) filter.updated_at.from = params.filter_updated_at_from;
245
+ if (params.filter_updated_at_to !== void 0) filter.updated_at.to = params.filter_updated_at_to;
246
+ }
247
+ if (Object.keys(filter).length > 0) {
248
+ queryParams.filter = filter;
249
+ }
250
+ const order = {};
251
+ if (params.order_complete_till !== void 0) order.complete_till = params.order_complete_till;
252
+ if (params.order_created_at !== void 0) order.created_at = params.order_created_at;
253
+ if (params.order_id !== void 0) order.id = params.order_id;
254
+ if (Object.keys(order).length > 0) {
255
+ queryParams.order = order;
256
+ }
257
+ }
258
+ const query = qs3.stringify(queryParams, { skipNulls: true, arrayFormat: "brackets" });
259
+ const qsStr = query ? `?${query}` : "";
260
+ const response = await this.client.get(`/api/v4/tasks${qsStr}`);
261
+ return response.data;
262
+ }
263
+ async getById(id) {
264
+ const response = await this.client.get(`/api/v4/tasks/${id}`);
265
+ return response.data;
266
+ }
267
+ async create(items) {
268
+ const payload = Array.isArray(items) ? items : [items];
269
+ const response = await this.client.post("/api/v4/tasks", payload);
270
+ return response.data;
271
+ }
272
+ async update(items) {
273
+ const payload = Array.isArray(items) ? items : [items];
274
+ const response = await this.client.patch("/api/v4/tasks", payload);
275
+ return response.data;
276
+ }
277
+ async updateOne(id, payload) {
278
+ const response = await this.client.patch(`/api/v4/tasks/${id}`, payload);
279
+ return response.data;
280
+ }
281
+ };
282
+
283
+ // src/resources/notes/index.ts
284
+ import qs4 from "qs";
285
+ var NotesResource = class extends BaseResource {
286
+ normalizeEntityType(type) {
287
+ if (type === "contatos") return "contacts";
288
+ if (type === "empresas") return "companies";
289
+ return type;
290
+ }
291
+ async listByEntity(entityType, entityId, params) {
292
+ const normalType = this.normalizeEntityType(entityType);
293
+ const queryParams = {};
294
+ if (params) {
295
+ if (params.page) queryParams.page = params.page;
296
+ if (params.limit) queryParams.limit = params.limit;
297
+ const filter = {};
298
+ if (params.filter_id !== void 0) filter.id = params.filter_id;
299
+ if (params.filter_note_type !== void 0) filter.note_type = params.filter_note_type;
300
+ if (params.filter_updated_at_from !== void 0 || params.filter_updated_at_to !== void 0) {
301
+ filter.updated_at = {};
302
+ if (params.filter_updated_at_from !== void 0) filter.updated_at.from = params.filter_updated_at_from;
303
+ if (params.filter_updated_at_to !== void 0) filter.updated_at.to = params.filter_updated_at_to;
304
+ }
305
+ if (Object.keys(filter).length > 0) {
306
+ queryParams.filter = filter;
307
+ }
308
+ const order = {};
309
+ if (params.order_updated_at !== void 0) order.updated_at = params.order_updated_at;
310
+ if (params.order_id !== void 0) order.id = params.order_id;
311
+ if (Object.keys(order).length > 0) {
312
+ queryParams.order = order;
313
+ }
314
+ }
315
+ const query = qs4.stringify(queryParams, { skipNulls: true, arrayFormat: "brackets" });
316
+ const qsStr = query ? `?${query}` : "";
317
+ const response = await this.client.get(`/api/v4/${normalType}/${entityId}/notes${qsStr}`);
318
+ return response.data;
319
+ }
320
+ async listByEntityType(entityType, params) {
321
+ const normalType = this.normalizeEntityType(entityType);
322
+ const queryParams = {};
323
+ if (params) {
324
+ if (params.page) queryParams.page = params.page;
325
+ if (params.limit) queryParams.limit = params.limit;
326
+ const filter = {};
327
+ if (params.filter_id !== void 0) filter.id = params.filter_id;
328
+ if (params.filter_note_type !== void 0) filter.note_type = params.filter_note_type;
329
+ if (params.filter_updated_at_from !== void 0 || params.filter_updated_at_to !== void 0) {
330
+ filter.updated_at = {};
331
+ if (params.filter_updated_at_from !== void 0) filter.updated_at.from = params.filter_updated_at_from;
332
+ if (params.filter_updated_at_to !== void 0) filter.updated_at.to = params.filter_updated_at_to;
333
+ }
334
+ if (Object.keys(filter).length > 0) {
335
+ queryParams.filter = filter;
336
+ }
337
+ const order = {};
338
+ if (params.order_updated_at !== void 0) order.updated_at = params.order_updated_at;
339
+ if (params.order_id !== void 0) order.id = params.order_id;
340
+ if (Object.keys(order).length > 0) {
341
+ queryParams.order = order;
342
+ }
343
+ }
344
+ const query = qs4.stringify(queryParams, { skipNulls: true, arrayFormat: "brackets" });
345
+ const qsStr = query ? `?${query}` : "";
346
+ const response = await this.client.get(`/api/v4/${normalType}/notes${qsStr}`);
347
+ return response.data;
348
+ }
349
+ async getById(entityType, id) {
350
+ const normalType = this.normalizeEntityType(entityType);
351
+ const response = await this.client.get(`/api/v4/${normalType}/notes/${id}`);
352
+ return response.data;
353
+ }
354
+ async create(entityType, items) {
355
+ const normalType = this.normalizeEntityType(entityType);
356
+ const payload = Array.isArray(items) ? items : [items];
357
+ const response = await this.client.post(`/api/v4/${normalType}/notes`, payload);
358
+ return response.data;
359
+ }
360
+ async update(entityType, items) {
361
+ const normalType = this.normalizeEntityType(entityType);
362
+ const payload = Array.isArray(items) ? items : [items];
363
+ const response = await this.client.patch(`/api/v4/${normalType}/notes`, payload);
364
+ return response.data;
365
+ }
366
+ async updateOne(entityType, id, payload) {
367
+ const normalType = this.normalizeEntityType(entityType);
368
+ const response = await this.client.patch(`/api/v4/${normalType}/notes/${id}`, payload);
369
+ return response.data;
370
+ }
371
+ async pin(entityType, id) {
372
+ const normalType = this.normalizeEntityType(entityType);
373
+ await this.client.post(`/api/v4/${normalType}/notes/${id}/pin`);
374
+ }
375
+ async unpin(entityType, id) {
376
+ const normalType = this.normalizeEntityType(entityType);
377
+ await this.client.post(`/api/v4/${normalType}/notes/${id}/unpin`);
378
+ }
379
+ };
380
+
381
+ // src/resources/custom-fields/index.ts
382
+ var CustomFieldsResource = class extends BaseResource {
383
+ // ==== CAMPOS DE ENTIDADES ====
384
+ async listEntity(entityType) {
385
+ const response = await this.client.get(`/api/v4/${entityType}/custom_fields`);
386
+ return response.data;
387
+ }
388
+ async getEntityById(entityType, id) {
389
+ const response = await this.client.get(`/api/v4/${entityType}/custom_fields/${id}`);
390
+ return response.data;
391
+ }
392
+ async createEntity(entityType, items) {
393
+ const payload = Array.isArray(items) ? items : [items];
394
+ const response = await this.client.post(`/api/v4/${entityType}/custom_fields`, payload);
395
+ return response.data;
396
+ }
397
+ async updateEntity(entityType, items) {
398
+ const payload = Array.isArray(items) ? items : [items];
399
+ const response = await this.client.patch(`/api/v4/${entityType}/custom_fields`, payload);
400
+ return response.data;
401
+ }
402
+ async updateEntityById(entityType, id, payload) {
403
+ const response = await this.client.patch(`/api/v4/${entityType}/custom_fields/${id}`, payload);
404
+ return response.data;
405
+ }
406
+ async deleteEntity(entityType, id) {
407
+ await this.client.delete(`/api/v4/${entityType}/custom_fields/${id}`);
408
+ }
409
+ // ==== CAMPOS DE LISTAS ====
410
+ async listList(listId) {
411
+ const response = await this.client.get(`/api/v4/catalogs/${listId}/custom_fields`);
412
+ return response.data;
413
+ }
414
+ async getListById(listId, customFieldId) {
415
+ const response = await this.client.get(`/api/v4/catalogs/${listId}/custom_fields/${customFieldId}`);
416
+ return response.data;
417
+ }
418
+ async createList(listId, items) {
419
+ const payload = Array.isArray(items) ? items : [items];
420
+ const response = await this.client.post(`/api/v4/catalogs/${listId}/custom_fields`, payload);
421
+ return response.data;
422
+ }
423
+ async updateList(listId, items) {
424
+ const payload = Array.isArray(items) ? items : [items];
425
+ const response = await this.client.patch(`/api/v4/catalogs/${listId}/custom_fields`, payload);
426
+ return response.data;
427
+ }
428
+ async updateListById(listId, customFieldId, payload) {
429
+ const response = await this.client.patch(`/api/v4/catalogs/${listId}/custom_fields/${customFieldId}`, payload);
430
+ return response.data;
431
+ }
432
+ async deleteList(listId, id) {
433
+ await this.client.delete(`/api/v4/catalogs/${listId}/custom_fields/${id}`);
434
+ }
435
+ };
436
+ var CustomFieldGroupsResource = class extends BaseResource {
437
+ async list(entityType) {
438
+ const response = await this.client.get(`/api/v4/${entityType}/custom_fields/groups`);
439
+ return response.data;
440
+ }
441
+ async getById(entityType, id) {
442
+ const response = await this.client.get(`/api/v4/${entityType}/custom_fields/groups/${id}`);
443
+ return response.data;
444
+ }
445
+ async create(entityType, items) {
446
+ const payload = Array.isArray(items) ? items : [items];
447
+ const response = await this.client.post(`/api/v4/${entityType}/custom_fields/groups`, payload);
448
+ return response.data;
449
+ }
450
+ async update(entityType, id, payload) {
451
+ const response = await this.client.patch(`/api/v4/${entityType}/custom_fields/groups/${id}`, payload);
452
+ return response.data;
453
+ }
454
+ async delete(entityType, id) {
455
+ await this.client.delete(`/api/v4/${entityType}/custom_fields/groups/${id}`);
456
+ }
457
+ };
458
+
459
+ // src/resources/unsorted/index.ts
460
+ import qs5 from "qs";
461
+ var UnsortedResource = class extends BaseResource {
462
+ async list(params) {
463
+ const query = qs5.stringify(
464
+ {
465
+ page: params?.page,
466
+ limit: params?.limit,
467
+ filter: {
468
+ uid: params?.uid,
469
+ category: params?.category,
470
+ pipeline_id: params?.pipeline_id
471
+ },
472
+ order: {
473
+ created_at: params?.order_created_at
474
+ }
475
+ },
476
+ { skipNulls: true, arrayFormat: "brackets" }
477
+ );
478
+ const qsStr = query ? `?${query}` : "";
479
+ const response = await this.client.get(`/api/v4/leads/unsorted${qsStr}`);
480
+ return response.data;
481
+ }
482
+ async getByUid(uid) {
483
+ const response = await this.client.get(`/api/v4/leads/unsorted/${uid}`);
484
+ return response.data;
485
+ }
486
+ async createSip(items) {
487
+ const payload = Array.isArray(items) ? items : [items];
488
+ const response = await this.client.post(
489
+ "/api/v4/leads/unsorted/sip",
490
+ payload
491
+ );
492
+ return response.data;
493
+ }
494
+ async createForms(items) {
495
+ const payload = Array.isArray(items) ? items : [items];
496
+ const response = await this.client.post(
497
+ "/api/v4/leads/unsorted/forms",
498
+ payload
499
+ );
500
+ return response.data;
501
+ }
502
+ async accept(uid, payload) {
503
+ const response = await this.client.post(
504
+ `/api/v4/leads/unsorted/${uid}/accept`,
505
+ payload || {}
506
+ );
507
+ return response.data;
508
+ }
509
+ async decline(uid, payload) {
510
+ const config = payload ? { data: payload } : {};
511
+ const response = await this.client.delete(
512
+ `/api/v4/leads/unsorted/${uid}/decline`,
513
+ config
514
+ );
515
+ return response.data;
516
+ }
517
+ async link(uid, payload) {
518
+ const response = await this.client.post(
519
+ `/api/v4/leads/unsorted/${uid}/link`,
520
+ payload
521
+ );
522
+ return response.data;
523
+ }
524
+ async summary(params) {
525
+ const queryParams = {};
526
+ if (params?.uid) queryParams.uid = params.uid;
527
+ if (params?.pipeline_id) queryParams.pipeline_id = params.pipeline_id;
528
+ if (params?.created_at) {
529
+ queryParams.created_at = params.created_at;
530
+ } else if (params?.created_at_from || params?.created_at_to) {
531
+ queryParams.created_at = {};
532
+ if (params.created_at_from) queryParams.created_at.from = params.created_at_from;
533
+ if (params.created_at_to) queryParams.created_at.to = params.created_at_to;
534
+ }
535
+ const query = qs5.stringify(
536
+ { filter: queryParams },
537
+ { skipNulls: true, arrayFormat: "brackets" }
538
+ );
539
+ const qsStr = query ? `?${query}` : "";
540
+ const response = await this.client.get(
541
+ `/api/v4/leads/unsorted/summary${qsStr}`
542
+ );
543
+ return response.data;
544
+ }
545
+ };
546
+
547
+ // src/resources/pipelines/index.ts
548
+ import qs6 from "qs";
549
+ var PipelinesResource = class extends BaseResource {
550
+ // PIPELINES
551
+ async list() {
552
+ const response = await this.client.get("/api/v4/leads/pipelines");
553
+ return response.data;
554
+ }
555
+ async getById(id) {
556
+ const response = await this.client.get(`/api/v4/leads/pipelines/${id}`);
557
+ return response.data;
558
+ }
559
+ async create(items) {
560
+ const payload = Array.isArray(items) ? items : [items];
561
+ const response = await this.client.post("/api/v4/leads/pipelines", payload);
562
+ return response.data;
563
+ }
564
+ async update(id, payload) {
565
+ const response = await this.client.patch(`/api/v4/leads/pipelines/${id}`, payload);
566
+ return response.data;
567
+ }
568
+ async delete(id) {
569
+ await this.client.delete(`/api/v4/leads/pipelines/${id}`);
570
+ }
571
+ // STATUSES (ETAPAS)
572
+ async listStatuses(pipelineId, params) {
573
+ const query = qs6.stringify({ with: params?.with }, { skipNulls: true });
574
+ const qsStr = query ? `?${query}` : "";
575
+ const response = await this.client.get(`/api/v4/leads/pipelines/${pipelineId}/statuses${qsStr}`);
576
+ return response.data;
577
+ }
578
+ async getStatusById(pipelineId, id, params) {
579
+ const query = qs6.stringify({ with: params?.with }, { skipNulls: true });
580
+ const qsStr = query ? `?${query}` : "";
581
+ const response = await this.client.get(`/api/v4/leads/pipelines/${pipelineId}/statuses/${id}${qsStr}`);
582
+ return response.data;
583
+ }
584
+ async createStatuses(pipelineId, items) {
585
+ const payload = Array.isArray(items) ? items : [items];
586
+ payload.forEach((item) => this.validateStatusDescriptions(item.descriptions));
587
+ const response = await this.client.post(`/api/v4/leads/pipelines/${pipelineId}/statuses`, payload);
588
+ return response.data;
589
+ }
590
+ async updateStatus(pipelineId, id, payload) {
591
+ this.validateStatusDescriptions(payload.descriptions);
592
+ const response = await this.client.patch(`/api/v4/leads/pipelines/${pipelineId}/statuses/${id}`, payload);
593
+ return response.data;
594
+ }
595
+ async deleteStatus(pipelineId, id) {
596
+ await this.client.delete(`/api/v4/leads/pipelines/${pipelineId}/statuses/${id}`);
597
+ }
598
+ validateStatusDescriptions(descriptions) {
599
+ if (!descriptions) return;
600
+ if (descriptions.length > 3) {
601
+ throw new Error("A etapa aceita no m\xE1ximo 3 descri\xE7\xF5es.");
602
+ }
603
+ const levels = descriptions.map((d) => d.level);
604
+ const uniqueLevels = new Set(levels);
605
+ if (levels.length !== uniqueLevels.size) {
606
+ throw new Error("N\xE3o \xE9 permitido repetir level em descriptions.");
607
+ }
608
+ for (const item of descriptions) {
609
+ if (item.description.length > 1e3) {
610
+ throw new Error("A descri\xE7\xE3o da etapa pode ter no m\xE1ximo 1000 caracteres.");
611
+ }
612
+ }
613
+ }
614
+ };
615
+
616
+ // src/resources/companies/index.ts
617
+ import qs7 from "qs";
618
+ var CompaniesResource = class extends BaseResource {
619
+ async list(params) {
620
+ const queryParams = {};
621
+ if (params) {
622
+ if (params.with) {
623
+ queryParams.with = Array.isArray(params.with) ? params.with.join(",") : params.with;
624
+ }
625
+ if (params.page) queryParams.page = params.page;
626
+ if (params.limit) queryParams.limit = params.limit;
627
+ if (params.query) queryParams.query = params.query;
628
+ const filter = {};
629
+ const arrayFilters = ["id", "name", "created_by", "updated_by", "responsible_user_id"];
630
+ arrayFilters.forEach((k) => {
631
+ const key = `filter_${k}`;
632
+ if (params[key] !== void 0) filter[k] = params[key];
633
+ });
634
+ const rangeFilters = ["created_at", "updated_at", "closest_task_at"];
635
+ rangeFilters.forEach((k) => {
636
+ const fromKey = `filter_${k}_from`;
637
+ const toKey = `filter_${k}_to`;
638
+ if (params[fromKey] !== void 0 || params[toKey] !== void 0) {
639
+ filter[k] = {};
640
+ if (params[fromKey] !== void 0) filter[k].from = params[fromKey];
641
+ if (params[toKey] !== void 0) filter[k].to = params[toKey];
642
+ }
643
+ });
644
+ if (Object.keys(filter).length > 0) {
645
+ queryParams.filter = filter;
646
+ }
647
+ const order = {};
648
+ if (params.order_updated_at) order.updated_at = params.order_updated_at;
649
+ if (params.order_id) order.id = params.order_id;
650
+ if (Object.keys(order).length > 0) {
651
+ queryParams.order = order;
652
+ }
653
+ }
654
+ const query = qs7.stringify(queryParams, { skipNulls: true, arrayFormat: "brackets" });
655
+ const qsStr = query ? `?${query}` : "";
656
+ const response = await this.client.get(`/api/v4/companies${qsStr}`);
657
+ return response.data;
658
+ }
659
+ async getById(id, params) {
660
+ try {
661
+ const queryParams = {};
662
+ if (params?.with) {
663
+ queryParams.with = Array.isArray(params.with) ? params.with.join(",") : params.with;
664
+ }
665
+ const query = qs7.stringify(queryParams, { skipNulls: true });
666
+ const qsStr = query ? `?${query}` : "";
667
+ const response = await this.client.get(`/api/v4/companies/${id}${qsStr}`);
668
+ if (response.status === 204 || !response.data || response.data === "") {
669
+ return null;
670
+ }
671
+ return response.data;
672
+ } catch (error) {
673
+ if (error.response && error.response.status === 204) {
674
+ return null;
675
+ }
676
+ throw error;
677
+ }
678
+ }
679
+ async create(items) {
680
+ const payload = Array.isArray(items) ? items : [items];
681
+ const response = await this.client.post("/api/v4/companies", payload);
682
+ return response.data;
683
+ }
684
+ async update(items) {
685
+ const payload = Array.isArray(items) ? items : [items];
686
+ const response = await this.client.patch("/api/v4/companies", payload);
687
+ return response.data;
688
+ }
689
+ async updateOne(id, payload) {
690
+ const response = await this.client.patch(`/api/v4/companies/${id}`, payload);
691
+ return response.data;
692
+ }
693
+ };
694
+
695
+ // src/resources/catalogs/index.ts
696
+ import qs8 from "qs";
697
+ var CatalogsResource = class extends BaseResource {
698
+ // ================= CATÁLOGOS (LISTAS) =================
699
+ async list(params) {
700
+ const query = qs8.stringify({ page: params?.page, limit: params?.limit }, { skipNulls: true });
701
+ const qsStr = query ? `?${query}` : "";
702
+ const response = await this.client.get(`/api/v4/catalogs${qsStr}`);
703
+ return response.data;
704
+ }
705
+ async getById(id) {
706
+ const response = await this.client.get(`/api/v4/catalogs/${id}`);
707
+ return response.data;
708
+ }
709
+ async create(items) {
710
+ const payload = Array.isArray(items) ? items : [items];
711
+ const response = await this.client.post("/api/v4/catalogs", payload);
712
+ return response.data;
713
+ }
714
+ async update(items) {
715
+ const payload = Array.isArray(items) ? items : [items];
716
+ const response = await this.client.patch("/api/v4/catalogs", payload);
717
+ return response.data;
718
+ }
719
+ async updateOne(id, payload) {
720
+ const response = await this.client.patch(`/api/v4/catalogs/${id}`, payload);
721
+ return response.data;
722
+ }
723
+ // ================= ELEMENTOS DO CATÁLOGO =================
724
+ async listElements(listId, params) {
725
+ const queryParams = {};
726
+ if (params) {
727
+ if (params.page) queryParams.page = params.page;
728
+ if (params.limit) queryParams.limit = params.limit;
729
+ if (params.query) queryParams.query = params.query;
730
+ const filter = {};
731
+ if (params.filter_id !== void 0) filter.id = params.filter_id;
732
+ if (Object.keys(filter).length > 0) {
733
+ queryParams.filter = filter;
734
+ }
735
+ }
736
+ const query = qs8.stringify(queryParams, { skipNulls: true, arrayFormat: "brackets" });
737
+ const qsStr = query ? `?${query}` : "";
738
+ const response = await this.client.get(`/api/v4/catalogs/${listId}/elements${qsStr}`);
739
+ return response.data;
740
+ }
741
+ async getElementById(listId, elementId) {
742
+ const response = await this.client.get(`/api/v4/catalogs/${listId}/elements/${elementId}`);
743
+ return response.data;
744
+ }
745
+ async createElements(listId, items) {
746
+ const payload = Array.isArray(items) ? items : [items];
747
+ const response = await this.client.post(`/api/v4/catalogs/${listId}/elements`, payload);
748
+ return response.data;
749
+ }
750
+ async updateElements(listId, items) {
751
+ const payload = Array.isArray(items) ? items : [items];
752
+ const response = await this.client.patch(`/api/v4/catalogs/${listId}/elements`, payload);
753
+ return response.data;
754
+ }
755
+ async updateElementById(listId, elementId, payload) {
756
+ const response = await this.client.patch(`/api/v4/catalogs/${listId}/elements/${elementId}`, payload);
757
+ return response.data;
758
+ }
759
+ };
760
+
761
+ // src/resources/users/index.ts
762
+ import qs9 from "qs";
763
+ var UsersResource = class extends BaseResource {
764
+ async list(params) {
765
+ const queryParams = {};
766
+ if (params) {
767
+ if (params.with) queryParams.with = Array.isArray(params.with) ? params.with.join(",") : params.with;
768
+ if (params.page) queryParams.page = params.page;
769
+ if (params.limit) queryParams.limit = params.limit;
770
+ }
771
+ const query = qs9.stringify(queryParams, { skipNulls: true });
772
+ const qsStr = query ? `?${query}` : "";
773
+ const response = await this.client.get(`/api/v4/users${qsStr}`);
774
+ return response.data;
775
+ }
776
+ async getById(id, params) {
777
+ const queryParams = {};
778
+ if (params?.with) {
779
+ queryParams.with = Array.isArray(params.with) ? params.with.join(",") : params.with;
780
+ }
781
+ const query = qs9.stringify(queryParams, { skipNulls: true });
782
+ const qsStr = query ? `?${query}` : "";
783
+ const response = await this.client.get(`/api/v4/users/${id}${qsStr}`);
784
+ return response.data;
785
+ }
786
+ async create(items) {
787
+ const payload = Array.isArray(items) ? items : [items];
788
+ if (payload.length > 10) {
789
+ throw new Error("A API da Kommo permite criar no m\xE1ximo 10 usu\xE1rios por requisi\xE7\xE3o.");
790
+ }
791
+ const response = await this.client.post("/api/v4/users", payload);
792
+ return response.data;
793
+ }
794
+ };
795
+ var RolesResource = class extends BaseResource {
796
+ async list(params) {
797
+ const queryParams = {};
798
+ if (params?.with) {
799
+ queryParams.with = Array.isArray(params.with) ? params.with.join(",") : params.with;
800
+ }
801
+ const query = qs9.stringify(queryParams, { skipNulls: true });
802
+ const qsStr = query ? `?${query}` : "";
803
+ const response = await this.client.get(`/api/v4/roles${qsStr}`);
804
+ return response.data;
805
+ }
806
+ async getById(id, params) {
807
+ const queryParams = {};
808
+ if (params?.with) {
809
+ queryParams.with = Array.isArray(params.with) ? params.with.join(",") : params.with;
810
+ }
811
+ const query = qs9.stringify(queryParams, { skipNulls: true });
812
+ const qsStr = query ? `?${query}` : "";
813
+ const response = await this.client.get(`/api/v4/roles/${id}${qsStr}`);
814
+ return response.data;
815
+ }
816
+ async create(items) {
817
+ const payload = Array.isArray(items) ? items : [items];
818
+ const response = await this.client.post("/api/v4/roles", payload);
819
+ return response.data;
820
+ }
821
+ async update(items) {
822
+ const payload = Array.isArray(items) ? items : [items];
823
+ const response = await this.client.patch("/api/v4/roles", payload);
824
+ return response.data;
825
+ }
826
+ async delete(id) {
827
+ await this.client.delete(`/api/v4/roles/${id}`);
828
+ }
829
+ };
830
+
831
+ // src/resources/events/index.ts
832
+ import qs10 from "qs";
833
+ var EventsResource = class extends BaseResource {
834
+ async list(params) {
835
+ const queryParams = {};
836
+ if (params) {
837
+ if (params.page) queryParams.page = params.page;
838
+ if (params.limit) queryParams.limit = params.limit;
839
+ if (params.with) queryParams.with = Array.isArray(params.with) ? params.with.join(",") : params.with;
840
+ const filter = {};
841
+ if (params.filter_id !== void 0) filter.id = params.filter_id;
842
+ if (params.filter_created_by !== void 0) filter.created_by = params.filter_created_by;
843
+ if (params.filter_entity !== void 0) filter.entity = params.filter_entity;
844
+ if (params.filter_entity_id !== void 0) {
845
+ if (!params.filter_entity || Array.isArray(params.filter_entity) && params.filter_entity.length !== 1) {
846
+ throw new Error("O filtro de 'entity_id' s\xF3 pode ser passado quando houver uma \xFAnica 'entity' informada.");
847
+ }
848
+ filter.entity_id = params.filter_entity_id;
849
+ }
850
+ if (params.filter_type !== void 0) filter.type = params.filter_type;
851
+ if (params.filter_created_at_from !== void 0 || params.filter_created_at_to !== void 0) {
852
+ filter.created_at = {};
853
+ if (params.filter_created_at_from !== void 0) filter.created_at.from = params.filter_created_at_from;
854
+ if (params.filter_created_at_to !== void 0) filter.created_at.to = params.filter_created_at_to;
855
+ }
856
+ if (params.filter_value_before !== void 0) filter.value_before = params.filter_value_before;
857
+ if (params.filter_value_after !== void 0) filter.value_after = params.filter_value_after;
858
+ if (Object.keys(filter).length > 0) {
859
+ queryParams.filter = filter;
860
+ }
861
+ }
862
+ const query = qs10.stringify(queryParams, { skipNulls: true, arrayFormat: "brackets" });
863
+ const qsStr = query ? `?${query}` : "";
864
+ const response = await this.client.get(`/api/v4/events${qsStr}`);
865
+ return response.data;
866
+ }
867
+ async getById(id, params) {
868
+ const queryParams = {};
869
+ if (params?.with) {
870
+ queryParams.with = Array.isArray(params.with) ? params.with.join(",") : params.with;
871
+ }
872
+ const query = qs10.stringify(queryParams, { skipNulls: true });
873
+ const qsStr = query ? `?${query}` : "";
874
+ const response = await this.client.get(`/api/v4/events/${id}${qsStr}`);
875
+ return response.data;
876
+ }
877
+ async listTypes(params) {
878
+ const queryParams = {};
879
+ if (params?.language_code) {
880
+ queryParams.language_code = params.language_code;
881
+ }
882
+ const query = qs10.stringify(queryParams, { skipNulls: true });
883
+ const qsStr = query ? `?${query}` : "";
884
+ const response = await this.client.get(`/api/v4/events/types${qsStr}`);
885
+ return response.data;
886
+ }
887
+ };
888
+
889
+ // src/resources/tags/index.ts
890
+ import qs11 from "qs";
891
+ var TagsResource = class extends BaseResource {
892
+ async list(entityType, params) {
893
+ const queryParams = {};
894
+ if (params) {
895
+ if (params.page !== void 0) queryParams.page = params.page;
896
+ if (params.limit !== void 0) queryParams.limit = params.limit;
897
+ if (params.query !== void 0) queryParams.query = params.query;
898
+ const filter = {};
899
+ if (params.filter_id !== void 0) filter.id = params.filter_id;
900
+ if (params.filter_name !== void 0) filter.name = params.filter_name;
901
+ if (Object.keys(filter).length > 0) {
902
+ queryParams.filter = filter;
903
+ }
904
+ }
905
+ const query = qs11.stringify(queryParams, { skipNulls: true, arrayFormat: "brackets" });
906
+ const qsStr = query ? `?${query}` : "";
907
+ const response = await this.client.get(`/api/v4/${entityType}/tags${qsStr}`);
908
+ if (response.status === 204 || !response.data) {
909
+ return { _page: 1, _embedded: { tags: [] } };
910
+ }
911
+ return response.data;
912
+ }
913
+ async create(entityType, items) {
914
+ const payload = Array.isArray(items) ? items : [items];
915
+ if (entityType !== "leads") {
916
+ const hasColor = payload.some((p) => p.color !== void 0);
917
+ if (hasColor) {
918
+ console.warn(`[TagsResource] A documenta\xE7\xE3o indica que propriedades de cor na tag s\xF3 s\xE3o aceitas para a entidade 'leads'. Sua requisi\xE7\xE3o foi feita para '${entityType}' com cor.`);
919
+ }
920
+ }
921
+ const response = await this.client.post(`/api/v4/${entityType}/tags`, payload);
922
+ return response.data;
923
+ }
924
+ async updateForMany(entityType, items) {
925
+ const payload = Array.isArray(items) ? items : [items];
926
+ const response = await this.client.patch(`/api/v4/${entityType}`, payload);
927
+ return response.data;
928
+ }
929
+ async updateForOne(entityType, id, payload) {
930
+ const response = await this.client.patch(`/api/v4/${entityType}/${id}`, payload);
931
+ return response.data;
932
+ }
933
+ };
934
+
935
+ // src/resources/links/index.ts
936
+ import qs12 from "qs";
937
+ var LinksResource = class extends BaseResource {
938
+ async list(entity, entityId, params) {
939
+ const queryParams = {};
940
+ if (params) {
941
+ const filter = {};
942
+ const hasEntityId = params.filter_to_entity_id !== void 0;
943
+ const hasEntityType = params.filter_to_entity_type !== void 0;
944
+ if (hasEntityId !== hasEntityType) {
945
+ throw new Error("A API exige que 'filter_to_entity_id' e 'filter_to_entity_type' sejam fornecidos juntos no filtro de v\xEDnculos.");
946
+ }
947
+ if (hasEntityId) filter.to_entity_id = params.filter_to_entity_id;
948
+ if (hasEntityType) filter.to_entity_type = params.filter_to_entity_type;
949
+ if (params.filter_to_catalog_id !== void 0) filter.to_catalog_id = params.filter_to_catalog_id;
950
+ if (Object.keys(filter).length > 0) {
951
+ queryParams.filter = filter;
952
+ }
953
+ }
954
+ const query = qs12.stringify(queryParams, { skipNulls: true, arrayFormat: "brackets" });
955
+ const qsStr = query ? `?${query}` : "";
956
+ const response = await this.client.get(`/api/v4/${entity}/${entityId}/links${qsStr}`);
957
+ return response.data;
958
+ }
959
+ async link(entity, entityId, items) {
960
+ const payload = Array.isArray(items) ? items : [items];
961
+ const response = await this.client.post(`/api/v4/${entity}/${entityId}/link`, payload);
962
+ return response.data;
963
+ }
964
+ async unlink(entity, entityId, items) {
965
+ const payload = Array.isArray(items) ? items : [items];
966
+ await this.client.post(`/api/v4/${entity}/${entityId}/unlink`, payload);
967
+ }
968
+ };
969
+
970
+ // src/resources/salesbots/index.ts
971
+ import qs13 from "qs";
972
+ var SalesbotsResource = class extends BaseResource {
973
+ async list(params) {
974
+ const queryParams = {};
975
+ if (params) {
976
+ if (params.page !== void 0) queryParams.page = params.page;
977
+ if (params.limit !== void 0) queryParams.limit = params.limit;
978
+ if (params.with !== void 0) queryParams.with = params.with;
979
+ const filter = {};
980
+ if (params.filter_id !== void 0) filter.id = params.filter_id;
981
+ if (params.filter_type_functionality !== void 0) filter.type_functionality = params.filter_type_functionality;
982
+ if (Object.keys(filter).length > 0) {
983
+ queryParams.filter = filter;
984
+ }
985
+ }
986
+ const query = qs13.stringify(queryParams, { skipNulls: true, arrayFormat: "brackets" });
987
+ const qsStr = query ? `?${query}` : "";
988
+ const response = await this.client.get(`/api/v4/bots${qsStr}`);
989
+ if (response.status === 204 || !response.data) {
990
+ return { _embedded: { items: [] } };
991
+ }
992
+ return response.data;
993
+ }
994
+ async run(botId, payload) {
995
+ await this.client.post(`/api/v4/bots/${botId}/run`, payload);
996
+ }
997
+ async runMany(items) {
998
+ const payload = Array.isArray(items) ? items : [items];
999
+ if (payload.length > 100) {
1000
+ throw new Error(`A API da Kommo limita o 'runMany' a no m\xE1ximo 100 salesbots por requisi\xE7\xE3o. Voc\xEA enviou ${payload.length}.`);
1001
+ }
1002
+ await this.client.post(`/api/v4/bots/run`, payload);
1003
+ }
1004
+ async stop(botId, payload) {
1005
+ await this.client.post(`/api/v4/bots/${botId}/stop`, payload);
1006
+ }
1007
+ async continueWidgetExecution(bot, botId, continueId, payload) {
1008
+ if (payload.execute_handlers) {
1009
+ if (payload.execute_handlers.length > 10) {
1010
+ throw new Error(`A API da Kommo limita 'execute_handlers' a no m\xE1ximo 10 itens. Voc\xEA enviou ${payload.execute_handlers.length}.`);
1011
+ }
1012
+ for (const handler of payload.execute_handlers) {
1013
+ if (handler.handler === "show") {
1014
+ if (handler.params.value && handler.params.value.length > 80) {
1015
+ console.warn(`[SalesbotResource] O campo 'value' em um handler 'show' tem ${handler.params.value.length} caracteres. A documenta\xE7\xE3o aconselha no m\xE1ximo 80.`);
1016
+ }
1017
+ if ("buttons" in handler.params && handler.params.buttons && handler.params.buttons.length > 25) {
1018
+ throw new Error(`A API da Kommo n\xE3o permite mais do que 25 bot\xF5es. Voc\xEA tentou enviar ${handler.params.buttons.length}.`);
1019
+ }
1020
+ }
1021
+ }
1022
+ }
1023
+ await this.client.post(`/api/v4/${bot}/${botId}/continue/${continueId}`, payload);
1024
+ }
1025
+ };
1026
+
1027
+ // src/resources/sources/index.ts
1028
+ import qs14 from "qs";
1029
+ var SourcesResource = class extends BaseResource {
1030
+ async list() {
1031
+ const response = await this.client.get("/api/v4/sources");
1032
+ return response.data;
1033
+ }
1034
+ async getById(id) {
1035
+ const response = await this.client.get(`/api/v4/sources/${id}`);
1036
+ return response.data;
1037
+ }
1038
+ async create(items) {
1039
+ const payload = Array.isArray(items) ? items : [items];
1040
+ if (payload.length > 100) {
1041
+ throw new Error(`A API da Kommo limita a cria\xE7\xE3o a no m\xE1ximo 100 fontes por vez.`);
1042
+ }
1043
+ const response = await this.client.post("/api/v4/sources", payload);
1044
+ return response.data;
1045
+ }
1046
+ async updateOne(id, payload) {
1047
+ const response = await this.client.patch(`/api/v4/sources/${id}`, payload);
1048
+ return response.data;
1049
+ }
1050
+ async update(items) {
1051
+ const payload = Array.isArray(items) ? items : [items];
1052
+ if (payload.length > 100) {
1053
+ throw new Error(`A API da Kommo limita a atualiza\xE7\xE3o a no m\xE1ximo 100 fontes por vez.`);
1054
+ }
1055
+ const response = await this.client.patch("/api/v4/sources", payload);
1056
+ return response.data;
1057
+ }
1058
+ async deleteOne(id) {
1059
+ await this.client.delete(`/api/v4/sources/${id}`);
1060
+ }
1061
+ async delete(items) {
1062
+ const payload = Array.isArray(items) ? items : [items];
1063
+ await this.client.delete("/api/v4/sources", { data: payload });
1064
+ }
1065
+ };
1066
+ var WebsiteButtonsResource = class extends BaseResource {
1067
+ async list(params) {
1068
+ const queryParams = {};
1069
+ if (params) {
1070
+ if (params.page !== void 0) queryParams.page = params.page;
1071
+ if (params.limit !== void 0) queryParams.limit = params.limit;
1072
+ if (params.with !== void 0) queryParams.with = params.with;
1073
+ }
1074
+ const query = qs14.stringify(queryParams, { skipNulls: true });
1075
+ const qsStr = query ? `?${query}` : "";
1076
+ const response = await this.client.get(`/api/v4/website_buttons${qsStr}`);
1077
+ return response.data;
1078
+ }
1079
+ async getBySourceId(sourceId, params) {
1080
+ const queryParams = {};
1081
+ if (params && params.with !== void 0) {
1082
+ queryParams.with = params.with;
1083
+ }
1084
+ const query = qs14.stringify(queryParams, { skipNulls: true });
1085
+ const qsStr = query ? `?${query}` : "";
1086
+ const response = await this.client.get(`/api/v4/website_buttons/${sourceId}${qsStr}`);
1087
+ return response.data;
1088
+ }
1089
+ async create(payload) {
1090
+ if ((!payload.trusted_websites || payload.trusted_websites.length === 0) && !payload.is_used_in_app) {
1091
+ console.warn(`[WebsiteButtonsResource] A Kommo indica que se n\xE3o enviar 'trusted_websites', \xE9 preciso enviar 'is_used_in_app' como true.`);
1092
+ }
1093
+ const response = await this.client.post("/api/v4/website_buttons", payload);
1094
+ return response.data;
1095
+ }
1096
+ async connectOnlineChat(sourceId) {
1097
+ await this.client.post(`/api/v4/website_buttons/${sourceId}/online_chat`);
1098
+ }
1099
+ async update(sourceId, payload) {
1100
+ const response = await this.client.patch(`/api/v4/website_buttons/${sourceId}`, payload);
1101
+ return response.data;
1102
+ }
1103
+ };
1104
+
1105
+ // src/resources/chat-templates/index.ts
1106
+ import qs15 from "qs";
1107
+ var ChatTemplatesResource = class extends BaseResource {
1108
+ async list(params) {
1109
+ const queryParams = {};
1110
+ if (params) {
1111
+ if (params.page !== void 0) queryParams.page = params.page;
1112
+ if (params.limit !== void 0) queryParams.limit = params.limit;
1113
+ if (params.with !== void 0) queryParams.with = params.with;
1114
+ const filter = {};
1115
+ if (params.filter_external_id !== void 0) filter.external_id = params.filter_external_id;
1116
+ if (Object.keys(filter).length > 0) {
1117
+ queryParams.filter = filter;
1118
+ }
1119
+ }
1120
+ const query = qs15.stringify(queryParams, { skipNulls: true, arrayFormat: "brackets" });
1121
+ const qsStr = query ? `?${query}` : "";
1122
+ const response = await this.client.get(`/api/v4/chats/templates${qsStr}`);
1123
+ if (response.status === 204 || !response.data) {
1124
+ return { _page: 1, _embedded: { chat_templates: [] } };
1125
+ }
1126
+ return response.data;
1127
+ }
1128
+ async getById(id, params) {
1129
+ const queryParams = {};
1130
+ if (params && params.with !== void 0) {
1131
+ queryParams.with = params.with;
1132
+ }
1133
+ const query = qs15.stringify(queryParams, { skipNulls: true });
1134
+ const qsStr = query ? `?${query}` : "";
1135
+ const response = await this.client.get(`/api/v4/chats/templates/${id}${qsStr}`);
1136
+ return response.data;
1137
+ }
1138
+ async create(items) {
1139
+ const payload = Array.isArray(items) ? items : [items];
1140
+ const response = await this.client.post("/api/v4/chats/templates", payload);
1141
+ return response.data;
1142
+ }
1143
+ async update(items) {
1144
+ const payload = Array.isArray(items) ? items : [items];
1145
+ const isWaba = payload.some((item) => item.type === "waba");
1146
+ if (isWaba) {
1147
+ console.warn(`[ChatTemplatesResource] Modelos do WhatsApp ('waba') s\xF3 podem ser editados se o status for 'draft'. Modificar um template em 'review' ou 'approved' causar\xE1 erro na API.`);
1148
+ }
1149
+ const response = await this.client.patch("/api/v4/chats/templates", payload);
1150
+ return response.data;
1151
+ }
1152
+ async submitForReview(id) {
1153
+ const response = await this.client.post(`/api/v4/chats/templates/${id}/review`);
1154
+ return response.data;
1155
+ }
1156
+ async updateReviewStatus(id, reviewId, payload) {
1157
+ const response = await this.client.post(`/api/v4/chats/templates/${id}/review/${reviewId}`, payload);
1158
+ return response.data;
1159
+ }
1160
+ async delete(items) {
1161
+ const payload = Array.isArray(items) ? items : [items];
1162
+ await this.client.delete("/api/v4/chats/templates", { data: payload });
1163
+ }
1164
+ async deleteOne(id) {
1165
+ await this.client.delete(`/api/v4/chats/templates/${id}`);
1166
+ }
1167
+ };
1168
+
1169
+ // src/resources/webhooks/index.ts
1170
+ import qs16 from "qs";
1171
+ var WebhooksResource = class extends BaseResource {
1172
+ async list(params) {
1173
+ const queryParams = {};
1174
+ if (params) {
1175
+ const filter = {};
1176
+ if (params.filter_destination !== void 0) filter.destination = params.filter_destination;
1177
+ if (Object.keys(filter).length > 0) {
1178
+ queryParams.filter = filter;
1179
+ }
1180
+ }
1181
+ const query = qs16.stringify(queryParams, { skipNulls: true, arrayFormat: "brackets" });
1182
+ const qsStr = query ? `?${query}` : "";
1183
+ const response = await this.client.get(`/api/v4/webhooks${qsStr}`);
1184
+ if (response.status === 204 || !response.data) {
1185
+ return { _total_items: 0, _embedded: { webhooks: [] } };
1186
+ }
1187
+ return response.data;
1188
+ }
1189
+ async create(payload) {
1190
+ const response = await this.client.post("/api/v4/webhooks", payload);
1191
+ return response.data;
1192
+ }
1193
+ async delete(payload) {
1194
+ const data = { destination: payload.destination };
1195
+ await this.client.delete("/api/v4/webhooks", { data });
1196
+ }
1197
+ };
1198
+
1199
+ // src/errors.ts
1200
+ var KommoApiError = class extends Error {
1201
+ status;
1202
+ title;
1203
+ detail;
1204
+ type;
1205
+ raw;
1206
+ constructor(message, info) {
1207
+ super(message);
1208
+ this.name = "KommoApiError";
1209
+ if (info) {
1210
+ this.status = info.status;
1211
+ this.title = info.title;
1212
+ this.detail = info.detail;
1213
+ this.type = info.type;
1214
+ this.raw = info.raw;
1215
+ }
1216
+ }
1217
+ };
1218
+
1219
+ // src/client.ts
1220
+ var KommoClient = class {
1221
+ config;
1222
+ auth;
1223
+ httpClient;
1224
+ leads;
1225
+ contacts;
1226
+ tasks;
1227
+ notes;
1228
+ customFields;
1229
+ customFieldGroups;
1230
+ unsorted;
1231
+ pipelines;
1232
+ companies;
1233
+ catalogs;
1234
+ users;
1235
+ roles;
1236
+ events;
1237
+ tags;
1238
+ links;
1239
+ salesbots;
1240
+ sources;
1241
+ websiteButtons;
1242
+ chatTemplates;
1243
+ webhooks;
1244
+ constructor(config) {
1245
+ this.config = config;
1246
+ this.auth = new AuthManager(
1247
+ config.domain,
1248
+ config.clientId,
1249
+ config.clientSecret,
1250
+ config.redirectUri
1251
+ );
1252
+ const baseURL = `https://${config.domain}.kommo.com`;
1253
+ this.httpClient = axios2.create({
1254
+ baseURL,
1255
+ headers: {
1256
+ "Content-Type": "application/json"
1257
+ },
1258
+ paramsSerializer: (params) => {
1259
+ return qs17.stringify(params, { arrayFormat: "brackets" });
1260
+ }
1261
+ });
1262
+ this.httpClient.interceptors.response.use(
1263
+ (response) => response,
1264
+ (error) => {
1265
+ if (error.response) {
1266
+ const errData = error.response.data;
1267
+ throw new KommoApiError(errData?.title || "Kommo API Error", {
1268
+ status: error.response.status,
1269
+ title: errData?.title,
1270
+ detail: errData?.detail,
1271
+ type: errData?.type,
1272
+ raw: errData
1273
+ });
1274
+ }
1275
+ throw error;
1276
+ }
1277
+ );
1278
+ if (config.accessToken) {
1279
+ this.httpClient.defaults.headers.common["Authorization"] = `Bearer ${config.accessToken}`;
1280
+ }
1281
+ this.leads = new LeadsResource(this.httpClient);
1282
+ this.contacts = new ContactsResource(this.httpClient);
1283
+ this.tasks = new TasksResource(this.httpClient);
1284
+ this.notes = new NotesResource(this.httpClient);
1285
+ this.customFields = new CustomFieldsResource(this.httpClient);
1286
+ this.customFieldGroups = new CustomFieldGroupsResource(this.httpClient);
1287
+ this.unsorted = new UnsortedResource(this.httpClient);
1288
+ this.pipelines = new PipelinesResource(this.httpClient);
1289
+ this.companies = new CompaniesResource(this.httpClient);
1290
+ this.catalogs = new CatalogsResource(this.httpClient);
1291
+ this.users = new UsersResource(this.httpClient);
1292
+ this.roles = new RolesResource(this.httpClient);
1293
+ this.events = new EventsResource(this.httpClient);
1294
+ this.tags = new TagsResource(this.httpClient);
1295
+ this.links = new LinksResource(this.httpClient);
1296
+ this.salesbots = new SalesbotsResource(this.httpClient);
1297
+ this.sources = new SourcesResource(this.httpClient);
1298
+ this.websiteButtons = new WebsiteButtonsResource(this.httpClient);
1299
+ this.chatTemplates = new ChatTemplatesResource(this.httpClient);
1300
+ this.webhooks = new WebhooksResource(this.httpClient);
1301
+ }
1302
+ setAccessToken(token) {
1303
+ this.config.accessToken = token;
1304
+ this.httpClient.defaults.headers.common["Authorization"] = `Bearer ${token}`;
1305
+ }
1306
+ };
1307
+
1308
+ // src/resources/pipelines/types.ts
1309
+ var KOMMO_PIPELINE_STAGE_COLORS = [
1310
+ "#fffeb2",
1311
+ "#fffd7f",
1312
+ "#fff000",
1313
+ "#ffeab2",
1314
+ "#ffdc7f",
1315
+ "#ffce5a",
1316
+ "#ffdbdb",
1317
+ "#ffc8c8",
1318
+ "#ff8f92",
1319
+ "#d6eaff",
1320
+ "#c1e0ff",
1321
+ "#98cbff",
1322
+ "#ebffb1",
1323
+ "#deff81",
1324
+ "#87f2c0",
1325
+ "#f9deff",
1326
+ "#f3beff",
1327
+ "#ccc8f9",
1328
+ "#eb93ff",
1329
+ "#f2f3f4",
1330
+ "#e6e8ea"
1331
+ ];
1332
+
1333
+ // src/resources/events/types.ts
1334
+ var eventUtils = {
1335
+ isLeadStatusChanged: (event) => event.type === "lead_status_changed",
1336
+ isCustomFieldChanged: (event) => event.type === "custom_field_value_changed" || /^custom_field_\d+_value_changed$/.test(event.type)
1337
+ };
1338
+
1339
+ // src/resources/tags/types.ts
1340
+ var KOMMO_TAG_COLORS = [
1341
+ "EBEBEB",
1342
+ "D0D0D0",
1343
+ "F2DDF7",
1344
+ "D1A4DC",
1345
+ "FF8F92",
1346
+ "FFC8C8",
1347
+ "C7DB8C",
1348
+ "DDEBB5",
1349
+ "8699DA",
1350
+ "AABDFF",
1351
+ "FFCE5A",
1352
+ "FFE193",
1353
+ "90CDB0",
1354
+ "C6F4DE",
1355
+ "A9A5D7",
1356
+ "D8D5FF",
1357
+ "86C0FC",
1358
+ "832161",
1359
+ "6A0F49",
1360
+ "0C7C59",
1361
+ "10599D",
1362
+ "9D2B32",
1363
+ "247BA0"
1364
+ ];
1365
+
1366
+ // src/resources/webhooks/types.ts
1367
+ var KNOWN_WEBHOOK_EVENTS = [
1368
+ "add_lead",
1369
+ "update_lead",
1370
+ "delete_lead",
1371
+ "restore_lead",
1372
+ "add_contact",
1373
+ "update_contact",
1374
+ "delete_contact",
1375
+ "restore_contact",
1376
+ "add_company",
1377
+ "update_company",
1378
+ "delete_company",
1379
+ "restore_company",
1380
+ "add_task",
1381
+ "update_task",
1382
+ "delete_task"
1383
+ ];
1384
+ export {
1385
+ AuthManager,
1386
+ CatalogsResource,
1387
+ ChatTemplatesResource,
1388
+ CompaniesResource,
1389
+ ContactsResource,
1390
+ CustomFieldGroupsResource,
1391
+ CustomFieldsResource,
1392
+ EventsResource,
1393
+ KNOWN_WEBHOOK_EVENTS,
1394
+ KOMMO_PIPELINE_STAGE_COLORS,
1395
+ KOMMO_TAG_COLORS,
1396
+ KommoApiError,
1397
+ KommoClient,
1398
+ LeadsResource,
1399
+ LinksResource,
1400
+ NotesResource,
1401
+ PipelinesResource,
1402
+ RolesResource,
1403
+ SalesbotsResource,
1404
+ SourcesResource,
1405
+ TagsResource,
1406
+ TasksResource,
1407
+ UnsortedResource,
1408
+ UsersResource,
1409
+ WebhooksResource,
1410
+ WebsiteButtonsResource,
1411
+ eventUtils
1412
+ };
1413
+ //# sourceMappingURL=index.mjs.map