Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix Internal Error 500: fetchInstances (apikey not found) #59

Merged
merged 11 commits into from
Aug 29, 2023
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"redis": "^4.6.5",
"sharp": "^0.30.7",
"socket.io": "^4.7.1",
"socks-proxy-agent": "^8.0.1",
"uuid": "^9.0.0"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions src/libs/redis.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { Redis } from '../config/env.config';
import { Logger } from '../config/logger.config';

export class RedisCache {
async disconnect() {
await this.client.disconnect();
this.statusConnection = false;
}
constructor() {
this.logger.verbose('instance created');
process.on('beforeExit', async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/whatsapp/controllers/instance.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,13 @@ export class InstanceController {
}

public async fetchInstances({ instanceName }: InstanceDto) {
this.logger.verbose('requested fetchInstances from ' + instanceName + ' instance');
if (instanceName) {
this.logger.verbose('requested fetchInstances from ' + instanceName + ' instance');
this.logger.verbose('instanceName: ' + instanceName);
return this.waMonitor.instanceInfo(instanceName);
}

this.logger.verbose('requested fetchInstances (all instances)');
return this.waMonitor.instanceInfo();
}

Expand Down
2 changes: 1 addition & 1 deletion src/whatsapp/guards/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function jwtGuard(req: Request, res: Response, next: NextFunction) {
}
}

async function apikey(req: Request, res: Response, next: NextFunction) {
async function apikey(req: Request, _: Response, next: NextFunction) {
const env = configService.get<Auth>('AUTHENTICATION').API_KEY;
const key = req.get('apikey');

Expand Down
9 changes: 5 additions & 4 deletions src/whatsapp/services/monitor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class WAMonitoringService {
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;

instanceData.instance['apikey'] = (await this.repository.auth.find(key)).apikey;
instanceData.instance['apikey'] = (await this.repository.auth.find(key))?.apikey;

instanceData.instance['chatwoot'] = chatwoot;
}
Expand All @@ -134,7 +134,7 @@ export class WAMonitoringService {
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;

instanceData.instance['apikey'] = (await this.repository.auth.find(key)).apikey;
instanceData.instance['apikey'] = (await this.repository.auth.find(key))?.apikey;

instanceData.instance['chatwoot'] = chatwoot;
}
Expand All @@ -161,8 +161,7 @@ export class WAMonitoringService {
});
this.logger.verbose('instance files deleted: ' + name);
});
// } else if (this.redis.ENABLED) {
} else {
} else if (!this.redis.ENABLED) {
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
for await (const dirent of dir) {
if (dirent.isDirectory()) {
Expand Down Expand Up @@ -200,6 +199,7 @@ export class WAMonitoringService {
this.logger.verbose('cleaning up instance in redis: ' + instanceName);
this.cache.reference = instanceName;
await this.cache.delAll();
this.cache.disconnect();
return;
}

Expand Down Expand Up @@ -263,6 +263,7 @@ export class WAMonitoringService {
} else {
this.logger.verbose('no instance keys found');
}
this.cache.disconnect();
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/whatsapp/services/whatsapp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ import { RepositoryBroker } from '../repository/repository.manager';
import { Events, MessageSubtype, TypeMediaMessage, wa } from '../types/wa.types';
import { waMonitor } from '../whatsapp.module';
import { ChatwootService } from './chatwoot.service';
//import { SocksProxyAgent } from './socks-proxy-agent';
import { TypebotService } from './typebot.service';

export class WAStartupService {
Expand Down