This commit is contained in:
2026-02-08 12:44:17 +01:00
parent 2e569a7ffd
commit d0ccfb5626
4 changed files with 170 additions and 1 deletions

View File

@ -513,6 +513,102 @@ public class PresupuestoController {
return ResponseEntity.ok(resumen);
}
@PostMapping("/public/prepare-claim")
public ResponseEntity<?> prepareClaim(
@RequestBody Map<String, Object> body,
HttpServletRequest request) {
Long presupuestoId = objectMapper.convertValue(body.get("presupuestoId"), Long.class);
if (presupuestoId == null) {
return ResponseEntity.badRequest().body(Map.of("message", "missing presupuestoId"));
}
Presupuesto p = presupuestoRepository.findById(presupuestoId).orElse(null);
if (p == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(Map.of("message", "presupuesto not found"));
}
if (p.getOrigen() != Presupuesto.Origen.publico) {
return ResponseEntity.badRequest().body(Map.of("message", "presupuesto not public"));
}
request.getSession(true).setAttribute("presupuesto_claim_id", presupuestoId);
return ResponseEntity.ok(Map.of("success", true));
}
@GetMapping("/claim")
@Transactional
public String claimPresupuesto(
HttpServletRequest request,
Authentication authentication,
RedirectAttributes redirectAttributes,
Locale locale) {
Object attr = request.getSession(false) != null
? request.getSession(false).getAttribute("presupuesto_claim_id")
: null;
Long presupuestoId = null;
if (attr instanceof Long) {
presupuestoId = (Long) attr;
} else if (attr != null) {
try {
presupuestoId = Long.valueOf(attr.toString());
} catch (NumberFormatException ignore) {
}
}
if (presupuestoId == null) {
redirectAttributes.addFlashAttribute("errorMessage",
messageSource.getMessage("presupuesto.errores.presupuesto-no-existe", new Object[] { 0 }, locale));
return "redirect:/presupuesto";
}
Presupuesto p = presupuestoRepository.findById(presupuestoId).orElse(null);
if (p == null) {
redirectAttributes.addFlashAttribute("errorMessage",
messageSource.getMessage("presupuesto.errores.presupuesto-no-existe",
new Object[] { presupuestoId }, locale));
return "redirect:/presupuesto";
}
if (p.getUser() != null && authentication != null) {
Long currentUserId = null;
if (authentication.getPrincipal() instanceof UserDetailsImpl udi) {
currentUserId = udi.getId();
} else {
currentUserId = userRepo.findIdByUserNameIgnoreCase(authentication.getName()).orElse(null);
}
if (currentUserId != null && p.getUser().getId().equals(currentUserId)) {
request.getSession().removeAttribute("presupuesto_claim_id");
return "redirect:/presupuesto/edit/" + p.getId();
}
}
if (p.getOrigen() != Presupuesto.Origen.publico) {
redirectAttributes.addFlashAttribute("errorMessage",
messageSource.getMessage("presupuesto.errores.presupuesto-no-existe",
new Object[] { presupuestoId }, locale));
return "redirect:/presupuesto";
}
if (authentication != null) {
if (authentication.getPrincipal() instanceof UserDetailsImpl udi) {
p.setUser(userRepo.getReferenceById(udi.getId()));
} else {
userRepo.findByUserNameIgnoreCase(authentication.getName()).ifPresent(p::setUser);
}
}
p.setOrigen(Presupuesto.Origen.privado);
p.setEstado(Presupuesto.Estado.borrador);
presupuestoRepository.saveAndFlush(p);
request.getSession().removeAttribute("presupuesto_claim_id");
return "redirect:/presupuesto/edit/" + p.getId();
}
// =============================================
// MÉTODOS PARA USUARIOS AUTENTICADOS
// =============================================