refactor: channel integration folders

This commit is contained in:
Davidson Gomes
2024-08-23 07:55:26 -03:00
parent fff11ea452
commit a83a358620
12 changed files with 209 additions and 93 deletions

View File

@@ -0,0 +1,72 @@
import { PrismaRepository } from '@api/repository/repository.service';
import { WAMonitoringService } from '@api/services/monitor.service';
import { Logger } from '@config/logger.config';
import axios from 'axios';
import { ChannelController, ChannelControllerInterface } from '../channel.controller';
export class MetaController extends ChannelController implements ChannelControllerInterface {
private readonly logger = new Logger(MetaController.name);
constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) {
super(prismaRepository, waMonitor);
}
integrationEnabled: boolean;
public async receiveWebhook(data: any) {
if (data.object === 'whatsapp_business_account') {
if (data.entry[0]?.changes[0]?.field === 'message_template_status_update') {
const template = await this.prismaRepository.template.findFirst({
where: { templateId: `${data.entry[0].changes[0].value.message_template_id}` },
});
if (!template) {
console.log('template not found');
return;
}
const { webhookUrl } = template;
await axios.post(webhookUrl, data.entry[0].changes[0].value, {
headers: {
'Content-Type': 'application/json',
},
});
return;
}
data.entry?.forEach(async (entry: any) => {
const numberId = entry.changes[0].value.metadata.phone_number_id;
if (!numberId) {
this.logger.error('WebhookService -> receiveWebhookMeta -> numberId not found');
return {
status: 'success',
};
}
const instance = await this.prismaRepository.instance.findFirst({
where: { number: numberId },
});
if (!instance) {
this.logger.error('WebhookService -> receiveWebhookMeta -> instance not found');
return {
status: 'success',
};
}
await this.waMonitor.waInstances[instance.name].connectToWhatsapp(data);
return {
status: 'success',
};
});
}
return {
status: 'success',
};
}
}

View File

@@ -0,0 +1,24 @@
import { RouterBroker } from '@api/abstract/abstract.router';
import { metaController } from '@api/server.module';
import { ConfigService, WaBusiness } from '@config/env.config';
import { Router } from 'express';
export class MetaRouter extends RouterBroker {
constructor(readonly configService: ConfigService) {
super();
this.router
.get(this.routerPath('webhook/meta', false), async (req, res) => {
if (req.query['hub.verify_token'] === configService.get<WaBusiness>('WA_BUSINESS').TOKEN_WEBHOOK)
res.send(req.query['hub.challenge']);
else res.send('Error, wrong validation token');
})
.post(this.routerPath('webhook/meta', false), async (req, res) => {
const { body } = req;
const response = await metaController.receiveWebhook(body);
return res.status(200).json(response);
});
}
public readonly router: Router = Router();
}

File diff suppressed because it is too large Load Diff