Fix: Ensure activate workflow webhook is sent
Check and adjust the logic to ensure the "ativarworkflow" webhook is sent correctly.
This commit is contained in:
parent
f16f2f4a49
commit
17810e5c86
@ -11,6 +11,7 @@ import { verificarInstanciaWhatsApp } from '@/services/gruposWhatsAppService';
|
|||||||
import { findOrCreateWhatsAppGroup } from '@/services/whatsAppGroupsService';
|
import { findOrCreateWhatsAppGroup } from '@/services/whatsAppGroupsService';
|
||||||
import { createWorkflowInN8n } from '@/services/n8nWorkflowService';
|
import { createWorkflowInN8n } from '@/services/n8nWorkflowService';
|
||||||
import { createEvolutionWebhook } from '@/services/whatsApp/webhookService';
|
import { createEvolutionWebhook } from '@/services/whatsApp/webhookService';
|
||||||
|
import { activateUserWorkflow } from '@/services/whatsAppInstance/workflowOperations';
|
||||||
|
|
||||||
interface CreateGroupFormProps {
|
interface CreateGroupFormProps {
|
||||||
userEmail: string;
|
userEmail: string;
|
||||||
@ -22,6 +23,7 @@ const CreateGroupFormSimple = ({ userEmail, onSuccess }: CreateGroupFormProps) =
|
|||||||
const [nomeGrupo, setNomeGrupo] = useState('');
|
const [nomeGrupo, setNomeGrupo] = useState('');
|
||||||
const [carregando, setCarregando] = useState(false);
|
const [carregando, setCarregando] = useState(false);
|
||||||
const [webhookEnviado, setWebhookEnviado] = useState(false);
|
const [webhookEnviado, setWebhookEnviado] = useState(false);
|
||||||
|
const [workflowAtivado, setWorkflowAtivado] = useState(false);
|
||||||
const [mensagemStatus, setMensagemStatus] = useState<{
|
const [mensagemStatus, setMensagemStatus] = useState<{
|
||||||
tipo: 'info' | 'success' | 'error';
|
tipo: 'info' | 'success' | 'error';
|
||||||
texto: string;
|
texto: string;
|
||||||
@ -42,6 +44,7 @@ const CreateGroupFormSimple = ({ userEmail, onSuccess }: CreateGroupFormProps) =
|
|||||||
setCarregando(true);
|
setCarregando(true);
|
||||||
setMensagemStatus(null);
|
setMensagemStatus(null);
|
||||||
setWebhookEnviado(false);
|
setWebhookEnviado(false);
|
||||||
|
setWorkflowAtivado(false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Verificar se o usuário tem instância WhatsApp
|
// Verificar se o usuário tem instância WhatsApp
|
||||||
@ -70,6 +73,21 @@ const CreateGroupFormSimple = ({ userEmail, onSuccess }: CreateGroupFormProps) =
|
|||||||
|
|
||||||
await createWorkflowInN8n(userEmail);
|
await createWorkflowInN8n(userEmail);
|
||||||
|
|
||||||
|
// Ativar workflow (webhook ativarworkflow) APENAS UMA VEZ
|
||||||
|
if (!workflowAtivado) {
|
||||||
|
setMensagemStatus({ tipo: 'info', texto: 'Ativando workflow...' });
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.log(`🔔 Enviando webhook ativarworkflow para usuário: ${userEmail}`);
|
||||||
|
await activateUserWorkflow(userEmail);
|
||||||
|
setWorkflowAtivado(true);
|
||||||
|
console.log('✅ Webhook ativarworkflow enviado com sucesso');
|
||||||
|
} catch (workflowError) {
|
||||||
|
console.error('❌ Erro ao enviar webhook ativarworkflow:', workflowError);
|
||||||
|
// Continua mesmo se o webhook falhar
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Enviar webhook para N8N configurar webhook da Evolution API APENAS UMA VEZ
|
// Enviar webhook para N8N configurar webhook da Evolution API APENAS UMA VEZ
|
||||||
if (!webhookEnviado) {
|
if (!webhookEnviado) {
|
||||||
setMensagemStatus({ tipo: 'info', texto: 'Configurando webhook...' });
|
setMensagemStatus({ tipo: 'info', texto: 'Configurando webhook...' });
|
||||||
|
|||||||
@ -2,7 +2,8 @@
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useToast } from '@/hooks/use-toast';
|
import { useToast } from '@/hooks/use-toast';
|
||||||
import { createWhatsAppInstance } from '@/services/whatsAppService';
|
import { createWhatsAppInstance } from '@/services/whatsAppService';
|
||||||
import { updateUserWhatsAppInstance, activateUserWorkflow } from '@/services/whatsAppInstanceService';
|
import { updateUserWhatsAppInstance } from '@/services/whatsAppInstanceService';
|
||||||
|
import { activateUserWorkflow } from '@/services/whatsAppInstance/workflowOperations';
|
||||||
import { WhatsAppInstance } from '@/types/whatsAppTypes';
|
import { WhatsAppInstance } from '@/types/whatsAppTypes';
|
||||||
|
|
||||||
export const useInstanceCreation = (
|
export const useInstanceCreation = (
|
||||||
@ -74,13 +75,14 @@ export const useInstanceCreation = (
|
|||||||
});
|
});
|
||||||
console.log('✅ [INSTÂNCIA] Instância registrada no banco de dados com status "conectado"');
|
console.log('✅ [INSTÂNCIA] Instância registrada no banco de dados com status "conectado"');
|
||||||
|
|
||||||
// 3. Ativar o workflow do usuário no n8n
|
// 3. Ativar o workflow do usuário no n8n - CORRIGINDO AQUI
|
||||||
console.log('🔄 [INSTÂNCIA] Passo 3: Ativando workflow do usuário no n8n...');
|
console.log('🔄 [INSTÂNCIA] Passo 3: Ativando workflow do usuário no n8n...');
|
||||||
try {
|
try {
|
||||||
|
console.log(`🔔 Enviando webhook ativarworkflow para usuário: ${userEmail}`);
|
||||||
await activateUserWorkflow(userEmail);
|
await activateUserWorkflow(userEmail);
|
||||||
console.log('✅ [INSTÂNCIA] Workflow ativado com sucesso no n8n');
|
console.log('✅ [INSTÂNCIA] Webhook ativarworkflow enviado com sucesso');
|
||||||
} catch (workflowError) {
|
} catch (workflowError) {
|
||||||
console.error('⚠️ [INSTÂNCIA] Erro ao ativar workflow, mas instância foi criada:', workflowError);
|
console.error('⚠️ [INSTÂNCIA] Erro ao enviar webhook ativarworkflow:', workflowError);
|
||||||
toast({
|
toast({
|
||||||
title: "Aviso",
|
title: "Aviso",
|
||||||
description: "Instância criada, mas houve um problema ao ativar a automação. Entre em contato com o suporte.",
|
description: "Instância criada, mas houve um problema ao ativar a automação. Entre em contato com o suporte.",
|
||||||
|
|||||||
@ -12,12 +12,14 @@ export async function activateUserWorkflow(userEmail: string): Promise<void> {
|
|||||||
try {
|
try {
|
||||||
console.log(`🔔 Enviando webhook para ativar workflow do usuário: ${userEmail}`);
|
console.log(`🔔 Enviando webhook para ativar workflow do usuário: ${userEmail}`);
|
||||||
|
|
||||||
// Nova lógica: webhook simples para o n8n
|
// URL do webhook para ativar workflow
|
||||||
const webhookUrl = 'https://webhookn8n.innova1001.com.br/webhook/ativarworkflow';
|
const webhookUrl = 'https://webhookn8n.innova1001.com.br/webhook/ativarworkflow';
|
||||||
console.log(`🔗 URL do webhook: ${webhookUrl}`);
|
console.log(`🔗 URL do webhook: ${webhookUrl}`);
|
||||||
|
|
||||||
const webhookBody = {
|
const webhookBody = {
|
||||||
email: userEmail
|
email: userEmail,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
action: 'activate_workflow'
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log('📦 Dados do webhook:', JSON.stringify(webhookBody, null, 2));
|
console.log('📦 Dados do webhook:', JSON.stringify(webhookBody, null, 2));
|
||||||
@ -39,7 +41,7 @@ export async function activateUserWorkflow(userEmail: string): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const responseData = await response.text();
|
const responseData = await response.text();
|
||||||
console.log('✅ Webhook enviado com sucesso:', responseData);
|
console.log('✅ Webhook ativarworkflow enviado com sucesso:', responseData);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('💥 Erro crítico ao enviar webhook de ativação:', error);
|
console.error('💥 Erro crítico ao enviar webhook de ativação:', error);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user