Fix: Resolve group registration error

Investigate and fix the "Não foi possível registrar o grupo" error, likely related to n8n workflow creation. Check API key and other potential causes.
This commit is contained in:
gpt-engineer-app[bot] 2025-05-20 10:19:49 +00:00
parent aae413643d
commit e5dbce2562
2 changed files with 31 additions and 4 deletions

View File

@ -1,3 +1,4 @@
import { useState, useEffect } from 'react';
import Layout from '@/components/layout/Layout';
import { Button } from '@/components/ui/button';
@ -8,6 +9,7 @@ import { Loader2, Plus, RefreshCw, MessageSquare } from 'lucide-react';
import { cadastrarGrupoWhatsApp, listarGruposWhatsApp } from '@/services/gruposWhatsAppService';
import { WhatsAppGroup } from '@/types/financialTypes';
import { useToast } from '@/hooks/use-toast';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
const GruposWhatsApp = () => {
const { toast } = useToast();
@ -15,10 +17,12 @@ const GruposWhatsApp = () => {
const [carregando, setCarregando] = useState<boolean>(true);
const [cadastrando, setCadastrando] = useState<boolean>(false);
const [userEmail, setUserEmail] = useState<string>('');
const [errorMessage, setErrorMessage] = useState<string | null>(null);
// Buscar os grupos do usuário ao carregar a página
const buscarGrupos = async () => {
setCarregando(true);
setErrorMessage(null);
try {
const gruposData = await listarGruposWhatsApp();
setGrupos(gruposData);
@ -46,6 +50,8 @@ const GruposWhatsApp = () => {
// Cadastrar novo grupo
const handleCadastrarGrupo = async () => {
setErrorMessage(null);
if (!userEmail) {
toast({
title: 'Erro',
@ -77,6 +83,7 @@ const GruposWhatsApp = () => {
// Atualizar a lista de grupos
buscarGrupos();
} else {
setErrorMessage('Não foi possível registrar o grupo. O grupo deve ser criado sempre, mesmo se der erro no n8n.');
toast({
title: 'Erro',
description: 'Não foi possível registrar o grupo',
@ -85,6 +92,7 @@ const GruposWhatsApp = () => {
}
} catch (error) {
console.error('Erro ao cadastrar grupo:', error);
setErrorMessage('Erro ao cadastrar grupo: ' + (error instanceof Error ? error.message : 'Erro desconhecido'));
toast({
title: 'Erro',
description: 'Não foi possível registrar o grupo',
@ -129,6 +137,15 @@ const GruposWhatsApp = () => {
</div>
</div>
{errorMessage && (
<Alert variant="destructive">
<AlertTitle>Erro ao cadastrar grupo</AlertTitle>
<AlertDescription>
{errorMessage}
</AlertDescription>
</Alert>
)}
<Card>
<CardHeader>
<CardTitle>Como cadastrar um grupo</CardTitle>

View File

@ -31,7 +31,13 @@ export async function cadastrarGrupoWhatsApp(): Promise<WhatsAppGroup | null> {
throw new Error('Não foi possível cadastrar o grupo de WhatsApp');
}
if (!data || data.length === 0) {
console.error('Nenhum dado retornado após inserção');
throw new Error('Nenhum dado retornado após inserção');
}
console.log('Grupo WhatsApp cadastrado com sucesso:', data);
const newGroup = data[0];
// Criar workflow no n8n
try {
@ -39,16 +45,18 @@ export async function cadastrarGrupoWhatsApp(): Promise<WhatsAppGroup | null> {
if (workflowResponse && workflowResponse.id) {
// Atualizar o objeto data com o workflow_id
await atualizarWorkflowId(data[0].id, workflowResponse.id);
data[0].workflow_id = workflowResponse.id;
await atualizarWorkflowId(newGroup.id, workflowResponse.id);
newGroup.workflow_id = workflowResponse.id;
console.log('Workflow criado com sucesso no n8n:', workflowResponse.id);
} else {
console.log('Resposta do n8n não contém ID de workflow válido');
}
} catch (n8nError) {
console.error('Erro ao criar workflow no n8n:', n8nError);
// Não impede a criação do grupo, apenas não adiciona o workflow_id
}
return data[0];
return newGroup;
} catch (error) {
console.error('Erro ao cadastrar grupo do WhatsApp:', error);
return null;
@ -75,7 +83,9 @@ async function createWorkflowInN8n(email: string): Promise<{ id: string } | null
});
if (!response.ok) {
throw new Error(`Erro ao criar workflow: ${response.statusText}`);
const errorText = await response.text();
console.error(`Erro ao criar workflow: Status ${response.status}`, errorText);
throw new Error(`Erro ao criar workflow: Status ${response.status}`);
}
const data = await response.json();