|
1 | | -import { delay } from '@evolution/base'; |
| 1 | +import { delay } from '@whiskeysockets/baileys'; |
2 | 2 | import EventEmitter2 from 'eventemitter2'; |
3 | 3 | import { Auth, ConfigService } from '../../config/env.config'; |
4 | 4 | import { BadRequestException, InternalServerErrorException } from '../../exceptions'; |
@@ -41,6 +41,8 @@ export class InstanceController { |
41 | 41 | ]); |
42 | 42 | } |
43 | 43 |
|
| 44 | + await this.authService.checkDuplicateToken(token); |
| 45 | + |
44 | 46 | const instance = new WAStartupService( |
45 | 47 | this.configService, |
46 | 48 | this.eventEmitter, |
@@ -84,6 +86,8 @@ export class InstanceController { |
84 | 86 | events: getEvents, |
85 | 87 | }; |
86 | 88 | } else { |
| 89 | + await this.authService.checkDuplicateToken(token); |
| 90 | + |
87 | 91 | const instance = new WAStartupService( |
88 | 92 | this.configService, |
89 | 93 | this.eventEmitter, |
@@ -159,6 +163,26 @@ export class InstanceController { |
159 | 163 | } |
160 | 164 | } |
161 | 165 |
|
| 166 | + public async restartInstance({ instanceName }: InstanceDto) { |
| 167 | + try { |
| 168 | + delete this.waMonitor.waInstances[instanceName]; |
| 169 | + console.log(this.waMonitor.waInstances[instanceName]); |
| 170 | + const instance = new WAStartupService( |
| 171 | + this.configService, |
| 172 | + this.eventEmitter, |
| 173 | + this.repository, |
| 174 | + ); |
| 175 | + |
| 176 | + instance.instanceName = instanceName; |
| 177 | + await instance.connectToWhatsapp(); |
| 178 | + this.waMonitor.waInstances[instance.instanceName] = instance; |
| 179 | + |
| 180 | + return { error: false, message: 'Instance restarted' }; |
| 181 | + } catch (error) { |
| 182 | + this.logger.error(error); |
| 183 | + } |
| 184 | + } |
| 185 | + |
162 | 186 | public async connectionState({ instanceName }: InstanceDto) { |
163 | 187 | return this.waMonitor.waInstances[instanceName]?.connectionStatus; |
164 | 188 | } |
@@ -195,8 +219,15 @@ export class InstanceController { |
195 | 219 | ]); |
196 | 220 | } |
197 | 221 | try { |
198 | | - delete this.waMonitor.waInstances[instanceName]; |
199 | | - return { error: false, message: 'Instance deleted' }; |
| 222 | + if (stateConn.state === 'connecting') { |
| 223 | + await this.logout({ instanceName }); |
| 224 | + delete this.waMonitor.waInstances[instanceName]; |
| 225 | + return { error: false, message: 'Instance deleted' }; |
| 226 | + } else { |
| 227 | + delete this.waMonitor.waInstances[instanceName]; |
| 228 | + this.eventEmitter.emit('remove.instance', instanceName, 'inner'); |
| 229 | + return { error: false, message: 'Instance deleted' }; |
| 230 | + } |
200 | 231 | } catch (error) { |
201 | 232 | throw new BadRequestException(error.toString()); |
202 | 233 | } |
|
0 commit comments