@in.pulse-crm/sdk 1.2.3 → 1.3.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.
Files changed (2) hide show
  1. package/package.json +4 -3
  2. package/src/socket.ts +174 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@in.pulse-crm/sdk",
3
- "version": "1.2.3",
3
+ "version": "1.3.0",
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",
@@ -23,8 +23,9 @@
23
23
  "author": "Renan G. Dutra <r.granatodutra@gmail.com>",
24
24
  "license": "ISC",
25
25
  "dependencies": {
26
- "@in.pulse-crm/utils": "^1.0.0",
27
- "axios": "^1.8.3"
26
+ "@in.pulse-crm/utils": "^1.3.0",
27
+ "axios": "^1.8.3",
28
+ "socket.io-client": "^4.8.1"
28
29
  },
29
30
  "devDependencies": {
30
31
  "@types/node": "^22.13.10",
package/src/socket.ts ADDED
@@ -0,0 +1,174 @@
1
+ import { Socket } from "socket.io-client";
2
+ import { PhoneNumber } from "@in.pulse-crm/utils";
3
+
4
+ /**
5
+ * Tipos de eventos de socket.
6
+ */
7
+ export enum SocketEventType {
8
+ MESSAGE = "message",
9
+ MESSAGE_EDIT = "message_edit",
10
+ MESSAGE_STATUS = "message_status",
11
+ NEW_CHAT = "new_chat",
12
+ CHAT_FINISHED = "chat_finished",
13
+ NOTIFICATION = "notification",
14
+ QR_CODE = "qr_code"
15
+ }
16
+
17
+ /**
18
+ * Sala de chat, recebe todos eventos de um chat específico.
19
+ */
20
+ export type SocketChatRoom = `chat:${PhoneNumber}`;
21
+
22
+ /**
23
+ * Sala de administrador, recebe eventos de adminstrador de um setor específico.
24
+ */
25
+ export type SocketAdminRoom = `sector:${SectorId}:admin`;
26
+
27
+ /**
28
+ * Sala de relatórios de chat, recebe eventos de relatórios de chat de um setor específico.
29
+ */
30
+ export type SocketChatReportsRoom = `sector:${SectorId}:chat_reports`;
31
+
32
+ /**
33
+ * Tipo de sala de socket.
34
+ */
35
+ export type SocketRoomType = SocketChatRoom | SocketAdminRoom | SocketChatReportsRoom;
36
+
37
+ /**
38
+ * Ainda não implementado.
39
+ */
40
+ export type NotImplemented = never;
41
+
42
+ /**
43
+ * String representando um QR Code.
44
+ */
45
+ export type QRCode = string;
46
+
47
+ /**
48
+ * Id de um chat.
49
+ */
50
+ export type ChatId = number;
51
+
52
+ /**
53
+ * Id de um setor.
54
+ */
55
+ export type SectorId = number;
56
+
57
+ /**
58
+ * Nome de uma instância.
59
+ */
60
+ export type InstanceName = string;
61
+
62
+ /**
63
+ * Função para entrar em uma sala de socket.
64
+ */
65
+ export type JoinRoomFunction = {
66
+ (room: SocketRoomType): void;
67
+ };
68
+
69
+ /**
70
+ * Função para entrar em um chat de socket.
71
+ */
72
+ export type JoinChatFunction = {
73
+ (phone: PhoneNumber): void;
74
+ };
75
+
76
+ /**
77
+ * Função para escutar eventos de socket.
78
+ */
79
+ export type ListenEventFunction = {
80
+ /**
81
+ * Escuta evento de mensagem
82
+ * Ainda não implementado.
83
+ */
84
+ (event: SocketEventType.MESSAGE, listener: (data: NotImplemented) => void): void;
85
+
86
+ /**
87
+ * Escuta evento de edição de mensagem
88
+ * Ainda não implementado.
89
+ */
90
+ (event: SocketEventType.MESSAGE_EDIT, listener: (data: NotImplemented) => void): void;
91
+
92
+ /**
93
+ * Escuta evento de status de mensagem
94
+ * Ainda não implementado.
95
+ */
96
+ (event: SocketEventType.MESSAGE_STATUS, listener: (data: NotImplemented) => void): void;
97
+
98
+ /**
99
+ * Escuta evento de novo chat
100
+ * Ainda não implementado.
101
+ */
102
+ (event: SocketEventType.NEW_CHAT, listener: (data: NotImplemented) => void): void;
103
+
104
+ /**
105
+ * Escuta evento de chat finalizado
106
+ * @trigger Quando um chat é finalizado
107
+ * @target Sala de monitoria e ao atendente do chat
108
+ * @data Um número contendo o ID do chat finalizado
109
+ */
110
+ (event: SocketEventType.CHAT_FINISHED, listener: (chatId: ChatId) => void): void;
111
+
112
+ /**
113
+ * Escuta evento de notificação
114
+ * @trigger Quando uma notificação é enviada
115
+ * @target Sala de administração do setor
116
+ */
117
+ (event: SocketEventType.NOTIFICATION, listener: (notification: NotImplemented) => void): void;
118
+
119
+ /**
120
+ * Escuta evento de QR Code
121
+ * @trigger Quando um QR Code é gerado
122
+ * @target Sala de administração do setor
123
+ */
124
+ (event: SocketEventType.QR_CODE, listener: (qr: QRCode) => void): void;
125
+ }
126
+
127
+ /**
128
+ * Classe para manipulação de sockets.
129
+ */
130
+ export default class SocketSDK {
131
+ constructor(private readonly io: Socket) {
132
+
133
+ this.on(SocketEventType.CHAT_FINISHED, (chatId) => {
134
+ console.log(`Chat ${chatId} finalizado.`);
135
+ });
136
+ }
137
+
138
+ /**
139
+ * Escuta eventos de socket.
140
+ *
141
+ * @param event - O tipo de evento a ser escutado.
142
+ * @param listener - A função a ser chamada quando o evento ocorrer.
143
+ */
144
+ public on: ListenEventFunction = (event, listener) => {
145
+ this.io.on(event, listener);
146
+ }
147
+
148
+ /**
149
+ * Entra em uma sala de socket.
150
+ *
151
+ * @param room - O tipo de sala a ser ingressada.
152
+ */
153
+ public joinRoom: JoinRoomFunction = (room) => {
154
+ this.io.emit("join", `${room}`);
155
+ }
156
+
157
+ /**
158
+ * Entra em um chat de socket.
159
+ *
160
+ * @param phone - O número de telefone do chat a ser ingressado.
161
+ */
162
+ public joinChat: JoinChatFunction = (phone) => {
163
+ this.io.emit("join", `chat:${phone}`);
164
+ }
165
+
166
+ /**
167
+ * Define o token de autenticação para o socket.
168
+ *
169
+ * @param token - O token de autenticação.
170
+ */
171
+ public setAuth(token: string) {
172
+ this.io.auth = { token: token ? `Bearer ${token}` : null };
173
+ }
174
+ }