Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions src/whatsapp/controllers/instance.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,23 +200,29 @@ export class InstanceController {
this.logger.verbose(
'requested connectToWhatsapp from ' + instanceName + ' instance',
);

const instance = this.waMonitor.waInstances[instanceName];
const state = instance?.connectionStatus?.state;

this.logger.verbose('state: ' + state);

switch (state) {
case 'close':
this.logger.verbose('connecting');

if (state == 'open') {
return await this.connectionState({ instanceName });
}

if (state == 'close') {
this.logger.verbose('connecting');
await instance.connectToWhatsapp();
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) {
this.logger.error(error);
}
Expand All @@ -237,7 +243,12 @@ export class InstanceController {

public async connectionState({ instanceName }: InstanceDto) {
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) {
Expand All @@ -252,9 +263,9 @@ export class InstanceController {

public async logout({ instanceName }: InstanceDto) {
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(
'The "' + instanceName + '" instance is not connected',
);
Expand All @@ -277,15 +288,15 @@ export class InstanceController {

public async deleteInstance({ instanceName }: InstanceDto) {
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(
'The "' + instanceName + '" instance needs to be disconnected',
);
}
try {
if (stateConn.state === 'connecting') {
if (instance.state === 'connecting') {
this.logger.verbose('logging out instance: ' + instanceName);

await this.logout({ instanceName });
Expand Down