Files
as-talange/as-talange-web/src/main/java/com/astalange/web/controller/AdminPreInscriptionController.java
T

77 lines
3.7 KiB
Java

package com.astalange.web.controller;
import com.astalange.core.entity.Adherent;
import com.astalange.core.repository.PreInscriptionRepository;
import com.astalange.core.service.PreInscriptionService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.astalange.core.entity.PreInscription;
import com.astalange.core.service.RelanceEmailService;
@Controller
@RequestMapping("/admin/pre-inscriptions")
public class AdminPreInscriptionController {
private final PreInscriptionRepository preInscriptionRepository;
private final PreInscriptionService preInscriptionService;
private final RelanceEmailService relanceEmailService;
public AdminPreInscriptionController(PreInscriptionRepository preInscriptionRepository,
PreInscriptionService preInscriptionService,
RelanceEmailService relanceEmailService) {
this.preInscriptionRepository = preInscriptionRepository;
this.preInscriptionService = preInscriptionService;
this.relanceEmailService = relanceEmailService;
}
@GetMapping
public String listPreInscriptions(Model model) {
model.addAttribute("preInscriptions", preInscriptionRepository.findByStatutTraiteFalseOrderByDateCreationDesc());
return "preinscriptions/list";
}
@PostMapping("/{id}/valider")
@ResponseBody
public String valider(@PathVariable Long id, HttpServletResponse response) {
Adherent adherent = preInscriptionService.validerPreInscription(id, null);
response.setHeader("HX-Trigger", "refreshBadge");
// En HTMX, on renvoie une ligne vide pour la faire disparaître, ou on redirige via HX-Redirect
// Mais comme on veut aller sur la page de l'adhérent pour finaliser la licence :
response.setHeader("HX-Redirect", "/adherents/" + adherent.getId() + "/edit");
return "";
}
@PostMapping("/{id}/rejeter")
@ResponseBody
public String rejeter(@PathVariable Long id, HttpServletResponse response) {
preInscriptionService.rejeterPreInscription(id);
response.setHeader("HX-Trigger", "refreshBadge");
return ""; // HTMX target la ligne avec outerHTML -> supprime la ligne
}
@PostMapping("/{id}/relancer")
@ResponseBody
public String relancer(@PathVariable Long id) {
PreInscription pre = preInscriptionRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Pré-inscription introuvable : " + id));
boolean sent = relanceEmailService.sendRelancePreInscription(pre);
if (sent) {
return "<span class=\"text-xs text-amber-700 font-medium bg-amber-50 px-2.5 py-1 rounded-full border border-amber-200 inline-flex items-center gap-1\"><svg class=\"w-3.5 h-3.5\" fill=\"none\" stroke=\"currentColor\" viewBox=\"0 0 24 24\"><path stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 002-2H5a2 2 0 00-2 2v10a2 2 0 002 2z\"/></svg> Relance envoyée</span>";
} else {
return "<span class=\"text-xs text-red-600 font-medium bg-red-50 px-2.5 py-1 rounded-full border border-red-200\">Erreur (sans email)</span>";
}
}
@GetMapping("/count")
@ResponseBody
public String countBadge() {
long count = preInscriptionRepository.countByStatutTraiteFalse();
if (count == 0) return "";
return "<span class=\"bg-red-500 text-white rounded-full px-2 py-0.5 text-xs font-bold ml-2\">" + count + "</span>";
}
}