diff --git a/src/components/whatsappGroups/CreateGroupForm.tsx b/src/components/whatsappGroups/CreateGroupForm.tsx index 6afa4c6..b3cdf06 100644 --- a/src/components/whatsappGroups/CreateGroupForm.tsx +++ b/src/components/whatsappGroups/CreateGroupForm.tsx @@ -117,9 +117,9 @@ const CreateGroupForm = ({ userEmail, onSuccess }: CreateGroupFormProps) => { throw new Error('Não foi possível cadastrar o grupo no banco de dados'); } - // 2. Criar grupo no WhatsApp via API com os parâmetros corretos + // 2. Criar grupo no WhatsApp via API com o nome escolhido pelo usuário try { - const groupResponse = await createWhatsAppGroup(userEmail); + const groupResponse = await createWhatsAppGroup(userEmail, nomeGrupo.trim()); console.log('Resposta da criação do grupo:', groupResponse); @@ -129,7 +129,7 @@ const CreateGroupForm = ({ userEmail, onSuccess }: CreateGroupFormProps) => { toast({ title: 'Sucesso!', - description: `Grupo "${groupResponse.subject}" criado com sucesso no seu WhatsApp!`, + description: `Grupo "${nomeGrupo}" criado com sucesso no seu WhatsApp!`, variant: 'default', }); } else { @@ -260,7 +260,7 @@ const CreateGroupForm = ({ userEmail, onSuccess }: CreateGroupFormProps) => { diff --git a/src/components/whatsappGroups/GroupCard.tsx b/src/components/whatsappGroups/GroupCard.tsx new file mode 100644 index 0000000..9c7ab37 --- /dev/null +++ b/src/components/whatsappGroups/GroupCard.tsx @@ -0,0 +1,58 @@ + +import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; +import { Button } from '@/components/ui/button'; +import { Badge } from '@/components/ui/badge'; +import { MessageSquare, Trash2 } from 'lucide-react'; +import { WhatsAppGroup } from '@/types/financialTypes'; + +interface GroupCardProps { + group: WhatsAppGroup; + onDelete: (groupId: number) => void; +} + +const GroupCard = ({ group, onDelete }: GroupCardProps) => { + return ( + + +
+
+ + {group.nome_grupo} +
+ +
+
+ +
+
+ Status: + + {group.remote_jid ? 'Ativo' : 'Pendente'} + +
+ +
+ Workflow: + + {group.workflow_id ? 'Configurado' : 'Não configurado'} + +
+ +
+ Criado em: + {new Date(group.created_at).toLocaleDateString()} +
+
+
+
+ ); +}; + +export default GroupCard; diff --git a/src/components/whatsappGroups/GroupsList.tsx b/src/components/whatsappGroups/GroupsList.tsx index 207a8bf..61b0279 100644 --- a/src/components/whatsappGroups/GroupsList.tsx +++ b/src/components/whatsappGroups/GroupsList.tsx @@ -1,16 +1,19 @@ -import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'; -import { Badge } from '@/components/ui/badge'; import { Loader2, MessageSquare } from 'lucide-react'; import { WhatsAppGroup } from '@/types/financialTypes'; +import GroupCard from './GroupCard'; interface GroupsListProps { grupos: WhatsAppGroup[]; carregando: boolean; + onDeleteGroup: (groupId: number) => void; } -const GroupsList = ({ grupos, carregando }: GroupsListProps) => { +const GroupsList = ({ grupos, carregando, onDeleteGroup }: GroupsListProps) => { + // Filtrar apenas grupos que têm nome_grupo preenchido + const gruposComNome = grupos.filter(grupo => grupo.nome_grupo && grupo.nome_grupo.trim() !== ''); + return ( @@ -24,49 +27,16 @@ const GroupsList = ({ grupos, carregando }: GroupsListProps) => {
- ) : grupos.length > 0 ? ( - - - - ID - Nome do grupo - Status - Workflow - Cadastro - - - - {grupos.map((grupo) => ( - - {grupo.id} - - {grupo.nome_grupo || ( - - {grupo.remote_jid ? 'Sem nome definido' : 'Aguardando vínculo...'} - - )} - - - - {grupo.remote_jid ? 'Ativo' : 'Pendente'} - - - - {grupo.workflow_id ? ( - - Configurado - - ) : ( - - Não configurado - - )} - - {new Date(grupo.created_at).toLocaleDateString()} - - ))} - -
+ ) : gruposComNome.length > 0 ? ( +
+ {gruposComNome.map((grupo) => ( + + ))} +
) : (
diff --git a/src/pages/GruposWhatsApp.tsx b/src/pages/GruposWhatsApp.tsx index 925b00f..5c464d1 100644 --- a/src/pages/GruposWhatsApp.tsx +++ b/src/pages/GruposWhatsApp.tsx @@ -4,6 +4,7 @@ import Layout from '@/components/layout/Layout'; import { Button } from '@/components/ui/button'; import { RefreshCw } from 'lucide-react'; import { listarGruposWhatsApp } from '@/services/gruposWhatsAppService'; +import { deleteWhatsAppGroup } from '@/services/whatsAppGroupsService'; import { WhatsAppGroup } from '@/types/financialTypes'; import { useToast } from '@/hooks/use-toast'; import CreateGroupForm from '@/components/whatsappGroups/CreateGroupForm'; @@ -33,6 +34,26 @@ const GruposWhatsApp = () => { } }; + // Deletar grupo + const handleDeleteGroup = async (groupId: number) => { + try { + await deleteWhatsAppGroup(groupId); + toast({ + title: 'Sucesso', + description: 'Grupo deletado com sucesso', + }); + // Atualizar a lista após deletar + buscarGrupos(); + } catch (error) { + console.error('Erro ao deletar grupo:', error); + toast({ + title: 'Erro', + description: 'Não foi possível deletar o grupo', + variant: 'destructive', + }); + } + }; + useEffect(() => { // Obter o email do usuário do localStorage const email = localStorage.getItem('userEmail'); @@ -62,7 +83,11 @@ const GruposWhatsApp = () => {
- + ); diff --git a/src/services/whatsAppGroupCreationService.ts b/src/services/whatsAppGroupCreationService.ts index 871506b..fac4335 100644 --- a/src/services/whatsAppGroupCreationService.ts +++ b/src/services/whatsAppGroupCreationService.ts @@ -1,4 +1,3 @@ - import { supabase } from "@/integrations/supabase/client"; interface CreateGroupResponse { @@ -9,20 +8,17 @@ interface CreateGroupResponse { } /** - * Cria um grupo WhatsApp via API da Evolution usando o padrão correto + * Cria um grupo WhatsApp via API da Evolution usando o nome escolhido pelo usuário */ export async function createWhatsAppGroup( - userEmail: string + userEmail: string, + groupName: string ): Promise { try { - // Extrair nome do usuário do email (parte antes do @) - const userName = userEmail.split('@')[0]; - const groupSubject = `finance${userName}`; - const url = `https://evolutionapi2.innova1001.com.br/group/create/${userEmail}`; const requestBody = { - subject: groupSubject, + subject: groupName, description: "Finance Home seu controle sem complicação", participants: ["5561992444275"] }; diff --git a/src/services/whatsAppGroupsService.ts b/src/services/whatsAppGroupsService.ts index 105b630..c3e05ad 100644 --- a/src/services/whatsAppGroupsService.ts +++ b/src/services/whatsAppGroupsService.ts @@ -1,4 +1,3 @@ - // Service dedicated to WhatsApp groups database operations import { supabase } from "@/integrations/supabase/client"; import { WhatsAppGroup } from "@/types/financialTypes"; @@ -114,6 +113,29 @@ export async function findOrCreateWhatsAppGroup(nomeGrupo?: string): Promise { + try { + const { error } = await supabase + .from('grupos_whatsapp') + .delete() + .eq('id', groupId); + + if (error) { + console.error('Erro ao deletar grupo:', error); + throw error; + } + + console.log(`Grupo ${groupId} deletado com sucesso`); + } catch (error) { + console.error('Erro ao deletar grupo do WhatsApp:', error); + throw error; + } +} + /** * Lists all WhatsApp groups for the current user * @returns Array of WhatsApp groups