From 74b91d0c8a912c1e7aeb7ef9b7e46ef9ee9b94f5 Mon Sep 17 00:00:00 2001 From: enlineawork Date: Thu, 10 Sep 2026 21:33:01 -0300 Subject: [PATCH] feat(web): load preventive inventory from frozen scope --- .../inspections/preventiveCandidatesApi.ts | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 web-v2/src/features/inspections/preventiveCandidatesApi.ts diff --git a/web-v2/src/features/inspections/preventiveCandidatesApi.ts b/web-v2/src/features/inspections/preventiveCandidatesApi.ts new file mode 100644 index 0000000..42257ba --- /dev/null +++ b/web-v2/src/features/inspections/preventiveCandidatesApi.ts @@ -0,0 +1,43 @@ +export interface InspectionPreventiveCandidate { + id: string; + code: string; + name: string; + typeName: string; +} + +interface PreventiveCandidateResponse { + data: InspectionPreventiveCandidate[]; +} + +export async function listInspectionPreventiveCandidates( + visitId: string, + search = '', +): Promise { + const query = new URLSearchParams(); + const term = search.trim(); + if (term) query.set('search', term); + + const response = await fetch( + `/api/v3/inspection-visits/${visitId}/preventive-candidates${query.size ? `?${query}` : ''}`, + { + method: 'GET', + credentials: 'same-origin', + headers: { Accept: 'application/json' }, + }, + ); + + if (!response.ok) { + let message = 'No se pudo cargar el Inventario disponible para esta Inspección'; + try { + const payload = await response.json() as { message?: string | string[] }; + if (Array.isArray(payload.message)) message = payload.message.join('. '); + else if (payload.message) message = payload.message; + } catch { + // Conserva el mensaje funcional cuando la respuesta no sea JSON. + } + throw new Error(message); + } + + const payload = await response.json() as PreventiveCandidateResponse; + return payload.data ?? []; +}