1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.technical.crm
21 
22 import akka.actor.typed.{ActorSystem, Behavior}
23 import org.make.api.technical.KafkaConsumerBehavior
24 import org.make.api.user.UserService
25 import org.make.core.user.MailingErrorLog
26 
27 import java.time.ZonedDateTime
28 import scala.concurrent.ExecutionContext.Implicits.global
29 import scala.concurrent.Future
30 
31 class MailJetEventConsumerBehavior(userService: UserService) extends KafkaConsumerBehavior[MailJetEventWrapper] {
32 
33   override protected val topicKey: String = MailJetEventProducerBehavior.topicKey
34   override val groupId: String = "mailJet-event-consumer"
35 
36   override def handleMessage(message: MailJetEventWrapper)(system: ActorSystem[_]): Future[_] = {
37     message.event match {
38       case event: MailJetBounceEvent      => handleBounceEvent(event, message.date)
39       case event: MailJetBlockedEvent     => doNothing(event)
40       case event: MailJetSpamEvent        => handleSpamEvent(event)
41       case event: MailJetUnsubscribeEvent => handleUnsubscribeEvent(event)
42       case event                          => doNothing(event)
43     }
44   }
45 
46   def handleBounceEvent(event: MailJetBounceEvent, date: ZonedDateTime): Future[Boolean] = {
47     for {
48       resultUpdateBounce <- userService.updateIsHardBounce(email = event.email, isHardBounce = event.hardBounce)
49       resultUpdateError  <- registerMailingError(email = event.email, maybeError = event.error, date = date)
50     } yield resultUpdateBounce && resultUpdateError
51   }
52 
53   def handleSpamEvent(event: MailJetSpamEvent): Future[Boolean] = {
54     userService.updateOptInNewsletter(email = event.email, optInNewsletter = false)
55   }
56 
57   def handleUnsubscribeEvent(event: MailJetUnsubscribeEvent): Future[Boolean] = {
58     userService.updateOptInNewsletter(email = event.email, optInNewsletter = false)
59   }
60 
61   private def registerMailingError(
62     email: String,
63     maybeError: Option[MailJetError],
64     date: ZonedDateTime
65   ): Future[Boolean] = {
66     maybeError match {
67       case None => Future.successful(false)
68       case Some(error) =>
69         userService.updateLastMailingError(
70           email = email,
71           lastMailingError = Some(MailingErrorLog(error = error.name, date = date))
72         )
73     }
74   }
75 }
76 
77 object MailJetEventConsumerBehavior {
78   def apply(userService: UserService): Behavior[KafkaConsumerBehavior.Protocol] =
79     new MailJetEventConsumerBehavior(userService).createBehavior(name)
80   val name: String = "mailJet-event-consumer"
81 }
Line Stmt Id Pos Tree Symbol Tests Code
33 7054 1241 - 1278 Select org.make.api.technical.crm.MailJetEventProducerBehavior.topicKey MailJetEventProducerBehavior.topicKey
34 6220 1312 - 1336 Literal <nosymbol> "mailJet-event-consumer"
37 5854 1440 - 1453 Select org.make.api.technical.crm.MailJetEventWrapper.event message.event
38 6420 1507 - 1545 Apply org.make.api.technical.crm.MailJetEventConsumerBehavior.handleBounceEvent MailJetEventConsumerBehavior.this.handleBounceEvent(event, message.date)
38 7302 1532 - 1544 Select org.make.api.technical.crm.MailJetEventWrapper.date message.date
39 7747 1591 - 1607 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing MailJetEventConsumerBehavior.this.doNothing(event)
40 7015 1653 - 1675 Apply org.make.api.technical.crm.MailJetEventConsumerBehavior.handleSpamEvent MailJetEventConsumerBehavior.this.handleSpamEvent(event)
41 6556 1721 - 1750 Apply org.make.api.technical.crm.MailJetEventConsumerBehavior.handleUnsubscribeEvent MailJetEventConsumerBehavior.this.handleUnsubscribeEvent(event)
42 5707 1796 - 1812 Apply org.make.api.technical.KafkaConsumerBehavior.doNothing MailJetEventConsumerBehavior.this.doNothing(event)
48 6294 2022 - 2038 Select org.make.api.technical.crm.MailJetBounceEvent.hardBounce event.hardBounce
48 5669 1921 - 2200 ApplyToImplicitArgs scala.concurrent.Future.flatMap MailJetEventConsumerBehavior.this.userService.updateIsHardBounce(event.email, event.hardBounce).flatMap[Boolean](((resultUpdateBounce: Boolean) => MailJetEventConsumerBehavior.this.registerMailingError(event.email, event.error, date).map[Boolean](((resultUpdateError: Boolean) => resultUpdateBounce.&&(resultUpdateError)))(scala.concurrent.ExecutionContext.Implicits.global)))(scala.concurrent.ExecutionContext.Implicits.global)
48 7137 1994 - 2005 Select org.make.api.technical.crm.MailJetBounceEvent.email event.email
48 6562 1952 - 1952 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
49 6979 2046 - 2200 ApplyToImplicitArgs scala.concurrent.Future.map MailJetEventConsumerBehavior.this.registerMailingError(event.email, event.error, date).map[Boolean](((resultUpdateError: Boolean) => resultUpdateBounce.&&(resultUpdateError)))(scala.concurrent.ExecutionContext.Implicits.global)
49 7272 2123 - 2134 Select org.make.api.technical.crm.MailJetBounceEvent.error event.error
49 7834 2065 - 2065 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
49 7710 2097 - 2108 Select org.make.api.technical.crm.MailJetBounceEvent.email event.email
50 6425 2161 - 2200 Apply scala.Boolean.&& resultUpdateBounce.&&(resultUpdateError)
54 7140 2320 - 2331 Select org.make.api.technical.crm.MailJetSpamEvent.email event.email
54 7669 2278 - 2357 Apply org.make.api.user.UserService.updateOptInNewsletter MailJetEventConsumerBehavior.this.userService.updateOptInNewsletter(event.email, false)
54 6257 2351 - 2356 Literal <nosymbol> false
58 6384 2522 - 2527 Literal <nosymbol> false
58 7737 2449 - 2528 Apply org.make.api.user.UserService.updateOptInNewsletter MailJetEventConsumerBehavior.this.userService.updateOptInNewsletter(event.email, false)
58 7204 2491 - 2502 Select org.make.api.technical.crm.MailJetUnsubscribeEvent.email event.email
67 6941 2718 - 2742 Apply scala.concurrent.Future.successful scala.concurrent.Future.successful[Boolean](false)
69 6217 2777 - 2931 Apply org.make.api.user.UserService.updateLastMailingError MailJetEventConsumerBehavior.this.userService.updateLastMailingError(email, scala.Some.apply[org.make.core.user.MailingErrorLog](org.make.core.user.MailingErrorLog.apply(error.name, date)))
71 7144 2867 - 2921 Apply scala.Some.apply scala.Some.apply[org.make.core.user.MailingErrorLog](org.make.core.user.MailingErrorLog.apply(error.name, date))
71 5677 2872 - 2920 Apply org.make.core.user.MailingErrorLog.apply org.make.core.user.MailingErrorLog.apply(error.name, date)
71 6531 2896 - 2906 Select org.make.api.technical.crm.MailJetError.name error.name
79 7189 3069 - 3135 Apply org.make.api.technical.KafkaConsumerBehavior.createBehavior new MailJetEventConsumerBehavior(userService).createBehavior(MailJetEventConsumerBehavior.this.name)
79 7617 3130 - 3134 Select org.make.api.technical.crm.MailJetEventConsumerBehavior.name MailJetEventConsumerBehavior.this.name
80 6391 3157 - 3181 Literal <nosymbol> "mailJet-event-consumer"