Fix: Display WhatsApp groups and implement n8n workflow creation
- Fixed the display of WhatsApp group cards. - Implemented the creation of an n8n workflow upon user registration, including API action setup, dynamic data modification, and post-request actions to store workflow ID and webhook URL. - Instructions provided for the user to paste the JSON body.
This commit is contained in:
parent
7970b1b99f
commit
e70444f320
@ -6,6 +6,8 @@ import { validateRegisterForm } from '@/utils/registerValidation';
|
||||
import RegisterFormFields from './RegisterFormFields';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { toast } from "sonner";
|
||||
import { createN8nWorkflowForUser } from '@/services/n8nWorkflowCreationService';
|
||||
import { N8N_WORKFLOW_TEMPLATE } from '@/constants/n8nWorkflowTemplate';
|
||||
|
||||
interface RegisterFormProps {
|
||||
isLoading: boolean;
|
||||
@ -39,39 +41,61 @@ const RegisterForm = ({ isLoading, setIsLoading }: RegisterFormProps) => {
|
||||
|
||||
setIsLoading(true);
|
||||
|
||||
const { data, error } = await supabase.auth.signUp({
|
||||
email,
|
||||
password: senha,
|
||||
options: {
|
||||
emailRedirectTo: `${window.location.origin}/`,
|
||||
data: {
|
||||
nome,
|
||||
empresa: empresa || null,
|
||||
whatsapp: whatsapp.replace(/\D/g, ''),
|
||||
try {
|
||||
const { data, error } = await supabase.auth.signUp({
|
||||
email,
|
||||
password: senha,
|
||||
options: {
|
||||
emailRedirectTo: `${window.location.origin}/`,
|
||||
data: {
|
||||
nome,
|
||||
empresa: empresa || null,
|
||||
whatsapp: whatsapp.replace(/\D/g, ''),
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
setIsLoading(false);
|
||||
|
||||
if (error) {
|
||||
toast.error("Erro no cadastro", {
|
||||
description: error.message || "Não foi possível completar o cadastro. Por favor, tente novamente.",
|
||||
});
|
||||
} else if (data.user) {
|
||||
|
||||
if (error) {
|
||||
toast.error("Erro no cadastro", {
|
||||
description: error.message || "Não foi possível completar o cadastro. Por favor, tente novamente.",
|
||||
});
|
||||
} else if (data.user) {
|
||||
// This condition (empty identities) indicates that the user already exists in Supabase Auth.
|
||||
// In this case, `signUp` resends the confirmation/invitation email.
|
||||
if (data.user.identities && data.user.identities.length === 0) {
|
||||
toast.info("E-mail já cadastrado. Verifique sua caixa de entrada!", {
|
||||
description: "Enviamos um novo link para você definir sua senha e acessar sua conta. Não se esqueça de checar a pasta de spam.",
|
||||
duration: 10000,
|
||||
});
|
||||
toast.info("E-mail já cadastrado. Verifique sua caixa de entrada!", {
|
||||
description: "Enviamos um novo link para você definir sua senha e acessar sua conta. Não se esqueça de checar a pasta de spam.",
|
||||
duration: 10000,
|
||||
});
|
||||
} else {
|
||||
toast.success("Cadastro realizado com sucesso!", {
|
||||
description: "Enviamos um link de confirmação para o seu e-mail. Por favor, verifique sua caixa de entrada e spam para ativar sua conta.",
|
||||
duration: 10000,
|
||||
});
|
||||
toast.success("Cadastro realizado com sucesso!", {
|
||||
description: "Enviamos um link de confirmação para o seu e-mail. Por favor, verifique sua caixa de entrada e spam para ativar sua conta.",
|
||||
duration: 10000,
|
||||
});
|
||||
|
||||
// Create n8n workflow for the new user
|
||||
console.log('User registered successfully, creating n8n workflow...');
|
||||
|
||||
try {
|
||||
const workflowResult = await createN8nWorkflowForUser(email, N8N_WORKFLOW_TEMPLATE);
|
||||
if (workflowResult) {
|
||||
console.log('N8n workflow created successfully:', workflowResult);
|
||||
} else {
|
||||
console.error('Failed to create n8n workflow');
|
||||
}
|
||||
} catch (workflowError) {
|
||||
console.error('Error creating n8n workflow:', workflowError);
|
||||
// Don't show error to user as the main registration was successful
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error during registration:', error);
|
||||
toast.error("Erro no cadastro", {
|
||||
description: "Ocorreu um erro inesperado. Tente novamente.",
|
||||
});
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
28
src/constants/n8nWorkflowTemplate.ts
Normal file
28
src/constants/n8nWorkflowTemplate.ts
Normal file
@ -0,0 +1,28 @@
|
||||
|
||||
// N8N Workflow Template
|
||||
// TODO: Replace this with your actual workflow template JSON
|
||||
|
||||
export const N8N_WORKFLOW_TEMPLATE = {
|
||||
// COLE SEU JSON TEMPLATE AQUI
|
||||
// Substitua todo este objeto pelo JSON que funcionou no Postman
|
||||
// Exemplo:
|
||||
// {
|
||||
// "name": "Finance Workflow - rodrigobm10@gmail.com",
|
||||
// "nodes": [
|
||||
// {
|
||||
// "parameters": {
|
||||
// "path": "rodrigobm10",
|
||||
// ...
|
||||
// },
|
||||
// ...
|
||||
// }
|
||||
// ],
|
||||
// "connections": {},
|
||||
// "settings": {}
|
||||
// }
|
||||
|
||||
name: "Finance Workflow Template",
|
||||
nodes: [],
|
||||
connections: {},
|
||||
settings: {}
|
||||
};
|
||||
@ -16,20 +16,11 @@ export async function listarGruposWhatsApp(): Promise<WhatsAppGroup[]> {
|
||||
|
||||
console.log('Buscando grupos para o usuário:', userEmail);
|
||||
|
||||
// Primeiro verificar se o usuário tem instância WhatsApp
|
||||
const instanceData = await getUserWhatsAppInstance(userEmail);
|
||||
console.log('Dados da instância do usuário:', instanceData);
|
||||
|
||||
if (!instanceData || !instanceData.instancia_zap) {
|
||||
console.log('Usuário não tem instância WhatsApp válida ou dados estão incompletos');
|
||||
return [];
|
||||
}
|
||||
|
||||
// Buscar grupos usando o email como user_id
|
||||
// Buscar grupos usando o email como login (não user_id)
|
||||
const { data: grupos, error } = await supabase
|
||||
.from('grupos_whatsapp')
|
||||
.select('*')
|
||||
.eq('user_id', userEmail.trim().toLowerCase())
|
||||
.eq('login', userEmail.trim().toLowerCase())
|
||||
.order('created_at', { ascending: false });
|
||||
|
||||
if (error) {
|
||||
|
||||
124
src/services/n8nWorkflowCreationService.ts
Normal file
124
src/services/n8nWorkflowCreationService.ts
Normal file
@ -0,0 +1,124 @@
|
||||
|
||||
// Service dedicated to creating n8n workflows for new users
|
||||
import { supabase } from "@/integrations/supabase/client";
|
||||
|
||||
interface N8nWorkflowPayload {
|
||||
name: string;
|
||||
nodes: any[];
|
||||
connections: any;
|
||||
settings: any;
|
||||
}
|
||||
|
||||
interface N8nWorkflowResponse {
|
||||
id: string;
|
||||
name: string;
|
||||
nodes: Array<{
|
||||
webhookUrls?: string[];
|
||||
}>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a workflow in n8n for a new user
|
||||
* @param userEmail The email of the user who just registered
|
||||
* @param workflowTemplate The JSON template for the workflow
|
||||
* @returns The created workflow data
|
||||
*/
|
||||
export async function createN8nWorkflowForUser(
|
||||
userEmail: string,
|
||||
workflowTemplate: N8nWorkflowPayload
|
||||
): Promise<{ workflowId: string; webhookUrl: string } | null> {
|
||||
try {
|
||||
console.log(`Creating n8n workflow for user: ${userEmail}`);
|
||||
|
||||
// Extract the username from email (part before @)
|
||||
const username = userEmail.split('@')[0];
|
||||
|
||||
// Clone the template to avoid modifying the original
|
||||
const modifiedTemplate = JSON.parse(JSON.stringify(workflowTemplate));
|
||||
|
||||
// 1. Modify the workflow name to include user email
|
||||
modifiedTemplate.name = modifiedTemplate.name.replace('rodrigobm10@gmail.com', userEmail);
|
||||
|
||||
// 2. Modify webhook path in the first node to use username
|
||||
if (modifiedTemplate.nodes && modifiedTemplate.nodes.length > 0) {
|
||||
const firstNode = modifiedTemplate.nodes[0];
|
||||
if (firstNode.parameters && firstNode.parameters.path) {
|
||||
firstNode.parameters.path = username;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Replace all occurrences of rodrigobm10@gmail.com with user email
|
||||
const templateString = JSON.stringify(modifiedTemplate);
|
||||
const updatedTemplateString = templateString.replace(/rodrigobm10@gmail\.com/g, userEmail);
|
||||
const finalTemplate = JSON.parse(updatedTemplateString);
|
||||
|
||||
console.log('Modified template for user:', finalTemplate.name);
|
||||
|
||||
// Make the API request to n8n
|
||||
const response = await fetch('https://n8n.innova1001.com.br/api/v1/workflows', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-N8N-API-KEY': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2YmM4MjQxOS0zZTk1LTRiYmMtODMwMy0xODAzZjk4YmQ4YjciLCJpc3MiOiJuOG4iLCJhdWQiOiJwdWJsaWMtYXBpIiwiaWF0IjoxNzUwMTA5ODU3fQ.cvqDVnD6ide9WCbtCx7bVDEvkPzJyO4EhGSDhY0xIjE'
|
||||
},
|
||||
body: JSON.stringify(finalTemplate)
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
console.error(`Error creating n8n workflow: ${response.status} - ${errorText}`);
|
||||
throw new Error(`Failed to create workflow: ${response.status}`);
|
||||
}
|
||||
|
||||
const workflowData: N8nWorkflowResponse = await response.json();
|
||||
console.log('Workflow created successfully:', workflowData);
|
||||
|
||||
// Extract workflow ID and webhook URL
|
||||
const workflowId = workflowData.id;
|
||||
const webhookUrl = workflowData.nodes[0]?.webhookUrls?.[0] || `https://n8n.innova1001.com.br/webhook/${username}`;
|
||||
|
||||
// Save the workflow info to the user's profile
|
||||
await saveWorkflowInfoToUser(userEmail, workflowId, webhookUrl);
|
||||
|
||||
return {
|
||||
workflowId,
|
||||
webhookUrl
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error creating n8n workflow:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves workflow information to the user's profile
|
||||
* @param userEmail User's email
|
||||
* @param workflowId The n8n workflow ID
|
||||
* @param webhookUrl The webhook URL
|
||||
*/
|
||||
async function saveWorkflowInfoToUser(
|
||||
userEmail: string,
|
||||
workflowId: string,
|
||||
webhookUrl: string
|
||||
): Promise<void> {
|
||||
try {
|
||||
// Update the user's profile with workflow information
|
||||
const { error } = await supabase
|
||||
.from('usuarios')
|
||||
.update({
|
||||
webhook: webhookUrl
|
||||
})
|
||||
.eq('email', userEmail.trim().toLowerCase());
|
||||
|
||||
if (error) {
|
||||
console.error('Error saving workflow info to user:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log(`Workflow info saved for user ${userEmail}: ID=${workflowId}, URL=${webhookUrl}`);
|
||||
} catch (error) {
|
||||
console.error('Error updating user with workflow info:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
|
||||
// Service dedicated to WhatsApp groups database operations
|
||||
import { supabase } from "@/integrations/supabase/client";
|
||||
import { WhatsAppGroup } from "@/types/financialTypes";
|
||||
@ -84,7 +85,7 @@ export async function findOrCreateWhatsAppGroup(nomeGrupo?: string): Promise<Wha
|
||||
const { data, error } = await supabase
|
||||
.from('grupos_whatsapp')
|
||||
.insert({
|
||||
user_id: localStorage.getItem('userId') || '',
|
||||
user_id: normalizedEmail, // Usando email também aqui para consistência
|
||||
remote_jid: '',
|
||||
login: normalizedEmail,
|
||||
status: 'pendente',
|
||||
|
||||
Loading…
Reference in New Issue
Block a user