feat: evolution channel in instance create

This commit is contained in:
Davidson Gomes
2024-08-22 19:49:51 -03:00
parent 2196f65b7a
commit 0e9cd21981
15 changed files with 597 additions and 64 deletions
@@ -187,7 +187,7 @@ export class WebhookController extends EventController implements EventControlle
}
}
public async receiveWebhook(data: any) {
public async receiveWebhookMeta(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({
@@ -213,7 +213,7 @@ export class WebhookController extends EventController implements EventControlle
const numberId = entry.changes[0].value.metadata.phone_number_id;
if (!numberId) {
this.logger.error('WebhookService -> receiveWebhook -> numberId not found');
this.logger.error('WebhookService -> receiveWebhookMeta -> numberId not found');
return;
}
@@ -222,7 +222,7 @@ export class WebhookController extends EventController implements EventControlle
});
if (!instance) {
this.logger.error('WebhookService -> receiveWebhook -> instance not found');
this.logger.error('WebhookService -> receiveWebhookMeta -> instance not found');
return;
}
@@ -234,4 +234,28 @@ export class WebhookController extends EventController implements EventControlle
return;
}
public async receiveWebhookEvolution(data: any) {
const numberId = data.numberId;
if (!numberId) {
this.logger.error('WebhookService -> receiveWebhookEvolution -> numberId not found');
return;
}
const instance = await this.prismaRepository.instance.findFirst({
where: { number: numberId },
});
if (!instance) {
this.logger.error('WebhookService -> receiveWebhook -> instance not found');
return;
}
await this.waMonitor.waInstances[instance.name].connectToWhatsapp(data);
return {
status: 'success',
};
}
}
@@ -32,14 +32,20 @@ export class WebhookRouter extends RouterBroker {
res.status(HttpStatus.OK).json(response);
})
.get('meta', async (req, res) => {
.get(this.routerPath('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('meta', async (req, res) => {
.post(this.routerPath('meta', false), async (req, res) => {
const { body } = req;
const response = await webhookController.receiveWebhook(body);
const response = await webhookController.receiveWebhookMeta(body);
return res.status(200).json(response);
})
.post(this.routerPath('evolution', false), async (req, res) => {
const { body } = req;
const response = await webhookController.receiveWebhookEvolution(body);
return res.status(200).json(response);
});