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 ?? []; +}