-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaultApplyNotificationRulesUseCase.kt
228 lines (195 loc) · 8.24 KB
/
DefaultApplyNotificationRulesUseCase.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package com.aamdigital.aambackendservice.notification.core
import com.aamdigital.aambackendservice.changes.domain.DocumentChangeEvent
import com.aamdigital.aambackendservice.domain.UseCaseOutcome
import com.aamdigital.aambackendservice.notification.core.event.CreateUserNotificationEvent
import com.aamdigital.aambackendservice.notification.di.NotificationQueueConfiguration.Companion.USER_NOTIFICATION_QUEUE
import com.aamdigital.aambackendservice.notification.domain.NotificationChannelType
import com.aamdigital.aambackendservice.notification.repositiory.NotificationConditionEntity
import com.aamdigital.aambackendservice.notification.repositiory.NotificationConfigRepository
import com.aamdigital.aambackendservice.notification.repositiory.NotificationRuleEntity
import org.apache.commons.lang3.StringUtils
import kotlin.jvm.optionals.getOrNull
class DefaultApplyNotificationRulesUseCase(
val notificationConfigRepository: NotificationConfigRepository,
val userNotificationPublisher: UserNotificationPublisher,
) : ApplyNotificationRulesUseCase() {
override fun apply(request: ApplyNotificationRulesRequest): UseCaseOutcome<ApplyNotificationRulesData> {
val changedEntity = request.documentChangeEvent.documentId.split(":").first()
val changeType = extractChangeType(request.documentChangeEvent)
val notificationConfigurations = notificationConfigRepository.findAll() // todo database access optimization
val userRuleMappings = notificationConfigurations.groupBy { it.userIdentifier }
.map { userRuleMapping ->
userRuleMapping.key to userRuleMapping.value.flatMap { notificationConfig ->
notificationConfig.notificationRules
.filter { it.enabled }
.filter { it.entityType == changedEntity }
.filter { it.changeType == changeType }
}
}.toMap()
return applyRules(userRuleMappings, request.documentChangeEvent)
}
private fun extractChangeType(documentChangeEvent: DocumentChangeEvent): String {
if (documentChangeEvent.deleted) {
// return "deleted" // todo frontend support for this?
return "updated"
}
if (documentChangeEvent.previousVersion.isEmpty()) {
return "created"
}
return "updated"
}
private fun applyRules(
userRoleMapping: Map<String, List<NotificationRuleEntity>>,
documentChangeEvent: DocumentChangeEvent,
): UseCaseOutcome<ApplyNotificationRulesData> {
userRoleMapping.forEach { rule ->
appleRulesForUser(
userIdentifier = rule.key,
rules = rule.value,
documentChangeEvent = documentChangeEvent
)
}
return UseCaseOutcome.Success(
ApplyNotificationRulesData(
0 // todo useful summery here
)
)
}
private fun appleRulesForUser(
userIdentifier: String,
rules: List<NotificationRuleEntity>,
documentChangeEvent: DocumentChangeEvent,
) {
rules.forEach { rule ->
logger.trace("{} -> {}", userIdentifier, rule)
val userConfig = notificationConfigRepository.findByUserIdentifier(userIdentifier).getOrNull() ?: return
rule.conditions.forEach { condition ->
val conditionOutcome: Boolean = checkConditionForDocument(condition, documentChangeEvent)
logger.trace(
"rule {} outcome {} for user {}",
rule.externalIdentifier,
conditionOutcome,
userIdentifier
)
if (!conditionOutcome) {
return
}
}
userNotificationPublisher.publish(
channel = USER_NOTIFICATION_QUEUE,
event = CreateUserNotificationEvent(
userIdentifier = userIdentifier,
notificationChannelType = NotificationChannelType.APP,
notificationRule = rule.externalIdentifier
)
)
if (userConfig.channelPush) {
userNotificationPublisher.publish(
channel = USER_NOTIFICATION_QUEUE,
event = CreateUserNotificationEvent(
userIdentifier = userIdentifier,
notificationChannelType = NotificationChannelType.PUSH,
notificationRule = rule.externalIdentifier
)
)
}
if (userConfig.channelEmail) {
userNotificationPublisher.publish(
channel = USER_NOTIFICATION_QUEUE,
event = CreateUserNotificationEvent(
userIdentifier = userIdentifier,
notificationChannelType = NotificationChannelType.EMAIL,
notificationRule = rule.externalIdentifier
)
)
}
}
}
/**
* todo: move this to an separate class with extended testing
*/
private fun checkConditionForDocument(
condition: NotificationConditionEntity,
documentChangeEvent: DocumentChangeEvent
): Boolean {
val currentValue = documentChangeEvent.currentVersion[condition.field]
val conditionValue = condition.value
return when (condition.operator) {
"\$eq" -> {
return when (currentValue) {
is String -> currentValue == conditionValue
is ArrayList<*> ->
currentValue.size == 1 && currentValue.first() == conditionValue
else -> false
}
}
"\$nq" -> {
return when (currentValue) {
is String -> currentValue != conditionValue
is ArrayList<*> ->
currentValue.size == 1 && currentValue.first() != conditionValue
else -> false
}
}
"\$elemMatch" -> {
return when (currentValue) {
is String -> currentValue == conditionValue
is ArrayList<*> -> currentValue.contains(conditionValue)
else -> false
}
}
"\$gt" -> {
return when (currentValue) {
is Number -> {
if (!StringUtils.isNumeric(conditionValue)) {
return false
} else {
currentValue.toFloat() > conditionValue.toFloat()
}
}
else -> false
}
}
"\$gte" -> {
return when (currentValue) {
is Number -> {
if (!StringUtils.isNumeric(conditionValue)) {
return false
} else {
currentValue.toFloat() >= conditionValue.toFloat()
}
}
else -> false
}
}
"\$lt" -> {
return when (currentValue) {
is Number -> {
if (!StringUtils.isNumeric(conditionValue)) {
return false
} else {
currentValue.toFloat() < conditionValue.toFloat()
}
}
else -> false
}
}
"\$lte" -> {
return when (currentValue) {
is Number -> {
if (!StringUtils.isNumeric(conditionValue)) {
return false
} else {
currentValue.toFloat() <= conditionValue.toFloat()
}
}
else -> false
}
}
else -> {
logger.warn("Unknown condition operator: ${condition.operator}")
true
}
}
}
}