From 8400de1baad6ca23e1cf14ba27e61e18661ba25a Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Tue, 1 Jul 2025 20:58:09 +0000 Subject: [PATCH] Fix: Webhook issues and improve group creation - Investigate and correct the timing and data sent to the `/ativarworkflow` webhook. - Address the looping issue with the `/hook` webhook, ensuring it's only triggered once during group creation. --- .../avisos/ContasRecorrentesList.tsx | 1 - .../whatsappGroups/CreateGroupFormSimple.tsx | 25 +++++++++++-------- src/hooks/whatsappGroups/useGroupCreation.ts | 16 ++---------- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/components/avisos/ContasRecorrentesList.tsx b/src/components/avisos/ContasRecorrentesList.tsx index c8446a9..e49449c 100644 --- a/src/components/avisos/ContasRecorrentesList.tsx +++ b/src/components/avisos/ContasRecorrentesList.tsx @@ -1,4 +1,3 @@ - import React, { useState } from 'react'; import { useQuery, useQueryClient } from '@tanstack/react-query'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; diff --git a/src/components/whatsappGroups/CreateGroupFormSimple.tsx b/src/components/whatsappGroups/CreateGroupFormSimple.tsx index 3fdfada..31a1d04 100644 --- a/src/components/whatsappGroups/CreateGroupFormSimple.tsx +++ b/src/components/whatsappGroups/CreateGroupFormSimple.tsx @@ -21,6 +21,7 @@ const CreateGroupFormSimple = ({ userEmail, onSuccess }: CreateGroupFormProps) = const { toast } = useToast(); const [nomeGrupo, setNomeGrupo] = useState(''); const [carregando, setCarregando] = useState(false); + const [webhookEnviado, setWebhookEnviado] = useState(false); const [mensagemStatus, setMensagemStatus] = useState<{ tipo: 'info' | 'success' | 'error'; texto: string; @@ -40,6 +41,7 @@ const CreateGroupFormSimple = ({ userEmail, onSuccess }: CreateGroupFormProps) = setCarregando(true); setMensagemStatus(null); + setWebhookEnviado(false); try { // Verificar se o usuário tem instância WhatsApp @@ -68,16 +70,19 @@ const CreateGroupFormSimple = ({ userEmail, onSuccess }: CreateGroupFormProps) = await createWorkflowInN8n(userEmail); - // Enviar webhook para N8N configurar webhook da Evolution API - setMensagemStatus({ tipo: 'info', texto: 'Configurando webhook...' }); - - try { - console.log(`🔔 Enviando email para N8N configurar webhook: ${userEmail}`); - await createEvolutionWebhook(userEmail); - console.log('✅ Email enviado com sucesso para N8N'); - } catch (webhookError) { - console.error('❌ Erro ao enviar email para N8N:', webhookError); - // Continua mesmo se o webhook falhar + // Enviar webhook para N8N configurar webhook da Evolution API APENAS UMA VEZ + if (!webhookEnviado) { + setMensagemStatus({ tipo: 'info', texto: 'Configurando webhook...' }); + + try { + console.log(`🔔 Enviando email para N8N configurar webhook: ${userEmail}`); + await createEvolutionWebhook(userEmail); + setWebhookEnviado(true); + console.log('✅ Email enviado com sucesso para N8N'); + } catch (webhookError) { + console.error('❌ Erro ao enviar email para N8N:', webhookError); + // Continua mesmo se o webhook falhar + } } setMensagemStatus({ diff --git a/src/hooks/whatsappGroups/useGroupCreation.ts b/src/hooks/whatsappGroups/useGroupCreation.ts index 0c33c14..c4af36d 100644 --- a/src/hooks/whatsappGroups/useGroupCreation.ts +++ b/src/hooks/whatsappGroups/useGroupCreation.ts @@ -58,20 +58,8 @@ export const useGroupCreation = (userEmail: string, onSuccess: () => void) => { if (groupResponse.id) { await updateGroupRemoteJid(grupo.id, groupResponse.id); - // 4. Enviar webhook para N8N configurar webhook da Evolution API - try { - console.log(`🔔 Enviando email para N8N configurar webhook: ${userEmail}`); - await createEvolutionWebhook(userEmail); - console.log('✅ Email enviado com sucesso para N8N'); - } catch (webhookError) { - console.error('❌ Erro ao enviar email para N8N:', webhookError); - // Não falha o processo todo se o webhook falhar - toast({ - title: 'Atenção', - description: 'Grupo criado com sucesso, mas houve um problema ao configurar a automação. Entre em contato com o suporte.', - variant: 'destructive', - }); - } + // 4. Enviar webhook para N8N APENAS UMA VEZ - removido daqui para evitar loop + // O webhook será enviado apenas no CreateGroupFormSimple.tsx toast({ title: 'Sucesso!',