mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 08:58:48 +00:00
aceptando ferro
This commit is contained in:
@ -420,12 +420,12 @@ public class skApiClient {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode root = mapper.readTree(jsonResponse);
|
||||
|
||||
if (root.get("data") == null || !root.get("data").isInt()) {
|
||||
if (root.get("data") == null) {
|
||||
throw new RuntimeException(
|
||||
"Sin respuesta desde el servidor del proveedor");
|
||||
}
|
||||
|
||||
String estado = root.get("estado").asText();
|
||||
String estado = root.get("data").asText();
|
||||
return Map.of(
|
||||
"estado", estado);
|
||||
|
||||
@ -436,6 +436,146 @@ public class skApiClient {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Object> getFilesTypes(Long presupuestoId, Locale locale) {
|
||||
|
||||
try {
|
||||
|
||||
Map<String, Object> result = performWithRetryMap(() -> {
|
||||
String url = this.skApiUrl + "api/files-presupuesto/" + presupuestoId;
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.setBearerAuth(authService.getToken()); // token actualizado
|
||||
|
||||
HttpEntity<Void> entity = new HttpEntity<>(headers);
|
||||
|
||||
ResponseEntity<String> response = restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.GET,
|
||||
entity,
|
||||
String.class);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
try {
|
||||
Map<String, Object> responseBody = mapper.readValue(
|
||||
response.getBody(),
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
|
||||
// Si la API devuelve "error" a nivel raíz
|
||||
if (responseBody.get("error") != null) {
|
||||
// Devolvemos un mapa con sólo el error para que el caller decida
|
||||
return Map.of("error", responseBody.get("error"));
|
||||
}
|
||||
|
||||
Boolean hasError = (Boolean) (responseBody.get("error") == null
|
||||
|| responseBody.get("error") == "null" ? false : true);
|
||||
Map<String, Boolean> files = (Map<String, Boolean>) responseBody.get("data");
|
||||
|
||||
if (files != null && !hasError) {
|
||||
return Map.of("data", files);
|
||||
} else {
|
||||
// Tu lógica actual: si success es true u otra cosa → error 2
|
||||
return Map.of("error", 2);
|
||||
}
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
return Map.of("error", 1);
|
||||
}
|
||||
});
|
||||
|
||||
if (result.get("error") != null) {
|
||||
throw new RuntimeException(
|
||||
messageSource.getMessage("pedido.errors.connecting-server-error", null, locale));
|
||||
}
|
||||
Map<String, Object> data = (Map<String, Object>) result.get("data");
|
||||
return data;
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
throw new RuntimeException(
|
||||
messageSource.getMessage("pedido.errors.connecting-server-error", null, locale));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public byte[] downloadFile(Long presupuestoId, String fileType, Locale locale) {
|
||||
return performWithRetryBytes(() -> {
|
||||
|
||||
String normalized = (fileType == null) ? "" : fileType.trim().toLowerCase();
|
||||
|
||||
String endpoint = switch (normalized) {
|
||||
case "ferro" -> "api/get-ferro/" + presupuestoId;
|
||||
case "cubierta" -> "api/get-cubierta/" + presupuestoId;
|
||||
case "tapa" -> "api/get-tapa/" + presupuestoId;
|
||||
default -> throw new IllegalArgumentException("Tipo de fichero no soportado: " + fileType);
|
||||
};
|
||||
|
||||
// OJO: skApiUrl debería terminar en "/" para que concatene bien
|
||||
String url = this.skApiUrl + endpoint;
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
// Si tu CI4 requiere Bearer, mantenlo. Si NO lo requiere, puedes quitar esta
|
||||
// línea.
|
||||
headers.setBearerAuth(authService.getToken());
|
||||
headers.setAccept(List.of(MediaType.APPLICATION_PDF, MediaType.APPLICATION_OCTET_STREAM));
|
||||
|
||||
try {
|
||||
ResponseEntity<byte[]> response = restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.GET,
|
||||
new HttpEntity<>(headers),
|
||||
byte[].class);
|
||||
|
||||
if (response.getStatusCode().is2xxSuccessful()) {
|
||||
return response.getBody(); // bytes del PDF
|
||||
}
|
||||
return null;
|
||||
|
||||
} catch (HttpClientErrorException.NotFound e) {
|
||||
// CI4 no tiene ese fichero
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Boolean aceptarFerro(Long presupuestoId, Locale locale) {
|
||||
|
||||
String result = performWithRetry(() -> {
|
||||
String url = this.skApiUrl + "api/aceptar-ferro/" + presupuestoId;
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.setBearerAuth(authService.getToken());
|
||||
|
||||
HttpEntity<Void> entity = new HttpEntity<>(headers);
|
||||
|
||||
ResponseEntity<String> response = restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.POST,
|
||||
entity,
|
||||
String.class);
|
||||
|
||||
try {
|
||||
Map<String, Object> responseBody = new ObjectMapper().readValue(
|
||||
response.getBody(),
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
|
||||
Boolean success = (Boolean) (responseBody.get("success") != null ? responseBody.get("success") : false);
|
||||
|
||||
return success.toString();
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
return "false"; // Fallback en caso de error
|
||||
}
|
||||
});
|
||||
return Boolean.parseBoolean(result);
|
||||
}
|
||||
|
||||
/******************
|
||||
* PRIVATE METHODS
|
||||
******************/
|
||||
@ -467,6 +607,19 @@ public class skApiClient {
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] performWithRetryBytes(Supplier<byte[]> request) {
|
||||
try {
|
||||
return request.get();
|
||||
} catch (HttpClientErrorException.Unauthorized e) {
|
||||
authService.invalidateToken();
|
||||
try {
|
||||
return request.get();
|
||||
} catch (HttpClientErrorException ex) {
|
||||
throw new RuntimeException("La autenticación ha fallado tras renovar el token.", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static BigDecimal calcularMargen(
|
||||
BigDecimal importe, BigDecimal importeMin, BigDecimal importeMax,
|
||||
BigDecimal margenMax, BigDecimal margenMin) {
|
||||
|
||||
Reference in New Issue
Block a user