|
|
|
@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import org.springframework.cache.annotation.CacheEvict; |
|
|
|
import org.springframework.cache.annotation.Cacheable; |
|
|
|
import org.springframework.data.domain.Sort; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Propagation; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
@ -30,8 +31,10 @@ import org.thingsboard.server.common.data.User; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.data.id.UserId; |
|
|
|
import org.thingsboard.server.common.data.notification.NotificationType; |
|
|
|
import org.thingsboard.server.common.data.notification.rule.NotificationRule; |
|
|
|
import org.thingsboard.server.common.data.notification.settings.NotificationSettings; |
|
|
|
import org.thingsboard.server.common.data.notification.settings.UserNotificationSettings; |
|
|
|
import org.thingsboard.server.common.data.notification.settings.UserNotificationSettings.NotificationPref; |
|
|
|
import org.thingsboard.server.common.data.notification.targets.NotificationTarget; |
|
|
|
import org.thingsboard.server.common.data.notification.targets.platform.AffectedTenantAdministratorsFilter; |
|
|
|
import org.thingsboard.server.common.data.notification.targets.platform.AffectedUserFilter; |
|
|
|
@ -43,12 +46,19 @@ import org.thingsboard.server.common.data.notification.targets.platform.TenantAd |
|
|
|
import org.thingsboard.server.common.data.notification.targets.platform.UsersFilter; |
|
|
|
import org.thingsboard.server.common.data.notification.targets.platform.UsersFilterType; |
|
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
|
import org.thingsboard.server.common.data.page.SortOrder; |
|
|
|
import org.thingsboard.server.dao.settings.AdminSettingsService; |
|
|
|
import org.thingsboard.server.dao.user.UserService; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
import static java.util.function.Predicate.not; |
|
|
|
|
|
|
|
@Service |
|
|
|
@RequiredArgsConstructor |
|
|
|
@ -57,6 +67,7 @@ public class DefaultNotificationSettingsService implements NotificationSettingsS |
|
|
|
private final AdminSettingsService adminSettingsService; |
|
|
|
private final NotificationTargetService notificationTargetService; |
|
|
|
private final NotificationTemplateService notificationTemplateService; |
|
|
|
private final NotificationRuleService notificationRuleService; |
|
|
|
private final DefaultNotifications defaultNotifications; |
|
|
|
private final UserService userService; |
|
|
|
|
|
|
|
@ -98,13 +109,42 @@ public class DefaultNotificationSettingsService implements NotificationSettingsS |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public UserNotificationSettings getUserNotificationSettings(TenantId tenantId, User user) { |
|
|
|
// TODO: decide whether to use user_settings or store it in the additionalInfo not to make more DB requests
|
|
|
|
JsonNode notificationSettings = user.getAdditionalInfo().get("notificationSettings"); |
|
|
|
if (notificationSettings == null || notificationSettings.isNull()) { |
|
|
|
return UserNotificationSettings.DEFAULT; |
|
|
|
public UserNotificationSettings getUserNotificationSettings(TenantId tenantId, User user, boolean format) { |
|
|
|
UserNotificationSettings settings = Optional.ofNullable(user.getAdditionalInfo().get("notificationSettings")) |
|
|
|
.filter(not(JsonNode::isNull)) |
|
|
|
.map(json -> JacksonUtil.treeToValue(json, UserNotificationSettings.class)) |
|
|
|
.orElse(null); |
|
|
|
if (!format) { |
|
|
|
if (settings != null) { |
|
|
|
return settings; |
|
|
|
} else { |
|
|
|
return UserNotificationSettings.DEFAULT; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Map<UUID, NotificationRule> rules = new HashMap<>(); |
|
|
|
notificationRuleService.findNotificationRulesByTenantId(tenantId, new PageLink(Integer.MAX_VALUE, 0,null, SortOrder.byCreatedTimeDesc)) |
|
|
|
.getData().forEach(rule -> rules.put(rule.getUuidId(), rule)); |
|
|
|
|
|
|
|
List<NotificationPref> prefs = new ArrayList<>(); |
|
|
|
if (settings == null) { |
|
|
|
rules.values().forEach(rule -> { |
|
|
|
prefs.add(NotificationPref.createDefault(rule)); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
settings.getPrefs().forEach(pref -> { |
|
|
|
NotificationRule rule = rules.remove(pref.getRuleId()); |
|
|
|
if (rule == null) { |
|
|
|
return; |
|
|
|
} |
|
|
|
pref.setRuleName(rule.getName()); |
|
|
|
prefs.add(pref); |
|
|
|
}); |
|
|
|
rules.values().forEach(rule -> { |
|
|
|
prefs.add(NotificationPref.createDefault(rule)); |
|
|
|
}); |
|
|
|
} |
|
|
|
return JacksonUtil.treeToValue(notificationSettings, UserNotificationSettings.class); |
|
|
|
return new UserNotificationSettings(prefs); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) // so that parent transaction is not aborted on method failure
|
|
|
|
|