Skip to content

Commit

Permalink
#2249 Send notification in all viewing organisations
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Nov 18, 2021
1 parent 1eaaa6a commit a0f4ab8
Showing 1 changed file with 56 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,64 +156,68 @@ class NotificationActor @Inject() (
.auditContextObjectOrganisation
.toIterator
.foreach {
case (audit, context, obj, Some(organisation)) =>
logger.debug(s"Notification is related to $audit, $context, $organisation")
triggerMap
.getOrElse(organisation._id, Map.empty)
.foreach {
case (trigger, (inOrg, userIds)) if trigger.preFilter(audit, context, organisation) =>
logger.debug(s"Notification trigger ${trigger.name} is applicable for $audit")
if (userIds.nonEmpty)
userSrv
.getByIds(userIds: _*)
.project(
_.by
.by(_.config("notification").value(_.value).fold)
)
.toIterator
.foreach {
case (user, notificationConfig) =>
val config = notificationConfig.flatMap(_.asOpt[NotificationConfig])
executeNotification(Some(user), config, audit, context, obj, organisation)
}
if (inOrg)
organisationSrv
.get(organisation)
.config
.has(_.name, "notification")
.value(_.value)
.toIterator
.foreach { notificationConfig: JsValue =>
val (userConfig, orgConfig) = notificationConfig
.asOpt[Seq[NotificationConfig]]
.getOrElse(Nil)
.partition(_.delegate)
organisationSrv
.get(organisation)
.users
.filter(_.config.hasNot(_.name, "notification"))
.toIterator
.foreach { user =>
executeNotification(Some(user), userConfig, audit, context, obj, organisation)
}
executeNotification(None, orgConfig, audit, context, obj, organisation)
}
case (trigger, _) => logger.debug(s"Notification trigger ${trigger.name} is NOT applicable for $audit")
}
case (audit, context, obj, organisations) =>
logger.debug(s"Notification is related to $audit, $context, ${organisations.map(_.name).mkString(",")}")
organisations.foreach { organisation =>
triggerMap
.getOrElse(organisation._id, Map.empty)
.foreach {
case (trigger, (inOrg, userIds)) if trigger.preFilter(audit, context, organisation) =>
logger.debug(s"Notification trigger ${trigger.name} is applicable for $audit")
if (userIds.nonEmpty)
userSrv
.getByIds(userIds: _*)
.project(
_.by
.by(_.config("notification").value(_.value).fold)
)
.toIterator
.foreach {
case (user, notificationConfig) =>
val config = notificationConfig.flatMap(_.asOpt[NotificationConfig])
executeNotification(Some(user), config, audit, context, obj, organisation)
}
if (inOrg)
organisationSrv
.get(organisation)
.config
.has(_.name, "notification")
.value(_.value)
.toIterator
.foreach { notificationConfig: JsValue =>
val (userConfig, orgConfig) = notificationConfig
.asOpt[Seq[NotificationConfig]]
.getOrElse(Nil)
.partition(_.delegate)
organisationSrv
.get(organisation)
.users
.filter(_.config.hasNot(_.name, "notification"))
.toIterator
.foreach { user =>
executeNotification(Some(user), userConfig, audit, context, obj, organisation)
}
executeNotification(None, orgConfig, audit, context, obj, organisation)
}
case (trigger, _) => logger.debug(s"Notification trigger ${trigger.name} is NOT applicable for $audit")
}
}
case _ =>
}
}
case NotificationExecution(userId, auditId, notificationConfig) =>
db.roTransaction { implicit graph =>
auditSrv.getByIds(auditId).auditContextObjectOrganisation.getOrFail("Audit").foreach {
case (audit, context, obj, Some(organisation)) =>
for {
user <- userId.map(userSrv.getOrFail).flip
trigger <- notificationSrv.getTrigger(notificationConfig.triggerConfig)
if trigger.filter(audit, context, organisation, user)
notifier <- notificationSrv.getNotifier(notificationConfig.notifierConfig)
_ = logger.debug(s"Execution of notifier ${notifier.name} for user $user")
} yield notifier.execute(audit, context, obj, organisation, user)
case (audit, context, obj, organisations) =>
organisations.foreach { organisation =>
for {
user <- userId.map(userSrv.getOrFail).flip
trigger <- notificationSrv.getTrigger(notificationConfig.triggerConfig)
if trigger.filter(audit, context, organisation, user)
notifier <- notificationSrv.getNotifier(notificationConfig.notifierConfig)
_ = logger.debug(s"Execution of notifier ${notifier.name} for user $user")
} yield notifier.execute(audit, context, obj, organisation, user)
}
case _ => // TODO
}
}
Expand Down

0 comments on commit a0f4ab8

Please sign in to comment.