Minor refactoring for user notification settings

This commit is contained in:
ViacheslavKlimov 2023-08-15 17:34:10 +03:00
parent ce06ea10ca
commit d8896fe9dd
3 changed files with 12 additions and 5 deletions

View File

@ -298,7 +298,7 @@ public class NotificationController extends BaseController {
if (targetType == NotificationTargetType.PLATFORM_USERS) { if (targetType == NotificationTargetType.PLATFORM_USERS) {
PageData<User> recipients = notificationTargetService.findRecipientsForNotificationTargetConfig(user.getTenantId(), PageData<User> recipients = notificationTargetService.findRecipientsForNotificationTargetConfig(user.getTenantId(),
(PlatformUsersNotificationTargetConfig) target.getConfiguration(), new PageLink(recipientsPreviewSize, 0, null, (PlatformUsersNotificationTargetConfig) target.getConfiguration(), new PageLink(recipientsPreviewSize, 0, null,
SortOrder.byCreatedTimeDesc)); SortOrder.BY_CREATED_TIME_DESC));
recipientsCount = (int) recipients.getTotalElements(); recipientsCount = (int) recipients.getTotalElements();
recipientsPart = recipients.getData().stream().map(r -> (NotificationRecipient) r).collect(Collectors.toList()); recipientsPart = recipients.getData().stream().map(r -> (NotificationRecipient) r).collect(Collectors.toList());
} else { } else {

View File

@ -36,6 +36,6 @@ public class SortOrder {
ASC, DESC ASC, DESC
} }
public static final SortOrder byCreatedTimeDesc = new SortOrder("createdTime", Direction.DESC); public static final SortOrder BY_CREATED_TIME_DESC = new SortOrder("createdTime", Direction.DESC);
} }

View File

@ -16,6 +16,7 @@
package org.thingsboard.server.dao.notification; package org.thingsboard.server.dao.notification;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -55,6 +56,7 @@ import java.util.Optional;
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j
public class DefaultNotificationSettingsService implements NotificationSettingsService { public class DefaultNotificationSettingsService implements NotificationSettingsService {
private final AdminSettingsService adminSettingsService; private final AdminSettingsService adminSettingsService;
@ -104,10 +106,15 @@ public class DefaultNotificationSettingsService implements NotificationSettingsS
@Override @Override
public UserNotificationSettings getUserNotificationSettings(TenantId tenantId, UserId userId, boolean format) { public UserNotificationSettings getUserNotificationSettings(TenantId tenantId, UserId userId, boolean format) {
UserSettings userSettings = userSettingsService.findUserSettings(tenantId, userId, UserSettingsType.NOTIFICATIONS); UserSettings userSettings = userSettingsService.findUserSettings(tenantId, userId, UserSettingsType.NOTIFICATIONS);
UserNotificationSettings settings; UserNotificationSettings settings = null;
if (userSettings != null) { if (userSettings != null) {
settings = JacksonUtil.treeToValue(userSettings.getSettings(), UserNotificationSettings.class); try {
} else { settings = JacksonUtil.treeToValue(userSettings.getSettings(), UserNotificationSettings.class);
} catch (Exception e) {
log.warn("Failed to parse notification settings for user {}", userId, e);
}
}
if (settings == null) {
settings = UserNotificationSettings.DEFAULT; settings = UserNotificationSettings.DEFAULT;
} }
if (format) { if (format) {