44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
export interface InspectionPreventiveCandidate {
|
|
id: string;
|
|
code: string;
|
|
name: string;
|
|
typeName: string;
|
|
}
|
|
|
|
interface PreventiveCandidateResponse {
|
|
data: InspectionPreventiveCandidate[];
|
|
}
|
|
|
|
export async function listInspectionPreventiveCandidates(
|
|
visitId: string,
|
|
search = '',
|
|
): Promise<InspectionPreventiveCandidate[]> {
|
|
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 ?? [];
|
|
}
|