Skip to content
Open
Show file tree
Hide file tree
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
40 changes: 24 additions & 16 deletions src/api/controllers/instance.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export class InstanceController {
};
} catch (error) {
this.logger.error(error);
return { error: true, message: error.toString() };
return { error: true, message: error?.message ?? String(error) };
}
}

Expand Down Expand Up @@ -386,7 +386,7 @@ export class InstanceController {
};
} catch (error) {
this.logger.error(error);
return { error: true, message: error.toString() };
return { error: true, message: error?.message ?? String(error) };
}
}

Expand Down Expand Up @@ -445,33 +445,41 @@ export class InstanceController {

return { status: 'SUCCESS', error: false, response: { message: 'Instance logged out' } };
} catch (error) {
throw new InternalServerErrorException(error.toString());
throw new InternalServerErrorException(error?.message ?? String(error));
}
}

public async deleteInstance({ instanceName }: InstanceDto) {
const { instance } = await this.connectionState({ instanceName });

const waInstances = this.waMonitor.waInstances[instanceName];

try {
const waInstances = this.waMonitor.waInstances[instanceName];
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) waInstances?.clearCacheChatwoot();
} catch (error) {
this.logger.warn(`clearCacheChatwoot failed for "${instanceName}": ${error?.message ?? String(error)}`);
}

if (instance.state === 'connecting' || instance.state === 'open') {
await this.logout({ instanceName });
}

if (instance.state === 'connecting' || instance.state === 'open') {
try {
waInstances?.sendDataWebhook(Events.INSTANCE_DELETE, {
instanceName,
instanceId: waInstances.instanceId,
});
await this.logout({ instanceName });
} catch (error) {
this.logger.error(error);
this.logger.warn(
`Logout failed for "${instanceName}": ${error?.message ?? String(error)}. Continuing cleanup.`,
);
}
}

this.eventEmitter.emit('remove.instance', instanceName, 'inner');
return { status: 'SUCCESS', error: false, response: { message: 'Instance deleted' } };
try {
waInstances?.sendDataWebhook(Events.INSTANCE_DELETE, {
instanceName,
instanceId: waInstances?.instanceId,
});
} catch (error) {
throw new BadRequestException(error.toString());
this.logger.error(error);
}

this.eventEmitter.emit('remove.instance', instanceName, 'inner');
return { status: 'SUCCESS', error: false, response: { message: 'Instance deleted' } };
}
}
23 changes: 16 additions & 7 deletions src/api/services/monitor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,20 +393,29 @@ export class WAMonitoringService {
this.eventEmitter.on('remove.instance', async (instanceName: string) => {
try {
await this.waInstances[instanceName]?.sendDataWebhook(Events.REMOVE_INSTANCE, null);
} catch (error) {
this.logger.warn(
`sendDataWebhook REMOVE_INSTANCE failed for "${instanceName}": ${error?.message ?? String(error)}`,
);
}

this.clearDelInstanceTime(instanceName);
this.clearDelInstanceTime(instanceName);

this.cleaningUp(instanceName);
this.cleaningStoreData(instanceName);
} finally {
this.logger.warn(`Instance "${instanceName}" - REMOVED`);
try {
await this.cleaningUp(instanceName);
} catch (error) {
this.logger.warn(`cleaningUp failed for "${instanceName}": ${error?.message ?? String(error)}`);
}

try {
delete this.waInstances[instanceName];
await this.cleaningStoreData(instanceName);
} catch (error) {
this.logger.error(error);
this.logger.warn(`cleaningStoreData failed for "${instanceName}": ${error?.message ?? String(error)}`);
}

delete this.waInstances[instanceName];

this.logger.warn(`Instance "${instanceName}" - REMOVED`);
});
this.eventEmitter.on('logout.instance', async (instanceName: string) => {
try {
Expand Down