Skip to content

Commit

Permalink
[TRELLO-2836] Send BauxPrecraire reports to the right DDs
Browse files Browse the repository at this point in the history
  • Loading branch information
charlescd committed Jan 8, 2025
1 parent d192978 commit 3b8add1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/models/report/ReportStatus.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 35 additions & 3 deletions app/orchestrators/EmailNotificationOrchestrator.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package orchestrators

import cats.implicits.toTraverseOps
import models.report.Report
import models.report.ReportTag
import play.api.Logger
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 3b8add1

Please sign in to comment.