-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotificationService.kt
53 lines (45 loc) · 2.03 KB
/
NotificationService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.aamdigital.aambackendservice.reporting.notification.core
import com.aamdigital.aambackendservice.domain.DomainReference
import com.aamdigital.aambackendservice.reporting.domain.event.NotificationEvent
import com.aamdigital.aambackendservice.reporting.notification.di.ReportingNotificationQueueConfiguration
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@Service
class NotificationService(
private val notificationStorage: NotificationStorage,
private val notificationEventPublisher: NotificationEventPublisher
) {
private val logger = LoggerFactory.getLogger(javaClass)
fun getAffectedWebhooks(report: DomainReference): List<DomainReference> {
val webhooks = notificationStorage.fetchAllWebhooks()
val affectedWebhooks: MutableList<DomainReference> = mutableListOf()
webhooks.forEach { webhook ->
if (webhook.reportSubscriptions.contains(report)) {
affectedWebhooks.add(DomainReference(webhook.id))
}
}
return affectedWebhooks
}
fun sendNotifications(report: DomainReference, reportCalculation: DomainReference) {
logger.debug("[NotificationService]: Trigger all affected webhooks for ${report.id}")
val affectedWebhooks = getAffectedWebhooks(report)
affectedWebhooks.map { webhook ->
triggerWebhook(
report = report,
reportCalculation = reportCalculation,
webhook = webhook
)
}
}
fun triggerWebhook(report: DomainReference, reportCalculation: DomainReference, webhook: DomainReference) {
logger.debug("[NotificationService]: Trigger NotificationEvent for ${webhook.id} and ${report.id}")
notificationEventPublisher.publish(
ReportingNotificationQueueConfiguration.NOTIFICATION_QUEUE,
NotificationEvent(
webhookId = webhook.id,
reportId = report.id,
calculationId = reportCalculation.id
)
)
}
}