Fix: Send webhooks on group creation
Move webhook calls to the "Cadastrar Grupo" button click in `CreateGroupFormSimple.tsx` to ensure they are sent when a group is created.
This commit is contained in:
parent
09ffd66b47
commit
3c9cf4de12
@ -29,6 +29,87 @@ const CreateGroupFormSimple = ({ userEmail, onSuccess }: CreateGroupFormProps) =
|
|||||||
texto: string;
|
texto: string;
|
||||||
} | null>(null);
|
} | null>(null);
|
||||||
|
|
||||||
|
const enviarWebhookAtivarWorkflow = async (email: string) => {
|
||||||
|
try {
|
||||||
|
console.log(`🔔 [WEBHOOK] Enviando webhook ativarworkflow para: ${email}`);
|
||||||
|
|
||||||
|
const webhookUrl = 'https://webhookn8n.innova1001.com.br/webhook/ativarworkflow';
|
||||||
|
|
||||||
|
const webhookBody = {
|
||||||
|
email: email,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
action: 'activate_workflow',
|
||||||
|
source: 'group_creation'
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log('📦 [WEBHOOK] Dados do webhook ativarworkflow:', JSON.stringify(webhookBody, null, 2));
|
||||||
|
|
||||||
|
const response = await fetch(webhookUrl, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(webhookBody)
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`📡 [WEBHOOK] Status da resposta ativarworkflow: ${response.status}`);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorText = await response.text();
|
||||||
|
console.error(`❌ [WEBHOOK] Erro ativarworkflow: ${response.status} - ${errorText}`);
|
||||||
|
throw new Error(`Erro webhook ativarworkflow: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const responseData = await response.text();
|
||||||
|
console.log('✅ [WEBHOOK] Webhook ativarworkflow enviado com sucesso:', responseData);
|
||||||
|
setWorkflowAtivado(true);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('💥 [WEBHOOK] Erro crítico webhook ativarworkflow:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const enviarWebhookHook = async (email: string) => {
|
||||||
|
try {
|
||||||
|
console.log(`🔔 [WEBHOOK] Enviando webhook hook para: ${email}`);
|
||||||
|
|
||||||
|
const webhookUrl = 'https://webhookn8n.innova1001.com.br/webhook/hook';
|
||||||
|
|
||||||
|
const webhookBody = {
|
||||||
|
email: email,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
source: 'group_creation'
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log('📦 [WEBHOOK] Dados do webhook hook:', JSON.stringify(webhookBody, null, 2));
|
||||||
|
|
||||||
|
const response = await fetch(webhookUrl, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(webhookBody)
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`📡 [WEBHOOK] Status da resposta hook: ${response.status}`);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorText = await response.text();
|
||||||
|
console.error(`❌ [WEBHOOK] Erro hook: ${response.status} - ${errorText}`);
|
||||||
|
throw new Error(`Erro webhook hook: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const responseData = await response.text();
|
||||||
|
console.log('✅ [WEBHOOK] Webhook hook enviado com sucesso:', responseData);
|
||||||
|
setWebhookEnviado(true);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('💥 [WEBHOOK] Erro crítico webhook hook:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent) => {
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
@ -47,7 +128,7 @@ const CreateGroupFormSimple = ({ userEmail, onSuccess }: CreateGroupFormProps) =
|
|||||||
setWorkflowAtivado(false);
|
setWorkflowAtivado(false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Verificar se o usuário tem instância WhatsApp
|
// 1. Verificar se o usuário tem instância WhatsApp
|
||||||
setMensagemStatus({ tipo: 'info', texto: 'Verificando sua instância do WhatsApp...' });
|
setMensagemStatus({ tipo: 'info', texto: 'Verificando sua instância do WhatsApp...' });
|
||||||
|
|
||||||
const instanciaInfo = await verificarInstanciaWhatsApp();
|
const instanciaInfo = await verificarInstanciaWhatsApp();
|
||||||
@ -60,7 +141,7 @@ const CreateGroupFormSimple = ({ userEmail, onSuccess }: CreateGroupFormProps) =
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Criar ou encontrar grupo
|
// 2. Criar ou encontrar grupo
|
||||||
setMensagemStatus({ tipo: 'info', texto: 'Cadastrando grupo...' });
|
setMensagemStatus({ tipo: 'info', texto: 'Cadastrando grupo...' });
|
||||||
|
|
||||||
const grupo = await findOrCreateWhatsAppGroup(nomeGrupo.trim());
|
const grupo = await findOrCreateWhatsAppGroup(nomeGrupo.trim());
|
||||||
@ -68,37 +149,43 @@ const CreateGroupFormSimple = ({ userEmail, onSuccess }: CreateGroupFormProps) =
|
|||||||
throw new Error('Falha ao criar o grupo');
|
throw new Error('Falha ao criar o grupo');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Criar workflow no n8n
|
// 3. Criar workflow no n8n
|
||||||
setMensagemStatus({ tipo: 'info', texto: 'Configurando automação...' });
|
setMensagemStatus({ tipo: 'info', texto: 'Configurando automação...' });
|
||||||
|
|
||||||
await createWorkflowInN8n(userEmail);
|
await createWorkflowInN8n(userEmail);
|
||||||
|
|
||||||
// AGORA SIM: Ativar workflow (webhook ativarworkflow) APENAS NO MOMENTO DE CADASTRAR GRUPO
|
// 4. ENVIAR WEBHOOK ATIVARWORKFLOW
|
||||||
console.log('🔔 [GRUPO] Enviando webhook ativarworkflow no momento do cadastro do grupo');
|
console.log('🚀 [GRUPO] Enviando webhook ativarworkflow no cadastro do grupo');
|
||||||
setMensagemStatus({ tipo: 'info', texto: 'Ativando workflow...' });
|
setMensagemStatus({ tipo: 'info', texto: 'Ativando workflow...' });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(`🔔 Enviando webhook ativarworkflow para usuário: ${userEmail}`);
|
await enviarWebhookAtivarWorkflow(userEmail);
|
||||||
await activateUserWorkflow(userEmail);
|
console.log('✅ [GRUPO] Webhook ativarworkflow enviado com sucesso');
|
||||||
setWorkflowAtivado(true);
|
|
||||||
console.log('✅ [GRUPO] Webhook ativarworkflow enviado com sucesso no cadastro do grupo');
|
|
||||||
} catch (workflowError) {
|
} catch (workflowError) {
|
||||||
console.error('❌ Erro ao enviar webhook ativarworkflow:', workflowError);
|
console.error('❌ [GRUPO] Erro ao enviar webhook ativarworkflow:', workflowError);
|
||||||
// Continua mesmo se o webhook falhar
|
// Continua mesmo se o webhook falhar
|
||||||
|
toast({
|
||||||
|
title: 'Aviso',
|
||||||
|
description: 'Grupo criado, mas houve erro ao ativar workflow',
|
||||||
|
variant: 'destructive',
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// AGORA SIM: Enviar webhook para N8N configurar webhook da Evolution API APENAS NO MOMENTO DE CADASTRAR GRUPO
|
// 5. ENVIAR WEBHOOK HOOK
|
||||||
console.log('🔔 [GRUPO] Enviando webhook de configuração no momento do cadastro do grupo');
|
console.log('🚀 [GRUPO] Enviando webhook hook no cadastro do grupo');
|
||||||
setMensagemStatus({ tipo: 'info', texto: 'Configurando webhook...' });
|
setMensagemStatus({ tipo: 'info', texto: 'Configurando webhook...' });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(`🔔 Enviando email para N8N configurar webhook: ${userEmail}`);
|
await enviarWebhookHook(userEmail);
|
||||||
await createEvolutionWebhook(userEmail);
|
console.log('✅ [GRUPO] Webhook hook enviado com sucesso');
|
||||||
setWebhookEnviado(true);
|
|
||||||
console.log('✅ [GRUPO] Email enviado com sucesso para N8N no cadastro do grupo');
|
|
||||||
} catch (webhookError) {
|
} catch (webhookError) {
|
||||||
console.error('❌ Erro ao enviar email para N8N:', webhookError);
|
console.error('❌ [GRUPO] Erro ao enviar webhook hook:', webhookError);
|
||||||
// Continua mesmo se o webhook falhar
|
// Continua mesmo se o webhook falhar
|
||||||
|
toast({
|
||||||
|
title: 'Aviso',
|
||||||
|
description: 'Grupo criado, mas houve erro na configuração do webhook',
|
||||||
|
variant: 'destructive',
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
setMensagemStatus({
|
setMensagemStatus({
|
||||||
@ -118,7 +205,7 @@ const CreateGroupFormSimple = ({ userEmail, onSuccess }: CreateGroupFormProps) =
|
|||||||
onSuccess();
|
onSuccess();
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Erro ao cadastrar grupo:', error);
|
console.error('❌ [GRUPO] Erro ao cadastrar grupo:', error);
|
||||||
setMensagemStatus({
|
setMensagemStatus({
|
||||||
tipo: 'error',
|
tipo: 'error',
|
||||||
texto: 'Erro ao cadastrar grupo. Tente novamente.'
|
texto: 'Erro ao cadastrar grupo. Tente novamente.'
|
||||||
@ -165,6 +252,24 @@ const CreateGroupFormSimple = ({ userEmail, onSuccess }: CreateGroupFormProps) =
|
|||||||
</Alert>
|
</Alert>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{/* Status dos webhooks */}
|
||||||
|
{(workflowAtivado || webhookEnviado) && (
|
||||||
|
<div className="space-y-2">
|
||||||
|
{workflowAtivado && (
|
||||||
|
<div className="flex items-center text-sm text-green-600">
|
||||||
|
<CheckCircle2 className="h-4 w-4 mr-2" />
|
||||||
|
Webhook ativarworkflow enviado
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{webhookEnviado && (
|
||||||
|
<div className="flex items-center text-sm text-green-600">
|
||||||
|
<CheckCircle2 className="h-4 w-4 mr-2" />
|
||||||
|
Webhook hook enviado
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
<Button type="submit" disabled={carregando || !nomeGrupo.trim()}>
|
<Button type="submit" disabled={carregando || !nomeGrupo.trim()}>
|
||||||
{carregando ? (
|
{carregando ? (
|
||||||
<>
|
<>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user