Files
unionflow-server-impl-quarkus/src/main/java/dev/lions/unionflow/server/service/NotificationService.java
dahoud 31330d95e9 feat: accumulated work — PI-SPI, KYC, RLS, mutuelle parts, comptabilité PDF + startup fixes
## PI-SPI BCEAO (P0.3 — deadline 30/06/2026)
- package payment/pispi/ complet : PispiAuth (OAuth2), PispiClient (HTTP brut),
  PispiIso20022Mapper (pacs.008/002), PispiSignatureVerifier (HMAC-SHA256),
  PispiWebhookResource (/api/pispi/webhook), DTOs ISO 20022
- PaymentOrchestrator + PaymentProviderRegistry pour l'orchestration multi-provider
- Mode mock automatique si credentials absents (dev)

## KYC AML
- entity/KycDossier, KycResource, KycAmlService + tests
- Migration V38 (create_kyc_dossier_table)

## RLS (PostgreSQL Row-Level Security) — isolation multi-tenant
- RlsConnectionInitializer, RlsContextInterceptor, @RlsEnabled annotation
- Migration V39 (PostgreSQL RLS Tenant Isolation) + V42 (app DB roles)
- Tests unitaires RlsConnectionInitializerTest, RlsContextInterceptorTest
- Tests d'intégration RlsCrossTenantIsolationTest (@QuarkusTest + IntegrationTestProfile)

## Mutuelle — Parts sociales
- entity/mutuelle/parts/ComptePartsSociales, TransactionPartsSociales
- Service, resource, mapper, repository + tests
- InteretsEpargneService + ReleveComptePdfService

## Comptabilité PDF
- ComptabilitePdfService (OpenPDF), ComptabilitePdfResource
- Tests ComptabilitePdfServiceTest, ComptabilitePdfResourceTest

## Migrations Flyway (SYSCOHADA + Keycloak Orgs)
- V36 SYSCOHADA Plan Comptable Complet : seeds comptes standards UEMOA,
  trigger init_plan_comptable_organisation, alignement schéma V1 → entités
- V37 keycloak_org_id sur organisations (P0.2 migration KC 26)
- V40 provider_defaut sur FormuleAbonnement
- V41 fcm_token sur utilisateurs (FCM notifications push)

## Fixes startup (SmallRye Config 3.20 + schéma)
- 8× @ConfigProperty(defaultValue = "") → Optional<String>
  (firebase, pispi.*, mtnmomo, orange) — empty default rejetés par SmallRye 3.20
- application.properties : mappings secrets env var sous %prod. uniquement
- V36 : drop colonne obsolète 'numero' de V1 quand Hibernate a créé 'numero_compte'
- V36 : remplacement UNIQUE global sur journaux_comptables.code par composite
  (organisation_id, code) pour autoriser plusieurs orgs avec code 'ACH'/'VTE'/etc
- V39 : escape placeholder ${VAR} → <VAR> dans lignes commentées
  (Flyway parser évalue les placeholders même dans les commentaires)
- V41 : table 'membres' → 'utilisateurs' (nom correct selon entité Membre)
- JournalComptable entity : @UniqueConstraint composite au lieu de unique=true
- MembreResource : example @Schema JSON valide (['...'] → [])
- IntegrationTestProfile : auto-détection Docker via `docker info`, fallback
  vers PostgreSQL local sans DevServices

## Dev config
- application-dev.properties : quarkus.devservices.enabled=false +
  quarkus.kafka.devservices.enabled=false (pas besoin de Docker pour dev)
- quarkus.flyway.placeholder-replacement=false
- Secrets dev (wave.*, firebase, pispi) en mode mock automatique

## Phase 8 tests (complète)
- 170 fichiers modifiés/ajoutés, 23425+ insertions
- Tests RBAC (@QuarkusTest) pour MembreResource lifecycle
- Tests OrganisationContextFilter multi-org
- Tests SouscriptionQuotaOptionC, KycAmlService, EmailTemplate, etc.

Résultat : Backend démarre en 64s sur port 8085 avec 36 features installées.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 12:40:55 +00:00

458 lines
16 KiB
Java

