feat: Sending template approval status webhook

This commit is contained in:
Davidson Gomes
2024-07-25 19:38:34 -03:00
parent 31cb83a40c
commit 0bb2b92853
11 changed files with 81 additions and 7 deletions

View File

@@ -90,7 +90,7 @@ export class InstanceController {
businessId,
}: InstanceDto) {
try {
if (token) await this.authService.checkDuplicateToken(token);
// if (token) await this.authService.checkDuplicateToken(token);
if (!token && integration === Integration.WHATSAPP_BUSINESS) {
throw new BadRequestException('token is required');
@@ -623,14 +623,14 @@ export class InstanceController {
// let arrayReturn = false;
if (env.KEY !== key) {
const instanceByKey = await this.prismaRepository.instance.findUnique({
const instanceByKey = await this.prismaRepository.instance.findMany({
where: {
token: key,
},
});
if (instanceByKey) {
name = instanceByKey.name;
name = instanceByKey[0].name;
// arrayReturn = true;
} else {
throw new UnauthorizedException();

View File

@@ -4,4 +4,5 @@ export class TemplateDto {
allowCategoryChange: boolean;
language: string;
components: any;
webhookUrl?: string;
}

View File

@@ -126,6 +126,7 @@ export class BusinessStartupService extends ChannelStartupService {
if (!data) return;
const content = data.entry[0].changes[0].value;
try {
this.loadWebhook();
this.loadChatwoot();
@@ -297,6 +298,7 @@ export class BusinessStartupService extends ChannelStartupService {
protected async messageHandle(received: any, database: Database, settings: any) {
try {
console.log(received);
let messageRaw: any;
let pushName: any;

View File

@@ -59,11 +59,21 @@ export class TemplateService {
const response = await this.requestTemplate(postData, 'POST');
if (!response) {
return response;
if (!response || response.error) {
throw new Error('Error to create template');
}
return response;
const template = await this.prismaRepository.template.create({
data: {
templateId: response.id,
name: data.name,
template: response,
webhookUrl: data.webhookUrl,
instanceId: getInstance.id,
},
});
return template;
} catch (error) {
this.logger.error(error);
throw new Error('Error to create template');

View File

@@ -1,4 +1,5 @@
import { Webhook } from '@prisma/client';
import axios from 'axios';
import { Logger } from '../../config/logger.config';
import { InstanceDto } from '../dto/instance.dto';
@@ -33,6 +34,26 @@ export class WebhookService {
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;