From 830160fe415c155f35e419b5269474b2035e3048 Mon Sep 17 00:00:00 2001 From: niladic Date: Wed, 9 Oct 2024 17:44:21 +0200 Subject: [PATCH] =?UTF-8?q?D=C3=A9sactive=20les=20email=20pour=20les=20uti?= =?UTF-8?q?lisateurs=20inactifs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/NotificationService.scala | 60 +++++++++++++++++--------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/app/services/NotificationService.scala b/app/services/NotificationService.scala index aade5cfa..c7424f6e 100644 --- a/app/services/NotificationService.scala +++ b/app/services/NotificationService.scala @@ -4,7 +4,7 @@ import cats.syntax.all._ import constants.Constants import controllers.routes import helper.EmailHelper.quoteEmailPhrase -import java.time.ZoneId +import java.time.{Instant, ZoneId} import java.util.UUID import javax.inject.{Inject, Singleton} import models._ @@ -70,9 +70,18 @@ class NotificationService @Inject() ( .byIds(application.invitedGroupIdsAtCreation) .filter(_.email.nonEmpty) - users - .map(generateInvitationEmail(application)) - .foreach(email => emailsService.sendBlocking(email, EmailPriority.Normal)) + eventService.lastActivity(users.map(_.id)).foreach { lastActivityResult => + val lastActivity: Map[UUID, Instant] = lastActivityResult.getOrElse(Nil).toMap + users + .filter(user => + lastActivity + .get(user.id) + .map(_.isAfter(Instant.now().minusSeconds(365 * 24 * 60 * 60))) + .getOrElse(false) + ) + .map(generateInvitationEmail(application)) + .foreach(email => emailsService.sendBlocking(email, EmailPriority.Normal)) + } groups .map(generateNotificationBALEmail(application, None, users)) @@ -97,23 +106,32 @@ class NotificationService @Inject() ( } // Send emails to users - users - .flatMap { user => - if (user.id === answer.creatorUserID) { - None - } else if ( - !Authorization.canSeeAnswer(answer, application)(Authorization.readUserRights(user)) - ) { - None - } else if (!user.instructor) { // Temporary case - None - } else if (answer.invitedUsers.contains(user.id)) { - Some(generateInvitationEmail(application, Some(answer))(user)) - } else { - Some(generateAnswerEmail(application, answer)(user)) + eventService.lastActivity(users.map(_.id)).foreach { lastActivityResult => + val lastActivity: Map[UUID, Instant] = lastActivityResult.getOrElse(Nil).toMap + users + .filter(user => + lastActivity + .get(user.id) + .map(_.isAfter(Instant.now().minusSeconds(365 * 24 * 60 * 60))) + .getOrElse(false) + ) + .flatMap { user => + if (user.id === answer.creatorUserID) { + None + } else if ( + !Authorization.canSeeAnswer(answer, application)(Authorization.readUserRights(user)) + ) { + None + } else if (!user.instructor) { // Temporary case + None + } else if (answer.invitedUsers.contains(user.id)) { + Some(generateInvitationEmail(application, Some(answer))(user)) + } else { + Some(generateAnswerEmail(application, answer)(user)) + } } - } - .foreach(email => emailsService.sendBlocking(email, EmailPriority.Normal)) + .foreach(email => emailsService.sendBlocking(email, EmailPriority.Normal)) + } val usersEmails: Set[String] = users.map(_.email).toSet @@ -130,10 +148,12 @@ class NotificationService @Inject() ( .foreach(email => emailsService.sendBlocking(email, EmailPriority.Normal)) if (answer.visibleByHelpers && answer.creatorUserID =!= application.creatorUserId) { + /* This is a temporary comment userService .byId(application.creatorUserId) .map(generateAnswerEmail(application, answer)) .foreach(email => emailsService.sendBlocking(email, EmailPriority.Normal)) + */ } } }