@in.pulse-crm/sdk 2.12.2 → 2.12.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -82,7 +82,7 @@ export default class WhatsappClient extends ApiClient {
82
82
  })[]>;
83
83
  getAutoResponseRules(): Promise<AutomaticResponseRule[]>;
84
84
  createAutoResponseRule(ruleData: AutomaticResponseRuleDTO): Promise<AutomaticResponseRule>;
85
- updateAutoResponseRule(id: number, ruleData: Omit<AutomaticResponseRuleDTO, 'instance'>): Promise<AutomaticResponseRule>;
85
+ updateAutoResponseRule(id: number, ruleData: Omit<AutomaticResponseRuleDTO, "instance">): Promise<AutomaticResponseRule>;
86
86
  deleteAutoResponseRule(id: number): Promise<void>;
87
87
  }
88
88
  export {};
@@ -40,6 +40,7 @@ class WhatsappClient extends api_client_1.default {
40
40
  formData.append("to", to);
41
41
  data.text && formData.append("text", data.text);
42
42
  data.file && formData.append("file", data.file);
43
+ data.fileId && formData.append("fileId", String(data.fileId));
43
44
  data.quotedId && formData.append("quotedId", String(data.quotedId));
44
45
  data.chatId && formData.append("chatId", String(data.chatId));
45
46
  data.contactId && formData.append("contactId", String(data.contactId));
@@ -55,7 +56,7 @@ class WhatsappClient extends api_client_1.default {
55
56
  return res.data;
56
57
  }
57
58
  async editMessage(messageId, newText, isInternal = false) {
58
- const type = isInternal ? 'internal' : 'whatsapp';
59
+ const type = isInternal ? "internal" : "whatsapp";
59
60
  const url = `/api/${type}/messages/${messageId}`;
60
61
  const body = { newText };
61
62
  await this.ax.put(url, body);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@in.pulse-crm/sdk",
3
- "version": "2.12.2",
3
+ "version": "2.12.3",
4
4
  "description": "SDKs for abstraction of api consumption of in.pulse-crm application",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -2,374 +2,394 @@ import ApiClient from "./api-client";
2
2
  import { RequestFilters } from "./types";
3
3
  import { DataResponse, MessageResponse } from "./types/response.types";
4
4
  import {
5
- AppNotification,
6
- AutomaticResponseRule,
7
- AutomaticResponseRuleDTO,
8
- CreateScheduleDTO,
9
- ForwardMessagesData,
10
- PaginatedNotificationsResponse,
11
- SendMessageData,
12
- WppChatsAndMessages,
13
- WppChatWithDetailsAndMessages,
14
- WppContact,
15
- WppContactWithCustomer,
16
- WppMessage,
17
- WppSchedule,
18
- WppWallet,
5
+ AppNotification,
6
+ AutomaticResponseRule,
7
+ AutomaticResponseRuleDTO,
8
+ CreateScheduleDTO,
9
+ ForwardMessagesData,
10
+ PaginatedNotificationsResponse,
11
+ SendMessageData,
12
+ WppChatsAndMessages,
13
+ WppChatWithDetailsAndMessages,
14
+ WppContact,
15
+ WppContactWithCustomer,
16
+ WppMessage,
17
+ WppSchedule,
18
+ WppWallet,
19
19
  } from "./types/whatsapp.types";
20
20
  import FormData from "form-data";
21
21
 
22
-
23
22
  type GetChatsResponse = DataResponse<WppChatsAndMessages>;
24
23
  type GetChatResponse = DataResponse<WppChatWithDetailsAndMessages>;
25
24
  type GetMessageResponse = DataResponse<WppMessage>;
26
25
  type MarkChatAsReadResponse = DataResponse<WppMessage[]>;
27
26
 
28
27
  interface FetchMessagesFilters {
29
- minDate: string;
30
- maxDate: string;
31
- userId?: number | null;
28
+ minDate: string;
29
+ maxDate: string;
30
+ userId?: number | null;
32
31
  }
33
32
 
34
33
  export default class WhatsappClient extends ApiClient {
35
- public async getChatsBySession(
36
- messages = false,
37
- contact = false,
38
- token: string | null = null,
39
- ) {
40
- const headers = token
41
- ? { Authorization: `Bearer ${token}` }
42
- : undefined;
43
- const url = `/api/whatsapp/session/chats?messages=${messages}&contact=${contact}`;
44
- const { data: res } = await this.ax.get<GetChatsResponse>(url, {
45
- headers,
46
- });
47
- return res.data;
48
- }
49
-
50
- public async getChatById(id: number) {
51
- const { data: res } = await this.ax.get<GetChatResponse>(
52
- `/api/whatsapp/chats/${id}`,
53
- );
54
- return res.data;
55
- }
56
-
57
- public async getMessageById(id: string) {
58
- const { data: res } = await this.ax.get<GetMessageResponse>(
59
- `/api/whatsapp/messages/${id}`,
60
- );
61
- return res.data;
62
- }
63
-
64
- public async getUserWallets(instance: string, userId: number) {
65
- const { data: res } = await this.ax.get<DataResponse<WppWallet[]>>(
66
- `/api/wallets?instance=${instance}&userId=${userId}`,
67
- );
68
- return res.data;
69
- }
70
-
71
- public async markContactMessagesAsRead(contactId: number) {
72
- const url = "/api/whatsapp/messages/mark-as-read";
73
- const body = { contactId };
74
- const { data: res } = await this.ax.patch<MarkChatAsReadResponse>(
75
- url,
76
- body,
77
- );
78
- return res.data;
79
- }
80
-
81
- public async sendMessage(to: string, data: SendMessageData) {
82
- const url = "/api/whatsapp/messages";
83
- const formData = new FormData();
84
- formData.append("to", to);
85
- data.text && formData.append("text", data.text);
86
- data.file && formData.append("file", data.file);
87
- data.quotedId && formData.append("quotedId", String(data.quotedId));
88
- data.chatId && formData.append("chatId", String(data.chatId));
89
- data.contactId && formData.append("contactId", String(data.contactId));
90
- data.sendAsAudio && formData.append("sendAsAudio", "true");
91
- data.sendAsDocument && formData.append("sendAsDocument", "true");
92
- data.sendAsChatOwner &&
93
- formData.append("sendAsChatOwner", String(data.sendAsChatOwner));
94
- const { data: res } = await this.ax.post<DataResponse<WppMessage>>(
95
- url,
96
- formData,
97
- {
98
- headers: {
99
- "Content-Type": "multipart/form-data",
100
- },
101
- },
102
- );
103
- return res.data;
104
- }
105
-
106
- public async editMessage(messageId: string, newText: string, isInternal = false) {
107
- const type = isInternal ? 'internal' : 'whatsapp';
108
-
109
- const url = `/api/${type}/messages/${messageId}`;
110
- const body = { newText };
111
- await this.ax.put(url, body);
112
- }
113
-
114
-
115
- public async finishChatById(id: number, resultId: number, triggerSatisfactionBot = false) {
116
- const url = `/api/whatsapp/chats/${id}/finish`;
117
- const body = { resultId, triggerSatisfactionBot };
118
- await this.ax.post<MessageResponse>(url, body);
119
- }
120
-
121
- public async startChatByContactId(contactId: number, template?: any) {
122
- const url = `/api/whatsapp/chats`;
123
- const body = { contactId, template };
124
-
125
- await this.ax.post<
126
- DataResponse<WppChatWithDetailsAndMessages>
127
- >(url, body);
128
- }
129
-
130
- public async getResults() {
131
- const url = `/api/whatsapp/results`;
132
- const { data: res } =
133
- await this.ax.get<DataResponse<{ id: number; name: string }[]>>(
134
- url,
135
- );
136
- return res.data;
137
- }
138
-
139
- public async getCustomerContacts(customerId: number) {
140
- const url = `/api/whatsapp/customer/${customerId}/contacts`;
141
- const { data: res } =
142
- await this.ax.get<DataResponse<WppContact[]>>(url);
143
- return res.data;
144
- }
145
-
146
- public async getContactsWithCustomer() {
147
- const url = `/api/whatsapp/contacts/customer`;
148
- const { data: res } =
149
- await this.ax.get<DataResponse<WppContactWithCustomer[]>>(url);
150
- return res.data;
151
- }
152
-
153
- public async getContacts() {
154
- const url = `/api/whatsapp/contacts`;
155
- const { data: res } =
156
- await this.ax.get<DataResponse<WppContact[]>>(url);
157
- return res.data;
158
- }
159
-
160
- public async createContact(
161
- name: string,
162
- phone: string,
163
- customerId?: number,
164
- ) {
165
- const baseUrl = `/api/whatsapp`;
166
- const url = customerId
167
- ? `${baseUrl}/customers/${customerId}/contacts`
168
- : `${baseUrl}/contacts`;
169
- const body = { name, phone };
170
- const { data: res } = await this.ax.post<DataResponse<WppContact>>(
171
- url,
172
- body,
173
- );
174
- return res.data;
175
- }
176
-
177
- public async forwardMessages(data: ForwardMessagesData) {
178
- const url = "/api/whatsapp/messages/forward";
179
- const body = data;
180
- await this.ax.post<MessageResponse>(url, body);
181
- }
182
-
183
- public async updateContact(
184
- contactId: number,
185
- name: string,
186
- customerId?: number | null,
187
- ) {
188
- const url = `/api/whatsapp/contacts/${contactId}`;
189
- const body: Record<string, any> = { name };
190
- customerId !== undefined && (body["customerId"] = customerId);
191
- const { data: res } = await this.ax.put<DataResponse<WppContact>>(
192
- url,
193
- body,
194
- );
195
- return res.data;
196
- }
197
-
198
- public async deleteContact(contactId: number) {
199
- const url = `/api/whatsapp/contacts/${contactId}`;
200
- await this.ax.delete<MessageResponse>(url);
201
- }
202
-
203
- public async getSectors() {
204
- const url = `/api/whatsapp/sectors`;
205
- const { data: res } =
206
- await this.ax.get<DataResponse<{ id: number; name: string }[]>>(
207
- url,
208
- );
209
- return res.data;
210
- }
211
-
212
- public setAuth(token: string) {
213
- this.ax.defaults.headers.common["Authorization"] = `Bearer ${token}`;
214
- }
215
-
216
- public async getChatsMonitor() {
217
- const url = `/api/whatsapp/session/monitor`;
218
- const { data: res } = await this.ax.get<GetChatsResponse>(url);
219
- return res.data;
220
- }
221
-
222
- public async transferAttendance(id: number, userId: number) {
223
- const url = `/api/whatsapp/chats/${id}/transfer`;
224
- const body = { userId };
225
- await this.ax.post<MessageResponse>(url, body);
226
- }
227
- /**
228
- * Busca as notificações do usuário de forma paginada.
229
- */
230
- public async getNotifications(params: { page: number; pageSize: number }) {
231
- const searchParams = new URLSearchParams({
232
- page: String(params.page),
233
- pageSize: String(params.pageSize),
234
- });
235
- const url = `/api/whatsapp/notifications?${searchParams.toString()}`;
236
- const { data: res } = await this.ax.get<DataResponse<PaginatedNotificationsResponse>>(url);
237
- return res;
238
- }
239
-
240
- /**
241
- * Marca todas as notificações do usuário como lidas.
242
- */
243
- public async markAllAsReadNotification() {
244
- const url = `/api/whatsapp/notifications/mark-all-read`;
245
- const { data: res } = await this.ax.patch<MessageResponse>(url);
246
- return res;
247
- }
248
-
249
- /**
250
- * Marca uma notificação específica como lida.
251
- * @param notificationId - O ID (numérico) da notificação a ser marcada.
252
- */
253
- public async markOneAsReadNotification(notificationId: number) {
254
- const url = `/api/whatsapp/notifications/${notificationId}/read`;
255
- const { data: res } = await this.ax.patch<DataResponse<AppNotification>>(url);
256
- return res;
257
- }
258
-
259
- /**
260
- * Obtém os detalhes de um agendamento.
261
- * @param filters - keys de WppSchedule.
262
- * @param userId/sectorId filtrar por usúario/setor
263
- * @returns Uma Promise que resolve para um array de objetos wppSchedule.
264
- */
265
- public async getSchedules(
266
- userId?: string,
267
- sectorId?: string,
268
- filters?: RequestFilters<WppSchedule>,
269
- ) {
270
- let baseUrl = `/api/whatsapp/schedules`;
271
- const params = new URLSearchParams(filters);
272
- if (params.toString()) {
273
- if (userId && sectorId) {
274
- baseUrl += `?userId=${userId}&sectorId=${sectorId}&${params.toString()}`;
275
- } else if (userId) {
276
- baseUrl += `?userId=${userId}&${params.toString()}`;
277
- } else if (sectorId) {
278
- baseUrl += `?sectorId=${sectorId}&${params.toString()}`;
279
- } else {
280
- baseUrl += `?${params.toString()}`;
281
- }
282
- } else if (userId || sectorId) {
283
- if (userId && sectorId) {
284
- baseUrl += `?userId=${userId}&sectorId=${sectorId}`;
285
- } else if (userId) {
286
- baseUrl += `?userId=${userId}`;
287
- } else if (sectorId) {
288
- baseUrl += `?sectorId=${sectorId}`;
289
- }
290
- }
291
- const response = await this.ax.get(baseUrl);
292
- return response.data;
293
- }
294
-
295
- /**
296
- * Cria um novo agendamento.
297
- * @param scheduleData - Os dados do agendamento, keys de wppSchedule.
298
- * @returns Uma Promise que resolve para um objeto wppSchedule.
299
- */
300
- public async createSchedule(data: CreateScheduleDTO) {
301
- const response = await this.ax.post(`/api/whatsapp/schedules`, data);
302
- return response.data;
303
- }
304
-
305
- /**
306
- * Edita um agendamento existente.
307
- * @param scheduleId - O ID do agendamento a ser editado.
308
- * @param updatedData - Os dados atualizados do agendamento.
309
- * @returns Uma Promise que resolve para um objeto wppSchedule.
310
- */
311
- public async updateSchedule(
312
- scheduleId: number,
313
- updatedData: Record<string, WppSchedule>,
314
- ) {
315
- const response = await this.ax.patch(
316
- `/api/whatsapp/schedules/${scheduleId}`,
317
- updatedData,
318
- );
319
- return response.data;
320
- }
321
-
322
- /**
323
- * Exclui um agendamento.
324
- * @param scheduleId - O ID do agendamento a ser excluído.
325
- * @returns Uma Promise que resolve para um objeto wppSchedule.
326
- */
327
- public async deleteSchedule(scheduleId: number) {
328
- const response = await this.ax.delete(
329
- `/api/whatsapp/schedules/${scheduleId}`,
330
- );
331
- return response.data;
332
- }
333
-
334
- public async getMessages(token: string, filters: FetchMessagesFilters) {
335
- const params = new URLSearchParams(
336
- Object.entries(filters)
337
- .filter(([_, v]) => v !== undefined && v !== null)
338
- .reduce<Record<string, string>>((acc, [k, v]) => {
339
- acc[k] = String(v);
340
- return acc;
341
- }, {}),
342
- );
343
- const url = `/api/whatsapp/messages?${params.toString()}`;
344
- const { data: res } = await this.ax.get<
345
- DataResponse<(WppMessage & { WppContact: WppContact | null })[]>
346
- >(url, {
347
- headers: {
348
- Authorization: `Bearer ${token}`,
349
- },
350
- });
351
- return res.data;
352
- }
353
- public async getAutoResponseRules() {
354
- const url = `/api/auto-response-rules`;
355
- const { data: res } = await this.ax.get<DataResponse<AutomaticResponseRule[]>>(url);
356
- return res.data;
357
- }
358
-
359
- public async createAutoResponseRule(ruleData: AutomaticResponseRuleDTO) {
360
- const url = `/api/auto-response-rules`;
361
- const { data: res } = await this.ax.post<DataResponse<AutomaticResponseRule>>(url, ruleData);
362
- return res.data;
363
- }
364
-
365
- public async updateAutoResponseRule(id: number, ruleData: Omit<AutomaticResponseRuleDTO, 'instance'>) {
366
- const url = `/api/auto-response-rules/${id}`;
367
- const { data: res } = await this.ax.put<DataResponse<AutomaticResponseRule>>(url, ruleData);
368
- return res.data;
369
- }
370
-
371
- public async deleteAutoResponseRule(id: number) {
372
- const url = `/api/auto-response-rules/${id}`;
373
- await this.ax.delete<MessageResponse>(url);
374
- }
375
- }
34
+ public async getChatsBySession(
35
+ messages = false,
36
+ contact = false,
37
+ token: string | null = null,
38
+ ) {
39
+ const headers = token
40
+ ? { Authorization: `Bearer ${token}` }
41
+ : undefined;
42
+ const url = `/api/whatsapp/session/chats?messages=${messages}&contact=${contact}`;
43
+ const { data: res } = await this.ax.get<GetChatsResponse>(url, {
44
+ headers,
45
+ });
46
+ return res.data;
47
+ }
48
+
49
+ public async getChatById(id: number) {
50
+ const { data: res } = await this.ax.get<GetChatResponse>(
51
+ `/api/whatsapp/chats/${id}`,
52
+ );
53
+ return res.data;
54
+ }
55
+
56
+ public async getMessageById(id: string) {
57
+ const { data: res } = await this.ax.get<GetMessageResponse>(
58
+ `/api/whatsapp/messages/${id}`,
59
+ );
60
+ return res.data;
61
+ }
62
+
63
+ public async getUserWallets(instance: string, userId: number) {
64
+ const { data: res } = await this.ax.get<DataResponse<WppWallet[]>>(
65
+ `/api/wallets?instance=${instance}&userId=${userId}`,
66
+ );
67
+ return res.data;
68
+ }
69
+
70
+ public async markContactMessagesAsRead(contactId: number) {
71
+ const url = "/api/whatsapp/messages/mark-as-read";
72
+ const body = { contactId };
73
+ const { data: res } = await this.ax.patch<MarkChatAsReadResponse>(
74
+ url,
75
+ body,
76
+ );
77
+ return res.data;
78
+ }
79
+
80
+ public async sendMessage(to: string, data: SendMessageData) {
81
+ const url = "/api/whatsapp/messages";
82
+ const formData = new FormData();
83
+ formData.append("to", to);
84
+ data.text && formData.append("text", data.text);
85
+ data.file && formData.append("file", data.file);
86
+ data.fileId && formData.append("fileId", String(data.fileId));
87
+ data.quotedId && formData.append("quotedId", String(data.quotedId));
88
+ data.chatId && formData.append("chatId", String(data.chatId));
89
+ data.contactId && formData.append("contactId", String(data.contactId));
90
+ data.sendAsAudio && formData.append("sendAsAudio", "true");
91
+ data.sendAsDocument && formData.append("sendAsDocument", "true");
92
+ data.sendAsChatOwner &&
93
+ formData.append("sendAsChatOwner", String(data.sendAsChatOwner));
94
+ const { data: res } = await this.ax.post<DataResponse<WppMessage>>(
95
+ url,
96
+ formData,
97
+ {
98
+ headers: {
99
+ "Content-Type": "multipart/form-data",
100
+ },
101
+ },
102
+ );
103
+ return res.data;
104
+ }
105
+
106
+ public async editMessage(
107
+ messageId: string,
108
+ newText: string,
109
+ isInternal = false,
110
+ ) {
111
+ const type = isInternal ? "internal" : "whatsapp";
112
+
113
+ const url = `/api/${type}/messages/${messageId}`;
114
+ const body = { newText };
115
+ await this.ax.put(url, body);
116
+ }
117
+
118
+ public async finishChatById(
119
+ id: number,
120
+ resultId: number,
121
+ triggerSatisfactionBot = false,
122
+ ) {
123
+ const url = `/api/whatsapp/chats/${id}/finish`;
124
+ const body = { resultId, triggerSatisfactionBot };
125
+ await this.ax.post<MessageResponse>(url, body);
126
+ }
127
+
128
+ public async startChatByContactId(contactId: number, template?: any) {
129
+ const url = `/api/whatsapp/chats`;
130
+ const body = { contactId, template };
131
+
132
+ await this.ax.post<DataResponse<WppChatWithDetailsAndMessages>>(
133
+ url,
134
+ body,
135
+ );
136
+ }
137
+
138
+ public async getResults() {
139
+ const url = `/api/whatsapp/results`;
140
+ const { data: res } =
141
+ await this.ax.get<DataResponse<{ id: number; name: string }[]>>(
142
+ url,
143
+ );
144
+ return res.data;
145
+ }
146
+
147
+ public async getCustomerContacts(customerId: number) {
148
+ const url = `/api/whatsapp/customer/${customerId}/contacts`;
149
+ const { data: res } =
150
+ await this.ax.get<DataResponse<WppContact[]>>(url);
151
+ return res.data;
152
+ }
153
+
154
+ public async getContactsWithCustomer() {
155
+ const url = `/api/whatsapp/contacts/customer`;
156
+ const { data: res } =
157
+ await this.ax.get<DataResponse<WppContactWithCustomer[]>>(url);
158
+ return res.data;
159
+ }
160
+
161
+ public async getContacts() {
162
+ const url = `/api/whatsapp/contacts`;
163
+ const { data: res } =
164
+ await this.ax.get<DataResponse<WppContact[]>>(url);
165
+ return res.data;
166
+ }
167
+
168
+ public async createContact(
169
+ name: string,
170
+ phone: string,
171
+ customerId?: number,
172
+ ) {
173
+ const baseUrl = `/api/whatsapp`;
174
+ const url = customerId
175
+ ? `${baseUrl}/customers/${customerId}/contacts`
176
+ : `${baseUrl}/contacts`;
177
+ const body = { name, phone };
178
+ const { data: res } = await this.ax.post<DataResponse<WppContact>>(
179
+ url,
180
+ body,
181
+ );
182
+ return res.data;
183
+ }
184
+
185
+ public async forwardMessages(data: ForwardMessagesData) {
186
+ const url = "/api/whatsapp/messages/forward";
187
+ const body = data;
188
+ await this.ax.post<MessageResponse>(url, body);
189
+ }
190
+
191
+ public async updateContact(
192
+ contactId: number,
193
+ name: string,
194
+ customerId?: number | null,
195
+ ) {
196
+ const url = `/api/whatsapp/contacts/${contactId}`;
197
+ const body: Record<string, any> = { name };
198
+ customerId !== undefined && (body["customerId"] = customerId);
199
+ const { data: res } = await this.ax.put<DataResponse<WppContact>>(
200
+ url,
201
+ body,
202
+ );
203
+ return res.data;
204
+ }
205
+
206
+ public async deleteContact(contactId: number) {
207
+ const url = `/api/whatsapp/contacts/${contactId}`;
208
+ await this.ax.delete<MessageResponse>(url);
209
+ }
210
+
211
+ public async getSectors() {
212
+ const url = `/api/whatsapp/sectors`;
213
+ const { data: res } =
214
+ await this.ax.get<DataResponse<{ id: number; name: string }[]>>(
215
+ url,
216
+ );
217
+ return res.data;
218
+ }
219
+
220
+ public setAuth(token: string) {
221
+ this.ax.defaults.headers.common["Authorization"] = `Bearer ${token}`;
222
+ }
223
+
224
+ public async getChatsMonitor() {
225
+ const url = `/api/whatsapp/session/monitor`;
226
+ const { data: res } = await this.ax.get<GetChatsResponse>(url);
227
+ return res.data;
228
+ }
229
+
230
+ public async transferAttendance(id: number, userId: number) {
231
+ const url = `/api/whatsapp/chats/${id}/transfer`;
232
+ const body = { userId };
233
+ await this.ax.post<MessageResponse>(url, body);
234
+ }
235
+ /**
236
+ * Busca as notificações do usuário de forma paginada.
237
+ */
238
+ public async getNotifications(params: { page: number; pageSize: number }) {
239
+ const searchParams = new URLSearchParams({
240
+ page: String(params.page),
241
+ pageSize: String(params.pageSize),
242
+ });
243
+ const url = `/api/whatsapp/notifications?${searchParams.toString()}`;
244
+ const { data: res } =
245
+ await this.ax.get<DataResponse<PaginatedNotificationsResponse>>(
246
+ url,
247
+ );
248
+ return res;
249
+ }
250
+
251
+ /**
252
+ * Marca todas as notificações do usuário como lidas.
253
+ */
254
+ public async markAllAsReadNotification() {
255
+ const url = `/api/whatsapp/notifications/mark-all-read`;
256
+ const { data: res } = await this.ax.patch<MessageResponse>(url);
257
+ return res;
258
+ }
259
+
260
+ /**
261
+ * Marca uma notificação específica como lida.
262
+ * @param notificationId - O ID (numérico) da notificação a ser marcada.
263
+ */
264
+ public async markOneAsReadNotification(notificationId: number) {
265
+ const url = `/api/whatsapp/notifications/${notificationId}/read`;
266
+ const { data: res } =
267
+ await this.ax.patch<DataResponse<AppNotification>>(url);
268
+ return res;
269
+ }
270
+
271
+ /**
272
+ * Obtém os detalhes de um agendamento.
273
+ * @param filters - keys de WppSchedule.
274
+ * @param userId/sectorId filtrar por usúario/setor
275
+ * @returns Uma Promise que resolve para um array de objetos wppSchedule.
276
+ */
277
+ public async getSchedules(
278
+ userId?: string,
279
+ sectorId?: string,
280
+ filters?: RequestFilters<WppSchedule>,
281
+ ) {
282
+ let baseUrl = `/api/whatsapp/schedules`;
283
+ const params = new URLSearchParams(filters);
284
+ if (params.toString()) {
285
+ if (userId && sectorId) {
286
+ baseUrl += `?userId=${userId}&sectorId=${sectorId}&${params.toString()}`;
287
+ } else if (userId) {
288
+ baseUrl += `?userId=${userId}&${params.toString()}`;
289
+ } else if (sectorId) {
290
+ baseUrl += `?sectorId=${sectorId}&${params.toString()}`;
291
+ } else {
292
+ baseUrl += `?${params.toString()}`;
293
+ }
294
+ } else if (userId || sectorId) {
295
+ if (userId && sectorId) {
296
+ baseUrl += `?userId=${userId}&sectorId=${sectorId}`;
297
+ } else if (userId) {
298
+ baseUrl += `?userId=${userId}`;
299
+ } else if (sectorId) {
300
+ baseUrl += `?sectorId=${sectorId}`;
301
+ }
302
+ }
303
+ const response = await this.ax.get(baseUrl);
304
+ return response.data;
305
+ }
306
+
307
+ /**
308
+ * Cria um novo agendamento.
309
+ * @param scheduleData - Os dados do agendamento, keys de wppSchedule.
310
+ * @returns Uma Promise que resolve para um objeto wppSchedule.
311
+ */
312
+ public async createSchedule(data: CreateScheduleDTO) {
313
+ const response = await this.ax.post(`/api/whatsapp/schedules`, data);
314
+ return response.data;
315
+ }
316
+
317
+ /**
318
+ * Edita um agendamento existente.
319
+ * @param scheduleId - O ID do agendamento a ser editado.
320
+ * @param updatedData - Os dados atualizados do agendamento.
321
+ * @returns Uma Promise que resolve para um objeto wppSchedule.
322
+ */
323
+ public async updateSchedule(
324
+ scheduleId: number,
325
+ updatedData: Record<string, WppSchedule>,
326
+ ) {
327
+ const response = await this.ax.patch(
328
+ `/api/whatsapp/schedules/${scheduleId}`,
329
+ updatedData,
330
+ );
331
+ return response.data;
332
+ }
333
+
334
+ /**
335
+ * Exclui um agendamento.
336
+ * @param scheduleId - O ID do agendamento a ser excluído.
337
+ * @returns Uma Promise que resolve para um objeto wppSchedule.
338
+ */
339
+ public async deleteSchedule(scheduleId: number) {
340
+ const response = await this.ax.delete(
341
+ `/api/whatsapp/schedules/${scheduleId}`,
342
+ );
343
+ return response.data;
344
+ }
345
+
346
+ public async getMessages(token: string, filters: FetchMessagesFilters) {
347
+ const params = new URLSearchParams(
348
+ Object.entries(filters)
349
+ .filter(([_, v]) => v !== undefined && v !== null)
350
+ .reduce<Record<string, string>>((acc, [k, v]) => {
351
+ acc[k] = String(v);
352
+ return acc;
353
+ }, {}),
354
+ );
355
+ const url = `/api/whatsapp/messages?${params.toString()}`;
356
+ const { data: res } = await this.ax.get<
357
+ DataResponse<(WppMessage & { WppContact: WppContact | null })[]>
358
+ >(url, {
359
+ headers: {
360
+ Authorization: `Bearer ${token}`,
361
+ },
362
+ });
363
+ return res.data;
364
+ }
365
+ public async getAutoResponseRules() {
366
+ const url = `/api/auto-response-rules`;
367
+ const { data: res } =
368
+ await this.ax.get<DataResponse<AutomaticResponseRule[]>>(url);
369
+ return res.data;
370
+ }
371
+
372
+ public async createAutoResponseRule(ruleData: AutomaticResponseRuleDTO) {
373
+ const url = `/api/auto-response-rules`;
374
+ const { data: res } = await this.ax.post<
375
+ DataResponse<AutomaticResponseRule>
376
+ >(url, ruleData);
377
+ return res.data;
378
+ }
379
+
380
+ public async updateAutoResponseRule(
381
+ id: number,
382
+ ruleData: Omit<AutomaticResponseRuleDTO, "instance">,
383
+ ) {
384
+ const url = `/api/auto-response-rules/${id}`;
385
+ const { data: res } = await this.ax.put<
386
+ DataResponse<AutomaticResponseRule>
387
+ >(url, ruleData);
388
+ return res.data;
389
+ }
390
+
391
+ public async deleteAutoResponseRule(id: number) {
392
+ const url = `/api/auto-response-rules/${id}`;
393
+ await this.ax.delete<MessageResponse>(url);
394
+ }
395
+ }