Skip to content

Commit 08ccf9f

Browse files
author
Hamza Shahid
committed
fix: handle undefined connectionStatus when connecting instance
1 parent d2e0e42 commit 08ccf9f

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

src/api/controllers/instance.controller.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,18 +373,30 @@ export class InstanceController {
373373

374374
const state = instance?.connectionStatus?.state;
375375

376-
if (state == 'open') {
376+
if (state === 'open') {
377377
return await this.connectionState({ instanceName });
378378
}
379379

380-
if (state == 'connecting') {
380+
if (state === 'connecting') {
381381
return instance.qrCode;
382382
}
383383

384-
// Connect if closed or no state
384+
// Connect if closed, no state, or just loaded from DB
385+
this.logger.info(`Connecting instance "${instanceName}" (current state: ${state || 'none'})`);
385386
await instance.connectToWhatsapp(number);
386-
await delay(2000);
387-
return instance.qrCode;
387+
await delay(3000);
388+
389+
// Return QR code or connection status
390+
if (instance.qrCode) {
391+
return instance.qrCode;
392+
}
393+
394+
return {
395+
instance: {
396+
instanceName: instanceName,
397+
status: instance?.connectionStatus?.state || 'connecting',
398+
},
399+
};
388400
} catch (error) {
389401
this.logger.error(error);
390402
const errorMessage = error?.message || error?.toString() || JSON.stringify(error);

0 commit comments

Comments
 (0)