-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaultReportCalculationChangeUseCase.kt
54 lines (45 loc) · 2.41 KB
/
DefaultReportCalculationChangeUseCase.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
54
package com.aamdigital.aambackendservice.reporting.reportcalculation.usecase
import com.aamdigital.aambackendservice.changes.domain.DocumentChangeEvent
import com.aamdigital.aambackendservice.domain.DomainReference
import com.aamdigital.aambackendservice.reporting.domain.ReportCalculationStatus
import com.aamdigital.aambackendservice.reporting.notification.core.NotificationService
import com.aamdigital.aambackendservice.reporting.reportcalculation.core.ReportCalculationChangeUseCase
import com.aamdigital.aambackendservice.reporting.reportcalculation.core.ReportCalculationStorage
import com.aamdigital.aambackendservice.reporting.reportcalculation.storage.ReportCalculationEntity
import com.fasterxml.jackson.databind.ObjectMapper
import org.slf4j.LoggerFactory
class DefaultReportCalculationChangeUseCase(
private val reportCalculationStorage: ReportCalculationStorage,
private val objectMapper: ObjectMapper,
private val notificationService: NotificationService,
) : ReportCalculationChangeUseCase {
private val logger = LoggerFactory.getLogger(javaClass)
override fun handle(documentChangeEvent: DocumentChangeEvent) {
val currentReportCalculation =
objectMapper.convertValue(documentChangeEvent.currentVersion, ReportCalculationEntity::class.java)
if (currentReportCalculation.status != ReportCalculationStatus.FINISHED_SUCCESS) {
return
}
try {
val calculations = reportCalculationStorage.fetchReportCalculations(
report = currentReportCalculation.report
)
val existingDigest = calculations
.filter { it.id != currentReportCalculation.id }
.sortedBy { it.calculationCompleted }
.lastOrNull()?.attachments?.get("data.json")?.digest
val currentDigest = currentReportCalculation.attachments["data.json"]?.digest
if (existingDigest != currentDigest
) {
notificationService.sendNotifications(
report = currentReportCalculation.report,
reportCalculation = DomainReference(currentReportCalculation.id)
)
} else {
logger.debug("skipped notification for ${currentReportCalculation.id}")
}
} catch (ex: Exception) {
logger.warn("Could not fetch ${currentReportCalculation.id}. Skipped.", ex)
}
}
}