feat: API Gateway (Express 5 + Prisma + Monday) y conexion de apps al API
- api-gateway: servicio Monday (retry 429/complejidad, paginacion cursor, column IDs por titulo), endpoints unidades/cotizaciones/clientes/planes, webhook con handshake challenge, Prisma SQLite dev, 55 tests - cotizador: inventario desde /api/unidades con fallback a seed - alta-clientes: POST /api/clientes + upload multipart de documentos - cobranza: persistencia via API con respaldo offline en localStorage - .env.example con board IDs del workspace Frasa-CAS
This commit is contained in:
67
apps/cotizador/src/services/api.test.ts
Normal file
67
apps/cotizador/src/services/api.test.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest'
|
||||
import { getUnidades } from './api'
|
||||
import { UNIDADES_SEED } from '../data/unidades'
|
||||
import type { Unidad } from '../types'
|
||||
|
||||
const unidadApi: Unidad = {
|
||||
codigo: 'U-101',
|
||||
modelo: '2RA',
|
||||
fechaEntrega: '2027-10-08',
|
||||
supEstacionamiento: 28.62,
|
||||
supConstruccion: 69.485,
|
||||
supExcedente: 21.336,
|
||||
precioSinAcabados: 4415000,
|
||||
precioConAcabados: 4579000,
|
||||
status: 'DISPONIBLE',
|
||||
desarrollo: 'DIAGONAL',
|
||||
}
|
||||
|
||||
function respuestaOk(body: unknown): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('getUnidades', () => {
|
||||
it('devuelve las unidades del API cuando la respuesta es OK', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue(respuestaOk([unidadApi]))
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
|
||||
const resultado = await getUnidades()
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledWith('http://192.168.10.123:3000/api/unidades')
|
||||
expect(resultado.usandoSeed).toBe(false)
|
||||
expect(resultado.unidades).toEqual([unidadApi])
|
||||
})
|
||||
|
||||
it('hace fallback al seed local cuando la respuesta no es OK', async () => {
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn().mockResolvedValue(new Response('error', { status: 500 })),
|
||||
)
|
||||
|
||||
const resultado = await getUnidades()
|
||||
|
||||
expect(resultado.usandoSeed).toBe(true)
|
||||
expect(resultado.unidades).toEqual(UNIDADES_SEED)
|
||||
expect(warn).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('hace fallback al seed local cuando falla la red', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new TypeError('fetch failed')))
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
||||
|
||||
const resultado = await getUnidades()
|
||||
|
||||
expect(resultado.usandoSeed).toBe(true)
|
||||
expect(resultado.unidades).toEqual(UNIDADES_SEED)
|
||||
expect(warn).toHaveBeenCalledOnce()
|
||||
})
|
||||
})
|
||||
@@ -1,10 +1,28 @@
|
||||
import type { Unidad } from '../types'
|
||||
import { UNIDADES_SEED } from '../data/unidades'
|
||||
|
||||
// TODO produccion: '/api' same-origin detras de NGINX
|
||||
const API_URL = import.meta.env.VITE_API_URL ?? 'http://192.168.10.123:3000/api'
|
||||
|
||||
export interface GetUnidadesResult {
|
||||
unidades: Unidad[]
|
||||
/** true cuando se usó el seed local por no poder contactar el API */
|
||||
usandoSeed: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Devuelve el inventario de unidades.
|
||||
* TODO: reemplazar por fetch('/api/unidades') cuando el API Gateway esté disponible
|
||||
* Intenta GET {API_URL}/unidades; si falla (red o respuesta no OK),
|
||||
* hace fallback al seed local con console.warn.
|
||||
*/
|
||||
export async function getUnidades(): Promise<Unidad[]> {
|
||||
return UNIDADES_SEED
|
||||
export async function getUnidades(): Promise<GetUnidadesResult> {
|
||||
try {
|
||||
const resp = await fetch(`${API_URL}/unidades`)
|
||||
if (!resp.ok) throw new Error(`HTTP ${resp.status}`)
|
||||
const unidades = (await resp.json()) as Unidad[]
|
||||
return { unidades, usandoSeed: false }
|
||||
} catch (err) {
|
||||
console.warn('[api] No se pudo obtener el inventario del API; usando seed local.', err)
|
||||
return { unidades: UNIDADES_SEED, usandoSeed: true }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user