diff --git a/src/components/whatsapp/CreateInstanceForm.tsx b/src/components/whatsapp/CreateInstanceForm.tsx
index 4bd7d44..d37b7d0 100644
--- a/src/components/whatsapp/CreateInstanceForm.tsx
+++ b/src/components/whatsapp/CreateInstanceForm.tsx
@@ -7,7 +7,7 @@ import { Label } from '@/components/ui/label';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { Loader2, Plus, AlertCircle } from 'lucide-react';
import { useToast } from '@/hooks/use-toast';
-import { createInstance } from '@/services/whatsAppService';
+import { restartInstance } from '@/services/whatsAppService';
import { WhatsAppInstance } from '@/types/whatsAppTypes';
import { updateUserWhatsAppInstance } from '@/services/whatsAppInstanceService';
import { createEvolutionWebhook } from '@/services/whatsApp/webhookService';
@@ -23,7 +23,6 @@ const CreateInstanceForm = ({ onInstanceCreated, initialInstanceName = '' }: Cre
const [userEmail, setUserEmail] = useState(initialInstanceName);
const [ddd, setDdd] = useState('');
const [phoneNumber, setPhoneNumber] = useState('');
- const [showAdvanced, setShowAdvanced] = useState(false);
const handleCreateInstance = async (e: React.FormEvent) => {
e.preventDefault();
@@ -67,7 +66,7 @@ const CreateInstanceForm = ({ onInstanceCreated, initialInstanceName = '' }: Cre
console.log(`🚀 Criando instância para: ${normalizedEmail} com número: ${fullPhoneNumber}`);
// Criar a instância no Evolution API
- const response = await createInstance(normalizedEmail, fullPhoneNumber);
+ const response = await restartInstance(normalizedEmail, fullPhoneNumber);
if (response && response.instance) {
const instanceData: WhatsAppInstance = {
@@ -77,7 +76,6 @@ const CreateInstanceForm = ({ onInstanceCreated, initialInstanceName = '' }: Cre
connectionState: response.instance.state || 'closed',
qrcode: response.qrcode || null,
status: response.instance.state === 'open' ? 'connected' : 'disconnected',
- createdAt: new Date().toISOString(),
lastSeen: new Date().toISOString(),
presence: 'offline'
};
@@ -210,15 +208,13 @@ const CreateInstanceForm = ({ onInstanceCreated, initialInstanceName = '' }: Cre
)}
- {showAdvanced && (
-
-
-
- Após criar a instância, você precisará escanear o QR Code com o WhatsApp
- do seu celular para estabelecer a conexão.
-
-
- )}
+
+
+
+ Após criar a instância, você precisará escanear o QR Code com o WhatsApp
+ do seu celular para estabelecer a conexão.
+
+
diff --git a/src/services/whatsApp/webhookService.ts b/src/services/whatsApp/webhookService.ts
index b06bf3b..788bf6f 100644
--- a/src/services/whatsApp/webhookService.ts
+++ b/src/services/whatsApp/webhookService.ts
@@ -39,7 +39,7 @@ export const createEvolutionWebhook = async (userEmail: string): Promise =>
return response;
} catch (error) {
- console.error(`❌ Erro ao criar webhook for ${normalizedEmail}:`, error);
+ console.error(`❌ Erro ao criar webhook for ${userEmail}:`, error);
throw error;
}
};
diff --git a/src/services/whatsAppInstance/index.ts b/src/services/whatsAppInstance/index.ts
index 637bbc1..dfea93b 100644
--- a/src/services/whatsAppInstance/index.ts
+++ b/src/services/whatsAppInstance/index.ts
@@ -1,6 +1,30 @@
-// Re-export all WhatsApp instance service functions
+// Re-export all functions from the new modular structure
export * from './databaseOperations';
export * from './workflowOperations';
-export * from './userOperations';
export * from './config';
+
+// For backwards compatibility, also export individual functions specifically
+export {
+ updateUserWhatsAppInstance,
+ getUserWhatsAppInstance,
+ removeUserWhatsAppInstance,
+ getUserDebugInfo
+} from './databaseOperations';
+
+export {
+ activateUserWorkflow
+} from './workflowOperations';
+
+// Export a helper function to check if user has instance
+export async function checkUserHasInstance(userEmail: string): Promise {
+ try {
+ const { getUserWhatsAppInstance } = await import('./databaseOperations');
+ const instanceData = await getUserWhatsAppInstance(userEmail.trim().toLowerCase());
+
+ return !!(instanceData && instanceData.instancia_zap && instanceData.instancia_zap.trim() !== '');
+ } catch (error) {
+ console.error('Erro ao verificar se usuário tem instância:', error);
+ return false;
+ }
+}
diff --git a/src/services/whatsAppInstance/userOperations.ts b/src/services/whatsAppInstance/userOperations.ts
index a36dddf..6620f29 100644
--- a/src/services/whatsAppInstance/userOperations.ts
+++ b/src/services/whatsAppInstance/userOperations.ts
@@ -34,8 +34,7 @@ export async function updateUserWhatsAppInstance(
.from('usuarios')
.update({
instancia_zap: normalizedInstanceName,
- status_instancia: status,
- updated_at: new Date().toISOString()
+ status_instancia: status
})
.eq('email', normalizedEmail);
@@ -52,9 +51,7 @@ export async function updateUserWhatsAppInstance(
.insert({
email: normalizedEmail,
instancia_zap: normalizedInstanceName,
- status_instancia: status,
- created_at: new Date().toISOString(),
- updated_at: new Date().toISOString()
+ status_instancia: status
});
if (insertError) {
@@ -122,8 +119,7 @@ export async function removeUserWhatsAppInstance(userEmail: string): Promise