Merge commit '71f9c9ab860f7c5597427fb5fc82492a925aa9cf'

This commit is contained in:
2026-01-19 14:27:07 -03:00
6 changed files with 68 additions and 311 deletions
@@ -48,7 +48,7 @@ import {
PaginationPrevious, PaginationPrevious,
} from "@/components/ui/pagination"; } from "@/components/ui/pagination";
import { Badge } from "@/components/ui/badge"; import { Badge } from "@/components/ui/badge";
import { userProfileService, FinancialIndicators, ExpenseItem, ExpensesResponse, ExpensesFilters, ExpenseCategory } from "@/services/userProfile"; import { personalAgent, FinancialIndicators, ExpenseItem, ExpensesResponse, ExpensesFilters, ExpenseCategory } from "@/services/personalAgent";
import { GlobalFunctions, TransferAreaProperties } from "@/GlobalFunctions"; import { GlobalFunctions, TransferAreaProperties } from "@/GlobalFunctions";
import { toast } from "@/hooks/use-toast"; import { toast } from "@/hooks/use-toast";
@@ -175,7 +175,7 @@ export default function Financas() {
} }
// Busca os indicadores financeiros da API // Busca os indicadores financeiros da API
const response = await userProfileService.getFinancialIndicators(userEmail); const response = await personalAgent.getFinancialIndicators(userEmail);
if (response.success) { if (response.success) {
setIndicators(response); setIndicators(response);
@@ -265,7 +265,7 @@ export default function Financas() {
filters.data_final = format(endDate, "yyyy-MM-dd"); filters.data_final = format(endDate, "yyyy-MM-dd");
} }
const response = await userProfileService.getExpenses(emailToUse, filters); const response = await personalAgent.getExpenses(emailToUse, filters);
if (response.success) { if (response.success) {
// Garante que a resposta tenha estrutura válida mesmo quando não há dados // Garante que a resposta tenha estrutura válida mesmo quando não há dados
@@ -308,8 +308,7 @@ export default function Financas() {
// Carrega categorias // Carrega categorias
try { try {
setLoadingCategories(true); setLoadingCategories(true);
const categoriesData = await userProfileService.getCategories(); const categoriesData = await personalAgent.getCategories();
console.log("Categorias carregadas:", categoriesData);
setCategories(categoriesData); setCategories(categoriesData);
} catch (error) { } catch (error) {
console.error("Erro ao carregar categorias:", error); console.error("Erro ao carregar categorias:", error);
@@ -4,7 +4,7 @@ import { GoogleCalendarCard } from "@/modules/intelligence-ia/components/integra
import { GoogleSheetsCard } from "@/modules/intelligence-ia/components/integrations/GoogleSheetsCard"; import { GoogleSheetsCard } from "@/modules/intelligence-ia/components/integrations/GoogleSheetsCard";
import { AsanaCard } from "@/modules/intelligence-ia/components/integrations/AsanaCard"; import { AsanaCard } from "@/modules/intelligence-ia/components/integrations/AsanaCard";
import { asanaService } from "@/services/asana"; import { asanaService } from "@/services/asana";
import { userProfileService } from "@/services/userProfile"; import { personalAgent } from "@/services/personalAgent";
import { GlobalFunctions, TransferAreaProperties } from "@/GlobalFunctions"; import { GlobalFunctions, TransferAreaProperties } from "@/GlobalFunctions";
interface Workspace { interface Workspace {
@@ -199,16 +199,14 @@ export default function Integrations() {
} }
if (!userEmail) { if (!userEmail) {
console.log("Integrations: Não foi possível obter email do usuário");
setLoadingIntegration(false); setLoadingIntegration(false);
return; return;
} }
// Busca o perfil do usuário para obter o ID // Busca o perfil do usuário para obter o ID
const userProfile = await userProfileService.getUserProfile(userEmail); const userProfile = await personalAgent.getUserProfile(userEmail);
if (!userProfile.success || !userProfile.id) { if (!userProfile.success || !userProfile.id) {
console.log("Integrations: Usuário não encontrado ou sem ID");
setLoadingIntegration(false); setLoadingIntegration(false);
return; return;
} }
@@ -14,7 +14,7 @@ import {
} from "@/components/ui/dialog"; } from "@/components/ui/dialog";
import { User, Mail, Phone, Save, Bell, Loader2, Plus } from "lucide-react"; import { User, Mail, Phone, Save, Bell, Loader2, Plus } from "lucide-react";
import { toast } from "@/hooks/use-toast"; import { toast } from "@/hooks/use-toast";
import { userProfileService, UserProfile } from "@/services/userProfile"; import { personalAgent, UserProfile } from "@/services/personalAgent";
import { GlobalFunctions, TransferAreaProperties } from "@/GlobalFunctions"; import { GlobalFunctions, TransferAreaProperties } from "@/GlobalFunctions";
const formatPhoneNumber = (value: string) => { const formatPhoneNumber = (value: string) => {
@@ -54,20 +54,15 @@ const MeuPerfil = () => {
// Verifica se há token no sessionStorage primeiro // Verifica se há token no sessionStorage primeiro
const jsonUsuario = sessionStorage.getItem('usuarioLogado'); const jsonUsuario = sessionStorage.getItem('usuarioLogado');
if (!jsonUsuario) { if (!jsonUsuario) {
console.log("MeuPerfil: Não há token no sessionStorage");
window.location.replace(import.meta.env.VITE_BASE_URL_HGTX_CORE || 'https://core.hgtx.com.br'); window.location.replace(import.meta.env.VITE_BASE_URL_HGTX_CORE || 'https://core.hgtx.com.br');
return; return;
} }
console.log("MeuPerfil: Token encontrado no sessionStorage");
// Tenta obter o email do usuário logado // Tenta obter o email do usuário logado
let userEmail = GlobalFunctions.getUsuarioLogado().email; let userEmail = GlobalFunctions.getUsuarioLogado().email;
console.log("MeuPerfil: Email obtido:", userEmail);
// Se não tem email, tenta fazer refresh do token // Se não tem email, tenta fazer refresh do token
if (!userEmail) { if (!userEmail) {
console.log("MeuPerfil: Email vazio, tentando fazer refresh do token");
try { try {
await GlobalFunctions.getToken(); await GlobalFunctions.getToken();
// Aguarda um pouco para o refresh ser processado (se necessário) // Aguarda um pouco para o refresh ser processado (se necessário)
@@ -75,7 +70,6 @@ const MeuPerfil = () => {
// Verifica novamente após refresh // Verifica novamente após refresh
userEmail = GlobalFunctions.getUsuarioLogado().email; userEmail = GlobalFunctions.getUsuarioLogado().email;
console.log("MeuPerfil: Email após refresh:", userEmail);
} catch (error) { } catch (error) {
console.error("MeuPerfil: Erro ao obter token:", error); console.error("MeuPerfil: Erro ao obter token:", error);
} }
@@ -85,24 +79,17 @@ const MeuPerfil = () => {
// Se o token existe mas não tem email, pode ser problema na decodificação // Se o token existe mas não tem email, pode ser problema na decodificação
if (!userEmail) { if (!userEmail) {
const usuarioData = GlobalFunctions.getUsuarioLogado(); const usuarioData = GlobalFunctions.getUsuarioLogado();
console.log("MeuPerfil: Dados do usuário após tentativas:", usuarioData);
// Se não tem email mas tem token no sessionStorage, tenta carregar mesmo assim // Se não tem email mas tem token no sessionStorage, tenta carregar mesmo assim
// O loadUserProfile vai tratar o erro adequadamente se o email for necessário // O loadUserProfile vai tratar o erro adequadamente se o email for necessário
// Só redireciona se o token estiver completamente inválido (sem UID e sem EID) // Só redireciona se o token estiver completamente inválido (sem UID e sem EID)
if (!usuarioData.email && usuarioData.UID === "0" && usuarioData.EID === "0") { if (!usuarioData.email && usuarioData.UID === "0" && usuarioData.EID === "0") {
console.log("MeuPerfil: Token completamente inválido (sem UID/EID), redirecionando");
window.location.replace(import.meta.env.VITE_BASE_URL_HGTX_CORE || 'https://core.hgtx.com.br'); window.location.replace(import.meta.env.VITE_BASE_URL_HGTX_CORE || 'https://core.hgtx.com.br');
return; return;
} }
// Se tem token mas não tem email, pode ser que o email esteja em outro campo
// ou o token precisa ser renovado. Tenta carregar o perfil mesmo assim.
console.log("MeuPerfil: Token existe mas email vazio, tentando carregar perfil mesmo assim");
} }
// Se passou na verificação (ou tem token válido), carrega o perfil // Se passou na verificação (ou tem token válido), carrega o perfil
console.log("MeuPerfil: Carregando perfil do usuário");
loadUserProfile(); loadUserProfile();
}; };
@@ -116,13 +103,11 @@ const MeuPerfil = () => {
// Se não tem email, tenta obter do Transfer Area como fallback // Se não tem email, tenta obter do Transfer Area como fallback
if (!userEmail) { if (!userEmail) {
console.log("MeuPerfil: Email não encontrado no token, tentando Transfer Area");
const transferEmail = GlobalFunctions.getTransferProperty( const transferEmail = GlobalFunctions.getTransferProperty(
TransferAreaProperties.UsuarioEmail TransferAreaProperties.UsuarioEmail
); );
if (transferEmail) { if (transferEmail) {
userEmail = transferEmail as string; userEmail = transferEmail as string;
console.log("MeuPerfil: Email do Transfer Area:", userEmail);
} }
} }
@@ -132,7 +117,6 @@ const MeuPerfil = () => {
await GlobalFunctions.getToken(); await GlobalFunctions.getToken();
await new Promise(resolve => setTimeout(resolve, 500)); await new Promise(resolve => setTimeout(resolve, 500));
userEmail = GlobalFunctions.getUsuarioLogado().email; userEmail = GlobalFunctions.getUsuarioLogado().email;
console.log("MeuPerfil: Email após refresh no loadUserProfile:", userEmail);
} catch (error) { } catch (error) {
console.error("MeuPerfil: Erro ao fazer refresh no loadUserProfile:", error); console.error("MeuPerfil: Erro ao fazer refresh no loadUserProfile:", error);
} }
@@ -151,7 +135,7 @@ const MeuPerfil = () => {
} }
setEmail(userEmail); setEmail(userEmail);
const response = await userProfileService.getUserProfile(userEmail); const response = await personalAgent.getUserProfile(userEmail);
if (response.success && response.id) { if (response.success && response.id) {
// Usuário existe // Usuário existe
@@ -203,7 +187,7 @@ const MeuPerfil = () => {
try { try {
setSaving(true); setSaving(true);
const response = await userProfileService.updateUser(userId, { const response = await personalAgent.updateUser(userId, {
nome: nomeCompleto, nome: nomeCompleto,
whatsapp: whatsapp, whatsapp: whatsapp,
followup: receberLembretes, followup: receberLembretes,
@@ -255,7 +239,7 @@ const MeuPerfil = () => {
try { try {
setCreating(true); setCreating(true);
const response = await userProfileService.createUser({ const response = await personalAgent.createUser({
nome: createNome.trim(), nome: createNome.trim(),
email: email, email: email,
whatsapp: createWhatsapp, whatsapp: createWhatsapp,
+9 -100
View File
@@ -1,58 +1,32 @@
/**
* Serviço para integração com API do Asana
*/
/**
* Interface para workspace do Asana (resposta da API)
*/
export interface AsanaWorkspaceResponse { export interface AsanaWorkspaceResponse {
gid: string; gid: string;
resource_type: string; resource_type: string;
name: string; name: string;
} }
/**
* Interface para resposta da API de workspaces
*/
export interface AsanaWorkspacesResponse { export interface AsanaWorkspacesResponse {
data: AsanaWorkspaceResponse[]; data: AsanaWorkspaceResponse[];
} }
/**
* Interface para workspace formatado (usado no componente)
*/
export interface AsanaWorkspace { export interface AsanaWorkspace {
id: string; id: string;
name: string; name: string;
} }
/**
* Interface para usuário do Asana (resposta da API)
*/
export interface AsanaUserResponse { export interface AsanaUserResponse {
gid: string; gid: string;
name: string; name: string;
resource_type: string; resource_type: string;
} }
/**
* Interface para resposta da API de usuários
*/
export interface AsanaUsersResponse { export interface AsanaUsersResponse {
data: AsanaUserResponse[]; data: AsanaUserResponse[];
} }
/**
* Interface para usuário formatado (usado no componente)
*/
export interface AsanaUser { export interface AsanaUser {
id: string; id: string;
name: string; name: string;
} }
/**
* Interface para resposta da API de integração do Asana
*/
export interface AsanaIntegrationResponse { export interface AsanaIntegrationResponse {
success: boolean; success: boolean;
integracao_id?: string; integracao_id?: string;
@@ -64,9 +38,6 @@ export interface AsanaIntegrationResponse {
usuario_asana_nome?: string; usuario_asana_nome?: string;
} }
/**
* Interface para request de criação/atualização de integração
*/
export interface AsanaIntegrationRequest { export interface AsanaIntegrationRequest {
user_id?: string; user_id?: string;
api_key: string; api_key: string;
@@ -76,20 +47,9 @@ export interface AsanaIntegrationRequest {
usuario_asana_nome: string; usuario_asana_nome: string;
} }
/**
* Serviço para integração com Asana
*/
class AsanaService { class AsanaService {
private readonly BASE_URL = 'https://app.asana.com/api/1.0'; private readonly ASANA_BASE_URL = 'https://app.asana.com/api/1.0';
private readonly INTEGRATION_BASE_URL = 'https://prod-hgtx-intelligence-n8n.hgtx.com.br';
private readonly INTEGRATION_WEBHOOK_ID = 'c898beff-84cb-44df-a69c-6eff27ccd7aa';
/**
* Lista os workspaces do Asana usando o token fornecido
*
* @param token - Token de acesso do Asana
* @returns Promise com a lista de workspaces formatados
*/
async getWorkspaces(token: string): Promise<AsanaWorkspace[]> { async getWorkspaces(token: string): Promise<AsanaWorkspace[]> {
if (!token || token.trim().length === 0) { if (!token || token.trim().length === 0) {
throw { throw {
@@ -107,12 +67,10 @@ class AsanaService {
}; };
const response = await axios.get<AsanaWorkspacesResponse>( const response = await axios.get<AsanaWorkspacesResponse>(
`${this.BASE_URL}/workspaces`, `${this.ASANA_BASE_URL}/workspaces`,
{ headers } { headers }
); );
// Converte a resposta da API para o formato esperado pelo componente
// A API retorna gid, mas o componente espera id
return response.data.data.map((workspace) => ({ return response.data.data.map((workspace) => ({
id: workspace.gid, id: workspace.gid,
name: workspace.name, name: workspace.name,
@@ -120,7 +78,6 @@ class AsanaService {
} catch (error: unknown) { } catch (error: unknown) {
console.error('Erro ao buscar workspaces do Asana:', error); console.error('Erro ao buscar workspaces do Asana:', error);
// Retorna o erro da API se existir
if (error && typeof error === 'object' && 'response' in error) { if (error && typeof error === 'object' && 'response' in error) {
const axiosError = error as { response?: { data?: any; status?: number } }; const axiosError = error as { response?: { data?: any; status?: number } };
@@ -146,13 +103,6 @@ class AsanaService {
} }
} }
/**
* Lista os usuários de um workspace do Asana
*
* @param token - Token de acesso do Asana
* @param workspaceId - ID do workspace (gid)
* @returns Promise com a lista de usuários formatados
*/
async getUsers(token: string, workspaceId: string): Promise<AsanaUser[]> { async getUsers(token: string, workspaceId: string): Promise<AsanaUser[]> {
if (!token || token.trim().length === 0) { if (!token || token.trim().length === 0) {
throw { throw {
@@ -177,7 +127,7 @@ class AsanaService {
}; };
const response = await axios.get<AsanaUsersResponse>( const response = await axios.get<AsanaUsersResponse>(
`${this.BASE_URL}/users?workspace=${workspaceId}`, `${this.ASANA_BASE_URL}/users?workspace=${workspaceId}`,
{ headers } { headers }
); );
@@ -190,7 +140,6 @@ class AsanaService {
} catch (error: unknown) { } catch (error: unknown) {
console.error('Erro ao buscar usuários do Asana:', error); console.error('Erro ao buscar usuários do Asana:', error);
// Retorna o erro da API se existir
if (error && typeof error === 'object' && 'response' in error) { if (error && typeof error === 'object' && 'response' in error) {
const axiosError = error as { response?: { data?: any; status?: number } }; const axiosError = error as { response?: { data?: any; status?: number } };
@@ -216,12 +165,6 @@ class AsanaService {
} }
} }
/**
* Obtém os detalhes da integração do Asana do usuário
*
* @param userId - ID do usuário
* @returns Promise com os detalhes da integração ou null se não houver
*/
async getIntegration(userId: string): Promise<AsanaIntegrationResponse | null> { async getIntegration(userId: string): Promise<AsanaIntegrationResponse | null> {
if (!userId || userId.trim().length === 0) { if (!userId || userId.trim().length === 0) {
throw { throw {
@@ -234,7 +177,6 @@ class AsanaService {
const axios = (await import('axios')).default; const axios = (await import('axios')).default;
const { GlobalFunctions } = await import('@/GlobalFunctions'); const { GlobalFunctions } = await import('@/GlobalFunctions');
// Obtém o token JWT para autenticação
const token = await GlobalFunctions.getToken(); const token = await GlobalFunctions.getToken();
const apiKey = import.meta.env.VITE_API_KEY || ''; const apiKey = import.meta.env.VITE_API_KEY || '';
@@ -243,27 +185,19 @@ class AsanaService {
'accept': 'application/json', 'accept': 'application/json',
}; };
// Adiciona API key (obrigatória)
if (apiKey) { if (apiKey) {
headers['apikey'] = apiKey; headers['apikey'] = apiKey;
} }
// Adiciona token JWT se disponível
if (token) { if (token) {
headers['Authorization'] = `Bearer ${token}`; headers['Authorization'] = `Bearer ${token}`;
} }
// Em desenvolvimento, usa proxy do Vite para evitar CORS
const baseUrl = import.meta.env.DEV
? '/api/intelligence'
: this.INTEGRATION_BASE_URL;
const response = await axios.get<AsanaIntegrationResponse>( const response = await axios.get<AsanaIntegrationResponse>(
`${baseUrl}/webhook/${this.INTEGRATION_WEBHOOK_ID}/codex/agente-pessoal/integracoes/asana/${userId}`, `https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/c898beff-84cb-44df-a69c-6eff27ccd7aa/codex/agente-pessoal/integracoes/asana/${userId}`,
{ headers } { headers }
); );
// Se success é false, retorna null (não tem integração)
if (!response.data.success) { if (!response.data.success) {
return null; return null;
} }
@@ -280,17 +214,10 @@ class AsanaService {
} }
} }
// Para outros erros, retorna null silenciosamente (não quebra a aplicação)
return null; return null;
} }
} }
/**
* Cria uma nova integração do Asana
*
* @param request - Dados da integração a ser criada
* @returns Promise com a resposta da criação
*/
async createIntegration(request: AsanaIntegrationRequest): Promise<AsanaIntegrationResponse> { async createIntegration(request: AsanaIntegrationRequest): Promise<AsanaIntegrationResponse> {
if (!request.user_id || !request.api_key || !request.workspace_gid || !request.usuario_asana_gid) { if (!request.user_id || !request.api_key || !request.workspace_gid || !request.usuario_asana_gid) {
throw { throw {
@@ -322,13 +249,8 @@ class AsanaService {
headers['Authorization'] = `Bearer ${token}`; headers['Authorization'] = `Bearer ${token}`;
} }
// Em desenvolvimento, usa proxy do Vite para evitar CORS
const baseUrl = import.meta.env.DEV
? '/api/intelligence'
: this.INTEGRATION_BASE_URL;
const response = await axios.post<AsanaIntegrationResponse>( const response = await axios.post<AsanaIntegrationResponse>(
`${baseUrl}/webhook/codex/agente-pessoal/integracoes/asana`, `https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/codex/agente-pessoal/integracoes/asana`,
request, request,
{ headers } { headers }
); );
@@ -357,13 +279,6 @@ class AsanaService {
} }
} }
/**
* Atualiza uma integração existente do Asana
*
* @param integracaoId - ID da integração a ser atualizada
* @param request - Dados da integração a ser atualizada
* @returns Promise com a resposta da atualização
*/
async updateIntegration(integracaoId: string, request: Omit<AsanaIntegrationRequest, 'user_id'>): Promise<AsanaIntegrationResponse> { async updateIntegration(integracaoId: string, request: Omit<AsanaIntegrationRequest, 'user_id'>): Promise<AsanaIntegrationResponse> {
if (!integracaoId || !request.api_key || !request.workspace_gid || !request.usuario_asana_gid) { if (!integracaoId || !request.api_key || !request.workspace_gid || !request.usuario_asana_gid) {
throw { throw {
@@ -376,7 +291,6 @@ class AsanaService {
const axios = (await import('axios')).default; const axios = (await import('axios')).default;
const { GlobalFunctions } = await import('@/GlobalFunctions'); const { GlobalFunctions } = await import('@/GlobalFunctions');
// Obtém o token JWT para autenticação
const token = await GlobalFunctions.getToken(); const token = await GlobalFunctions.getToken();
const apiKey = import.meta.env.VITE_API_KEY || ''; const apiKey = import.meta.env.VITE_API_KEY || '';
@@ -385,23 +299,18 @@ class AsanaService {
'accept': 'application/json', 'accept': 'application/json',
}; };
// Adiciona API key (obrigatória)
if (apiKey) { if (apiKey) {
headers['apikey'] = apiKey; headers['apikey'] = apiKey;
} }
// Adiciona token JWT se disponível
if (token) { if (token) {
headers['Authorization'] = `Bearer ${token}`; headers['Authorization'] = `Bearer ${token}`;
} }
// Em desenvolvimento, usa proxy do Vite para evitar CORS const url = `https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/c898beff-84cb-44df-a69c-6eff27ccd7aa/codex/agente-pessoal/integracoes/asana/${integracaoId}`;
const baseUrl = import.meta.env.DEV
? '/api/intelligence' const response = await axios.post<AsanaIntegrationResponse>(
: this.INTEGRATION_BASE_URL; url,
const response = await axios.put<AsanaIntegrationResponse>(
`${baseUrl}/webhook/${this.INTEGRATION_WEBHOOK_ID}/codex/agente-pessoal/integracoes/asana/${integracaoId}`,
request, request,
{ headers } { headers }
); );
+2 -2
View File
@@ -6,7 +6,7 @@ export { apiService } from './api';
export { transcriptionService } from './transcription'; export { transcriptionService } from './transcription';
export { audioGenerationService, VOICE_OPTIONS } from './audioGeneration'; export { audioGenerationService, VOICE_OPTIONS } from './audioGeneration';
export { imageGenerationService, IMAGE_SIZE_OPTIONS } from './imageGeneration'; export { imageGenerationService, IMAGE_SIZE_OPTIONS } from './imageGeneration';
export { userProfileService } from './userProfile'; export { personalAgent } from './personalAgent';
export { asanaService } from './asana'; export { asanaService } from './asana';
export type { TranscriptionResponse, TranscriptionRequest } from './transcription'; export type { TranscriptionResponse, TranscriptionRequest } from './transcription';
export type { AudioGenerationResponse, AudioGenerationRequest, VoiceType } from './audioGeneration'; export type { AudioGenerationResponse, AudioGenerationRequest, VoiceType } from './audioGeneration';
@@ -20,7 +20,7 @@ export type {
ExpensesResponse, ExpensesResponse,
ExpensesFilters, ExpensesFilters,
ExpenseCategory ExpenseCategory
} from './userProfile'; } from './personalAgent';
export type { export type {
AsanaWorkspace, AsanaWorkspace,
AsanaWorkspaceResponse, AsanaWorkspaceResponse,
@@ -1,9 +1,4 @@
import { apiService } from './api';
import { GlobalFunctions } from '@/GlobalFunctions'; import { GlobalFunctions } from '@/GlobalFunctions';
/**
* Interfaces para o serviço de perfil do usuário
*/
export interface UserProfile { export interface UserProfile {
success: boolean; success: boolean;
id?: string; id?: string;
@@ -27,9 +22,6 @@ export interface UpdateUserRequest {
followup: boolean; followup: boolean;
} }
/**
* Interface para indicadores financeiros
*/
export interface FinancialIndicators { export interface FinancialIndicators {
success: boolean; success: boolean;
id: string; id: string;
@@ -41,9 +33,6 @@ export interface FinancialIndicators {
message?: string; message?: string;
} }
/**
* Interface para despesa individual da API
*/
export interface ExpenseItem { export interface ExpenseItem {
id: string; id: string;
data_hora: string; data_hora: string;
@@ -56,9 +45,6 @@ export interface ExpenseItem {
usuario_email: string; usuario_email: string;
} }
/**
* Interface para resposta da API de despesas
*/
export interface ExpensesResponse { export interface ExpensesResponse {
success: boolean; success: boolean;
total_registros: number; total_registros: number;
@@ -68,9 +54,6 @@ export interface ExpensesResponse {
data: ExpenseItem[]; data: ExpenseItem[];
} }
/**
* Interface para filtros de busca de despesas
*/
export interface ExpensesFilters { export interface ExpensesFilters {
page?: number; page?: number;
per_page?: number; per_page?: number;
@@ -80,9 +63,6 @@ export interface ExpensesFilters {
data_final?: string; data_final?: string;
} }
/**
* Interface para categoria de despesas
*/
export interface ExpenseCategory { export interface ExpenseCategory {
id: number; id: number;
nome: string; nome: string;
@@ -91,29 +71,8 @@ export interface ExpenseCategory {
atualizado_em: string; atualizado_em: string;
} }
/** class PersonalAgent {
* Serviço para gerenciamento de perfil do usuário
*/
class UserProfileService {
// Em desenvolvimento, usa proxy do Vite para evitar CORS
// Em produção, usa URL direta (requer que servidor permita PUT no CORS)
private readonly BASE_URL = import.meta.env.DEV
? '/api/intelligence'
: 'https://prod-hgtx-intelligence-n8n.hgtx.com.br';
private readonly WEBHOOK_ID = '2299acaf-70ee-47e3-a6fc-56dcc678d651';
private readonly GET_USER_ENDPOINT = `${this.BASE_URL}/webhook/${this.WEBHOOK_ID}/codex/agente-pessoal/user`;
private readonly CREATE_USER_ENDPOINT = `${this.BASE_URL}/webhook/codex/agente-pessoal/user`;
private readonly UPDATE_USER_ENDPOINT = `${this.BASE_URL}/webhook/${this.WEBHOOK_ID}/codex/agente-pessoal/user`;
private readonly GET_FINANCIAL_INDICATORS_ENDPOINT = `${this.BASE_URL}/webhook/${this.WEBHOOK_ID}/codex/agente-pessoal/financas/indicadores`;
private readonly GET_EXPENSES_ENDPOINT = `${this.BASE_URL}/webhook/${this.WEBHOOK_ID}/codex/agente-pessoal/financas`;
private readonly GET_CATEGORIES_ENDPOINT = `${this.BASE_URL}/webhook/codex/agente-pessoal/categorias`;
/**
* Obtém os detalhes do usuário pelo email
*
* @param userEmail - Email do usuário (opcional, usa do GlobalFunctions se não fornecido)
* @returns Promise com os dados do usuário ou erro se não existir
*/
async getUserProfile(userEmail?: string): Promise<UserProfile> { async getUserProfile(userEmail?: string): Promise<UserProfile> {
const email = userEmail || GlobalFunctions.getUsuarioLogado().email; const email = userEmail || GlobalFunctions.getUsuarioLogado().email;
@@ -125,36 +84,30 @@ class UserProfileService {
} }
try { try {
// Usa axios diretamente pois a URL é absoluta e diferente do baseURL
const axios = (await import('axios')).default; const axios = (await import('axios')).default;
// Obtém o token JWT para autenticação
const token = await GlobalFunctions.getToken(); const token = await GlobalFunctions.getToken();
const apiKey = import.meta.env.VITE_API_KEY || ''; const apiKey = import.meta.env.VITE_API_KEY || '';
// Prepara headers de autenticação
const headers: Record<string, string> = { const headers: Record<string, string> = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}; };
// Adiciona API key (obrigatória)
if (apiKey) { if (apiKey) {
headers['apikey'] = apiKey; headers['apikey'] = apiKey;
} }
// Adiciona token JWT se disponível
if (token) { if (token) {
headers['Authorization'] = `Bearer ${token}`; headers['Authorization'] = `Bearer ${token}`;
} }
const response = await axios.get<UserProfile>( const response = await axios.get<UserProfile>(
`${this.GET_USER_ENDPOINT}/${email}`, `https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/2299acaf-70ee-47e3-a6fc-56dcc678d651/codex/agente-pessoal/user/${email}`,
{ headers } { headers }
); );
return response.data; return response.data;
} catch (error: any) { } catch (error: any) {
// Se o usuário não existir, retorna o erro da API
if (error.response?.data) { if (error.response?.data) {
return error.response.data; return error.response.data;
} }
@@ -167,14 +120,8 @@ class UserProfileService {
} }
} }
/**
* Cria um novo usuário
*
* @param request - Dados do usuário a ser criado
* @returns Promise com os dados do usuário criado
*/
async createUser(request: CreateUserRequest): Promise<UserProfile> { async createUser(request: CreateUserRequest): Promise<UserProfile> {
// Validações
if (!request.nome || request.nome.trim().length === 0) { if (!request.nome || request.nome.trim().length === 0) {
throw { throw {
success: false, success: false,
@@ -196,48 +143,30 @@ class UserProfileService {
}; };
} }
// Remove formatação do WhatsApp (apenas números)
const whatsappNumbers = request.whatsapp.replace(/\D/g, ''); const whatsappNumbers = request.whatsapp.replace(/\D/g, '');
try { try {
const axios = (await import('axios')).default; const axios = (await import('axios')).default;
// Obtém o token JWT para autenticação
const token = await GlobalFunctions.getToken(); const token = await GlobalFunctions.getToken();
const apiKey = import.meta.env.VITE_API_KEY || ''; const apiKey = import.meta.env.VITE_API_KEY || '';
// Prepara headers de autenticação
// A API pode precisar apenas da apikey, não do token JWT
const headers: Record<string, string> = { const headers: Record<string, string> = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}; };
// Adiciona API key (obrigatória)
if (apiKey) { if (apiKey) {
headers['apikey'] = apiKey; headers['apikey'] = apiKey;
} else { } else {
console.warn('VITE_API_KEY não configurada'); console.warn('API_KEY não configurada');
} }
// Adiciona token JWT se disponível (pode ser necessário)
if (token) { if (token) {
headers['Authorization'] = `Bearer ${token}`; headers['Authorization'] = `Bearer ${token}`;
} }
console.log('Criando usuário:', {
url: this.CREATE_USER_ENDPOINT,
hasApiKey: !!apiKey,
hasToken: !!token,
data: {
nome: request.nome.trim(),
email: request.email.trim(),
whatsapp: whatsappNumbers,
followup: request.followup,
},
});
const response = await axios.post<UserProfile>( const response = await axios.post<UserProfile>(
this.CREATE_USER_ENDPOINT, 'https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/codex/agente-pessoal/user',
{ {
nome: request.nome.trim(), nome: request.nome.trim(),
email: request.email.trim(), email: request.email.trim(),
@@ -249,7 +178,6 @@ class UserProfileService {
return response.data; return response.data;
} catch (error: any) { } catch (error: any) {
// Retorna o erro da API se existir
if (error.response?.data) { if (error.response?.data) {
throw error.response.data; throw error.response.data;
} }
@@ -262,15 +190,8 @@ class UserProfileService {
} }
} }
/**
* Atualiza os dados do usuário
*
* @param userId - ID do usuário
* @param request - Dados a serem atualizados
* @returns Promise com os dados atualizados do usuário
*/
async updateUser(userId: string, request: UpdateUserRequest): Promise<UserProfile> { async updateUser(userId: string, request: UpdateUserRequest): Promise<UserProfile> {
// Validações
if (!userId || userId.trim().length === 0) { if (!userId || userId.trim().length === 0) {
throw { throw {
success: false, success: false,
@@ -292,47 +213,28 @@ class UserProfileService {
}; };
} }
// Remove formatação do WhatsApp (apenas números)
const whatsappNumbers = request.whatsapp.replace(/\D/g, ''); const whatsappNumbers = request.whatsapp.replace(/\D/g, '');
try { try {
const axios = (await import('axios')).default; const axios = (await import('axios')).default;
// Obtém o token JWT para autenticação
const token = await GlobalFunctions.getToken(); const token = await GlobalFunctions.getToken();
const apiKey = import.meta.env.VITE_API_KEY || ''; const apiKey = import.meta.env.VITE_API_KEY || '';
// Prepara headers de autenticação
const headers: Record<string, string> = { const headers: Record<string, string> = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}; };
// Adiciona API key (obrigatória)
if (apiKey) { if (apiKey) {
headers['apikey'] = apiKey; headers['apikey'] = apiKey;
} }
// Adiciona token JWT se disponível
if (token) { if (token) {
headers['Authorization'] = `Bearer ${token}`; headers['Authorization'] = `Bearer ${token}`;
} }
console.log('Atualizando usuário:', { const response = await axios.post<UserProfile>(
url: `${this.UPDATE_USER_ENDPOINT}/${userId}`, `https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/2299acaf-70ee-47e3-a6fc-56dcc678d651/codex/agente-pessoal/user/${userId}`,
method: 'PUT',
hasApiKey: !!apiKey,
hasToken: !!token,
isDev: import.meta.env.DEV,
data: {
nome: request.nome.trim(),
whatsapp: whatsappNumbers,
followup: request.followup,
},
});
// Usa PUT conforme documentação da API
const response = await axios.put<UserProfile>(
`${this.UPDATE_USER_ENDPOINT}/${userId}`,
{ {
nome: request.nome.trim(), nome: request.nome.trim(),
whatsapp: whatsappNumbers, whatsapp: whatsappNumbers,
@@ -343,7 +245,6 @@ class UserProfileService {
return response.data; return response.data;
} catch (error: any) { } catch (error: any) {
// Retorna o erro da API se existir
if (error.response?.data) { if (error.response?.data) {
throw error.response.data; throw error.response.data;
} }
@@ -356,12 +257,6 @@ class UserProfileService {
} }
} }
/**
* Obtém os indicadores financeiros do usuário
*
* @param userEmail - Email do usuário (opcional, usa do GlobalFunctions se não fornecido)
* @returns Promise com os indicadores financeiros
*/
async getFinancialIndicators(userEmail?: string): Promise<FinancialIndicators> { async getFinancialIndicators(userEmail?: string): Promise<FinancialIndicators> {
const email = userEmail || GlobalFunctions.getUsuarioLogado().email; const email = userEmail || GlobalFunctions.getUsuarioLogado().email;
@@ -374,34 +269,29 @@ class UserProfileService {
try { try {
const axios = (await import('axios')).default; const axios = (await import('axios')).default;
// Obtém o token JWT para autenticação
const token = await GlobalFunctions.getToken(); const token = await GlobalFunctions.getToken();
const apiKey = import.meta.env.VITE_API_KEY || ''; const apiKey = import.meta.env.VITE_API_KEY || '';
// Prepara headers de autenticação
const headers: Record<string, string> = { const headers: Record<string, string> = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}; };
// Adiciona API key (obrigatória)
if (apiKey) { if (apiKey) {
headers['apikey'] = apiKey; headers['apikey'] = apiKey;
} }
// Adiciona token JWT se disponível
if (token) { if (token) {
headers['Authorization'] = `Bearer ${token}`; headers['Authorization'] = `Bearer ${token}`;
} }
const response = await axios.get<FinancialIndicators>( const response = await axios.get<FinancialIndicators>(
`${this.GET_FINANCIAL_INDICATORS_ENDPOINT}/${email}`, `https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/2299acaf-70ee-47e3-a6fc-56dcc678d651/codex/agente-pessoal/financas/indicadores/${email}`,
{ headers } { headers }
); );
return response.data; return response.data;
} catch (error: unknown) { } catch (error: unknown) {
// Retorna o erro da API se existir
if (error && typeof error === 'object' && 'response' in error) { if (error && typeof error === 'object' && 'response' in error) {
const axiosError = error as { response?: { data?: FinancialIndicators } }; const axiosError = error as { response?: { data?: FinancialIndicators } };
if (axiosError.response?.data) { if (axiosError.response?.data) {
@@ -412,20 +302,13 @@ class UserProfileService {
throw { throw {
success: false, success: false,
message: error instanceof Error ? error.message : 'Erro ao buscar indicadores financeiros', message: error instanceof Error ? error.message : 'Erro ao buscar indicadores financeiros',
status: error && typeof error === 'object' && 'response' in error status: error && typeof error === 'object' && 'response' in error
? (error as { response?: { status?: number } }).response?.status ? (error as { response?: { status?: number } }).response?.status
: undefined, : undefined,
}; };
} }
} }
/**
* Obtém a lista de despesas do usuário com paginação e filtros
*
* @param userEmail - Email do usuário (opcional, usa do GlobalFunctions se não fornecido)
* @param filters - Filtros de busca (página, itens por página, descrição, categoria, datas)
* @returns Promise com a lista de despesas paginada
*/
async getExpenses(userEmail?: string, filters?: ExpensesFilters): Promise<ExpensesResponse> { async getExpenses(userEmail?: string, filters?: ExpensesFilters): Promise<ExpensesResponse> {
const email = userEmail || GlobalFunctions.getUsuarioLogado().email; const email = userEmail || GlobalFunctions.getUsuarioLogado().email;
@@ -438,22 +321,18 @@ class UserProfileService {
try { try {
const axios = (await import('axios')).default; const axios = (await import('axios')).default;
// Obtém o token JWT para autenticação
const token = await GlobalFunctions.getToken(); const token = await GlobalFunctions.getToken();
const apiKey = import.meta.env.VITE_API_KEY || ''; const apiKey = import.meta.env.VITE_API_KEY || '';
// Prepara headers de autenticação
const headers: Record<string, string> = { const headers: Record<string, string> = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}; };
// Adiciona API key (obrigatória)
if (apiKey) { if (apiKey) {
headers['apikey'] = apiKey; headers['apikey'] = apiKey;
} }
// Adiciona token JWT se disponível
if (token) { if (token) {
headers['Authorization'] = `Bearer ${token}`; headers['Authorization'] = `Bearer ${token}`;
} }
@@ -468,14 +347,13 @@ class UserProfileService {
if (filters?.data_final) params.append('data_final', filters.data_final); if (filters?.data_final) params.append('data_final', filters.data_final);
const queryString = params.toString(); const queryString = params.toString();
const url = `${this.GET_EXPENSES_ENDPOINT}/${email}${queryString ? `?${queryString}` : ''}`; const url = `https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/2299acaf-70ee-47e3-a6fc-56dcc678d651/codex/agente-pessoal/financas/${email}${queryString ? `?${queryString}` : ''}`;
const response = await axios.get<ExpensesResponse[]>(url, { headers }); const response = await axios.get<ExpensesResponse[]>(url, { headers });
// A API retorna um array com um único objeto
if (Array.isArray(response.data) && response.data.length > 0) { if (Array.isArray(response.data) && response.data.length > 0) {
const expensesResponse = response.data[0]; const expensesResponse = response.data[0];
// Valida e limpa o array de dados, removendo objetos vazios // Valida e limpa o array de dados, removendo objetos vazios
if (expensesResponse.data && Array.isArray(expensesResponse.data)) { if (expensesResponse.data && Array.isArray(expensesResponse.data)) {
// Filtra objetos vazios (sem propriedades ou apenas com propriedades vazias) // Filtra objetos vazios (sem propriedades ou apenas com propriedades vazias)
@@ -483,7 +361,7 @@ class UserProfileService {
// Verifica se o objeto tem pelo menos uma propriedade válida // Verifica se o objeto tem pelo menos uma propriedade válida
return item && typeof item === 'object' && Object.keys(item).length > 0 && item.id; return item && typeof item === 'object' && Object.keys(item).length > 0 && item.id;
}); });
// Se após filtrar não há dados, garante que data seja um array vazio // Se após filtrar não há dados, garante que data seja um array vazio
if (expensesResponse.data.length === 0) { if (expensesResponse.data.length === 0) {
expensesResponse.data = []; expensesResponse.data = [];
@@ -496,7 +374,7 @@ class UserProfileService {
expensesResponse.total_registros = 0; expensesResponse.total_registros = 0;
expensesResponse.total_paginas = 0; expensesResponse.total_paginas = 0;
} }
return expensesResponse; return expensesResponse;
} }
@@ -521,42 +399,33 @@ class UserProfileService {
throw { throw {
success: false, success: false,
message: error instanceof Error ? error.message : 'Erro ao buscar despesas', message: error instanceof Error ? error.message : 'Erro ao buscar despesas',
status: error && typeof error === 'object' && 'response' in error status: error && typeof error === 'object' && 'response' in error
? (error as { response?: { status?: number } }).response?.status ? (error as { response?: { status?: number } }).response?.status
: undefined, : undefined,
}; };
} }
} }
/**
* Obtém a lista de categorias de despesas
*
* @returns Promise com a lista de categorias
*/
async getCategories(): Promise<ExpenseCategory[]> { async getCategories(): Promise<ExpenseCategory[]> {
try { try {
const axios = (await import('axios')).default; const axios = (await import('axios')).default;
// Obtém o token JWT para autenticação
const token = await GlobalFunctions.getToken(); const token = await GlobalFunctions.getToken();
const apiKey = import.meta.env.VITE_API_KEY || ''; const apiKey = import.meta.env.VITE_API_KEY || '';
// Prepara headers de autenticação
const headers: Record<string, string> = { const headers: Record<string, string> = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}; };
// Adiciona API key (obrigatória)
if (apiKey) { if (apiKey) {
headers['apikey'] = apiKey; headers['apikey'] = apiKey;
} }
// Adiciona token JWT se disponível
if (token) { if (token) {
headers['Authorization'] = `Bearer ${token}`; headers['Authorization'] = `Bearer ${token}`;
} }
const response = await axios.get<ExpenseCategory[]>(this.GET_CATEGORIES_ENDPOINT, { headers }); const response = await axios.get<ExpenseCategory[]>('https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/codex/agente-pessoal/categorias', { headers });
// A API retorna um array de categorias // A API retorna um array de categorias
if (Array.isArray(response.data)) { if (Array.isArray(response.data)) {
@@ -566,12 +435,10 @@ class UserProfileService {
return []; return [];
} catch (error: unknown) { } catch (error: unknown) {
console.error('Erro ao buscar categorias:', error); console.error('Erro ao buscar categorias:', error);
// Retorna array vazio em caso de erro para não quebrar a aplicação
return []; return [];
} }
} }
} }
// Exporta instância única (Singleton) export const personalAgent = new PersonalAgent();
export const userProfileService = new UserProfileService();