Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 189a2a8f2c | |||
| 50aca767d7 |
@@ -21,7 +21,7 @@ export const Layout = ({ children, activeTab, onTabChange }: LayoutProps) => {
|
||||
{ id: "images" as TabType, label: "Imagens", icon: Image },
|
||||
{ id: "transcription" as TabType, label: "Transcrição de Áudio", icon: AudioLines },
|
||||
{ id: "generation" as TabType, label: "Geração de Áudio", icon: Mic },
|
||||
{ id: "bots" as TabType, label: "Bots", icon: Bot },
|
||||
//{ id: "bots" as TabType, label: "Bots", icon: Bot },
|
||||
{ id: "agent" as TabType, label: "Agente de Parecer", icon: Brain },
|
||||
];
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ import {
|
||||
PaginationPrevious,
|
||||
} from "@/components/ui/pagination";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { personalAgent, FinancialIndicators, ExpenseItem, ExpensesResponse, ExpensesFilters, ExpenseCategory } from "@/services/personalAgent";
|
||||
import { userProfileService, FinancialIndicators, ExpenseItem, ExpensesResponse, ExpensesFilters, ExpenseCategory } from "@/services/userProfile";
|
||||
import { GlobalFunctions, TransferAreaProperties } from "@/GlobalFunctions";
|
||||
import { toast } from "@/hooks/use-toast";
|
||||
|
||||
@@ -175,7 +175,7 @@ export default function Financas() {
|
||||
}
|
||||
|
||||
// Busca os indicadores financeiros da API
|
||||
const response = await personalAgent.getFinancialIndicators(userEmail);
|
||||
const response = await userProfileService.getFinancialIndicators(userEmail);
|
||||
|
||||
if (response.success) {
|
||||
setIndicators(response);
|
||||
@@ -265,7 +265,7 @@ export default function Financas() {
|
||||
filters.data_final = format(endDate, "yyyy-MM-dd");
|
||||
}
|
||||
|
||||
const response = await personalAgent.getExpenses(emailToUse, filters);
|
||||
const response = await userProfileService.getExpenses(emailToUse, filters);
|
||||
|
||||
if (response.success) {
|
||||
// Garante que a resposta tenha estrutura válida mesmo quando não há dados
|
||||
@@ -308,7 +308,8 @@ export default function Financas() {
|
||||
// Carrega categorias
|
||||
try {
|
||||
setLoadingCategories(true);
|
||||
const categoriesData = await personalAgent.getCategories();
|
||||
const categoriesData = await userProfileService.getCategories();
|
||||
console.log("Categorias carregadas:", categoriesData);
|
||||
setCategories(categoriesData);
|
||||
} catch (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 { AsanaCard } from "@/modules/intelligence-ia/components/integrations/AsanaCard";
|
||||
import { asanaService } from "@/services/asana";
|
||||
import { personalAgent } from "@/services/personalAgent";
|
||||
import { userProfileService } from "@/services/userProfile";
|
||||
import { GlobalFunctions, TransferAreaProperties } from "@/GlobalFunctions";
|
||||
|
||||
interface Workspace {
|
||||
@@ -199,14 +199,16 @@ export default function Integrations() {
|
||||
}
|
||||
|
||||
if (!userEmail) {
|
||||
console.log("Integrations: Não foi possível obter email do usuário");
|
||||
setLoadingIntegration(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// Busca o perfil do usuário para obter o ID
|
||||
const userProfile = await personalAgent.getUserProfile(userEmail);
|
||||
const userProfile = await userProfileService.getUserProfile(userEmail);
|
||||
|
||||
if (!userProfile.success || !userProfile.id) {
|
||||
console.log("Integrations: Usuário não encontrado ou sem ID");
|
||||
setLoadingIntegration(false);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
} from "@/components/ui/dialog";
|
||||
import { User, Mail, Phone, Save, Bell, Loader2, Plus } from "lucide-react";
|
||||
import { toast } from "@/hooks/use-toast";
|
||||
import { personalAgent, UserProfile } from "@/services/personalAgent";
|
||||
import { userProfileService, UserProfile } from "@/services/userProfile";
|
||||
import { GlobalFunctions, TransferAreaProperties } from "@/GlobalFunctions";
|
||||
|
||||
const formatPhoneNumber = (value: string) => {
|
||||
@@ -54,15 +54,20 @@ const MeuPerfil = () => {
|
||||
// Verifica se há token no sessionStorage primeiro
|
||||
const jsonUsuario = sessionStorage.getItem('usuarioLogado');
|
||||
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');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("MeuPerfil: Token encontrado no sessionStorage");
|
||||
|
||||
// Tenta obter o email do usuário logado
|
||||
let userEmail = GlobalFunctions.getUsuarioLogado().email;
|
||||
console.log("MeuPerfil: Email obtido:", userEmail);
|
||||
|
||||
// Se não tem email, tenta fazer refresh do token
|
||||
if (!userEmail) {
|
||||
console.log("MeuPerfil: Email vazio, tentando fazer refresh do token");
|
||||
try {
|
||||
await GlobalFunctions.getToken();
|
||||
// Aguarda um pouco para o refresh ser processado (se necessário)
|
||||
@@ -70,6 +75,7 @@ const MeuPerfil = () => {
|
||||
|
||||
// Verifica novamente após refresh
|
||||
userEmail = GlobalFunctions.getUsuarioLogado().email;
|
||||
console.log("MeuPerfil: Email após refresh:", userEmail);
|
||||
} catch (error) {
|
||||
console.error("MeuPerfil: Erro ao obter token:", error);
|
||||
}
|
||||
@@ -79,17 +85,24 @@ const MeuPerfil = () => {
|
||||
// Se o token existe mas não tem email, pode ser problema na decodificação
|
||||
if (!userEmail) {
|
||||
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
|
||||
// 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)
|
||||
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');
|
||||
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
|
||||
console.log("MeuPerfil: Carregando perfil do usuário");
|
||||
loadUserProfile();
|
||||
};
|
||||
|
||||
@@ -103,11 +116,13 @@ const MeuPerfil = () => {
|
||||
|
||||
// Se não tem email, tenta obter do Transfer Area como fallback
|
||||
if (!userEmail) {
|
||||
console.log("MeuPerfil: Email não encontrado no token, tentando Transfer Area");
|
||||
const transferEmail = GlobalFunctions.getTransferProperty(
|
||||
TransferAreaProperties.UsuarioEmail
|
||||
);
|
||||
if (transferEmail) {
|
||||
userEmail = transferEmail as string;
|
||||
console.log("MeuPerfil: Email do Transfer Area:", userEmail);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,6 +132,7 @@ const MeuPerfil = () => {
|
||||
await GlobalFunctions.getToken();
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
userEmail = GlobalFunctions.getUsuarioLogado().email;
|
||||
console.log("MeuPerfil: Email após refresh no loadUserProfile:", userEmail);
|
||||
} catch (error) {
|
||||
console.error("MeuPerfil: Erro ao fazer refresh no loadUserProfile:", error);
|
||||
}
|
||||
@@ -135,7 +151,7 @@ const MeuPerfil = () => {
|
||||
}
|
||||
|
||||
setEmail(userEmail);
|
||||
const response = await personalAgent.getUserProfile(userEmail);
|
||||
const response = await userProfileService.getUserProfile(userEmail);
|
||||
|
||||
if (response.success && response.id) {
|
||||
// Usuário existe
|
||||
@@ -187,7 +203,7 @@ const MeuPerfil = () => {
|
||||
|
||||
try {
|
||||
setSaving(true);
|
||||
const response = await personalAgent.updateUser(userId, {
|
||||
const response = await userProfileService.updateUser(userId, {
|
||||
nome: nomeCompleto,
|
||||
whatsapp: whatsapp,
|
||||
followup: receberLembretes,
|
||||
@@ -239,7 +255,7 @@ const MeuPerfil = () => {
|
||||
|
||||
try {
|
||||
setCreating(true);
|
||||
const response = await personalAgent.createUser({
|
||||
const response = await userProfileService.createUser({
|
||||
nome: createNome.trim(),
|
||||
email: email,
|
||||
whatsapp: createWhatsapp,
|
||||
|
||||
+99
-8
@@ -1,32 +1,58 @@
|
||||
/**
|
||||
* Serviço para integração com API do Asana
|
||||
*/
|
||||
|
||||
/**
|
||||
* Interface para workspace do Asana (resposta da API)
|
||||
*/
|
||||
export interface AsanaWorkspaceResponse {
|
||||
gid: string;
|
||||
resource_type: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface para resposta da API de workspaces
|
||||
*/
|
||||
export interface AsanaWorkspacesResponse {
|
||||
data: AsanaWorkspaceResponse[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface para workspace formatado (usado no componente)
|
||||
*/
|
||||
export interface AsanaWorkspace {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface para usuário do Asana (resposta da API)
|
||||
*/
|
||||
export interface AsanaUserResponse {
|
||||
gid: string;
|
||||
name: string;
|
||||
resource_type: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface para resposta da API de usuários
|
||||
*/
|
||||
export interface AsanaUsersResponse {
|
||||
data: AsanaUserResponse[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface para usuário formatado (usado no componente)
|
||||
*/
|
||||
export interface AsanaUser {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface para resposta da API de integração do Asana
|
||||
*/
|
||||
export interface AsanaIntegrationResponse {
|
||||
success: boolean;
|
||||
integracao_id?: string;
|
||||
@@ -38,6 +64,9 @@ export interface AsanaIntegrationResponse {
|
||||
usuario_asana_nome?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface para request de criação/atualização de integração
|
||||
*/
|
||||
export interface AsanaIntegrationRequest {
|
||||
user_id?: string;
|
||||
api_key: string;
|
||||
@@ -47,9 +76,20 @@ export interface AsanaIntegrationRequest {
|
||||
usuario_asana_nome: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serviço para integração com Asana
|
||||
*/
|
||||
class AsanaService {
|
||||
private readonly ASANA_BASE_URL = 'https://app.asana.com/api/1.0';
|
||||
private readonly 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[]> {
|
||||
if (!token || token.trim().length === 0) {
|
||||
throw {
|
||||
@@ -67,10 +107,12 @@ class AsanaService {
|
||||
};
|
||||
|
||||
const response = await axios.get<AsanaWorkspacesResponse>(
|
||||
`${this.ASANA_BASE_URL}/workspaces`,
|
||||
`${this.BASE_URL}/workspaces`,
|
||||
{ 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) => ({
|
||||
id: workspace.gid,
|
||||
name: workspace.name,
|
||||
@@ -78,6 +120,7 @@ class AsanaService {
|
||||
} catch (error: unknown) {
|
||||
console.error('Erro ao buscar workspaces do Asana:', error);
|
||||
|
||||
// Retorna o erro da API se existir
|
||||
if (error && typeof error === 'object' && 'response' in error) {
|
||||
const axiosError = error as { response?: { data?: any; status?: number } };
|
||||
|
||||
@@ -103,6 +146,13 @@ 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[]> {
|
||||
if (!token || token.trim().length === 0) {
|
||||
throw {
|
||||
@@ -127,7 +177,7 @@ class AsanaService {
|
||||
};
|
||||
|
||||
const response = await axios.get<AsanaUsersResponse>(
|
||||
`${this.ASANA_BASE_URL}/users?workspace=${workspaceId}`,
|
||||
`${this.BASE_URL}/users?workspace=${workspaceId}`,
|
||||
{ headers }
|
||||
);
|
||||
|
||||
@@ -140,6 +190,7 @@ class AsanaService {
|
||||
} catch (error: unknown) {
|
||||
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) {
|
||||
const axiosError = error as { response?: { data?: any; status?: number } };
|
||||
|
||||
@@ -165,6 +216,12 @@ 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> {
|
||||
if (!userId || userId.trim().length === 0) {
|
||||
throw {
|
||||
@@ -177,6 +234,7 @@ class AsanaService {
|
||||
const axios = (await import('axios')).default;
|
||||
const { GlobalFunctions } = await import('@/GlobalFunctions');
|
||||
|
||||
// Obtém o token JWT para autenticação
|
||||
const token = await GlobalFunctions.getToken();
|
||||
const apiKey = import.meta.env.VITE_API_KEY || '';
|
||||
|
||||
@@ -185,19 +243,27 @@ class AsanaService {
|
||||
'accept': 'application/json',
|
||||
};
|
||||
|
||||
// Adiciona API key (obrigatória)
|
||||
if (apiKey) {
|
||||
headers['apikey'] = apiKey;
|
||||
}
|
||||
|
||||
// Adiciona token JWT se disponível
|
||||
if (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>(
|
||||
`https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/c898beff-84cb-44df-a69c-6eff27ccd7aa/codex/agente-pessoal/integracoes/asana/${userId}`,
|
||||
`${baseUrl}/webhook/${this.INTEGRATION_WEBHOOK_ID}/codex/agente-pessoal/integracoes/asana/${userId}`,
|
||||
{ headers }
|
||||
);
|
||||
|
||||
// Se success é false, retorna null (não tem integração)
|
||||
if (!response.data.success) {
|
||||
return null;
|
||||
}
|
||||
@@ -214,10 +280,17 @@ class AsanaService {
|
||||
}
|
||||
}
|
||||
|
||||
// Para outros erros, retorna null silenciosamente (não quebra a aplicação)
|
||||
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> {
|
||||
if (!request.user_id || !request.api_key || !request.workspace_gid || !request.usuario_asana_gid) {
|
||||
throw {
|
||||
@@ -249,8 +322,13 @@ class AsanaService {
|
||||
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>(
|
||||
`https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/codex/agente-pessoal/integracoes/asana`,
|
||||
`${baseUrl}/webhook/codex/agente-pessoal/integracoes/asana`,
|
||||
request,
|
||||
{ headers }
|
||||
);
|
||||
@@ -279,6 +357,13 @@ 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> {
|
||||
if (!integracaoId || !request.api_key || !request.workspace_gid || !request.usuario_asana_gid) {
|
||||
throw {
|
||||
@@ -291,6 +376,7 @@ class AsanaService {
|
||||
const axios = (await import('axios')).default;
|
||||
const { GlobalFunctions } = await import('@/GlobalFunctions');
|
||||
|
||||
// Obtém o token JWT para autenticação
|
||||
const token = await GlobalFunctions.getToken();
|
||||
const apiKey = import.meta.env.VITE_API_KEY || '';
|
||||
|
||||
@@ -299,18 +385,23 @@ class AsanaService {
|
||||
'accept': 'application/json',
|
||||
};
|
||||
|
||||
// Adiciona API key (obrigatória)
|
||||
if (apiKey) {
|
||||
headers['apikey'] = apiKey;
|
||||
}
|
||||
|
||||
// Adiciona token JWT se disponível
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
const url = `https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/c898beff-84cb-44df-a69c-6eff27ccd7aa/codex/agente-pessoal/integracoes/asana/${integracaoId}`;
|
||||
// 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>(
|
||||
url,
|
||||
const response = await axios.put<AsanaIntegrationResponse>(
|
||||
`${baseUrl}/webhook/${this.INTEGRATION_WEBHOOK_ID}/codex/agente-pessoal/integracoes/asana/${integracaoId}`,
|
||||
request,
|
||||
{ headers }
|
||||
);
|
||||
|
||||
@@ -6,7 +6,7 @@ export { apiService } from './api';
|
||||
export { transcriptionService } from './transcription';
|
||||
export { audioGenerationService, VOICE_OPTIONS } from './audioGeneration';
|
||||
export { imageGenerationService, IMAGE_SIZE_OPTIONS } from './imageGeneration';
|
||||
export { personalAgent } from './personalAgent';
|
||||
export { userProfileService } from './userProfile';
|
||||
export { asanaService } from './asana';
|
||||
export type { TranscriptionResponse, TranscriptionRequest } from './transcription';
|
||||
export type { AudioGenerationResponse, AudioGenerationRequest, VoiceType } from './audioGeneration';
|
||||
@@ -20,7 +20,7 @@ export type {
|
||||
ExpensesResponse,
|
||||
ExpensesFilters,
|
||||
ExpenseCategory
|
||||
} from './personalAgent';
|
||||
} from './userProfile';
|
||||
export type {
|
||||
AsanaWorkspace,
|
||||
AsanaWorkspaceResponse,
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import { apiService } from './api';
|
||||
import { GlobalFunctions } from '@/GlobalFunctions';
|
||||
|
||||
/**
|
||||
* Interfaces para o serviço de perfil do usuário
|
||||
*/
|
||||
export interface UserProfile {
|
||||
success: boolean;
|
||||
id?: string;
|
||||
@@ -22,6 +27,9 @@ export interface UpdateUserRequest {
|
||||
followup: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface para indicadores financeiros
|
||||
*/
|
||||
export interface FinancialIndicators {
|
||||
success: boolean;
|
||||
id: string;
|
||||
@@ -33,6 +41,9 @@ export interface FinancialIndicators {
|
||||
message?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface para despesa individual da API
|
||||
*/
|
||||
export interface ExpenseItem {
|
||||
id: string;
|
||||
data_hora: string;
|
||||
@@ -45,6 +56,9 @@ export interface ExpenseItem {
|
||||
usuario_email: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface para resposta da API de despesas
|
||||
*/
|
||||
export interface ExpensesResponse {
|
||||
success: boolean;
|
||||
total_registros: number;
|
||||
@@ -54,6 +68,9 @@ export interface ExpensesResponse {
|
||||
data: ExpenseItem[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface para filtros de busca de despesas
|
||||
*/
|
||||
export interface ExpensesFilters {
|
||||
page?: number;
|
||||
per_page?: number;
|
||||
@@ -63,6 +80,9 @@ export interface ExpensesFilters {
|
||||
data_final?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface para categoria de despesas
|
||||
*/
|
||||
export interface ExpenseCategory {
|
||||
id: number;
|
||||
nome: string;
|
||||
@@ -71,8 +91,29 @@ export interface ExpenseCategory {
|
||||
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> {
|
||||
const email = userEmail || GlobalFunctions.getUsuarioLogado().email;
|
||||
|
||||
@@ -84,30 +125,36 @@ class PersonalAgent {
|
||||
}
|
||||
|
||||
try {
|
||||
// Usa axios diretamente pois a URL é absoluta e diferente do baseURL
|
||||
const axios = (await import('axios')).default;
|
||||
|
||||
// Obtém o token JWT para autenticação
|
||||
const token = await GlobalFunctions.getToken();
|
||||
const apiKey = import.meta.env.VITE_API_KEY || '';
|
||||
|
||||
// Prepara headers de autenticação
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
// Adiciona API key (obrigatória)
|
||||
if (apiKey) {
|
||||
headers['apikey'] = apiKey;
|
||||
}
|
||||
|
||||
// Adiciona token JWT se disponível
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
const response = await axios.get<UserProfile>(
|
||||
`https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/2299acaf-70ee-47e3-a6fc-56dcc678d651/codex/agente-pessoal/user/${email}`,
|
||||
`${this.GET_USER_ENDPOINT}/${email}`,
|
||||
{ headers }
|
||||
);
|
||||
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
// Se o usuário não existir, retorna o erro da API
|
||||
if (error.response?.data) {
|
||||
return error.response.data;
|
||||
}
|
||||
@@ -120,8 +167,14 @@ class PersonalAgent {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
|
||||
// Validações
|
||||
if (!request.nome || request.nome.trim().length === 0) {
|
||||
throw {
|
||||
success: false,
|
||||
@@ -143,30 +196,48 @@ class PersonalAgent {
|
||||
};
|
||||
}
|
||||
|
||||
// Remove formatação do WhatsApp (apenas números)
|
||||
const whatsappNumbers = request.whatsapp.replace(/\D/g, '');
|
||||
|
||||
try {
|
||||
const axios = (await import('axios')).default;
|
||||
|
||||
// Obtém o token JWT para autenticação
|
||||
const token = await GlobalFunctions.getToken();
|
||||
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> = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
// Adiciona API key (obrigatória)
|
||||
if (apiKey) {
|
||||
headers['apikey'] = apiKey;
|
||||
} else {
|
||||
console.warn('API_KEY não configurada');
|
||||
console.warn('VITE_API_KEY não configurada');
|
||||
}
|
||||
|
||||
// Adiciona token JWT se disponível (pode ser necessário)
|
||||
if (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>(
|
||||
'https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/codex/agente-pessoal/user',
|
||||
this.CREATE_USER_ENDPOINT,
|
||||
{
|
||||
nome: request.nome.trim(),
|
||||
email: request.email.trim(),
|
||||
@@ -178,6 +249,7 @@ class PersonalAgent {
|
||||
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
// Retorna o erro da API se existir
|
||||
if (error.response?.data) {
|
||||
throw error.response.data;
|
||||
}
|
||||
@@ -190,8 +262,15 @@ class PersonalAgent {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
|
||||
// Validações
|
||||
if (!userId || userId.trim().length === 0) {
|
||||
throw {
|
||||
success: false,
|
||||
@@ -213,28 +292,47 @@ class PersonalAgent {
|
||||
};
|
||||
}
|
||||
|
||||
// Remove formatação do WhatsApp (apenas números)
|
||||
const whatsappNumbers = request.whatsapp.replace(/\D/g, '');
|
||||
|
||||
try {
|
||||
const axios = (await import('axios')).default;
|
||||
|
||||
// Obtém o token JWT para autenticação
|
||||
const token = await GlobalFunctions.getToken();
|
||||
const apiKey = import.meta.env.VITE_API_KEY || '';
|
||||
|
||||
// Prepara headers de autenticação
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
// Adiciona API key (obrigatória)
|
||||
if (apiKey) {
|
||||
headers['apikey'] = apiKey;
|
||||
}
|
||||
|
||||
// Adiciona token JWT se disponível
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
const response = await axios.post<UserProfile>(
|
||||
`https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/2299acaf-70ee-47e3-a6fc-56dcc678d651/codex/agente-pessoal/user/${userId}`,
|
||||
console.log('Atualizando usuário:', {
|
||||
url: `${this.UPDATE_USER_ENDPOINT}/${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(),
|
||||
whatsapp: whatsappNumbers,
|
||||
@@ -245,6 +343,7 @@ class PersonalAgent {
|
||||
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
// Retorna o erro da API se existir
|
||||
if (error.response?.data) {
|
||||
throw error.response.data;
|
||||
}
|
||||
@@ -257,6 +356,12 @@ class PersonalAgent {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
const email = userEmail || GlobalFunctions.getUsuarioLogado().email;
|
||||
|
||||
@@ -270,28 +375,33 @@ class PersonalAgent {
|
||||
try {
|
||||
const axios = (await import('axios')).default;
|
||||
|
||||
// Obtém o token JWT para autenticação
|
||||
const token = await GlobalFunctions.getToken();
|
||||
const apiKey = import.meta.env.VITE_API_KEY || '';
|
||||
|
||||
// Prepara headers de autenticação
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
// Adiciona API key (obrigatória)
|
||||
if (apiKey) {
|
||||
headers['apikey'] = apiKey;
|
||||
}
|
||||
|
||||
// Adiciona token JWT se disponível
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
const response = await axios.get<FinancialIndicators>(
|
||||
`https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/2299acaf-70ee-47e3-a6fc-56dcc678d651/codex/agente-pessoal/financas/indicadores/${email}`,
|
||||
`${this.GET_FINANCIAL_INDICATORS_ENDPOINT}/${email}`,
|
||||
{ headers }
|
||||
);
|
||||
|
||||
return response.data;
|
||||
} catch (error: unknown) {
|
||||
// Retorna o erro da API se existir
|
||||
if (error && typeof error === 'object' && 'response' in error) {
|
||||
const axiosError = error as { response?: { data?: FinancialIndicators } };
|
||||
if (axiosError.response?.data) {
|
||||
@@ -309,6 +419,13 @@ class PersonalAgent {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
const email = userEmail || GlobalFunctions.getUsuarioLogado().email;
|
||||
|
||||
@@ -322,17 +439,21 @@ class PersonalAgent {
|
||||
try {
|
||||
const axios = (await import('axios')).default;
|
||||
|
||||
// Obtém o token JWT para autenticação
|
||||
const token = await GlobalFunctions.getToken();
|
||||
const apiKey = import.meta.env.VITE_API_KEY || '';
|
||||
|
||||
// Prepara headers de autenticação
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
// Adiciona API key (obrigatória)
|
||||
if (apiKey) {
|
||||
headers['apikey'] = apiKey;
|
||||
}
|
||||
|
||||
// Adiciona token JWT se disponível
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
@@ -347,10 +468,11 @@ class PersonalAgent {
|
||||
if (filters?.data_final) params.append('data_final', filters.data_final);
|
||||
|
||||
const queryString = params.toString();
|
||||
const url = `https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/2299acaf-70ee-47e3-a6fc-56dcc678d651/codex/agente-pessoal/financas/${email}${queryString ? `?${queryString}` : ''}`;
|
||||
const url = `${this.GET_EXPENSES_ENDPOINT}/${email}${queryString ? `?${queryString}` : ''}`;
|
||||
|
||||
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) {
|
||||
const expensesResponse = response.data[0];
|
||||
|
||||
@@ -406,26 +528,35 @@ class PersonalAgent {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtém a lista de categorias de despesas
|
||||
*
|
||||
* @returns Promise com a lista de categorias
|
||||
*/
|
||||
async getCategories(): Promise<ExpenseCategory[]> {
|
||||
try {
|
||||
const axios = (await import('axios')).default;
|
||||
|
||||
// Obtém o token JWT para autenticação
|
||||
const token = await GlobalFunctions.getToken();
|
||||
const apiKey = import.meta.env.VITE_API_KEY || '';
|
||||
|
||||
// Prepara headers de autenticação
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
// Adiciona API key (obrigatória)
|
||||
if (apiKey) {
|
||||
headers['apikey'] = apiKey;
|
||||
}
|
||||
|
||||
// Adiciona token JWT se disponível
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
const response = await axios.get<ExpenseCategory[]>('https://prod-hgtx-intelligence-n8n.hgtx.com.br/webhook/codex/agente-pessoal/categorias', { headers });
|
||||
const response = await axios.get<ExpenseCategory[]>(this.GET_CATEGORIES_ENDPOINT, { headers });
|
||||
|
||||
// A API retorna um array de categorias
|
||||
if (Array.isArray(response.data)) {
|
||||
@@ -436,9 +567,11 @@ class PersonalAgent {
|
||||
} catch (error: unknown) {
|
||||
console.error('Erro ao buscar categorias:', error);
|
||||
|
||||
// Retorna array vazio em caso de erro para não quebrar a aplicação
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const personalAgent = new PersonalAgent();
|
||||
// Exporta instância única (Singleton)
|
||||
export const userProfileService = new UserProfileService();
|
||||
Reference in New Issue
Block a user