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