mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-02-10 21:09:12 +00:00
terminado
This commit is contained in:
@ -398,13 +398,12 @@ public class skApiClient {
|
||||
public Map<String, Object> checkPedidoEstado(Long presupuestoId, Locale locale) {
|
||||
|
||||
try {
|
||||
|
||||
String jsonResponse = performWithRetry(() -> {
|
||||
String url = this.skApiUrl + "api/estado-pedido/" + presupuestoId;
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.setBearerAuth(authService.getToken()); // token actualizado
|
||||
headers.setBearerAuth(authService.getToken());
|
||||
headers.setAccept(java.util.List.of(MediaType.APPLICATION_JSON));
|
||||
|
||||
HttpEntity<Void> entity = new HttpEntity<>(headers);
|
||||
|
||||
@ -420,19 +419,34 @@ public class skApiClient {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode root = mapper.readTree(jsonResponse);
|
||||
|
||||
if (root.get("data") == null) {
|
||||
throw new RuntimeException(
|
||||
"Sin respuesta desde el servidor del proveedor");
|
||||
// ✅ Si falta data, devolvemos mapa sin "estado" (o con estado=null pero con
|
||||
// HashMap)
|
||||
if (root == null || root.get("data") == null || root.get("data").isNull()) {
|
||||
Map<String, Object> out = new HashMap<>();
|
||||
out.put("message", "Respuesta sin campo 'data' desde el servidor del proveedor");
|
||||
return out;
|
||||
}
|
||||
|
||||
String estado = root.get("data").asText();
|
||||
return Map.of(
|
||||
"estado", estado);
|
||||
return Map.of("estado", estado); // aquí NO es null, así que Map.of OK
|
||||
|
||||
} catch (HttpClientErrorException ex) {
|
||||
|
||||
if (ex.getStatusCode() == HttpStatus.NOT_FOUND) {
|
||||
// ✅ 404: devolvemos mapa sin "estado" para evitar null en Map.of
|
||||
Map<String, Object> out = new HashMap<>();
|
||||
out.put("notFound", true);
|
||||
out.put("message", "Orden de trabajo no encontrada para presupuestoId=" + presupuestoId);
|
||||
return out;
|
||||
}
|
||||
|
||||
throw ex;
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
// Fallback al 80% del ancho
|
||||
return Map.of(
|
||||
"estado", null);
|
||||
// ✅ no parseable (HTML, debugbar, etc.)
|
||||
Map<String, Object> out = new HashMap<>();
|
||||
out.put("message", "Respuesta no-JSON o JSON inválido desde el proveedor");
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
@ -542,7 +556,7 @@ public class skApiClient {
|
||||
}
|
||||
|
||||
public Boolean aceptarFerro(Long presupuestoId, Locale locale) {
|
||||
|
||||
|
||||
String result = performWithRetry(() -> {
|
||||
String url = this.skApiUrl + "api/aceptar-ferro/" + presupuestoId;
|
||||
|
||||
@ -576,9 +590,8 @@ public class skApiClient {
|
||||
return Boolean.parseBoolean(result);
|
||||
}
|
||||
|
||||
|
||||
public Boolean cancelarPedido(Long pedidoId) {
|
||||
|
||||
|
||||
String result = performWithRetry(() -> {
|
||||
String url = this.skApiUrl + "api/cancelar-pedido/" + pedidoId;
|
||||
|
||||
@ -618,12 +631,21 @@ public class skApiClient {
|
||||
private String performWithRetry(Supplier<String> request) {
|
||||
try {
|
||||
return request.get();
|
||||
|
||||
} catch (HttpClientErrorException.Unauthorized e) {
|
||||
// Token expirado, renovar y reintentar
|
||||
authService.invalidateToken();
|
||||
|
||||
try {
|
||||
return request.get(); // segundo intento
|
||||
|
||||
} catch (HttpClientErrorException ex) {
|
||||
// ✅ IMPORTANTe: si el segundo intento es 404, NO lo envuelvas
|
||||
if (ex.getStatusCode() == HttpStatus.NOT_FOUND) {
|
||||
throw ex;
|
||||
}
|
||||
|
||||
// Si es otro 4xx/5xx, sí lo envolvemos
|
||||
throw new RuntimeException("La autenticación ha fallado tras renovar el token.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user