- App Mi día: KPIs, mis actividades, mis leads con acciones rápidas (contactado/intento/agenda cita/descartar con motivo), captura rápida de lead (409 en duplicado por teléfono), vista equipo para coordinadores - API: GET /api/mi-dia/resumen, POST /api/leads, /leads/:id/estado, /leads/:id/descartar; mapeo Usuario.mondayUserId (seed listo para cuando inviten a los vendedores a Monday) - Landing: tarjeta Mi día; tests api 188, mi-dia 8, landing 4
55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
// Cliente del API para la app "Mi día" (mismas convenciones que las demás apps)
|
|
import type { ResumenMiDia } from '../lib/mi-dia'
|
|
import { conAuth, manejar401 } from './auth'
|
|
|
|
const API_URL = import.meta.env.VITE_API_URL ?? 'http://192.168.10.123:3000/api'
|
|
|
|
async function fetchJson<T>(path: string, init: RequestInit = {}): Promise<T> {
|
|
const res = await fetch(`${API_URL}${path}`, conAuth(init))
|
|
manejar401(res)
|
|
if (!res.ok) {
|
|
const body = (await res.json().catch(() => null)) as { error?: string } | null
|
|
throw new Error(body?.error ?? `Error ${res.status}`)
|
|
}
|
|
return (await res.json()) as T
|
|
}
|
|
|
|
export function fetchResumenMiDia(): Promise<ResumenMiDia> {
|
|
return fetchJson<ResumenMiDia>('/mi-dia/resumen')
|
|
}
|
|
|
|
export interface LeadCreado {
|
|
id: string
|
|
estado: string
|
|
}
|
|
|
|
/** Crea un lead; en duplicado (409) lanza Error con el mensaje del API */
|
|
export async function crearLead(datos: {
|
|
nombre: string
|
|
telefono?: string
|
|
origen?: string
|
|
notas?: string
|
|
}): Promise<LeadCreado> {
|
|
return fetchJson<LeadCreado>('/leads', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(datos),
|
|
})
|
|
}
|
|
|
|
export function cambiarEstadoLead(itemId: string, estado: string, fecha?: string): Promise<unknown> {
|
|
return fetchJson(`/leads/${itemId}/estado`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(fecha !== undefined && fecha !== '' ? { estado, fecha } : { estado }),
|
|
})
|
|
}
|
|
|
|
export function descartarLead(itemId: string, motivo: string, comentario?: string): Promise<unknown> {
|
|
return fetchJson(`/leads/${itemId}/descartar`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(comentario !== undefined && comentario !== '' ? { motivo, comentario } : { motivo }),
|
|
})
|
|
}
|