package dev.lions.unionflow.server.service;
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;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
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;
/**
* Service métier pour la gestion des notifications
*
* @author UnionFlow Team
* @version 3.0
* @since 2025-01-29
*/
@ApplicationScoped
public class NotificationService {
private static final Logger LOG = Logger.getLogger(NotificationService.class);
@Inject
NotificationRepository notificationRepository;
@Inject
TemplateNotificationRepository templateNotificationRepository;
@Inject
MembreRepository membreRepository;
@Inject
OrganisationRepository organisationRepository;
@Inject
Mailer mailer;
@Inject
KeycloakService keycloakService;
@Inject
FirebasePushService firebasePushService;
/**
* Crée un nouveau template de notification
*
* @param templateDTO DTO du template à créer
* @return DTO du template créé
*/
@Transactional
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(request.code()).isPresent()) {
throw new IllegalArgumentException("Un template avec ce code existe déjà: " + request.code());
}
TemplateNotification template = convertToEntity(request);
template.setCreePar(keycloakService.getCurrentUserEmail());
templateNotificationRepository.persist(template);
LOG.infof("Template créé avec succès: ID=%s, Code=%s", template.getId(), template.getCode());
return convertToDTO(template);
}
/**
* Crée une nouvelle notification
*
* @param notificationDTO DTO de la notification à créer
* @return DTO de la notification créée
*/
@Transactional
public NotificationResponse creerNotification(CreateNotificationRequest request) {
LOG.infof("Création d'une nouvelle notification: %s", request.typeNotification());
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 selon le canal
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());
}
} else if ("PUSH".equals(notification.getTypeNotification())) {
try {
envoyerPush(notification);
} catch (Exception e) {
LOG.warnf("Erreur push notification %s (non bloquant): %s", notification.getId(), e.getMessage());
}
}
return convertToDTO(notification);
}
/**
* Marque une notification comme lue
*
* @param id ID de la notification
* @return DTO de la notification mise à jour
*/
@Transactional
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.setStatut("LUE");
notification.setDateLecture(LocalDateTime.now());
notification.setModifiePar(keycloakService.getCurrentUserEmail());
notificationRepository.persist(notification);
LOG.infof("Notification marquée comme lue: ID=%s", id);
return convertToDTO(notification);
}
/**
* Trouve une notification par son ID
*
* @param id ID de la notification
* @return DTO de la notification
*/
public NotificationResponse trouverNotificationParId(UUID id) {
return notificationRepository
.findNotificationById(id)
.map(this::convertToDTO)
.orElseThrow(() -> new NotFoundException("Notification non trouvée avec l'ID: " + id));
}
/**
* Liste toutes les notifications d'un membre
*
* @param membreId ID du membre
* @return Liste des notifications
*/
public List<NotificationResponse> listerNotificationsParMembre(UUID membreId) {
return notificationRepository.findByMembreId(membreId).stream()
.map(this::convertToDTO)
.collect(Collectors.toList());
}
/**
* Liste les notifications non lues d'un membre
*
* @param membreId ID du membre
* @return Liste des notifications non lues
*/
public List<NotificationResponse> listerNotificationsNonLuesParMembre(UUID membreId) {
return notificationRepository.findNonLuesByMembreId(membreId).stream()
.map(this::convertToDTO)
.collect(Collectors.toList());
}
/**
* Liste les notifications en attente d'envoi
*
* @return Liste des notifications en attente
*/
public List<NotificationResponse> listerNotificationsEnAttenteEnvoi() {
return notificationRepository.findEnAttenteEnvoi().stream()
.map(this::convertToDTO)
.collect(Collectors.toList());
}
/**
* 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.)
* @return Nombre de notifications créées
*/
@Transactional
public int envoyerNotificationsGroupees(
List<UUID> membreIds, String sujet, String corps, List<String> canaux) {
if (membreIds == null || membreIds.isEmpty()) {
throw new IllegalArgumentException("La liste des membres ne peut pas être vide");
}
LOG.infof(
"Envoi de notifications groupées à %d membres - sujet: %s", membreIds.size(), sujet);
int notificationsCreees = 0;
for (UUID membreId : membreIds) {
try {
Membre membre = membreRepository
.findByIdOptional(membreId)
.orElseThrow(
() -> new IllegalArgumentException(
"Membre non trouvé avec l'ID: " + membreId));
// 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);
}
}
} catch (Exception e) {
LOG.warnf(
"Erreur lors de la création de la notification pour le membre %s: %s",
membreId, e.getMessage());
}
}
LOG.infof(
"%d notifications créées sur %d membres demandés", notificationsCreees, membreIds.size());
return notificationsCreees;
}
// ========================================
// MÉTHODES PRIVÉES
// ========================================
/** Convertit une entité TemplateNotification en DTO */
private TemplateNotificationResponse convertToDTO(TemplateNotification template) {
if (template == null) {
return null;
}
TemplateNotificationResponse dto = new TemplateNotificationResponse();
dto.setId(template.getId());
dto.setCode(template.getCode());
dto.setSujet(template.getSujet());
dto.setCorpsTexte(template.getCorpsTexte());
dto.setCorpsHtml(template.getCorpsHtml());
dto.setVariablesDisponibles(template.getVariablesDisponibles());
dto.setCanauxSupportes(template.getCanauxSupportes());
dto.setLangue(template.getLangue());
dto.setDescription(template.getDescription());
dto.setDateCreation(template.getDateCreation());
dto.setDateModification(template.getDateModification());
dto.setActif(template.getActif());
return dto;
}
/** Convertit un DTO en entité TemplateNotification */
private TemplateNotification convertToEntity(CreateTemplateNotificationRequest dto) {
if (dto == null) {
return null;
}
TemplateNotification template = new TemplateNotification();
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 NotificationResponse convertToDTO(Notification notification) {
if (notification == null) {
return null;
}
NotificationResponse dto = new NotificationResponse();
dto.setId(notification.getId());
dto.setTypeNotification(notification.getTypeNotification());
dto.setPriorite(notification.getPriorite());
dto.setStatut(notification.getStatut());
dto.setSujet(notification.getSujet());
dto.setCorps(notification.getCorps());
dto.setDateEnvoiPrevue(notification.getDateEnvoiPrevue());
dto.setDateEnvoi(notification.getDateEnvoi());
dto.setDateLecture(notification.getDateLecture());
dto.setNombreTentatives(notification.getNombreTentatives());
dto.setMessageErreur(notification.getMessageErreur());
dto.setDonneesAdditionnelles(notification.getDonneesAdditionnelles());
if (notification.getMembre() != null) {
dto.setMembreId(notification.getMembre().getId());
}
if (notification.getOrganisation() != null) {
dto.setOrganisationId(notification.getOrganisation().getId());
}
if (notification.getTemplate() != null) {
dto.setTemplateId(notification.getTemplate().getId());
}
dto.setDateCreation(notification.getDateCreation());
dto.setDateModification(notification.getDateModification());
dto.setActif(notification.getActif());
return dto;
}
/** Convertit un DTO en entité Notification */
private Notification convertToEntity(CreateNotificationRequest dto) {
if (dto == null) {
return null;
}
Notification notification = new Notification();
notification.setTypeNotification(dto.typeNotification());
notification.setPriorite(
dto.priorite() != null ? dto.priorite() : "NORMALE");
notification.setStatut("EN_ATTENTE");
notification.setSujet(dto.sujet());
notification.setCorps(dto.corps());
notification.setDateEnvoiPrevue(
dto.dateEnvoiPrevue() != null ? dto.dateEnvoiPrevue() : LocalDateTime.now());
notification.setDateLecture(null);
notification.setNombreTentatives(0);
notification.setMessageErreur(null);
notification.setDonneesAdditionnelles(dto.donneesAdditionnelles());
// Relations
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.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.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 une notification push FCM pour une notification.
*/
private void envoyerPush(Notification notification) {
if (notification.getMembre() == null) {
LOG.warnf("Impossible d'envoyer le push pour la notification %s : pas de membre", notification.getId());
notification.setStatut("ECHEC_ENVOI");
notification.setMessageErreur("Pas de membre défini");
return;
}
String fcmToken = notification.getMembre().getFcmToken();
if (fcmToken == null || fcmToken.isBlank()) {
LOG.debugf("Membre %s sans token FCM — push ignoré", notification.getMembre().getId());
notification.setStatut("IGNOREE");
notification.setMessageErreur("Pas de token FCM");
return;
}
boolean ok = firebasePushService.envoyerNotification(
fcmToken,
notification.getSujet(),
notification.getCorps(),
java.util.Map.of("notificationId", notification.getId().toString()));
if (ok) {
notification.setStatut("ENVOYEE");
notification.setDateEnvoi(java.time.LocalDateTime.now());
} else {
notification.setStatut("ECHEC_ENVOI");
notification.setMessageErreur("FCM: envoi échoué");
}
notificationRepository.persist(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());
String corps = notification.getCorps();
boolean isHtml = corps != null && (corps.startsWith("<html") || corps.startsWith("<!DOCTYPE") || corps.startsWith("<HTML"));
Mail mail = isHtml
? Mail.withHtml(notification.getMembre().getEmail(), notification.getSujet(), corps)
: Mail.withText(notification.getMembre().getEmail(), notification.getSujet(), corps);
mailer.send(mail);
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
}
}