fixed notifications since

This commit is contained in:
Timo Bejan 2021-11-23 11:18:31 +02:00
parent 271f0ac266
commit fd7bfde8fc
6 changed files with 40 additions and 35 deletions

View File

@ -255,12 +255,12 @@ public class FileStatusPersistenceService {
@Transactional
public void setAssignee(String fileId, String currentReviewer, String lastReviewer, String lastApprover) {
public void setAssignee(String fileId, String currentAssignee, String lastReviewer, String lastApprover) {
if (isFileDeleted(fileId)) {
return;
}
int updateCount = fileRepository.setAssignee(fileId, currentReviewer, lastReviewer, lastApprover,
int updateCount = fileRepository.setAssignee(fileId, currentAssignee, lastReviewer, lastApprover,
OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS));
if (updateCount == 0) {
throw new NotFoundException("Unknown file=" + fileId);

View File

@ -18,16 +18,14 @@ import java.util.List;
@RequiredArgsConstructor
public class NotificationPersistenceService {
private final NotificationRepository notificationRepository;
public boolean hasNewNotificationsSince(String userId, OffsetDateTime since) {
return notificationRepository.existsByUserIdAndCreationDateIsAfter(userId, since.truncatedTo(ChronoUnit.MILLIS));
return notificationRepository.hasInAppNotificationForUser(userId, since.truncatedTo(ChronoUnit.MILLIS)) > 0;
}
@SneakyThrows
public void insertNotification(AddNotificationRequest addNotificationRequest) {
var notification = new NotificationEntity();
BeanUtils.copyProperties(addNotificationRequest, notification);
notification.setCreationDate(OffsetDateTime.now());
@ -35,7 +33,6 @@ public class NotificationPersistenceService {
notificationRepository.save(notification);
}
@Transactional
public void setSeenDate(String userId, long notificationId, OffsetDateTime seenDate) {
@ -48,7 +45,7 @@ public class NotificationPersistenceService {
@Transactional
public void setReadDate(String userId, long notificationId, OffsetDateTime readDate) {
int countUpdate = notificationRepository.setReadDate(userId, notificationId, readDate);
int countUpdate = notificationRepository.setReadDate(userId, notificationId, readDate);
if (countUpdate == 0) {
throw new NotFoundException("Notification not found");
}
@ -56,7 +53,7 @@ public class NotificationPersistenceService {
@Transactional
public void softDelete(String userId, long notificationId) {
int countUpdate = notificationRepository.softDelete(userId, notificationId, OffsetDateTime.now());
int countUpdate = notificationRepository.softDelete(userId, notificationId, OffsetDateTime.now());
if (countUpdate == 0) {
throw new NotFoundException("Notification not found");
}
@ -72,9 +69,9 @@ public class NotificationPersistenceService {
public List<NotificationEntity> getNotifications(String userId, boolean includeSeen) {
if (includeSeen) {
return notificationRepository.findForUser(userId);
return notificationRepository.findAppNotificationsForUser(userId);
} else {
return notificationRepository.findNotSeenForUser(userId);
return notificationRepository.findNotNotificationsSeenForUser(userId);
}
}

View File

@ -7,21 +7,36 @@ import org.springframework.data.jpa.repository.Query;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;
public interface NotificationRepository extends JpaRepository<NotificationEntity, Long> {
boolean existsByUserIdAndCreationDateIsAfter(String userId, OffsetDateTime since);
List<NotificationEntity> findByUserIdOrderByCreationDateDesc(String userId);
@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 )) " +
" or" +
" not exists (select e from NotificationPreferencesEntity e where e.userId = :userId) " +
" ) " +
" and n.softDeleted is null and n.userId = :userId and n.creationDate > :since")
int hasInAppNotificationForUser(String userId, OffsetDateTime since);
@Query("Select n from NotificationEntity n where n.softDeleted is null and n.userId = :userId order by n.creationDate desc")
List<NotificationEntity> findForUser(String userId);
@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 )) " +
" or" +
" not exists (select e from NotificationPreferencesEntity e where e.userId = :userId) " +
" ) " +
" and n.softDeleted is null and n.userId = :userId order by n.creationDate desc")
List<NotificationEntity> findAppNotificationsForUser(String userId);
@Query("Select n from NotificationEntity n where n.seenDate is null and n.softDeleted is null and n.userId = :userId order by n.creationDate desc")
List<NotificationEntity> findNotSeenForUser(String userId);
Optional<NotificationEntity> findByIdAndUserId(long notificationId, String userId);
@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 )) " +
" or" +
" not exists (select e from NotificationPreferencesEntity e where e.userId = :userId) " +
" ) " +
" and n.seenDate is null and n.softDeleted is null and n.userId = :userId order by n.creationDate desc")
List<NotificationEntity> findNotNotificationsSeenForUser(String userId);
@Modifying
@Query("update NotificationEntity n set n.seenDate = :seenDate where n.id = :notificationId and n.userId = :userId")

View File

@ -1,6 +1,5 @@
package com.iqser.red.service.peristence.v1.server.controller;
import com.iqser.red.service.persistence.management.v1.processor.exception.NotFoundException;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.NotificationPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.NotificationPreferencesPersistenceService;
import com.iqser.red.service.persistence.service.v1.api.model.audit.AddNotificationRequest;
@ -24,24 +23,13 @@ public class NotificationController implements NotificationResource {
private final NotificationPersistenceService notificationPersistenceService;
private final NotificationPreferencesPersistenceService notificationPreferencesPersistenceService;
@Override
public JSONPrimitive<Boolean> hasNewNotificationsSince(@PathVariable(USER_ID_PARAM) String userId, @RequestBody JSONPrimitive<OffsetDateTime> since) {
public JSONPrimitive<Boolean> hasNewNotificationsSince(@PathVariable(USER_ID_PARAM) String userId, @RequestBody JSONPrimitive<OffsetDateTime> since) {
return JSONPrimitive.of(notificationPersistenceService.hasNewNotificationsSince(userId, since.getValue()));
}
public void addNotification(@RequestBody AddNotificationRequest addNotificationRequest) {
try {
var userPreferences = notificationPreferencesPersistenceService.getNotificationPreferences(addNotificationRequest.getUserId());
if (userPreferences.getInAppNotifications().contains(addNotificationRequest.getNotificationType())
|| userPreferences.getEmailNotifications().contains(addNotificationRequest.getNotificationType())) {
notificationPersistenceService.insertNotification(addNotificationRequest);
}
} catch (NotFoundException e) {
notificationPersistenceService.insertNotification(addNotificationRequest);
}
notificationPersistenceService.insertNotification(addNotificationRequest);
}
public void toggleSeen(@PathVariable(USER_ID_PARAM) String userId, @RequestBody List<Long> notificationIds,

View File

@ -204,10 +204,10 @@ public class FileStatusService {
FileEntity fileStatus = fileStatusPersistenceService.getStatus(fileId);
String lastReviewer = fileStatus.getLastReviewer();
String lastApprover = fileStatus.getLastApprover();
if(StringUtils.isNotEmpty(assignee) && WorkflowStatus.UNDER_REVIEW.equals(fileStatus.getWorkflowStatus())) {
if(StringUtils.isNotEmpty(assignee) && WorkflowStatus.UNDER_REVIEW.equals(fileStatus.getWorkflowStatus())) {
lastReviewer = assignee;
}
if(StringUtils.isNotEmpty(assignee) && WorkflowStatus.UNDER_APPROVAL.equals(fileStatus.getWorkflowStatus())) {
if(StringUtils.isNotEmpty(assignee) && WorkflowStatus.UNDER_APPROVAL.equals(fileStatus.getWorkflowStatus())) {
lastApprover = assignee;
}

View File

@ -3,11 +3,13 @@ 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.utils.AbstractPersistenceServerServiceTest;
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.notification.Notification;
import org.assertj.core.util.Lists;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.OffsetDateTime;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
@ -28,6 +30,9 @@ public class NotificationTest extends AbstractPersistenceServerServiceTest {
assertThat(notification.getReadDate()).isNull();
notificationClient.toggleRead("1", Lists.newArrayList(notification.getId()), true);
notification = getNotification();
var hasNewNotifications = notificationClient.hasNewNotificationsSince("1", JSONPrimitive.of(OffsetDateTime.now().minusHours(1)));
assertThat(hasNewNotifications.getValue()).isTrue();
assertThat(notification.getReadDate()).isNotNull();