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

@@ -186,76 +186,4 @@ export class WebhookController extends EventController implements EventControlle
}
}
}
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({
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;
}
const instance = await this.prismaRepository.instance.findFirst({
where: { number: numberId },
});
if (!instance) {
this.logger.error('WebhookService -> receiveWebhookMeta -> instance not found');
return;
}
await this.waMonitor.waInstances[instance.name].connectToWhatsapp(data);
return;
});
}
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',
};
}
}

View File

@@ -2,7 +2,7 @@ import { RouterBroker } from '@api/abstract/abstract.router';
import { InstanceDto } from '@api/dto/instance.dto';
import { HttpStatus } from '@api/routes/index.router';
import { webhookController } from '@api/server.module';
import { ConfigService, WaBusiness } from '@config/env.config';
import { ConfigService } from '@config/env.config';
import { instanceSchema, webhookSchema } from '@validate/validate.schema';
import { RequestHandler, Router } from 'express';
@@ -31,23 +31,6 @@ export class WebhookRouter extends RouterBroker {
});
res.status(HttpStatus.OK).json(response);
})
.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(this.routerPath('meta', false), async (req, res) => {
const { body } = req;
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);
});
}