Melhoria da Função connectToWhatsapp e connectionState

This commit is contained in:
Alan Mosko 2023-07-21 10:15:46 -03:00 committed by GitHub
parent 3e3a175bdc
commit 2b87883e7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 29 deletions

View File

@ -337,23 +337,29 @@ export class InstanceController {
this.logger.verbose( this.logger.verbose(
'requested connectToWhatsapp from ' + instanceName + ' instance', 'requested connectToWhatsapp from ' + instanceName + ' instance',
); );
const instance = this.waMonitor.waInstances[instanceName]; const instance = this.waMonitor.waInstances[instanceName];
const state = instance?.connectionStatus?.state; const state = instance?.connectionStatus?.state;
this.logger.verbose('state: ' + state); this.logger.verbose('state: ' + state);
switch (state) { if (state == 'open') {
case 'close': return await this.connectionState({ instanceName });
this.logger.verbose('connecting'); }
if (state == 'close') {
this.logger.verbose('connecting');
await instance.connectToWhatsapp(); await instance.connectToWhatsapp();
await delay(2000); await delay(2000);
return instance.qrCode;
case 'connecting':
return instance.qrCode;
default:
return await this.connectionState({ instanceName });
} }
return {
instance: {
instanceName: instanceName,
status: state,
},
qrcode: instance?.qrCode
};
} catch (error) { } catch (error) {
this.logger.error(error); this.logger.error(error);
} }
@ -374,7 +380,12 @@ export class InstanceController {
public async connectionState({ instanceName }: InstanceDto) { public async connectionState({ instanceName }: InstanceDto) {
this.logger.verbose('requested connectionState from ' + instanceName + ' instance'); this.logger.verbose('requested connectionState from ' + instanceName + ' instance');
return this.waMonitor.waInstances[instanceName]?.connectionStatus; return {
instance: {
instanceName: instanceName,
state: this.waMonitor.waInstances[instanceName]?.connectionStatus?.state,
}
};
} }
public async fetchInstances({ instanceName }: InstanceDto) { public async fetchInstances({ instanceName }: InstanceDto) {
@ -389,9 +400,9 @@ export class InstanceController {
public async logout({ instanceName }: InstanceDto) { public async logout({ instanceName }: InstanceDto) {
this.logger.verbose('requested logout from ' + instanceName + ' instance'); this.logger.verbose('requested logout from ' + instanceName + ' instance');
const stateConn = await this.connectionState({ instanceName }); const { instance } = await this.connectionState({ instanceName });
if (stateConn.state === 'close') { if (instance.state === 'close') {
throw new BadRequestException( throw new BadRequestException(
'The "' + instanceName + '" instance is not connected', 'The "' + instanceName + '" instance is not connected',
); );
@ -414,15 +425,15 @@ export class InstanceController {
public async deleteInstance({ instanceName }: InstanceDto) { public async deleteInstance({ instanceName }: InstanceDto) {
this.logger.verbose('requested deleteInstance from ' + instanceName + ' instance'); this.logger.verbose('requested deleteInstance from ' + instanceName + ' instance');
const stateConn = await this.connectionState({ instanceName }); const { instance } = await this.connectionState({ instanceName });
if (stateConn.state === 'open') { if (instance.state === 'open') {
throw new BadRequestException( throw new BadRequestException(
'The "' + instanceName + '" instance needs to be disconnected', 'The "' + instanceName + '" instance needs to be disconnected',
); );
} }
try { try {
if (stateConn.state === 'connecting') { if (instance.state === 'connecting') {
this.logger.verbose('logging out instance: ' + instanceName); this.logger.verbose('logging out instance: ' + instanceName);
await this.logout({ instanceName }); await this.logout({ instanceName });

View File

@ -165,18 +165,7 @@ export async function initInstance() {
// } // }
try { try {
const state = instance.connectionStatus?.state; return await instance.connectToWhatsapp();
switch (state) {
case 'close':
await instance.connectToWhatsapp();
await delay(2000);
return instance.qrCode;
case 'connecting':
return instance.qrCode;
default:
return await this.connectionState({ instanceName });
}
} catch (error) { } catch (error) {
logger.log(error); logger.log(error);
} }