From 286bfff83c5300d1600654f47ef67f8246a0b51a Mon Sep 17 00:00:00 2001 From: Charles Dufour Date: Wed, 8 Jan 2025 11:34:44 +0100 Subject: [PATCH] [TRELLO-2836] Send BauxPrecraire reports to the right DDs (#1825) --- app/models/report/ReportStatus.scala | 2 +- .../EmailNotificationOrchestrator.scala | 38 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/app/models/report/ReportStatus.scala b/app/models/report/ReportStatus.scala index bca7e8e1..ecfd5326 100644 --- a/app/models/report/ReportStatus.scala +++ b/app/models/report/ReportStatus.scala @@ -15,13 +15,13 @@ object ReportStatus extends PlayEnum[ReportStatus] { /** Not read by pro status */ case object TraitementEnCours extends ReportStatus + case object NonConsulte extends ReportStatus /** Read by pro status */ case object Transmis extends ReportStatus case object PromesseAction extends ReportStatus case object Infonde extends ReportStatus - case object NonConsulte extends ReportStatus case object ConsulteIgnore extends ReportStatus case object MalAttribue extends ReportStatus case object SuppressionRGPD extends ReportStatus diff --git a/app/orchestrators/EmailNotificationOrchestrator.scala b/app/orchestrators/EmailNotificationOrchestrator.scala index 6ccdfced..64b31fdc 100644 --- a/app/orchestrators/EmailNotificationOrchestrator.scala +++ b/app/orchestrators/EmailNotificationOrchestrator.scala @@ -1,5 +1,6 @@ package orchestrators +import cats.implicits.toTraverseOps import models.report.Report import models.report.ReportTag import play.api.Logger @@ -33,13 +34,44 @@ class EmailNotificationOrchestrator(mailService: MailService, subscriptionReposi } } + private val postalCodeRegex = "\\d{5}".r + private def extractPostalCode(s: String) = + postalCodeRegex.findFirstIn(s) + + private def getDDEmails(report: Report) = { + val maybeTag = shouldNotifyDgccrf(report) + + maybeTag match { + // Cas spécial BauxPrecaire + case Some(ReportTag.BauxPrecaire) => + for { + // On doit envoyer tous les signalements Baux précaire à la dd 33. Ce sont eux qui les traitent + dd33Email <- subscriptionRepository.getDirectionDepartementaleEmail("33") + // Adresse fournie par le conso à l'étape 3. + storeDDEmail <- report.details + .find(_.label == "Adresse précise du magasin (numéro, nom de rue, code postal et nom de la ville) :") + .map(_.value) + .flatMap(extractPostalCode) + .traverse(postalCode => subscriptionRepository.getDirectionDepartementaleEmail(postalCode.take(2))) + // En fallback, l'adresse de l'entreprise identifiée + companyDDEmail <- report.companyAddress.postalCode.traverse(postalCode => + subscriptionRepository.getDirectionDepartementaleEmail(postalCode.take(2)) + ) + } yield dd33Email ++ companyDDEmail.getOrElse(Seq.empty) ++ storeDDEmail.getOrElse(Seq.empty) + case Some(_) => + // Cas nominal, on prend les emails de la DD de l'entreprise + report.companyAddress.postalCode + .map(postalCode => subscriptionRepository.getDirectionDepartementaleEmail(postalCode.take(2))) + .getOrElse(Future.successful(Seq.empty)) + case None => Future.successful(Seq.empty) + } + } + def notifyDgccrfIfNeeded(report: Report): Future[Unit] = getNotificationEmail(report) match { case Some(email) => for { - ddEmails <- report.companyAddress.postalCode - .map(postalCode => subscriptionRepository.getDirectionDepartementaleEmail(postalCode.take(2))) - .getOrElse(Future.successful(Seq.empty)) + ddEmails <- getDDEmails(report) _ <- if (ddEmails.nonEmpty) { mailService.send(email(ddEmails))