RED-3584 / RED-3581 - notification issues - preferences validation test

This commit is contained in:
Timo Bejan 2022-06-03 09:58:50 +03:00
parent 356b15c837
commit 37a84d9049
5 changed files with 68 additions and 29 deletions

View File

@ -37,7 +37,7 @@ public class NotificationPersistenceService {
notification.setCreationDate(OffsetDateTime.now()); notification.setCreationDate(OffsetDateTime.now());
var notificationPreferences = notificationPreferencesPersistenceService.getOrCreateNotificationPreferences(addNotificationRequest.getUserId()); var notificationPreferences = notificationPreferencesPersistenceService.getOrCreateNotificationPreferences(addNotificationRequest.getUserId());
if (!notificationPreferences.isInAppNotificationsEnabled() || notificationPreferences.getInAppNotifications().contains(notification.getNotificationType())) { if (!notificationPreferences.isInAppNotificationsEnabled() || !notificationPreferences.getInAppNotifications().contains(notification.getNotificationType())) {
notification.setSoftDeleted(OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS)); notification.setSoftDeleted(OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS));
} }

View File

@ -29,17 +29,20 @@ public class NotificationPreferencesPersistenceService {
public void setNotificationPreference(String userId, NotificationPreferences newNotificationPreferences) { public void setNotificationPreference(String userId, NotificationPreferences newNotificationPreferences) {
notificationPreferencesRepository.findById(userId) notificationPreferencesRepository.findById(userId)
.ifPresentOrElse(oldNotificationPreferences -> { .ifPresentOrElse(oldNotificationPreferences -> {
BeanUtils.copyProperties(newNotificationPreferences, oldNotificationPreferences);
// if we disabled the notification preferences just now // if we disabled the notification preferences just now
if (oldNotificationPreferences.isInAppNotificationsEnabled() && !newNotificationPreferences.isInAppNotificationsEnabled()) { if (oldNotificationPreferences.isInAppNotificationsEnabled() != newNotificationPreferences.isInAppNotificationsEnabled()) {
notificationRepository.deleteAllByUserId(userId, OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS)); notificationRepository.deleteAllByUserId(userId, OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS));
} }
var removedNotificationTypes = new ArrayList<>(oldNotificationPreferences.getInAppNotifications()); var removedNotificationTypes = new ArrayList<>(oldNotificationPreferences.getInAppNotifications());
removedNotificationTypes.removeAll(newNotificationPreferences.getInAppNotifications()); removedNotificationTypes.removeAll(newNotificationPreferences.getInAppNotifications());
notificationRepository.deleteAllByUserIdAndNotificationTypeIn(userId, removedNotificationTypes, OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS)); if(!removedNotificationTypes.isEmpty()) {
notificationRepository.deleteAllByUserIdAndNotificationTypeIn(userId, removedNotificationTypes, OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS));
}
BeanUtils.copyProperties(newNotificationPreferences, oldNotificationPreferences);
}, () -> { }, () -> {
NotificationPreferencesEntity notificationPreferencesEntity = new NotificationPreferencesEntity(); NotificationPreferencesEntity notificationPreferencesEntity = new NotificationPreferencesEntity();

View File

@ -13,23 +13,17 @@ public interface NotificationRepository extends JpaRepository<NotificationEntity
@Query("select count(n) from NotificationEntity n where " + @Query("select count(n) from NotificationEntity n where " +
" ( " +
" (exists (select e from NotificationPreferencesEntity e where e.userId = :userId) and n.notificationType in ( select apn from NotificationPreferencesEntity p join p.inAppNotifications apn where p.userId = :userId and p.inAppNotificationsEnabled = true )) " + " (exists (select e from NotificationPreferencesEntity e where e.userId = :userId) and n.notificationType in ( select apn from NotificationPreferencesEntity p join p.inAppNotifications apn where p.userId = :userId and p.inAppNotificationsEnabled = true )) " +
" ) " +
" and n.softDeleted is null and n.userId = :userId and n.creationDate > :since") " and n.softDeleted is null and n.userId = :userId and n.creationDate > :since")
int hasInAppNotificationForUser(String userId, OffsetDateTime since); int hasInAppNotificationForUser(String userId, OffsetDateTime since);
@Query("select n from NotificationEntity n where " + @Query("select n from NotificationEntity n where " +
" ( " +
" (exists (select e from NotificationPreferencesEntity e where e.userId = :userId) and n.notificationType in ( select apn from NotificationPreferencesEntity p join p.inAppNotifications apn where p.userId = :userId and p.inAppNotificationsEnabled = true )) " + " (exists (select e from NotificationPreferencesEntity e where e.userId = :userId) and n.notificationType in ( select apn from NotificationPreferencesEntity p join p.inAppNotifications apn where p.userId = :userId and p.inAppNotificationsEnabled = true )) " +
" ) " +
" and n.softDeleted is null and n.userId = :userId order by n.creationDate desc") " and n.softDeleted is null and n.userId = :userId order by n.creationDate desc")
List<NotificationEntity> findAppNotificationsForUser(String userId); List<NotificationEntity> findAppNotificationsForUser(String userId);
@Query("select n from NotificationEntity n where " + @Query("select n from NotificationEntity n where " +
" ( " +
" (exists (select e from NotificationPreferencesEntity e where e.userId = :userId) and n.notificationType in ( select apn from NotificationPreferencesEntity p join p.inAppNotifications apn where p.userId = :userId and p.inAppNotificationsEnabled = true )) " + " (exists (select e from NotificationPreferencesEntity e where e.userId = :userId) and n.notificationType in ( select apn from NotificationPreferencesEntity p join p.inAppNotifications apn where p.userId = :userId and p.inAppNotificationsEnabled = true )) " +
" ) " +
" and n.seenDate is null and n.softDeleted is null and n.userId = :userId order by n.creationDate desc ") " and n.seenDate is null and n.softDeleted is null and n.userId = :userId order by n.creationDate desc ")
List<NotificationEntity> findUnseenNotificationsForUser(String userId); List<NotificationEntity> findUnseenNotificationsForUser(String userId);
@ -56,9 +50,7 @@ public interface NotificationRepository extends JpaRepository<NotificationEntity
@Query("select n from NotificationEntity n where " + @Query("select n from NotificationEntity n where " +
" ( " +
" (exists (select e from NotificationPreferencesEntity e where e.userId = :userId) and n.notificationType in ( select apn from NotificationPreferencesEntity p join p.emailNotifications apn where p.userId = :userId and p.emailNotificationsEnabled = true )) " + " (exists (select e from NotificationPreferencesEntity e where e.userId = :userId) and n.notificationType in ( select apn from NotificationPreferencesEntity p join p.emailNotifications apn where p.userId = :userId and p.emailNotificationsEnabled = true )) " +
" ) " + " and n.userId = :userId and n.creationDate >= :from and n.creationDate <= :to order by n.creationDate desc")
" and n.userId = :userId and n.creationDate :from and n.creationDate <= :to order by n.creationDate desc")
List<NotificationEntity> findEmailNotificationsForUserId(String userId, OffsetDateTime from, OffsetDateTime to); List<NotificationEntity> findEmailNotificationsForUserId(String userId, OffsetDateTime from, OffsetDateTime to);
} }

View File

@ -65,6 +65,20 @@ public class ReanalysisRequiredStatusService {
Map<String, Long> dossierVersionMap, Map<String, Long> dossierVersionMap,
Map<String, DossierEntity> dossierMap) { Map<String, DossierEntity> dossierMap) {
// enhance with dossierTemplateId
DossierEntity dossier;
try {
dossier = dossierMap.computeIfAbsent(fileStatus.getDossierId(), k -> dossierPersistenceService.getAndValidateDossier(fileStatus.getDossierId()));
} catch (DossierNotFoundException e) {
log.info("Dossier {} was not found, thus analysis is not required", fileStatus.getDossierId());
return new AnalysisRequiredResult(false, false);
}
fileStatus.setDossierArchived(dossier.getArchivedTime() != null);
fileStatus.setDossierTemplateId(dossier.getDossierTemplateId());
fileStatus.setDossierStatusId(dossier.getDossierStatusId());
if (ProcessingStatus.ERROR.equals(fileStatus.getProcessingStatus()) && !ignoreProcessingStates) { if (ProcessingStatus.ERROR.equals(fileStatus.getProcessingStatus()) && !ignoreProcessingStates) {
return new AnalysisRequiredResult(false, true); return new AnalysisRequiredResult(false, true);
} }
@ -83,7 +97,7 @@ public class ReanalysisRequiredStatusService {
if (fileStatus.getLastFileAttributeChange() != null && fileStatus.getLastProcessed().isBefore(fileStatus.getLastFileAttributeChange())) { if (fileStatus.getLastFileAttributeChange() != null && fileStatus.getLastProcessed().isBefore(fileStatus.getLastFileAttributeChange())) {
return new AnalysisRequiredResult(true, false); return new AnalysisRequiredResult(true, false);
} else { } else {
return requiresReanalysisBasedOnVersionDifference(fileStatus, dossierTemplateVersionMap, dossierVersionMap, dossierMap); return requiresReanalysisBasedOnVersionDifference(fileStatus, dossier, dossierTemplateVersionMap, dossierVersionMap);
} }
default: default:
return new AnalysisRequiredResult(false, false); return new AnalysisRequiredResult(false, false);
@ -94,22 +108,10 @@ public class ReanalysisRequiredStatusService {
} }
private AnalysisRequiredResult requiresReanalysisBasedOnVersionDifference(FileModel fileStatus, private AnalysisRequiredResult requiresReanalysisBasedOnVersionDifference(FileModel fileStatus,
DossierEntity dossier,
Map<String, Map<VersionType, Long>> dossierTemplateVersionMap, Map<String, Map<VersionType, Long>> dossierTemplateVersionMap,
Map<String, Long> dossierVersionMap, Map<String, Long> dossierVersionMap) {
Map<String, DossierEntity> dossierMap) {
// enhance with dossierTemplateId
DossierEntity dossier;
try {
dossier = dossierMap.computeIfAbsent(fileStatus.getDossierId(), k -> dossierPersistenceService.getAndValidateDossier(fileStatus.getDossierId()));
} catch (DossierNotFoundException e) {
log.info("Dossier {} was not found, thus analysis is not required", fileStatus.getDossierId());
return new AnalysisRequiredResult(false, false);
}
fileStatus.setDossierArchived(dossier.getArchivedTime() != null);
fileStatus.setDossierTemplateId(dossier.getDossierTemplateId());
fileStatus.setDossierStatusId(dossier.getDossierStatusId());
// get relevant versions // get relevant versions
var dossierTemplateVersions = dossierTemplateVersionMap.computeIfAbsent(dossier.getDossierTemplateId(), var dossierTemplateVersions = dossierTemplateVersionMap.computeIfAbsent(dossier.getDossierTemplateId(),

View File

@ -1,10 +1,12 @@
package com.iqser.red.service.peristence.v1.server.integration.tests; package com.iqser.red.service.peristence.v1.server.integration.tests;
import com.iqser.red.service.peristence.v1.server.integration.client.NotificationClient; import com.iqser.red.service.peristence.v1.server.integration.client.NotificationClient;
import com.iqser.red.service.peristence.v1.server.integration.client.NotificationPreferencesClient;
import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest; import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest;
import com.iqser.red.service.persistence.service.v1.api.model.audit.AddNotificationRequest; import com.iqser.red.service.persistence.service.v1.api.model.audit.AddNotificationRequest;
import com.iqser.red.service.persistence.service.v1.api.model.common.JSONPrimitive; import com.iqser.red.service.persistence.service.v1.api.model.common.JSONPrimitive;
import com.iqser.red.service.persistence.service.v1.api.model.notification.Notification; import com.iqser.red.service.persistence.service.v1.api.model.notification.Notification;
import com.iqser.red.service.persistence.service.v1.api.model.notification.NotificationType;
import org.assertj.core.util.Lists; import org.assertj.core.util.Lists;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -19,10 +21,50 @@ public class NotificationTest extends AbstractPersistenceServerServiceTest {
@Autowired @Autowired
private NotificationClient notificationClient; private NotificationClient notificationClient;
@Autowired
private NotificationPreferencesClient notificationPreferencesClient;
@Test
public void testNotificationPreferences() {
// initialize in app notifications
var preferences = notificationPreferencesClient.getNotificationPreferences("1");
notificationClient.addNotification(AddNotificationRequest.builder()
.notificationType(NotificationType.ASSIGN_REVIEWER.name())
.userId("1").build());
assertThat(notificationClient.getNotifications("1", true).size()).isEqualTo(1);
// disable in app notifications
preferences.setInAppNotificationsEnabled(false);
notificationPreferencesClient.setNotificationPreferences("1", preferences);
assertThat(notificationClient.getNotifications("1", true).size()).isEqualTo(0);
// re-enable in app notifications
preferences.setInAppNotificationsEnabled(true);
notificationPreferencesClient.setNotificationPreferences("1", preferences);
assertThat(notificationClient.getNotifications("1", true).size()).isEqualTo(0);
notificationClient.addNotification(AddNotificationRequest.builder()
.notificationType(NotificationType.ASSIGN_REVIEWER.name())
.userId("1").build());
assertThat(notificationClient.getNotifications("1", true).size()).isEqualTo(1);
preferences.getInAppNotifications().remove(NotificationType.ASSIGN_REVIEWER.name());
notificationPreferencesClient.setNotificationPreferences("1", preferences);
assertThat(notificationClient.getNotifications("1", true).size()).isEqualTo(0);
}
@Test @Test
public void testNotification() { public void testNotification() {
notificationClient.addNotification(AddNotificationRequest.builder().notificationType("test").issuerId("1").userId("1").target(Map.of("test", "test")).build()); notificationClient.addNotification(AddNotificationRequest.builder().notificationType(NotificationType.ASSIGN_REVIEWER.name()).issuerId("1").userId("1").target(Map.of("test", "test")).build());
var notification = getNotification(); var notification = getNotification();