import { API_URL, conAuth, manejar401, type UsuarioSesion } from './auth' export interface UsuarioMonday { id: number nombre: string email: string } /** GET /auth/me — perfil del usuario autenticado (incluye mondayUserId y modulos) */ export async function getMe(): Promise { const resp = await fetch(`${API_URL}/auth/me`, conAuth()) manejar401(resp) if (!resp.ok) throw new Error(`HTTP ${resp.status}`) return (await resp.json()) as UsuarioSesion } export interface Contacto { id: string nombre: string telefono: string email: string estado: string updatedAt: string } /** GET /contactos — contactos del tablero Contactos de Monday (máx 100) */ export async function getContactos(): Promise { const resp = await fetch(`${API_URL}/contactos`, conAuth()) manejar401(resp) if (!resp.ok) throw new Error(`HTTP ${resp.status}`) return (await resp.json()) as Contacto[] } /** GET /monday/usuarios — usuarios activos del workspace para el dropdown */ export async function getUsuariosMonday(): Promise { const resp = await fetch(`${API_URL}/monday/usuarios`, conAuth()) manejar401(resp) if (!resp.ok) throw new Error(`HTTP ${resp.status}`) return (await resp.json()) as UsuarioMonday[] } /** PATCH /usuarios/me/monday — vincula (number) o desvincula (null) la cuenta Monday */ export async function guardarVinculoMonday(mondayUserId: number | null): Promise { const resp = await fetch( `${API_URL}/usuarios/me/monday`, conAuth({ method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ mondayUserId }), }), ) manejar401(resp) if (!resp.ok) throw new Error(`HTTP ${resp.status}`) return (await resp.json()) as UsuarioSesion } // ---------- Detalle de contacto (modal del módulo Contactos) ---------- export interface RelacionCrm { id: string nombre: string estado: string /** Quién lo atiende ahora (columna people del tablero); null si sin asignar */ atendidoPor: string | null detalle: string } export interface EventoHistorialContacto { fechaISO: string usuario: string | null detalle: string fuente: string // 'Contactos' | 'Leads' | 'Oportunidades' | 'Apartado' | 'App' } export interface DetalleContacto { /** Columnas del board por título ('nombre' incluido); '' si vacía */ contacto: Record propiedad: { id: string unidadCodigo: string desarrollo: string estado: string precioFinal: number fechaCompra: string fechaLimite: string atendidoPor: string | null } | null relaciones: { leads: RelacionCrm[] oportunidades: RelacionCrm[] apartados: RelacionCrm[] } historial: EventoHistorialContacto[] } /** GET /contactos/:id/detalle — ficha completa: info, propiedad, quién atiende e historial */ export async function getContactoDetalle(id: string): Promise { const resp = await fetch(`${API_URL}/contactos/${id}/detalle`, conAuth()) manejar401(resp) if (!resp.ok) throw new Error(`HTTP ${resp.status}`) return (await resp.json()) as DetalleContacto }