Refactor: Send user email to N8N webhook

Modify `createEvolutionWebhook` to send only the user's email to the N8N webhook for processing.  Update the webhook URL to `https://webhookn8n.innova1001.com.br/webhook/hook`.
This commit is contained in:
gpt-engineer-app[bot] 2025-06-25 17:53:26 +00:00
parent 31659a2157
commit d2070e6b34

View File

@ -2,41 +2,44 @@
import { makeRequest } from './apiHelpers'; import { makeRequest } from './apiHelpers';
/** /**
* Cria um webhook na Evolution API para o usuário * Envia o email do usuário para o N8N configurar o webhook
*/ */
export const createEvolutionWebhook = async (userEmail: string): Promise<any> => { export const createEvolutionWebhook = async (userEmail: string): Promise<any> => {
try { try {
console.log(`Criando webhook para o usuário: ${userEmail}`); console.log(`Enviando email do usuário para N8N configurar webhook: ${userEmail}`);
// URL do endpoint - mantém o @ normal no email const webhookUrl = 'https://webhookn8n.innova1001.com.br/webhook/hook';
const endpoint = `/webhook/set/${userEmail}`;
// Agora mantemos o email original com @ também na URL do webhook
const webhookBody = { const webhookBody = {
webhook: { email: userEmail
enabled: true,
url: `https://webhookn8n.innova1001.com.br/webhook/${userEmail}`,
webhookByEvents: true,
webhookBase64: true,
events: [
"MESSAGES_UPSERT"
]
}
}; };
console.log('Dados do webhook:', { console.log('Enviando dados para N8N:', {
endpoint, webhookUrl,
emailOriginal: userEmail, email: userEmail
webhookUrl: webhookBody.webhook.url
}); });
const response = await makeRequest(endpoint, 'POST', webhookBody); const response = await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(webhookBody)
});
console.log(`✅ Webhook criado com sucesso para ${userEmail}:`, response); if (!response.ok) {
return response; const errorText = await response.text();
console.error(`Erro ao enviar webhook para N8N: ${response.status} - ${errorText}`);
throw new Error(`Erro ao notificar N8N: ${response.status} - ${errorText}`);
}
const responseData = await response.text();
console.log('✅ Email enviado com sucesso para N8N:', responseData);
return { success: true, message: 'Email enviado para N8N configurar webhook' };
} catch (error) { } catch (error) {
console.error(`❌ Erro ao criar webhook for ${userEmail}:`, error); console.error('❌ Erro ao enviar email para N8N:', error);
throw error; throw error;
} }
}; };