Fix: Inconsistent WhatsApp instance detection
Unifies instance verification logic using `useExistingInstanceCheck` across components, corrects the Supabase query, and adds re-verification after instance creation to resolve inconsistencies in WhatsApp instance detection and group creation.
This commit is contained in:
parent
7fbce3ca0b
commit
a9769b76db
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
import { useWhatsAppInstanceVerification } from '@/hooks/whatsappGroups/useWhatsAppInstanceVerification';
|
import { useExistingInstanceCheck } from '@/hooks/whatsapp/useExistingInstanceCheck'; // <--- Hook UNIFICADO!
|
||||||
import { useGroupCreation } from '@/hooks/whatsappGroups/useGroupCreation';
|
import { useGroupCreation } from '@/hooks/whatsappGroups/useGroupCreation';
|
||||||
import LoadingState from './LoadingState';
|
import LoadingState from './LoadingState';
|
||||||
import NoInstanceState from './NoInstanceState';
|
import NoInstanceState from './NoInstanceState';
|
||||||
@ -12,53 +12,36 @@ interface CreateGroupFormProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const CreateGroupForm = ({ userEmail, onSuccess }: CreateGroupFormProps) => {
|
const CreateGroupForm = ({ userEmail, onSuccess }: CreateGroupFormProps) => {
|
||||||
|
// Use o hook UNIFICADO E CORRETO
|
||||||
const {
|
const {
|
||||||
hasWhatsAppInstance,
|
hasExistingInstance,
|
||||||
checkingInstance,
|
checkingExistingInstance,
|
||||||
userInstance,
|
existingInstanceData,
|
||||||
recheckInstance
|
recheckInstance
|
||||||
} = useWhatsAppInstanceVerification(userEmail);
|
} = useExistingInstanceCheck(userEmail);
|
||||||
|
|
||||||
const { cadastrando, handleCadastrarGrupo } = useGroupCreation(userEmail, onSuccess);
|
const { cadastrando, handleCadastrarGrupo } = useGroupCreation(userEmail, onSuccess);
|
||||||
|
|
||||||
// Re-verificar a instância quando o componente for montado ou quando houver mudanças
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log('🔄 [GRUPO] CreateGroupForm montado, verificando instância');
|
|
||||||
recheckInstance();
|
recheckInstance();
|
||||||
}, [userEmail]);
|
}, [userEmail]); // Re-verifica quando o email muda
|
||||||
|
|
||||||
// Log detalhado do estado atual
|
if (checkingExistingInstance) {
|
||||||
useEffect(() => {
|
|
||||||
console.log('📊 [GRUPO] Estado atual do CreateGroupForm:', {
|
|
||||||
userEmail,
|
|
||||||
hasWhatsAppInstance,
|
|
||||||
checkingInstance,
|
|
||||||
userInstance,
|
|
||||||
instanceStatus: userInstance?.status_instancia,
|
|
||||||
instanceName: userInstance?.instancia_zap
|
|
||||||
});
|
|
||||||
}, [userEmail, hasWhatsAppInstance, checkingInstance, userInstance]);
|
|
||||||
|
|
||||||
if (checkingInstance) {
|
|
||||||
console.log('⏳ [GRUPO] Verificando instância WhatsApp...');
|
|
||||||
return <LoadingState message="Verificando instância WhatsApp..." />;
|
return <LoadingState message="Verificando instância WhatsApp..." />;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hasWhatsAppInstance) {
|
// A lógica agora é a mesma da outra página: `hasExistingInstance`
|
||||||
console.log('❌ [GRUPO] Instância WhatsApp não encontrada ou não conectada');
|
if (!hasExistingInstance) {
|
||||||
return <NoInstanceState userInstance={userInstance} />;
|
return <NoInstanceState userInstance={existingInstanceData} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('✅ [GRUPO] Instância válida encontrada, mostrando formulário de criação');
|
|
||||||
|
|
||||||
const handleSubmit = (nomeGrupo: string) => {
|
const handleSubmit = (nomeGrupo: string) => {
|
||||||
console.log('🚀 [GRUPO] Iniciando criação de grupo:', nomeGrupo);
|
handleCadastrarGrupo(nomeGrupo, existingInstanceData);
|
||||||
handleCadastrarGrupo(nomeGrupo, userInstance);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<GroupCreationForm
|
<GroupCreationForm
|
||||||
userInstance={userInstance!}
|
userInstance={existingInstanceData!}
|
||||||
userEmail={userEmail}
|
userEmail={userEmail}
|
||||||
cadastrando={cadastrando}
|
cadastrando={cadastrando}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect, useCallback } from 'react';
|
||||||
import { getUserWhatsAppInstance } from '@/services/whatsAppInstanceService';
|
import { getUserWhatsAppInstance } from '@/services/whatsAppInstanceService';
|
||||||
|
|
||||||
export const useExistingInstanceCheck = (userEmail: string) => {
|
export const useExistingInstanceCheck = (userEmail: string) => {
|
||||||
@ -7,9 +7,8 @@ export const useExistingInstanceCheck = (userEmail: string) => {
|
|||||||
const [checkingExistingInstance, setCheckingExistingInstance] = useState(true);
|
const [checkingExistingInstance, setCheckingExistingInstance] = useState(true);
|
||||||
const [existingInstanceData, setExistingInstanceData] = useState<any>(null);
|
const [existingInstanceData, setExistingInstanceData] = useState<any>(null);
|
||||||
|
|
||||||
const checkExistingInstance = async () => {
|
const checkExistingInstance = useCallback(async () => {
|
||||||
if (!userEmail) {
|
if (!userEmail) {
|
||||||
console.log('❌ [EXISTING_INSTANCE] Email do usuário não encontrado');
|
|
||||||
setCheckingExistingInstance(false);
|
setCheckingExistingInstance(false);
|
||||||
setHasExistingInstance(false);
|
setHasExistingInstance(false);
|
||||||
setExistingInstanceData(null);
|
setExistingInstanceData(null);
|
||||||
@ -17,53 +16,37 @@ export const useExistingInstanceCheck = (userEmail: string) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setCheckingExistingInstance(true);
|
setCheckingExistingInstance(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log('🔍 [EXISTING_INSTANCE] Verificando instância para:', userEmail);
|
console.log('🔍 [HOOK] Verificando instância para:', userEmail);
|
||||||
const existingInstance = await getUserWhatsAppInstance(userEmail);
|
const data = await getUserWhatsAppInstance(userEmail);
|
||||||
|
|
||||||
console.log('📋 [EXISTING_INSTANCE] Dados retornados:', existingInstance);
|
const hasValidInstance = !!(data && data.instancia_zap && data.status_instancia === 'conectado');
|
||||||
|
|
||||||
// Verificação DIRETA: se existe instancia_zap E status é 'conectado'
|
console.log('✅ [HOOK] Resultado da verificação:', { hasValidInstance, data });
|
||||||
const hasValidInstance = !!(
|
|
||||||
existingInstance &&
|
|
||||||
existingInstance.instancia_zap &&
|
|
||||||
existingInstance.status_instancia === 'conectado'
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log('✅ [EXISTING_INSTANCE] Resultado:', {
|
|
||||||
hasValidInstance,
|
|
||||||
instancia_zap: existingInstance?.instancia_zap,
|
|
||||||
status_instancia: existingInstance?.status_instancia
|
|
||||||
});
|
|
||||||
|
|
||||||
setHasExistingInstance(hasValidInstance);
|
setHasExistingInstance(hasValidInstance);
|
||||||
setExistingInstanceData(hasValidInstance ? existingInstance : null);
|
setExistingInstanceData(data); // Armazena os dados, independentemente do status
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ [EXISTING_INSTANCE] Erro:', error);
|
console.error('❌ [HOOK] Erro ao verificar instância:', error);
|
||||||
setHasExistingInstance(false);
|
setHasExistingInstance(false);
|
||||||
setExistingInstanceData(null);
|
setExistingInstanceData(null);
|
||||||
} finally {
|
} finally {
|
||||||
setCheckingExistingInstance(false);
|
setCheckingExistingInstance(false);
|
||||||
}
|
}
|
||||||
};
|
}, [userEmail]); // useCallback para evitar re-criações desnecessárias
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
checkExistingInstance();
|
checkExistingInstance();
|
||||||
}, [userEmail]);
|
}, [checkExistingInstance]); // O useEffect agora depende da função memoizada
|
||||||
|
|
||||||
const recheckInstance = () => {
|
// Retornamos a função de verificação para que possa ser chamada manualmente
|
||||||
console.log('🔄 [EXISTING_INSTANCE] Re-verificação manual');
|
return {
|
||||||
checkExistingInstance();
|
hasExistingInstance,
|
||||||
};
|
checkingExistingInstance,
|
||||||
|
existingInstanceData,
|
||||||
return {
|
recheckInstance: checkExistingInstance,
|
||||||
hasExistingInstance,
|
|
||||||
checkingExistingInstance,
|
|
||||||
existingInstanceData,
|
|
||||||
setHasExistingInstance,
|
setHasExistingInstance,
|
||||||
setExistingInstanceData,
|
setExistingInstanceData
|
||||||
recheckInstance
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@ -50,7 +50,7 @@ const WhatsApp = () => {
|
|||||||
|
|
||||||
const userEmail = (localStorage.getItem('userEmail') || '').toLowerCase();
|
const userEmail = (localStorage.getItem('userEmail') || '').toLowerCase();
|
||||||
|
|
||||||
// Hook centralizado para verificação de instância existente
|
// Hook UNIFICADO para verificação de instância existente
|
||||||
const {
|
const {
|
||||||
hasExistingInstance,
|
hasExistingInstance,
|
||||||
checkingExistingInstance,
|
checkingExistingInstance,
|
||||||
@ -62,32 +62,22 @@ const WhatsApp = () => {
|
|||||||
|
|
||||||
usePeriodicStatusCheck(instances.length, checkAllInstancesStatus);
|
usePeriodicStatusCheck(instances.length, checkAllInstancesStatus);
|
||||||
|
|
||||||
const handleInstanceCreated = (newInstance: WhatsAppInstance) => {
|
const handleInstanceCreated = async (newInstance: WhatsAppInstance) => {
|
||||||
console.log('🎉 [WHATSAPP] Nova instância criada:', newInstance);
|
console.log('🎉 [WHATSAPP_PAGE] Nova instância criada, acionando re-verificação.');
|
||||||
addInstance(newInstance);
|
addInstance(newInstance);
|
||||||
setInstanceFound(true);
|
setInstanceFound(true);
|
||||||
|
|
||||||
// Atualizar estado do hook centralizado
|
|
||||||
setHasExistingInstance(true);
|
|
||||||
setExistingInstanceData({
|
|
||||||
instancia_zap: newInstance.instanceName,
|
|
||||||
status_instancia: 'conectado'
|
|
||||||
});
|
|
||||||
|
|
||||||
saveInstanceName(newInstance.instanceName);
|
saveInstanceName(newInstance.instanceName);
|
||||||
|
|
||||||
if (newInstance.qrcode) {
|
if (newInstance.qrcode) {
|
||||||
handleViewQrCode(newInstance);
|
handleViewQrCode(newInstance);
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(async () => {
|
// O mais importante: após criar, chame a função para re-verificar do banco de dados!
|
||||||
try {
|
// Isso garante que o estado seja baseado em dados reais, não em uma suposição.
|
||||||
await checkAllInstancesStatus();
|
setTimeout(() => {
|
||||||
recheckInstance();
|
recheckInstance();
|
||||||
} catch (error) {
|
checkAllInstancesStatus();
|
||||||
console.error("Error checking status after instance creation:", error);
|
}, 2000); // Um pequeno delay para dar tempo ao backend de atualizar
|
||||||
}
|
|
||||||
}, 2000);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDeleteInstanceWrapper = (instanceId: string) => {
|
const handleDeleteInstanceWrapper = (instanceId: string) => {
|
||||||
@ -128,17 +118,15 @@ const WhatsApp = () => {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex gap-2">
|
<Button
|
||||||
<Button
|
variant="outline"
|
||||||
variant="outline"
|
onClick={recheckInstance}
|
||||||
onClick={recheckInstance}
|
disabled={checkingExistingInstance}
|
||||||
disabled={checkingExistingInstance}
|
className="flex items-center gap-2"
|
||||||
className="flex items-center gap-2"
|
>
|
||||||
>
|
<RefreshCw className={`h-4 w-4 ${checkingExistingInstance ? 'animate-spin' : ''}`} />
|
||||||
<RefreshCw className={`h-4 w-4 ${checkingExistingInstance ? 'animate-spin' : ''}`} />
|
Atualizar
|
||||||
Atualizar
|
</Button>
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Formulário de criação - APENAS se NÃO tiver instância conectada */}
|
{/* Formulário de criação - APENAS se NÃO tiver instância conectada */}
|
||||||
|
|||||||
@ -1,13 +1,78 @@
|
|||||||
|
|
||||||
// Re-export all functions from the new modular structure
|
import { supabase } from '@/integrations/supabase/client';
|
||||||
export * from './whatsAppInstance';
|
|
||||||
|
|
||||||
// For backwards compatibility, also export individual functions
|
/**
|
||||||
export {
|
* Busca os dados da instância de um usuário na tabela 'usuarios'.
|
||||||
updateUserWhatsAppInstance,
|
* Retorna o primeiro registro encontrado ou null se não houver.
|
||||||
getUserWhatsAppInstance,
|
*
|
||||||
removeUserWhatsAppInstance,
|
* @param userEmail O e-mail do usuário para buscar.
|
||||||
getUserDebugInfo,
|
* @returns Um objeto com os dados da instância ou null.
|
||||||
activateUserWorkflow,
|
*/
|
||||||
checkUserHasInstance
|
export const getUserWhatsAppInstance = async (userEmail: string) => {
|
||||||
} from './whatsAppInstance';
|
if (!userEmail) {
|
||||||
|
console.error('[SERVICE_ERROR] getUserWhatsAppInstance chamado sem userEmail.');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[SERVICE] Buscando instância no DB para: ${userEmail}`);
|
||||||
|
|
||||||
|
// A query correta para buscar os dados
|
||||||
|
const { data, error } = await supabase
|
||||||
|
.from('usuarios') // Nome da sua tabela
|
||||||
|
.select('instancia_zap, status_instancia, whatsapp') // Seleciona apenas as colunas que precisamos
|
||||||
|
.eq('email', userEmail) // Filtra pelo e-mail do usuário logado
|
||||||
|
.maybeSingle(); // Retorna um único objeto (ou null) em vez de um array
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
console.error('[SERVICE_ERROR] Erro ao buscar instância no Supabase:', error);
|
||||||
|
throw new Error(error.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('[SERVICE] Dados retornados do DB:', data);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Re-export das outras funções para manter compatibilidade
|
||||||
|
export const updateUserWhatsAppInstance = async (userEmail: string, instanceData: any) => {
|
||||||
|
const { error } = await supabase
|
||||||
|
.from('usuarios')
|
||||||
|
.update({
|
||||||
|
instancia_zap: instanceData.instanceName,
|
||||||
|
status_instancia: instanceData.status
|
||||||
|
})
|
||||||
|
.eq('email', userEmail);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
console.error('[SERVICE_ERROR] Erro ao atualizar instância:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const removeUserWhatsAppInstance = async (userEmail: string) => {
|
||||||
|
const { error } = await supabase
|
||||||
|
.from('usuarios')
|
||||||
|
.update({
|
||||||
|
instancia_zap: null,
|
||||||
|
status_instancia: 'desconectado'
|
||||||
|
})
|
||||||
|
.eq('email', userEmail);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
console.error('[SERVICE_ERROR] Erro ao remover instância:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getUserDebugInfo = async (userEmail: string) => {
|
||||||
|
return await getUserWhatsAppInstance(userEmail);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const activateUserWorkflow = async (userEmail: string) => {
|
||||||
|
console.log('[SERVICE] Ativando workflow para:', userEmail);
|
||||||
|
// Implementação do workflow se necessário
|
||||||
|
};
|
||||||
|
|
||||||
|
export const checkUserHasInstance = async (userEmail: string) => {
|
||||||
|
const data = await getUserWhatsAppInstance(userEmail);
|
||||||
|
return !!(data && data.instancia_zap && data.status_instancia === 'conectado');
|
||||||
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user