Sync: code local unifié
Synchronisation du code source local (fait foi). Signed-off-by: lions dev Team
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
package dev.lions.unionflow.server.service;
|
||||
|
||||
import dev.lions.unionflow.server.api.dto.notification.NotificationDTO;
|
||||
import dev.lions.unionflow.server.api.dto.notification.TemplateNotificationDTO;
|
||||
import dev.lions.unionflow.server.api.enums.notification.PrioriteNotification;
|
||||
import dev.lions.unionflow.server.api.enums.notification.StatutNotification;
|
||||
import dev.lions.unionflow.server.api.dto.notification.request.CreateNotificationRequest;
|
||||
import dev.lions.unionflow.server.api.dto.notification.request.CreateTemplateNotificationRequest;
|
||||
import dev.lions.unionflow.server.api.dto.notification.response.NotificationResponse;
|
||||
import dev.lions.unionflow.server.api.dto.notification.response.TemplateNotificationResponse;
|
||||
|
||||
import dev.lions.unionflow.server.entity.*;
|
||||
import dev.lions.unionflow.server.repository.*;
|
||||
import dev.lions.unionflow.server.service.KeycloakService;
|
||||
@@ -13,6 +14,9 @@ import jakarta.transaction.Transactional;
|
||||
import jakarta.ws.rs.NotFoundException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import io.quarkus.mailer.Mail;
|
||||
import io.quarkus.mailer.Mailer;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import org.jboss.logging.Logger;
|
||||
@@ -29,15 +33,23 @@ public class NotificationService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(NotificationService.class);
|
||||
|
||||
@Inject NotificationRepository notificationRepository;
|
||||
@Inject
|
||||
NotificationRepository notificationRepository;
|
||||
|
||||
@Inject TemplateNotificationRepository templateNotificationRepository;
|
||||
@Inject
|
||||
TemplateNotificationRepository templateNotificationRepository;
|
||||
|
||||
@Inject MembreRepository membreRepository;
|
||||
@Inject
|
||||
MembreRepository membreRepository;
|
||||
|
||||
@Inject OrganisationRepository organisationRepository;
|
||||
@Inject
|
||||
OrganisationRepository organisationRepository;
|
||||
|
||||
@Inject KeycloakService keycloakService;
|
||||
@Inject
|
||||
Mailer mailer;
|
||||
|
||||
@Inject
|
||||
KeycloakService keycloakService;
|
||||
|
||||
/**
|
||||
* Crée un nouveau template de notification
|
||||
@@ -46,15 +58,15 @@ public class NotificationService {
|
||||
* @return DTO du template créé
|
||||
*/
|
||||
@Transactional
|
||||
public TemplateNotificationDTO creerTemplate(TemplateNotificationDTO templateDTO) {
|
||||
LOG.infof("Création d'un nouveau template: %s", templateDTO.getCode());
|
||||
public TemplateNotificationResponse creerTemplate(CreateTemplateNotificationRequest request) {
|
||||
LOG.infof("Création d'un nouveau template: %s", request.code());
|
||||
|
||||
// Vérifier l'unicité du code
|
||||
if (templateNotificationRepository.findByCode(templateDTO.getCode()).isPresent()) {
|
||||
throw new IllegalArgumentException("Un template avec ce code existe déjà: " + templateDTO.getCode());
|
||||
if (templateNotificationRepository.findByCode(request.code()).isPresent()) {
|
||||
throw new IllegalArgumentException("Un template avec ce code existe déjà: " + request.code());
|
||||
}
|
||||
|
||||
TemplateNotification template = convertToEntity(templateDTO);
|
||||
TemplateNotification template = convertToEntity(request);
|
||||
template.setCreePar(keycloakService.getCurrentUserEmail());
|
||||
|
||||
templateNotificationRepository.persist(template);
|
||||
@@ -70,15 +82,26 @@ public class NotificationService {
|
||||
* @return DTO de la notification créée
|
||||
*/
|
||||
@Transactional
|
||||
public NotificationDTO creerNotification(NotificationDTO notificationDTO) {
|
||||
LOG.infof("Création d'une nouvelle notification: %s", notificationDTO.getTypeNotification());
|
||||
public NotificationResponse creerNotification(CreateNotificationRequest request) {
|
||||
LOG.infof("Création d'une nouvelle notification: %s", request.typeNotification());
|
||||
|
||||
Notification notification = convertToEntity(notificationDTO);
|
||||
Notification notification = convertToEntity(request);
|
||||
notification.setCreePar(keycloakService.getCurrentUserEmail());
|
||||
|
||||
notificationRepository.persist(notification);
|
||||
LOG.infof("Notification créée avec succès: ID=%s", notification.getId());
|
||||
|
||||
// Envoi immédiat si type EMAIL
|
||||
if ("EMAIL".equals(notification.getTypeNotification())) {
|
||||
try {
|
||||
envoyerEmail(notification);
|
||||
} catch (Exception e) {
|
||||
LOG.errorf("Erreur lors de l'envoi de l'email pour la notification %s: %s", notification.getId(),
|
||||
e.getMessage());
|
||||
// On ne relance pas l'exception pour ne pas bloquer la transaction de création
|
||||
}
|
||||
}
|
||||
|
||||
return convertToDTO(notification);
|
||||
}
|
||||
|
||||
@@ -89,15 +112,14 @@ public class NotificationService {
|
||||
* @return DTO de la notification mise à jour
|
||||
*/
|
||||
@Transactional
|
||||
public NotificationDTO marquerCommeLue(UUID id) {
|
||||
public NotificationResponse marquerCommeLue(UUID id) {
|
||||
LOG.infof("Marquage de la notification comme lue: ID=%s", id);
|
||||
|
||||
Notification notification =
|
||||
notificationRepository
|
||||
.findNotificationById(id)
|
||||
.orElseThrow(() -> new NotFoundException("Notification non trouvée avec l'ID: " + id));
|
||||
Notification notification = notificationRepository
|
||||
.findNotificationById(id)
|
||||
.orElseThrow(() -> new NotFoundException("Notification non trouvée avec l'ID: " + id));
|
||||
|
||||
notification.setStatut(StatutNotification.LUE);
|
||||
notification.setStatut("LUE");
|
||||
notification.setDateLecture(LocalDateTime.now());
|
||||
notification.setModifiePar(keycloakService.getCurrentUserEmail());
|
||||
|
||||
@@ -113,7 +135,7 @@ public class NotificationService {
|
||||
* @param id ID de la notification
|
||||
* @return DTO de la notification
|
||||
*/
|
||||
public NotificationDTO trouverNotificationParId(UUID id) {
|
||||
public NotificationResponse trouverNotificationParId(UUID id) {
|
||||
return notificationRepository
|
||||
.findNotificationById(id)
|
||||
.map(this::convertToDTO)
|
||||
@@ -126,7 +148,7 @@ public class NotificationService {
|
||||
* @param membreId ID du membre
|
||||
* @return Liste des notifications
|
||||
*/
|
||||
public List<NotificationDTO> listerNotificationsParMembre(UUID membreId) {
|
||||
public List<NotificationResponse> listerNotificationsParMembre(UUID membreId) {
|
||||
return notificationRepository.findByMembreId(membreId).stream()
|
||||
.map(this::convertToDTO)
|
||||
.collect(Collectors.toList());
|
||||
@@ -138,7 +160,7 @@ public class NotificationService {
|
||||
* @param membreId ID du membre
|
||||
* @return Liste des notifications non lues
|
||||
*/
|
||||
public List<NotificationDTO> listerNotificationsNonLuesParMembre(UUID membreId) {
|
||||
public List<NotificationResponse> listerNotificationsNonLuesParMembre(UUID membreId) {
|
||||
return notificationRepository.findNonLuesByMembreId(membreId).stream()
|
||||
.map(this::convertToDTO)
|
||||
.collect(Collectors.toList());
|
||||
@@ -149,7 +171,7 @@ public class NotificationService {
|
||||
*
|
||||
* @return Liste des notifications en attente
|
||||
*/
|
||||
public List<NotificationDTO> listerNotificationsEnAttenteEnvoi() {
|
||||
public List<NotificationResponse> listerNotificationsEnAttenteEnvoi() {
|
||||
return notificationRepository.findEnAttenteEnvoi().stream()
|
||||
.map(this::convertToDTO)
|
||||
.collect(Collectors.toList());
|
||||
@@ -159,9 +181,9 @@ public class NotificationService {
|
||||
* Envoie des notifications groupées à plusieurs membres (WOU/DRY)
|
||||
*
|
||||
* @param membreIds Liste des IDs des membres destinataires
|
||||
* @param sujet Sujet de la notification
|
||||
* @param corps Corps du message
|
||||
* @param canaux Canaux d'envoi (EMAIL, SMS, etc.)
|
||||
* @param sujet Sujet de la notification
|
||||
* @param corps Corps du message
|
||||
* @param canaux Canaux d'envoi (EMAIL, SMS, etc.)
|
||||
* @return Nombre de notifications créées
|
||||
*/
|
||||
@Transactional
|
||||
@@ -177,27 +199,43 @@ public class NotificationService {
|
||||
int notificationsCreees = 0;
|
||||
for (UUID membreId : membreIds) {
|
||||
try {
|
||||
Membre membre =
|
||||
membreRepository
|
||||
.findByIdOptional(membreId)
|
||||
.orElseThrow(
|
||||
() ->
|
||||
new IllegalArgumentException(
|
||||
"Membre non trouvé avec l'ID: " + membreId));
|
||||
Membre membre = membreRepository
|
||||
.findByIdOptional(membreId)
|
||||
.orElseThrow(
|
||||
() -> new IllegalArgumentException(
|
||||
"Membre non trouvé avec l'ID: " + membreId));
|
||||
|
||||
Notification notification = new Notification();
|
||||
notification.setMembre(membre);
|
||||
notification.setSujet(sujet);
|
||||
notification.setCorps(corps);
|
||||
notification.setTypeNotification(
|
||||
dev.lions.unionflow.server.api.enums.notification.TypeNotification.IN_APP);
|
||||
notification.setPriorite(PrioriteNotification.NORMALE);
|
||||
notification.setStatut(StatutNotification.EN_ATTENTE);
|
||||
notification.setDateEnvoiPrevue(java.time.LocalDateTime.now());
|
||||
notification.setCreePar(keycloakService.getCurrentUserEmail());
|
||||
// Parcourir les canaux demandés
|
||||
if (canaux == null || canaux.isEmpty()) {
|
||||
canaux = List.of("IN_APP");
|
||||
}
|
||||
|
||||
for (String canal : canaux) {
|
||||
try {
|
||||
String type = canal;
|
||||
|
||||
Notification notification = new Notification();
|
||||
notification.setMembre(membre);
|
||||
notification.setSujet(sujet);
|
||||
notification.setCorps(corps);
|
||||
notification.setTypeNotification(type); // Utiliser le canal demandé
|
||||
notification.setPriorite("NORMALE");
|
||||
notification.setStatut("EN_ATTENTE");
|
||||
notification.setDateEnvoiPrevue(java.time.LocalDateTime.now());
|
||||
notification.setCreePar(keycloakService.getCurrentUserEmail());
|
||||
|
||||
notificationRepository.persist(notification);
|
||||
notificationsCreees++;
|
||||
|
||||
// Envoi immédiat si EMAIL
|
||||
if ("EMAIL".equals(type)) {
|
||||
envoyerEmail(notification);
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
LOG.warnf("Type de notification inconnu: %s", canal);
|
||||
}
|
||||
}
|
||||
|
||||
notificationRepository.persist(notification);
|
||||
notificationsCreees++;
|
||||
} catch (Exception e) {
|
||||
LOG.warnf(
|
||||
"Erreur lors de la création de la notification pour le membre %s: %s",
|
||||
@@ -215,12 +253,12 @@ public class NotificationService {
|
||||
// ========================================
|
||||
|
||||
/** Convertit une entité TemplateNotification en DTO */
|
||||
private TemplateNotificationDTO convertToDTO(TemplateNotification template) {
|
||||
private TemplateNotificationResponse convertToDTO(TemplateNotification template) {
|
||||
if (template == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
TemplateNotificationDTO dto = new TemplateNotificationDTO();
|
||||
TemplateNotificationResponse dto = new TemplateNotificationResponse();
|
||||
dto.setId(template.getId());
|
||||
dto.setCode(template.getCode());
|
||||
dto.setSujet(template.getSujet());
|
||||
@@ -238,31 +276,31 @@ public class NotificationService {
|
||||
}
|
||||
|
||||
/** Convertit un DTO en entité TemplateNotification */
|
||||
private TemplateNotification convertToEntity(TemplateNotificationDTO dto) {
|
||||
private TemplateNotification convertToEntity(CreateTemplateNotificationRequest dto) {
|
||||
if (dto == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
TemplateNotification template = new TemplateNotification();
|
||||
template.setCode(dto.getCode());
|
||||
template.setSujet(dto.getSujet());
|
||||
template.setCorpsTexte(dto.getCorpsTexte());
|
||||
template.setCorpsHtml(dto.getCorpsHtml());
|
||||
template.setVariablesDisponibles(dto.getVariablesDisponibles());
|
||||
template.setCanauxSupportes(dto.getCanauxSupportes());
|
||||
template.setLangue(dto.getLangue() != null ? dto.getLangue() : "fr");
|
||||
template.setDescription(dto.getDescription());
|
||||
template.setCode(dto.code());
|
||||
template.setSujet(dto.sujet());
|
||||
template.setCorpsTexte(dto.corpsTexte());
|
||||
template.setCorpsHtml(dto.corpsHtml());
|
||||
template.setVariablesDisponibles(dto.variablesDisponibles());
|
||||
template.setCanauxSupportes(dto.canauxSupportes());
|
||||
template.setLangue(dto.langue() != null ? dto.langue() : "fr");
|
||||
template.setDescription(dto.description());
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
/** Convertit une entité Notification en DTO */
|
||||
private NotificationDTO convertToDTO(Notification notification) {
|
||||
private NotificationResponse convertToDTO(Notification notification) {
|
||||
if (notification == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
NotificationDTO dto = new NotificationDTO();
|
||||
NotificationResponse dto = new NotificationResponse();
|
||||
dto.setId(notification.getId());
|
||||
dto.setTypeNotification(notification.getTypeNotification());
|
||||
dto.setPriorite(notification.getPriorite());
|
||||
@@ -294,59 +332,83 @@ public class NotificationService {
|
||||
}
|
||||
|
||||
/** Convertit un DTO en entité Notification */
|
||||
private Notification convertToEntity(NotificationDTO dto) {
|
||||
private Notification convertToEntity(CreateNotificationRequest dto) {
|
||||
if (dto == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Notification notification = new Notification();
|
||||
notification.setTypeNotification(dto.getTypeNotification());
|
||||
notification.setTypeNotification(dto.typeNotification());
|
||||
notification.setPriorite(
|
||||
dto.getPriorite() != null ? dto.getPriorite() : PrioriteNotification.NORMALE);
|
||||
notification.setStatut(
|
||||
dto.getStatut() != null ? dto.getStatut() : StatutNotification.EN_ATTENTE);
|
||||
notification.setSujet(dto.getSujet());
|
||||
notification.setCorps(dto.getCorps());
|
||||
dto.priorite() != null ? dto.priorite() : "NORMALE");
|
||||
notification.setStatut("EN_ATTENTE");
|
||||
notification.setSujet(dto.sujet());
|
||||
notification.setCorps(dto.corps());
|
||||
notification.setDateEnvoiPrevue(
|
||||
dto.getDateEnvoiPrevue() != null ? dto.getDateEnvoiPrevue() : LocalDateTime.now());
|
||||
notification.setDateEnvoi(dto.getDateEnvoi());
|
||||
notification.setDateLecture(dto.getDateLecture());
|
||||
notification.setNombreTentatives(dto.getNombreTentatives() != null ? dto.getNombreTentatives() : 0);
|
||||
notification.setMessageErreur(dto.getMessageErreur());
|
||||
notification.setDonneesAdditionnelles(dto.getDonneesAdditionnelles());
|
||||
dto.dateEnvoiPrevue() != null ? dto.dateEnvoiPrevue() : LocalDateTime.now());
|
||||
notification.setDateLecture(null);
|
||||
notification.setNombreTentatives(0);
|
||||
notification.setMessageErreur(null);
|
||||
notification.setDonneesAdditionnelles(dto.donneesAdditionnelles());
|
||||
|
||||
// Relations
|
||||
if (dto.getMembreId() != null) {
|
||||
Membre membre =
|
||||
membreRepository
|
||||
.findByIdOptional(dto.getMembreId())
|
||||
.orElseThrow(
|
||||
() -> new NotFoundException("Membre non trouvé avec l'ID: " + dto.getMembreId()));
|
||||
if (dto.membreId() != null) {
|
||||
Membre membre = membreRepository
|
||||
.findByIdOptional(dto.membreId())
|
||||
.orElseThrow(
|
||||
() -> new NotFoundException("Membre non trouvé avec l'ID: " + dto.membreId()));
|
||||
notification.setMembre(membre);
|
||||
}
|
||||
|
||||
if (dto.getOrganisationId() != null) {
|
||||
Organisation org =
|
||||
organisationRepository
|
||||
.findByIdOptional(dto.getOrganisationId())
|
||||
.orElseThrow(
|
||||
() ->
|
||||
new NotFoundException(
|
||||
"Organisation non trouvée avec l'ID: " + dto.getOrganisationId()));
|
||||
if (dto.organisationId() != null) {
|
||||
Organisation org = organisationRepository
|
||||
.findByIdOptional(dto.organisationId())
|
||||
.orElseThrow(
|
||||
() -> new NotFoundException(
|
||||
"Organisation non trouvée avec l'ID: " + dto.organisationId()));
|
||||
notification.setOrganisation(org);
|
||||
}
|
||||
|
||||
if (dto.getTemplateId() != null) {
|
||||
TemplateNotification template =
|
||||
templateNotificationRepository
|
||||
.findTemplateNotificationById(dto.getTemplateId())
|
||||
.orElseThrow(
|
||||
() ->
|
||||
new NotFoundException(
|
||||
"Template non trouvé avec l'ID: " + dto.getTemplateId()));
|
||||
if (dto.templateId() != null) {
|
||||
TemplateNotification template = templateNotificationRepository
|
||||
.findTemplateNotificationById(dto.templateId())
|
||||
.orElseThrow(
|
||||
() -> new NotFoundException(
|
||||
"Template non trouvé avec l'ID: " + dto.templateId()));
|
||||
notification.setTemplate(template);
|
||||
}
|
||||
|
||||
return notification;
|
||||
}
|
||||
|
||||
/**
|
||||
* Envoie un email pour une notification
|
||||
*/
|
||||
private void envoyerEmail(Notification notification) {
|
||||
if (notification.getMembre() == null || notification.getMembre().getEmail() == null) {
|
||||
LOG.warnf("Impossible d'envoyer l'email pour la notification %s : pas d'email", notification.getId());
|
||||
notification.setStatut("ECHEC_ENVOI");
|
||||
notification.setMessageErreur("Pas d'email défini pour le membre");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
LOG.infof("Envoi de l'email à %s", notification.getMembre().getEmail());
|
||||
mailer.send(Mail.withText(notification.getMembre().getEmail(),
|
||||
notification.getSujet(),
|
||||
notification.getCorps())); // TODO: Support HTML body if needed
|
||||
|
||||
notification.setStatut("ENVOYEE");
|
||||
notification.setDateEnvoi(LocalDateTime.now());
|
||||
} catch (Exception e) {
|
||||
LOG.errorf("Echec de l'envoi de l'email: %s", e.getMessage());
|
||||
notification.setStatut("ECHEC_ENVOI");
|
||||
notification.setMessageErreur(e.getMessage());
|
||||
notification.setNombreTentatives(notification.getNombreTentatives() + 1);
|
||||
}
|
||||
// La mise à jour du statut sera persistée car l'entité est gérée (si dans une
|
||||
// transaction active)
|
||||
// Note: l'appelant doit être transactionnel
|
||||
notificationRepository.persist(notification); // Just to be safe/update
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user