Skip to content

Commit

Permalink
Détermine les icônes en fonction des organismes
Browse files Browse the repository at this point in the history
  • Loading branch information
niladic committed Sep 4, 2023
1 parent 0a5635f commit 8bc9761
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 36 deletions.
14 changes: 11 additions & 3 deletions app/controllers/ApplicationController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -876,17 +876,24 @@ case class ApplicationController @Inject() (
areaInQueryString
.getOrElse(Area.fromId(application.area).getOrElse(Area.all.head))
val selectedAreaId = selectedArea.id
val applicationUsers: List[UUID] =
application.creatorUserId :: application.invitedUsers.map(_._1).toList :::
application.answers.map(_.creatorUserID)
usersWhoCanBeInvitedOn(application, selectedAreaId).flatMap { usersWhoCanBeInvited =>
groupsWhichCanBeInvited(selectedAreaId, application).flatMap { invitableGroups =>
fileService
.byApplicationId(application.id)
val filesF = EitherT(fileService.byApplicationId(application.id))
val organisationsF = EitherT(userService.usersOrganisations(applicationUsers))
(for {
files <- filesF
organisations <- organisationsF
} yield (files, organisations)).value
.map(
_.fold(
error => {
eventService.logError(error)
InternalServerError(Constants.genericError500Message)
},
files => {
{ case (files, organisations) =>
val groups = userGroupService
.byIds(usersWhoCanBeInvited.flatMap(_.groupIds))
.filter(_.areaIds.contains[UUID](selectedAreaId))
Expand All @@ -903,6 +910,7 @@ case class ApplicationController @Inject() (
selectedArea,
readSharedAccountUserSignature(request.session),
files,
organisations
)
).withHeaders(CACHE_CONTROL -> "no-store")
}
Expand Down
2 changes: 2 additions & 0 deletions app/models/EventType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ object EventType {
object SearchUsersNotAuthorized extends Warn
object SearchUsersError extends Error

object UsersQueryError extends Error

// Signups
object SignupFormShowed extends Info
object SignupFormValidationError extends Warn
Expand Down
28 changes: 20 additions & 8 deletions app/models/Organisation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,20 @@ object Organisation {
val association = Organisation("Association", "Association")
val cafId = Organisation.Id("CAF")
val caf = Organisation(cafId, "CAF", "Caisse d’allocations familiales")
val carsatId = Organisation.Id("CARSAT")
val cpamId = Organisation.Id("CPAM")
val cpam = Organisation(cpamId, "CPAM", "Caisse primaire d'assurance maladie")
val cnavId = Organisation.Id("CNAV")
val cnamId = Organisation.Id("CNAM")
val cramId = Organisation.Id("CRAM")
val ddfipId = Organisation.Id("DDFIP")
val drfipId = Organisation.Id("DRFIP")
val franceServicesId = Organisation.Id("MFS")
val franceServices = Organisation(franceServicesId, "FS", "France Services")
val msaId = Organisation.Id("MSA")
val msap = Organisation("MSAP", "Maison de services au public")
val hopital = Organisation("Hôpital", "Hôpital")
val poleEmploiId = Organisation.Id("Pôle emploi")
val prefId = Organisation.Id("Préf")
val pref = Organisation(prefId, "Préf", "Préfecture")
val sousPrefId = Organisation.Id("Sous-Préf")
Expand All @@ -80,26 +88,30 @@ object Organisation {
association,
Organisation("BDF", "Banque de France"),
caf, // Département
Organisation("CARSAT", "Caisse d'assurance retraite et de la santé au travail"), //
Organisation(carsatId, "CARSAT", "Caisse d'assurance retraite et de la santé au travail"),
Organisation("CCAS", "Centre communal d'action sociale"), // Ville
Organisation("CDAD", "Conseils départementaux d'accès au droit"), // Département
Organisation("CNAV", "Caisse nationale d'assurance vieillesse"), // Département
Organisation(cnavId, "CNAV", "Caisse nationale d'assurance vieillesse"), // Département
cpam, // Département
Organisation("CNAM", "Caisse nationale d'assurance maladie"),
Organisation("CRAM", "Caisse régionale d'assurance maladie"), // Région
Organisation("DDFIP", "Direction départementale des Finances publiques"), // Département
Organisation(cnamId, "CNAM", "Caisse nationale d'assurance maladie"),
Organisation(cramId, "CRAM", "Caisse régionale d'assurance maladie"), // Région
Organisation(
ddfipId,
"DDFIP",
"Direction départementale des Finances publiques"
), // Département
Organisation("Département", "Conseil départemental"),
Organisation("DRFIP", "Direction régionale des Finances publiques"), // Région
Organisation(drfipId, "DRFIP", "Direction régionale des Finances publiques"), // Région
hopital, // Ville
Organisation("OFPRA", "Office français de protection des réfugiés et apatrides"), // Nationale
Organisation("La Poste", "La Poste"),
Organisation("Mairie", "Mairie"), // Ville
Organisation("MDPH", "Maison départementale des personnes handicapées"),
franceServices,
Organisation("Mission locale", "Mission locale"), // Ville
Organisation("MSA", "Mutualité sociale agricole"),
Organisation(msaId, "MSA", "Mutualité sociale agricole"),
msap, // Ville
Organisation("Pôle emploi", "Pôle emploi"),
Organisation(poleEmploiId, "Pôle emploi", "Pôle emploi"),
pref, // Département
sousPref,
Organisation(
Expand Down
34 changes: 33 additions & 1 deletion app/services/UserService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import helper.{Hash, StringHelper, Time, UUIDHelper}
import java.sql.Connection
import java.util.UUID
import javax.inject.Inject
import models.{Error, EventType, User}
import models.{Error, EventType, Organisation, User}
import models.dataModels.UserRow
import modules.AppConfig
import org.postgresql.util.PSQLException
Expand Down Expand Up @@ -179,6 +179,38 @@ class UserService @Inject() (
def byEmailsFuture(emails: List[String]): Future[Either[Error, List[User]]] =
Future(byEmails(emails).asRight)

def usersOrganisations(
userIds: List[UUID]
): Future[Either[Error, Map[UUID, List[Organisation.Id]]]] =
Future(
Try {
val ids = userIds.distinct
db.withConnection { implicit connection =>
SQL(s"""SELECT u.id as user_id, g.organisation
FROM "user" u, UNNEST(u.group_ids) as gid
JOIN user_group g ON g.id = gid
WHERE g.organisation IS NOT NULL
AND ARRAY[{ids}]::uuid[] @> ARRAY[u.id]::uuid[]
""")
.on("ids" -> ids)
.as((SqlParser.get[UUID]("user_id") ~ SqlParser.get[String]("organisation")).*)
.map(SqlParser.flatten)
.groupBy(_._1)
.view
.mapValues(_.map { case (_, id) => Organisation.Id(id) })
.toMap
}
}.toEither.left
.map(e =>
Error.SqlException(
EventType.UsersQueryError,
s"Impossible de lister les organismes d'un utilisateur",
e,
none
)
)
)

// Note: empty string will return an `Error`
//
// The configuration is
Expand Down
66 changes: 46 additions & 20 deletions app/views/application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import controllers.routes.{ApplicationController, Assets, UserController}
import helper.Time
import helpers.forms.CSRFInput
import java.util.UUID
import models.{Answer, Application, Area, Authorization, FileMetadata, User, UserGroup}
import models.{
Answer,
Application,
Area,
Authorization,
FileMetadata,
Organisation,
User,
UserGroup
}
import modules.AppConfig
import org.webjars.play.WebJarsUtil
import play.api.data.Form
Expand Down Expand Up @@ -118,31 +127,43 @@ object application {
)
}

def organisationIcon(creatorUserName: String): Tag =
Map(
"A+" -> "aplus",
"Pôle Emploi".toUpperCase() -> "pe",
"MSA" -> "msa",
"CPAM" -> "cpam",
"CRAM" -> "cpam",
"CNAM" -> "cpam",
"CAF" -> "caf",
"CNAF" -> "caf",
"CNAV" -> "cnav",
"CARSAT" -> "cnav",
"DGFIP" -> "dgfip",
"DDFIP" -> "dgfip",
"DRFIP" -> "dgfip",
"Défenseur des droits".toUpperCase() -> "ddd"
).find { case (name, _) => creatorUserName.toUpperCase.contains(name) } match {
case Some((_, icon)) =>
def organisationIcon(
userId: UUID,
creatorUserName: String,
usersOrganisations: Map[UUID, List[Organisation.Id]]
): Tag = {
val icons = Map(
Organisation.poleEmploiId -> "pe",
Organisation.msaId -> "msa",
Organisation.cpamId -> "cpam",
Organisation.cramId -> "cpam",
Organisation.cnamId -> "cpam",
Organisation.cafId -> "caf",
Organisation.cnavId -> "cnav",
Organisation.carsatId -> "cnav",
Organisation.ddfipId -> "dgfip",
Organisation.drfipId -> "dgfip",
)
val iconName: Option[String] = for {
organisations <- usersOrganisations.get(userId)
name <- organisations.flatMap(id => icons.get(id)).headOption
} yield name

iconName.orElse(
Map(
"A+" -> "aplus",
"Défenseur des droits".toUpperCase() -> "ddd"
).find { case (name, _) => creatorUserName.toUpperCase.contains(name) }.map(_._2)
) match {
case Some(icon) =>
img(
cls := "mdl-list__item-avatar",
src := Assets.versioned("images/admin/" + icon + "-icon.png").url
)
case None =>
i(cls := "material-icons mdl-list__item-avatar", "person")
}
}

def closeApplicationModal(
applicationId: UUID
Expand Down Expand Up @@ -275,6 +296,7 @@ object application {
attachments: List[FileMetadata],
currentUser: User,
currentUserRights: Authorization.UserRights,
usersOrganisations: Map[UUID, List[Organisation.Id]],
config: AppConfig,
): Frag =
frag(
Expand Down Expand Up @@ -366,7 +388,11 @@ object application {
cls := "mdl-list__item",
div(
cls := "mdl-list__item-primary-content",
organisationIcon(answer.creatorUserName),
organisationIcon(
answer.creatorUserID,
answer.creatorUserName,
usersOrganisations
),
span(cls := "single--font-weight-600", answer.creatorUserName),
(currentUser.admin).some
.filter(identity)
Expand Down
8 changes: 4 additions & 4 deletions app/views/showApplication.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@import models.Answer.AnswerType
@import serializers.Keys

@(currentUser: User, currentUserRights: Authorization.UserRights)(groupsWithUsersThatCanBeInvited: List[(UserGroup,List[User])], groupsThatCanBeInvited: List[UserGroup], application: Application, answerToAgentsForm: Form[_], openedTab: String, selectedArea: Area, userSignature: Option[String], attachments: List[FileMetadata])(implicit webJarsUtil: org.webjars.play.WebJarsUtil, flash: Flash, messagesProvider: MessagesProvider, request: RequestHeader, mainInfos: MainInfos)
@(currentUser: User, currentUserRights: Authorization.UserRights)(groupsWithUsersThatCanBeInvited: List[(UserGroup,List[User])], groupsThatCanBeInvited: List[UserGroup], application: Application, answerToAgentsForm: Form[_], openedTab: String, selectedArea: Area, userSignature: Option[String], attachments: List[FileMetadata], usersOrganisations: Map[java.util.UUID, List[Organisation.Id]])(implicit webJarsUtil: org.webjars.play.WebJarsUtil, flash: Flash, messagesProvider: MessagesProvider, request: RequestHeader, mainInfos: MainInfos)


@main(currentUser, currentUserRights, modals = toHtml(views.application.closeApplicationModal(application.id)))(s"Demande de ${application.creatorUserName} - ${Area.fromId(application.area).get.name}") {
Expand Down Expand Up @@ -232,7 +232,7 @@ <h2 class="mdl-card__title-text">@application.subject</h2>
Créée par
<div class="mdl-list__item">
<div class="mdl-list__item-primary-content">
@toHtml(views.application.organisationIcon(application.creatorUserName))
@toHtml(views.application.organisationIcon(application.creatorUserId, application.creatorUserName, usersOrganisations))
<span>
@application.creatorUserName
@if(Authorization.isAdmin(currentUserRights)) {
Expand All @@ -249,7 +249,7 @@ <h2 class="mdl-card__title-text">@application.subject</h2>
@for((userId, userName) <- application.invitedUsers) {
<div class="mdl-list__item">
<div class="mdl-list__item-primary-content">
@toHtml(views.application.organisationIcon(userName))
@toHtml(views.application.organisationIcon(userId, userName, usersOrganisations))
<span>
@userName
@if(Authorization.isAdmin(currentUserRights)) {
Expand Down Expand Up @@ -279,7 +279,7 @@ <h2 class="mdl-card__title-text">@application.subject</h2>
</div>
</div>

@toHtml(views.application.answerThread(application, attachments, currentUser, currentUserRights, mainInfos.config))
@toHtml(views.application.answerThread(application, attachments, currentUser, currentUserRights, usersOrganisations, mainInfos.config))



Expand Down

0 comments on commit 8bc9761

Please sign in to comment.