Pull request #496: RED-4771: Reformatted code and added @Transactional
Merge in RED/persistence-service from RED-4771 to master * commit '8276e199c1d4e9370a96caf34c69c1444b224430': RED-4771: Reformatted code and added @Transactional
This commit is contained in:
commit
dbfe0e1cc3
@ -1,15 +1,5 @@
|
|||||||
package com.iqser.red.service.persistence.management.v1.processor.service.persistence;
|
package com.iqser.red.service.persistence.management.v1.processor.service.persistence;
|
||||||
|
|
||||||
import com.iqser.red.service.persistence.management.v1.processor.entity.notification.NotificationPreferencesEntity;
|
|
||||||
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.NotificationPreferencesRepository;
|
|
||||||
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.NotificationRepository;
|
|
||||||
import com.iqser.red.service.persistence.service.v1.api.model.notification.NotificationPreferences;
|
|
||||||
import com.iqser.red.service.persistence.service.v1.api.model.notification.NotificationType;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.springframework.beans.BeanUtils;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import javax.transaction.Transactional;
|
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -17,6 +7,19 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import javax.transaction.Transactional;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.iqser.red.service.persistence.management.v1.processor.entity.notification.NotificationPreferencesEntity;
|
||||||
|
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.NotificationPreferencesRepository;
|
||||||
|
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.NotificationRepository;
|
||||||
|
import com.iqser.red.service.persistence.service.v1.api.model.notification.NotificationPreferences;
|
||||||
|
import com.iqser.red.service.persistence.service.v1.api.model.notification.NotificationType;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class NotificationPreferencesPersistenceService {
|
public class NotificationPreferencesPersistenceService {
|
||||||
@ -25,40 +28,45 @@ public class NotificationPreferencesPersistenceService {
|
|||||||
|
|
||||||
private final NotificationRepository notificationRepository;
|
private final NotificationRepository notificationRepository;
|
||||||
|
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void setNotificationPreference(String userId, NotificationPreferences newNotificationPreferences) {
|
public void setNotificationPreference(String userId, NotificationPreferences newNotificationPreferences) {
|
||||||
notificationPreferencesRepository.findById(userId)
|
|
||||||
.ifPresentOrElse(oldNotificationPreferences -> {
|
|
||||||
|
|
||||||
// if we disabled the notification preferences just now
|
notificationPreferencesRepository.findById(userId).ifPresentOrElse(oldNotificationPreferences -> {
|
||||||
if (oldNotificationPreferences.isInAppNotificationsEnabled() != newNotificationPreferences.isInAppNotificationsEnabled()) {
|
|
||||||
notificationRepository.deleteAllByUserId(userId, OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS));
|
|
||||||
}
|
|
||||||
|
|
||||||
var removedNotificationTypes = new ArrayList<>(oldNotificationPreferences.getInAppNotifications());
|
// if we disabled the notification preferences just now
|
||||||
removedNotificationTypes.removeAll(newNotificationPreferences.getInAppNotifications());
|
if (oldNotificationPreferences.isInAppNotificationsEnabled() != newNotificationPreferences.isInAppNotificationsEnabled()) {
|
||||||
|
notificationRepository.deleteAllByUserId(userId, OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS));
|
||||||
|
}
|
||||||
|
|
||||||
if(!removedNotificationTypes.isEmpty()) {
|
var removedNotificationTypes = new ArrayList<>(oldNotificationPreferences.getInAppNotifications());
|
||||||
notificationRepository.deleteAllByUserIdAndNotificationTypeIn(userId, removedNotificationTypes, OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS));
|
removedNotificationTypes.removeAll(newNotificationPreferences.getInAppNotifications());
|
||||||
}
|
|
||||||
|
|
||||||
BeanUtils.copyProperties(newNotificationPreferences, oldNotificationPreferences);
|
if (!removedNotificationTypes.isEmpty()) {
|
||||||
|
notificationRepository.deleteAllByUserIdAndNotificationTypeIn(userId, removedNotificationTypes, OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS));
|
||||||
|
}
|
||||||
|
|
||||||
}, () -> {
|
BeanUtils.copyProperties(newNotificationPreferences, oldNotificationPreferences);
|
||||||
NotificationPreferencesEntity notificationPreferencesEntity = new NotificationPreferencesEntity();
|
|
||||||
BeanUtils.copyProperties(newNotificationPreferences, notificationPreferencesEntity);
|
}, () -> {
|
||||||
notificationPreferencesEntity.setUserId(userId);
|
NotificationPreferencesEntity notificationPreferencesEntity = new NotificationPreferencesEntity();
|
||||||
notificationPreferencesRepository.save(notificationPreferencesEntity);
|
BeanUtils.copyProperties(newNotificationPreferences, notificationPreferencesEntity);
|
||||||
});
|
notificationPreferencesEntity.setUserId(userId);
|
||||||
|
notificationPreferencesRepository.save(notificationPreferencesEntity);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteNotificationPreferences(String userId) {
|
public void deleteNotificationPreferences(String userId) {
|
||||||
|
|
||||||
notificationPreferencesRepository.deleteById(userId);
|
notificationPreferencesRepository.deleteById(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public NotificationPreferencesEntity getOrCreateNotificationPreferences(String userId) {
|
public NotificationPreferencesEntity getOrCreateNotificationPreferences(String userId) {
|
||||||
|
|
||||||
return notificationPreferencesRepository.findById(userId).orElseGet(() -> {
|
return notificationPreferencesRepository.findById(userId).orElseGet(() -> {
|
||||||
|
|
||||||
var notificationPreference = new NotificationPreferencesEntity();
|
var notificationPreference = new NotificationPreferencesEntity();
|
||||||
@ -70,6 +78,8 @@ public class NotificationPreferencesPersistenceService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public void initializePreferencesIfNotExists(String userId) {
|
public void initializePreferencesIfNotExists(String userId) {
|
||||||
|
|
||||||
if (!notificationPreferencesRepository.existsByUserId(userId)) {
|
if (!notificationPreferencesRepository.existsByUserId(userId)) {
|
||||||
@ -77,7 +87,10 @@ public class NotificationPreferencesPersistenceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public List<NotificationPreferencesEntity> findAll() {
|
public List<NotificationPreferencesEntity> findAll() {
|
||||||
|
|
||||||
return notificationPreferencesRepository.findAll();
|
return notificationPreferencesRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user