Skip to content

Commit

Permalink
[TRELLO-2737] Fix subcategories listing (#1830)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlescd authored Jan 9, 2025
1 parent c1c4c88 commit c305180
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/loader/SignalConsoApplicationLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ class SignalConsoComponents(
responseConsumerReviewRepository,
reportEngagementReviewRepository,
accessTokenRepository,
websiteApiService
websiteApiService,
subcategoryLabelRepository
)

val rgpdOrchestrator = new RgpdOrchestrator(
Expand Down
30 changes: 28 additions & 2 deletions app/orchestrators/StatsOrchestrator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import repositories.event.EventRepositoryInterface
import repositories.report.ReportRepositoryInterface
import repositories.reportconsumerreview.ResponseConsumerReviewRepositoryInterface
import repositories.reportengagementreview.ReportEngagementReviewRepositoryInterface
import repositories.subcategorylabel.SubcategoryLabel
import repositories.subcategorylabel.SubcategoryLabelRepositoryInterface
import services.WebsiteApiServiceInterface
import utils.Constants.ActionEvent._
import utils.Constants.ActionEvent
Expand All @@ -39,20 +41,44 @@ class StatsOrchestrator(
reportConsumerReviewRepository: ResponseConsumerReviewRepositoryInterface,
reportEngagementReviewRepository: ReportEngagementReviewRepositoryInterface,
accessTokenRepository: AccessTokenRepositoryInterface,
websiteApiService: WebsiteApiServiceInterface
websiteApiService: WebsiteApiServiceInterface,
subcategoryLabelRepository: SubcategoryLabelRepositoryInterface
)(implicit val executionContext: ExecutionContext) {

def reportsCountBySubcategories(user: User, filters: ReportsCountBySubcategoriesFilter): Future[ReportNodes] =
for {
maybeMinimizedAnomalies <- websiteApiService.fetchMinimizedAnomalies()
minimizedAnomalies <- maybeMinimizedAnomalies.liftTo[Future](WebsiteApiError)
labels <- subcategoryLabelRepository.list()
reportNodesFr <- reportRepository
.reportsCountBySubcategories(user, filters, Locale.FRENCH)
.map(StatsOrchestrator.buildReportNodes(minimizedAnomalies.fr, _))
reportNodesEn <- reportRepository
.reportsCountBySubcategories(user, filters, Locale.ENGLISH)
.map(StatsOrchestrator.buildReportNodes(minimizedAnomalies.en, _))
} yield ReportNodes(reportNodesFr, reportNodesEn)
} yield ReportNodes(translateSubcategories(reportNodesFr, labels), translateSubcategories(reportNodesEn, labels))

private def translateSubcategories(nodes: List[ReportNode], labels: List[SubcategoryLabel]): List[ReportNode] =
nodes.map { node =>
node.copy(children = translateSubcategories(node.name, List.empty, node.children, labels))
}

private def translateSubcategories(
categories: String,
subcategories: List[String],
nodes: List[ReportNode],
labels: List[SubcategoryLabel]
): List[ReportNode] =
nodes.map { node =>
node.copy(
name = labels
.find(label => label.category == categories && label.subcategories == subcategories :+ node.name)
.flatMap(label => label.subcategoryLabelsFr.orElse(label.subcategoryLabelsEn))
.flatMap(_.lastOption)
.getOrElse(node.name),
children = translateSubcategories(categories, subcategories :+ node.name, node.children, labels)
)
}

def countByDepartments(start: Option[LocalDate], end: Option[LocalDate]): Future[Seq[(String, Int)]] =
for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ class SubcategoryLabelRepository(val dbConfig: DatabaseConfig[JdbcProfile])(impl
override def get(category: String, subcategories: List[String]): Future[Option[SubcategoryLabel]] = db.run(
table.filter(_.category === category).filter(_.subcategories === subcategories).result.headOption
)

override def list(): Future[List[SubcategoryLabel]] = db.run(table.to[List].result)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ trait SubcategoryLabelRepositoryInterface {

def createOrUpdateAll(elements: List[SubcategoryLabel]): Future[Unit]
def get(category: String, subcategories: List[String]): Future[Option[SubcategoryLabel]]
def list(): Future[List[SubcategoryLabel]]
}

0 comments on commit c305180

Please sign in to comment.