chore: Simplified payloads and instance endpoint

This commit is contained in:
Davidson Gomes
2024-06-07 11:09:08 -03:00
parent eed32a3bd9
commit a578384e85
17 changed files with 207 additions and 138 deletions

View File

@@ -1,40 +1,18 @@
import { v4 } from 'uuid';
import { ConfigService } from '../../config/env.config';
import { Logger } from '../../config/logger.config';
import { BadRequestException } from '../../exceptions';
import { PrismaRepository } from '../repository/repository.service';
import { WAMonitoringService } from './monitor.service';
export class AuthService {
constructor(
private readonly waMonitor: WAMonitoringService,
private readonly configService: ConfigService,
private readonly prismaRepository: PrismaRepository,
) {}
private readonly logger = new Logger(AuthService.name);
private async apikey(token?: string) {
const apikey = token ? token : v4().toUpperCase();
return apikey;
}
constructor(private readonly prismaRepository: PrismaRepository) {}
public async checkDuplicateToken(token: string) {
const instances = await this.waMonitor.instanceInfo();
const instances = await this.prismaRepository.instance.findMany({
where: { token },
});
const instance = instances.find((instance) => instance.instance.token === token);
if (instance) {
if (instances.length > 0) {
throw new BadRequestException('Token already exists');
}
return true;
}
public async generateHash(token?: string) {
const hash = await this.apikey(token);
return hash;
}
}

View File

@@ -369,6 +369,15 @@ export class BaileysStartupService extends ChannelStartupService {
status: 'closed',
});
if (this.configService.get<Database>('DATABASE').ENABLED) {
await this.prismaRepository.instance.update({
where: { id: this.instanceId },
data: {
connectionStatus: 'close',
},
});
}
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
this.chatwootService.eventWhatsapp(
Events.STATUS_INSTANCE,
@@ -404,6 +413,17 @@ export class BaileysStartupService extends ChannelStartupService {
`,
);
if (this.configService.get<Database>('DATABASE').ENABLED) {
await this.prismaRepository.instance.update({
where: { id: this.instanceId },
data: {
ownerJid: this.instance.wuid,
profilePicUrl: this.instance.profilePictureUrl,
connectionStatus: 'open',
},
});
}
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
this.chatwootService.eventWhatsapp(
Events.CONNECTION_UPDATE,