Skip to content

Commit

Permalink
[TRELLO-2037] Remove userRole from Subscription (useless, there is th…
Browse files Browse the repository at this point in the history
…e userId)
  • Loading branch information
charlescd committed Oct 18, 2023
1 parent 1803a42 commit d440f36
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 106 deletions.
3 changes: 1 addition & 2 deletions app/controllers/AdminController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ class AdminController(
withoutTags = Nil,
categories = Nil,
sirets = Nil,
frequency = java.time.Period.ofDays(1),
userRole = Some(UserRole.DGCCRF)
frequency = java.time.Period.ofDays(1)
)

case class EmailContent(subject: String, body: play.twirl.api.Html)
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/SubscriptionController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class SubscriptionController(
withoutTags = draftSubscription.withoutTags,
countries = draftSubscription.countries.map(Country.fromCode),
sirets = draftSubscription.sirets,
frequency = draftSubscription.frequency,
userRole = Some(request.identity.userRole)
frequency = draftSubscription.frequency
)
)
.map(subscription => Ok(Json.toJson(subscription)))
Expand Down
9 changes: 8 additions & 1 deletion app/loader/SignalConsoApplicationLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,14 @@ class SignalConsoComponents(
logger.debug("Starting App and sending sentry alert")

val reportNotificationTask =
new ReportNotificationTask(actorSystem, reportRepository, subscriptionRepository, mailService, taskConfiguration)
new ReportNotificationTask(
actorSystem,
reportRepository,
subscriptionRepository,
userRepository,
mailService,
taskConfiguration
)

val inactiveDgccrfAccountRemoveTask =
new InactiveDgccrfAccountRemoveTask(userRepository, subscriptionRepository, asyncFileRepository)
Expand Down
3 changes: 1 addition & 2 deletions app/models/Subscription.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ case class Subscription(
withoutTags: List[ReportTag] = List.empty,
countries: List[Country] = List.empty,
sirets: List[SIRET] = List.empty,
frequency: Period,
userRole: Option[UserRole]
frequency: Period
)

object Subscription {
Expand Down
25 changes: 6 additions & 19 deletions app/repositories/subscription/SubscriptionTable.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package repositories.subscription

import models.Subscription
import models.UserRole
import models.report.ReportCategory
import models.report.ReportTag
import repositories.DatabaseTable
Expand All @@ -14,14 +13,9 @@ import java.time.OffsetDateTime
import java.time.Period
import java.util.UUID
import repositories.report.ReportColumnType.ReportTagListColumnType
import slick.ast.BaseTypedType
import slick.jdbc.JdbcType

class SubscriptionTable(tag: Tag) extends DatabaseTable[Subscription](tag, "subscriptions") {

implicit val userRoleColumnType: JdbcType[UserRole] with BaseTypedType[UserRole] =
MappedColumnType.base[UserRole, String](_.entryName, UserRole.withName)

def creationDate = column[OffsetDateTime]("creation_date")
def userId = column[Option[UUID]]("user_id")
def email = column[Option[EmailAddress]]("email")
Expand All @@ -32,7 +26,6 @@ class SubscriptionTable(tag: Tag) extends DatabaseTable[Subscription](tag, "subs
def countries = column[List[Country]]("countries")
def sirets = column[List[SIRET]]("sirets")
def frequency = column[Period]("frequency")
def userRole = column[Option[UserRole]]("user_role")

type SubscriptionData = (
UUID,
Expand All @@ -45,8 +38,7 @@ class SubscriptionTable(tag: Tag) extends DatabaseTable[Subscription](tag, "subs
List[ReportTag],
List[Country],
List[SIRET],
Period,
Option[UserRole]
Period
)

def constructSubscription: SubscriptionData => Subscription = {
Expand All @@ -61,8 +53,7 @@ class SubscriptionTable(tag: Tag) extends DatabaseTable[Subscription](tag, "subs
withoutTags,
countries,
sirets,
frequency,
userRole
frequency
) =>
Subscription(
id = id,
Expand All @@ -75,8 +66,7 @@ class SubscriptionTable(tag: Tag) extends DatabaseTable[Subscription](tag, "subs
withoutTags = withoutTags,
countries = countries,
sirets = sirets,
frequency = frequency,
userRole = userRole
frequency = frequency
)
}

Expand All @@ -92,8 +82,7 @@ class SubscriptionTable(tag: Tag) extends DatabaseTable[Subscription](tag, "subs
withoutTags,
countries,
sirets,
frequency,
userRole
frequency
) =>
(
id,
Expand All @@ -106,8 +95,7 @@ class SubscriptionTable(tag: Tag) extends DatabaseTable[Subscription](tag, "subs
withoutTags,
countries,
sirets,
frequency,
userRole
frequency
)
}

Expand All @@ -123,8 +111,7 @@ class SubscriptionTable(tag: Tag) extends DatabaseTable[Subscription](tag, "subs
withoutTags,
countries,
sirets,
frequency,
userRole
frequency
) <> (constructSubscription, extractSubscription.lift)
}

Expand Down
99 changes: 57 additions & 42 deletions app/tasks/report/ReportNotificationTask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import akka.actor.ActorSystem
import cats.implicits.toTraverseOps
import config.TaskConfiguration
import models.Subscription
import models.User
import models.UserRole
import models.report.PreFilter
import models.report.Report
Expand All @@ -12,6 +13,7 @@ import models.report.ReportFilter
import play.api.Logger
import repositories.report.ReportRepositoryInterface
import repositories.subscription.SubscriptionRepositoryInterface
import repositories.user.UserRepositoryInterface
import services.Email.DgccrfReportNotification
import services.MailService
import tasks.report.ReportNotificationTask.refineReportBasedOnSubscriptionFilters
Expand All @@ -32,6 +34,7 @@ class ReportNotificationTask(
actorSystem: ActorSystem,
reportRepository: ReportRepositoryInterface,
subscriptionRepository: SubscriptionRepositoryInterface,
userRepository: UserRepositoryInterface,
mailService: MailService,
taskConfiguration: TaskConfiguration
)(implicit executionContext: ExecutionContext) {
Expand Down Expand Up @@ -76,10 +79,11 @@ class ReportNotificationTask(
)
)
_ = logger.debug(s"Found ${reportsWithFiles.size} reports for this period ($period)")
subscriptionsEmailAndReports = subscriptionsWithEmails.map { case (subscription, emailAddress) =>
val filteredReport = refineReportBasedOnSubscriptionFilters(reportsWithFiles, subscription)
(subscription, emailAddress, filteredReport)
}
subscriptionsEmailAndReports <- Future.sequence(subscriptionsWithEmails.map { case (subscription, emailAddress) =>
refineReportBasedOnSubscriptionFilters(userRepository, reportsWithFiles, subscription).map { filteredReport =>
(subscription, emailAddress, filteredReport)
}
})
subscriptionEmailAndNonEmptyReports = subscriptionsEmailAndReports.filter(_._3.nonEmpty)
_ = logger.debug(
s"We have ${subscriptionEmailAndNonEmptyReports.size} emails of notifications to send (period $period)"
Expand Down Expand Up @@ -115,47 +119,58 @@ class ReportNotificationTask(

object ReportNotificationTask {

private def userFromSubscription(
userRepository: UserRepositoryInterface,
subscription: Subscription
): Future[Option[User]] = subscription.userId match {
case Some(userId) => userRepository.get(userId)
case None => Future.successful(None)
}

def refineReportBasedOnSubscriptionFilters(
userRepository: UserRepositoryInterface,
reportsWithFiles: SortedMap[Report, List[ReportFile]],
subscription: Subscription
): SortedMap[Report, List[ReportFile]] = reportsWithFiles
.filter { case (report, _) =>
subscription.userRole match {
case Some(UserRole.DGAL) =>
PreFilter.DGALFilter.category.forall(_.entryName == report.category) || (if (
PreFilter.DGALFilter.tags.isEmpty
) true
else
PreFilter.DGALFilter.tags
.intersect(report.tags)
.nonEmpty)
case Some(UserRole.DGCCRF) => true
case Some(UserRole.Admin) => true
case Some(UserRole.Professionnel) => true
case None => true
}
}
.filter { case (report, _) =>
subscription.departments.isEmpty || (report.companyAddress.country.isEmpty && subscription.departments
.map(Some(_))
.contains(report.companyAddress.postalCode.flatMap(Departments.fromPostalCode)))
}
.filter { case (report, _) =>
subscription.categories.isEmpty || subscription.categories.map(_.entryName).contains(report.category)
}
.filter { case (report, _) =>
subscription.sirets.isEmpty || subscription.sirets.map(Some(_)).contains(report.companySiret)
}
.filter { case (report, _) =>
subscription.countries.isEmpty || subscription.countries
.map(Some(_))
.contains(report.companyAddress.country)
}
.filter { case (report, _) =>
subscription.withTags.isEmpty || subscription.withTags.intersect(report.tags).nonEmpty
}
.filter { case (report, _) =>
subscription.withoutTags.isEmpty || subscription.withoutTags.intersect(report.tags).isEmpty
)(implicit ec: ExecutionContext): Future[SortedMap[Report, List[ReportFile]]] =
userFromSubscription(userRepository, subscription).map { maybeUser =>
reportsWithFiles
.filter { case (report, _) =>
maybeUser.map(_.userRole) match {
case Some(UserRole.DGAL) =>
PreFilter.DGALFilter.category
.forall(_.entryName == report.category) || (if (PreFilter.DGALFilter.tags.isEmpty) true
else
PreFilter.DGALFilter.tags
.intersect(report.tags)
.nonEmpty)
case Some(UserRole.DGCCRF) => true
case Some(UserRole.Admin) => true
case Some(UserRole.Professionnel) => true
case None => true
}
}
.filter { case (report, _) =>
subscription.departments.isEmpty || (report.companyAddress.country.isEmpty && subscription.departments
.map(Some(_))
.contains(report.companyAddress.postalCode.flatMap(Departments.fromPostalCode)))
}
.filter { case (report, _) =>
subscription.categories.isEmpty || subscription.categories.map(_.entryName).contains(report.category)
}
.filter { case (report, _) =>
subscription.sirets.isEmpty || subscription.sirets.map(Some(_)).contains(report.companySiret)
}
.filter { case (report, _) =>
subscription.countries.isEmpty || subscription.countries
.map(Some(_))
.contains(report.companyAddress.country)
}
.filter { case (report, _) =>
subscription.withTags.isEmpty || subscription.withTags.intersect(report.tags).nonEmpty
}
.filter { case (report, _) =>
subscription.withoutTags.isEmpty || subscription.withoutTags.intersect(report.tags).isEmpty
}
}

}

This file was deleted.

7 changes: 2 additions & 5 deletions test/tasks/account/InactiveAccountTaskSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import models.AsyncFile
import models.AsyncFileKind
import models.Subscription
import models.User
import models.UserRole
import org.specs2.concurrent.ExecutionEnv
import org.specs2.matcher.FutureMatchers
import play.api.mvc.Results
Expand Down Expand Up @@ -84,8 +83,7 @@ class InactiveAccountTaskSpec(implicit ee: ExecutionEnv)
email = None,
creationDate = OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS),
userId = Some(inactiveDGCCRFUser.id),
frequency = Period.ofDays(1),
userRole = Some(UserRole.Admin)
frequency = Period.ofDays(1)
)

// Subscriptions that should be kept
Expand All @@ -94,8 +92,7 @@ class InactiveAccountTaskSpec(implicit ee: ExecutionEnv)
email = None,
creationDate = OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS),
userId = Some(activeDGCCRFUser.id),
frequency = Period.ofDays(1),
userRole = Some(UserRole.Admin)
frequency = Period.ofDays(1)
)

val (userList, deletedUsersList, activeSubscriptionList, inactiveSubscriptionList, inactivefiles, activefiles) =
Expand Down
9 changes: 3 additions & 6 deletions test/tasks/report/DailyReportNotificationTaskSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,22 @@ abstract class DailyReportNotificationTaskSpec(implicit ee: ExecutionEnv)
email = Some(covidEmail),
departments = List(covidDept),
categories = List(ReportCategory.Coronavirus),
frequency = Period.ofDays(1),
userRole = Some(UserRole.Admin)
frequency = Period.ofDays(1)
)

val tagSubscription = Subscription(
userId = None,
email = Some(tagEmail),
departments = List(tagDept),
withTags = List(ReportTag.ProduitDangereux),
frequency = Period.ofDays(1),
userRole = Some(UserRole.Admin)
frequency = Period.ofDays(1)
)

val countrySubscription = Subscription(
userId = None,
email = Some(countryEmail),
countries = List(Country.Suisse),
frequency = Period.ofDays(1),
userRole = Some(UserRole.Admin)
frequency = Period.ofDays(1)
)

val company = Fixtures.genCompany.sample.get
Expand Down
9 changes: 3 additions & 6 deletions test/tasks/report/NoReportNotificationTaskSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,22 @@ abstract class NoReportNotificationTaskSpec(implicit ee: ExecutionEnv)
email = Some(covidEmail),
departments = List(covidDept),
categories = List(ReportCategory.Coronavirus),
frequency = Period.ofDays(1),
userRole = Some(UserRole.Admin)
frequency = Period.ofDays(1)
)

val tagSubscription = Subscription(
userId = None,
email = Some(tagEmail),
departments = List(tagDept),
withTags = List(ReportTag.ProduitDangereux),
frequency = Period.ofDays(1),
userRole = Some(UserRole.Admin)
frequency = Period.ofDays(1)
)

val countrySubscription = Subscription(
userId = None,
email = Some(countryEmail),
countries = List(Country.Suisse),
frequency = Period.ofDays(1),
userRole = Some(UserRole.Admin)
frequency = Period.ofDays(1)
)

val company = Fixtures.genCompany.sample.get
Expand Down
Loading

0 comments on commit d440f36

Please sign in to comment.