- Added an input field in the GruposWhatsApp menu for users to name their WhatsApp groups, and store the name in the database. - Added delete and edit buttons to the transactions table, with database integration.
59 lines
2.3 KiB
TypeScript
59 lines
2.3 KiB
TypeScript
|
|
import { WhatsAppGroup } from "@/types/financialTypes";
|
|
import { createWorkflowInN8n } from "./n8nWorkflowService";
|
|
import { findOrCreateWhatsAppGroup, listWhatsAppGroups, updateWorkflowId } from "./whatsAppGroupsService";
|
|
|
|
/**
|
|
* Main function to register a WhatsApp group and create its workflow
|
|
* Orchestrates the process using the specialized services
|
|
* @param nomeGrupo Nome definido pelo usuário para o grupo
|
|
*/
|
|
export async function cadastrarGrupoWhatsApp(nomeGrupo?: string): Promise<WhatsAppGroup | null> {
|
|
try {
|
|
// Find or create a WhatsApp group for the current user
|
|
const group = await findOrCreateWhatsAppGroup(nomeGrupo);
|
|
|
|
// If no group was found or created, return null
|
|
if (!group) {
|
|
console.error('Erro inesperado: não foi possível obter ou criar um grupo');
|
|
return null;
|
|
}
|
|
|
|
// Create workflow in n8n if it doesn't exist
|
|
try {
|
|
if (!group.workflow_id) {
|
|
const userEmail = localStorage.getItem('userEmail')?.trim().toLowerCase();
|
|
|
|
if (!userEmail) {
|
|
throw new Error('Email do usuário não encontrado no localStorage');
|
|
}
|
|
|
|
console.log(`Iniciando criação de workflow para o email: ${userEmail} e grupo ID: ${group.id}`);
|
|
const workflowResponse = await createWorkflowInN8n(userEmail);
|
|
|
|
if (workflowResponse && workflowResponse.id) {
|
|
// Update the group with workflow_id
|
|
await updateWorkflowId(group.id, workflowResponse.id);
|
|
group.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:', workflowResponse);
|
|
}
|
|
}
|
|
} catch (n8nError) {
|
|
console.error('Erro ao criar workflow no n8n:', n8nError);
|
|
// Don't prevent group creation, just continue without workflow_id
|
|
}
|
|
|
|
// Always return the group, even if workflow creation fails
|
|
return group;
|
|
} catch (error) {
|
|
console.error('Erro ao cadastrar grupo do WhatsApp:', error);
|
|
throw error; // Propagate error to be handled in the component
|
|
}
|
|
}
|
|
|
|
// Re-export functions from the specialized services for backward compatibility
|
|
export { listWhatsAppGroups as listarGruposWhatsApp };
|
|
export { updateWorkflowId as atualizarWorkflowId };
|