From 909328429bf4c11a74f61a50614f418111183011 Mon Sep 17 00:00:00 2001 From: ssedoudbgouv <85867707+ssedoudbgouv@users.noreply.github.com> Date: Mon, 26 Aug 2024 14:46:53 +0200 Subject: [PATCH] TRELLO-2442: pro comment api (#1716) * TRELLO-2442: pro comment api --- app/controllers/ReportController.scala | 11 ++++-- app/models/event/Event.scala | 2 -- .../report/reportmetadata/ReportComment.scala | 10 ++++++ .../ReportAssignmentOrchestrator.scala | 35 ++++++++++++++----- app/services/emails/EmailDefinitionsPro.scala | 14 ++++++-- .../professional/reportAssigned.scala.html | 27 +++++++++++--- 6 files changed, 80 insertions(+), 19 deletions(-) create mode 100644 app/models/report/reportmetadata/ReportComment.scala diff --git a/app/controllers/ReportController.scala b/app/controllers/ReportController.scala index f95a0865a..a8641c7f4 100644 --- a/app/controllers/ReportController.scala +++ b/app/controllers/ReportController.scala @@ -10,6 +10,7 @@ import controllers.error.AppError.SpammerEmailBlocked import models._ import models.report._ import models.report.delete.ReportAdminAction +import models.report.reportmetadata.ReportComment import orchestrators._ import play.api.Logger import play.api.i18n.Lang @@ -261,10 +262,16 @@ class ReportController( } def updateReportAssignedUser(uuid: UUID, userId: UUID) = - SecuredAction.andThen(WithRole(UserRole.Professionnel)).async { implicit request => + SecuredAction.andThen(WithRole(UserRole.Professionnel)).async(parse.json) { implicit request => for { + reportComment <- request.parseBody[ReportComment]() updatedReportWithMetadata <- reportAssignmentOrchestrator - .assignReportToUser(reportId = uuid, assigningUser = request.identity, newAssignedUserId = userId) + .assignReportToUser( + reportId = uuid, + assigningUser = request.identity, + newAssignedUserId = userId, + reportComment + ) } yield Ok(Json.toJson(updatedReportWithMetadata)) } diff --git a/app/models/event/Event.scala b/app/models/event/Event.scala index 4655ba8d4..a262f1052 100644 --- a/app/models/event/Event.scala +++ b/app/models/event/Event.scala @@ -27,6 +27,4 @@ object Event { implicit val eventFormat: OFormat[Event] = Json.format[Event] def stringToDetailsJsValue(value: String): JsValue = Json.obj("description" -> value) - def jsValueToString(jsValue: Option[JsValue]) = - jsValue.flatMap(_.as[JsObject].value.get("description").map(_.toString)) } diff --git a/app/models/report/reportmetadata/ReportComment.scala b/app/models/report/reportmetadata/ReportComment.scala new file mode 100644 index 000000000..d03a3c552 --- /dev/null +++ b/app/models/report/reportmetadata/ReportComment.scala @@ -0,0 +1,10 @@ +package models.report.reportmetadata + +import play.api.libs.json.Json +import play.api.libs.json.OFormat + +case class ReportComment(comment: Option[String]) + +object ReportComment { + implicit val ReportCommentFormat: OFormat[ReportComment] = Json.format[ReportComment] +} diff --git a/app/orchestrators/ReportAssignmentOrchestrator.scala b/app/orchestrators/ReportAssignmentOrchestrator.scala index 3eccb9a83..e6c567530 100644 --- a/app/orchestrators/ReportAssignmentOrchestrator.scala +++ b/app/orchestrators/ReportAssignmentOrchestrator.scala @@ -4,10 +4,11 @@ import cats.implicits.catsSyntaxOption import controllers.error.AppError import models.User import models.event.Event -import models.event.Event.stringToDetailsJsValue import models.report.Report +import models.report.reportmetadata.ReportComment import models.report.reportmetadata.ReportWithMetadata import play.api.Logger +import play.api.libs.json.Json import repositories.event.EventRepositoryInterface import repositories.reportmetadata.ReportMetadataRepositoryInterface import repositories.user.UserRepositoryInterface @@ -32,19 +33,29 @@ class ReportAssignmentOrchestrator( ) { val logger = Logger(this.getClass) - def assignReportToUser(reportId: UUID, assigningUser: User, newAssignedUserId: UUID): Future[User] = { + def assignReportToUser( + reportId: UUID, + assigningUser: User, + newAssignedUserId: UUID, + reportComment: ReportComment + ): Future[User] = { val assigningToSelf = assigningUser.id == newAssignedUserId for { maybeReportWithMetadata <- reportOrchestrator.getVisibleReportForUser(reportId, assigningUser) reportWithMetadata <- maybeReportWithMetadata.liftTo[Future](AppError.ReportNotFound(reportId)) newAssignedUser <- checkAssignableToUser(reportWithMetadata, newAssignedUserId) _ <- reportMetadataRepository.setAssignedUser(reportId, newAssignedUserId) - _ <- createAssignmentEvent(reportWithMetadata.report, assigningUser, newAssignedUser) + _ <- createAssignmentEvent(reportWithMetadata.report, assigningUser, newAssignedUser, reportComment) _ <- if (assigningToSelf) Future.unit else mailService.send( - ProReportAssignedNotification.Email(reportWithMetadata.report, assigningUser, newAssignedUser) + ProReportAssignedNotification.Email( + reportWithMetadata.report, + assigningUser, + newAssignedUser, + reportComment.comment + ) ) } yield newAssignedUser } @@ -71,7 +82,12 @@ class ReportAssignmentOrchestrator( } yield user } - private def createAssignmentEvent(report: Report, assigningUser: User, assignedUser: User) = + private def createAssignmentEvent( + report: Report, + assigningUser: User, + assignedUser: User, + reportComment: ReportComment + ) = eventRepository .create( Event( @@ -82,10 +98,13 @@ class ReportAssignmentOrchestrator( OffsetDateTime.now(), Constants.EventType.PRO, Constants.ActionEvent.REPORT_ASSIGNED, - stringToDetailsJsValue( - s"Affectation du signalement à ${assignedUser.firstName} ${assignedUser.lastName} (${assignedUser.email})" - ) + formatEventDesc(assignedUser, reportComment) ) ) + private def formatEventDesc(assignedUser: User, reportComment: ReportComment) = + Json.obj( + "description" -> s"Affectation du signalement à ${assignedUser.fullName} (${assignedUser.email})" + ) ++ reportComment.comment.filterNot(_.isBlank).map(c => Json.obj("comment" -> c)).getOrElse(Json.obj()) + } diff --git a/app/services/emails/EmailDefinitionsPro.scala b/app/services/emails/EmailDefinitionsPro.scala index 9e9ad3e33..318e99280 100644 --- a/app/services/emails/EmailDefinitionsPro.scala +++ b/app/services/emails/EmailDefinitionsPro.scala @@ -1,6 +1,7 @@ package services.emails import cats.data.NonEmptyList +import cats.implicits.catsSyntaxOptionId import models.User import models.company.Company import models.report.ExistingReportResponse @@ -241,17 +242,24 @@ object EmailDefinitionsPro { override def examples = Seq( "report_assignement_to_other" -> ((recipient, _) => - Email(report = genReport, assigningUser = genUser, assignedUser = genUser.copy(email = recipient)) + Email( + report = genReport, + assigningUser = genUser.copy(firstName = "Max", lastName = "Payne"), + assignedUser = genUser.copy(email = recipient), + "Bonjour,Je t’affecte ce ticket car ton expertise sur ce type de problématique est bien reconnue. La tâche nécessite une attention particulière, et je suis convaincu que tu pourras gérer cela efficacement. De plus, ta connaissance approfondie sera un atout précieux pour résoudre ce problème rapidement et de manière optimale.\n\nMerci pour ton aide et n’hésite pas à me contacter si tu as besoin de plus d’informations.Cordialement,".some + ) ) ) - final case class Email(report: Report, assigningUser: User, assignedUser: User) + final case class Email(report: Report, assigningUser: User, assignedUser: User, reportComment: Option[String]) extends ProFilteredEmailSingleReport { override val recipients: List[EmailAddress] = List(assignedUser.email) override val subject: String = EmailSubjects.REPORT_ASSIGNED override def getBody: (FrontRoute, EmailAddress) => String = { (frontRoute, _) => - views.html.mails.professional.reportAssigned(report, assigningUser, assignedUser)(frontRoute).toString + views.html.mails.professional + .reportAssigned(report, assigningUser, assignedUser, reportComment)(frontRoute) + .toString } } } diff --git a/app/views/mails/professional/reportAssigned.scala.html b/app/views/mails/professional/reportAssigned.scala.html index b082fa6a0..3e7ee9c97 100644 --- a/app/views/mails/professional/reportAssigned.scala.html +++ b/app/views/mails/professional/reportAssigned.scala.html @@ -2,18 +2,37 @@ @import models.report.Report @import models.User -@(report: Report, assigningUser: User, assignedUser: User)(implicit frontRoute: FrontRoute) +@(report: Report, assigningUser: User, assignedUser: User, comment: Option[String])(implicit frontRoute: FrontRoute) @views.html.mails.layout(s"Signalement affecté") {

- Bonjour @assignedUser.firstName @assignedUser.lastName, + Bonjour @assignedUser.fullName,

Un signalement vous a été affecté par @assigningUser.firstName @assigningUser.lastName.

+ @if(comment.isDefined) { +

+ @assigningUser.fullName vous a laissé un commentaire : +

+
+ @comment.getOrElse("") +
+ } +

- Pour le consulter et y apporter une réponse, il vous suffit de vous connecter sur votre - Espace Professionnel à l'aide de votre adresse e-mail et du mot de passe que vous avez créé à votre première connexion sur SignalConso. + Pour consulter le signalement et y apporter une réponse, il vous suffit de vous connecter sur votre + Espace Professionnel + à l'aide de votre adresse e-mail et du mot de passe que vous avez créé à votre première connexion sur SignalConso.

Cordialement,