Updated Apis, Code, Configurations for Notification.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.gepafin.tendermanagement.config.Translator;
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
@@ -19,6 +18,7 @@ import net.gepafin.tendermanagement.repositories.NotificationRepository;
|
||||
import net.gepafin.tendermanagement.repositories.NotificationTypeRepository;
|
||||
import net.gepafin.tendermanagement.repositories.UserRepository;
|
||||
import net.gepafin.tendermanagement.repositories.UserWithCompanyRepository;
|
||||
import net.gepafin.tendermanagement.service.ApplicationService;
|
||||
import net.gepafin.tendermanagement.util.DateTimeUtil;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
|
||||
@@ -34,6 +34,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.hibernate.internal.util.collections.CollectionHelper.listOf;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class NotificationDao {
|
||||
@@ -56,6 +58,9 @@ public class NotificationDao {
|
||||
@Autowired
|
||||
private CompanyDao companyDao;
|
||||
|
||||
@Autowired
|
||||
private ApplicationService applicationService;
|
||||
|
||||
public NotificationResponse sendNotification(NotificationReq notificationReq) {
|
||||
|
||||
// Ensure userId is properly set in notificationReq if not already
|
||||
@@ -68,7 +73,7 @@ public class NotificationDao {
|
||||
log.info("Sending notification to user {} with content: {}", userId, notificationReq.getMessage());
|
||||
List<Long> companyIds = notificationReq.getCompanyIds();
|
||||
|
||||
if (companyIds==null || companyIds.isEmpty()) {
|
||||
if (companyIds == null || companyIds.isEmpty()) {
|
||||
sendToUser(userId, notificationEntity);
|
||||
} else {
|
||||
sendToCompanies(userId, companyIds, notificationEntity);
|
||||
@@ -79,40 +84,50 @@ public class NotificationDao {
|
||||
|
||||
private NotificationEntity saveNotification(NotificationReq notificationReq) {
|
||||
|
||||
NotificationEntity notificationEntity = convertToNotificationEntity(notificationReq);
|
||||
return notificationRepository.save(notificationEntity);
|
||||
return notificationRepository.save(convertNotificationRequestToNotificationEntity(notificationReq));
|
||||
}
|
||||
|
||||
private void sendToUser(Long userId, NotificationEntity notificationEntity) {
|
||||
|
||||
String userChannel = GepafinConstant.COMMON_SINGLE_CHANNEL_PREFIX + userId;
|
||||
log.info("Channel for User {}", userChannel);
|
||||
messagingTemplate.convertAndSend(userChannel, notificationEntity);
|
||||
NotificationResponse notificationResponse = convertNotificationEntityToNotificationResponse(notificationEntity);
|
||||
messagingTemplate.convertAndSend(userChannel, notificationResponse);
|
||||
}
|
||||
|
||||
private void sendToCompanies(Long userId, List<Long> companyIds, NotificationEntity notificationEntity) {
|
||||
// Send notification to each company provided in the companyIds list
|
||||
companyIds.forEach(companyId -> {
|
||||
UserWithCompanyEntity userWithCompany = userWithCompanyRepository.findByUserIdAndCompanyIdAndIsDeletedFalseForNotification(userId, companyId);
|
||||
String companyChannel = Utils.createChannelForUserAndCompany(userId, companyId);
|
||||
log.info("Channel for User and Company {}, {}", userId, companyChannel);
|
||||
messagingTemplate.convertAndSend(companyChannel, notificationEntity);
|
||||
if (userWithCompany == null) {
|
||||
throw new CustomValidationException(Status.BAD_REQUEST, GepafinConstant.USER_WITH_COMPANY_NOT_FOUND);
|
||||
}
|
||||
notificationEntity.setUserWithCompany(userWithCompany);
|
||||
notificationRepository.save(notificationEntity);
|
||||
NotificationResponse notificationResponse = convertNotificationEntityToNotificationResponse(notificationEntity);
|
||||
messagingTemplate.convertAndSend(companyChannel, notificationResponse);
|
||||
});
|
||||
}
|
||||
|
||||
private NotificationReq convertToNotificationReq(NotificationEntity notificationEntity) {
|
||||
|
||||
NotificationReq notificationReq = new NotificationReq();
|
||||
notificationReq.setId(notificationEntity.getId());
|
||||
notificationReq.setUserId(notificationEntity.getUserId());
|
||||
notificationReq.setStatus(NotificationEnum.UNREAD.getValue());
|
||||
notificationReq.setMessage(notificationEntity.getMessage());
|
||||
notificationReq.setCreatedDate(notificationEntity.getCreatedDate());
|
||||
notificationReq.setUpdatedDate(notificationEntity.getUpdatedDate());
|
||||
return notificationReq;
|
||||
private NotificationResponse convertNotificationEntityToNotificationResponse(NotificationEntity notificationEntity) {
|
||||
|
||||
NotificationResponse notificationResponse = new NotificationResponse();
|
||||
notificationResponse.setId(notificationEntity.getId());
|
||||
notificationResponse.setUserId(notificationEntity.getUserId());
|
||||
notificationResponse.setStatus(notificationEntity.getStatus());
|
||||
notificationResponse.setMessage(notificationEntity.getMessage());
|
||||
notificationResponse.setCreatedDate(notificationEntity.getCreatedDate());
|
||||
notificationResponse.setUpdatedDate(notificationEntity.getUpdatedDate());
|
||||
notificationResponse.setRedirectUrl(notificationEntity.getNotificationType());
|
||||
notificationResponse.setCompanyId(notificationEntity.getUserWithCompany() != null ? notificationEntity.getUserWithCompany().getCompanyId() : null);
|
||||
notificationResponse.setNotificationType(notificationEntity.getNotificationType());
|
||||
notificationResponse.setTitle(notificationEntity.getTitle());
|
||||
return notificationResponse;
|
||||
}
|
||||
|
||||
private NotificationEntity convertToNotificationEntity(NotificationReq notificationReq) {
|
||||
private NotificationEntity convertNotificationRequestToNotificationEntity(NotificationReq notificationReq) {
|
||||
|
||||
NotificationEntity notificationEntity = new NotificationEntity();
|
||||
String message = notificationReq.getMessage();
|
||||
@@ -120,108 +135,107 @@ public class NotificationDao {
|
||||
notificationEntity.setUserId(notificationReq.getUserId());
|
||||
notificationEntity.setStatus(NotificationEnum.UNREAD.getValue());
|
||||
notificationEntity.setIsDeleted(Boolean.FALSE);
|
||||
notificationEntity.setUserWithCompany(notificationReq.getUserWithCompanyEntity() != null ? notificationReq.getUserWithCompanyEntity() : null);
|
||||
notificationEntity.setMessage(message);
|
||||
notificationEntity.setTitle(notificationReq.getTitle());
|
||||
return notificationEntity;
|
||||
}
|
||||
|
||||
public NotificationReq createNotificationReq(String notificationType, Map<String, String> placeholders, Long userId) {
|
||||
public NotificationReq createNotificationReq(String notificationType, Map<String, String> placeholders, Long userId, UserWithCompanyEntity userWithCompanyEntity,
|
||||
List<Long> companyIds) {
|
||||
// Create NotificationReq object
|
||||
NotificationReq notificationReq = new NotificationReq();
|
||||
List<Long> companyIds = userWithCompanyRepository.findActiveCompanyIdsByUserId(notificationReq.getUserId());
|
||||
notificationReq.setCompanyIds(companyIds);
|
||||
NotificationTypeEntity notificationTypeEntity = notificationTypeRepository.findByNotificationNameAndIsDeletedFalse(notificationType);
|
||||
notificationReq.setNotificationType(notificationType);
|
||||
String message = Utils.replacePlaceholders(notificationTypeEntity.getJsonTemplate(), placeholders);
|
||||
notificationReq.setMessage(message);
|
||||
notificationReq.setUserId(userId);
|
||||
notificationReq.setCompanyIds(companyIds);
|
||||
String title = Utils.replacePlaceholders(notificationTypeEntity.getTitle(), placeholders);
|
||||
notificationReq.setTitle(title);
|
||||
notificationReq.setUserWithCompanyEntity(userWithCompanyEntity);
|
||||
return notificationReq;
|
||||
}
|
||||
|
||||
public Map<String, String> sendNotificationToBeneficiary(ApplicationEntity application, NotificationTypeEnum notificationTypeEnum) {
|
||||
|
||||
Map<String, String> placeHolders = new HashMap<>();
|
||||
placeHolders.put("{{call_name}}", application.getCall().getName());
|
||||
placeHolders.put("{{protocol_number}}", String.valueOf(application.getProtocol().getProtocolNumber()));
|
||||
NotificationReq notificationReq = createNotificationReq(notificationTypeEnum.getValue(), placeHolders, application.getUserId());
|
||||
NotificationReq notificationReq = createNotificationReq(notificationTypeEnum.getValue(), placeHolders, application.getUserId(), application.getUserWithCompany(),
|
||||
listOf(application.getCompanyId()));
|
||||
sendNotification(notificationReq);
|
||||
return placeHolders;
|
||||
}
|
||||
|
||||
public void sendNotificationToInstructor(Map<String, String> placeHolders, ApplicationEvaluationEntity applicationEvaluationEntity, NotificationTypeEnum notificationTypeEnum) {
|
||||
Long instructorId=applicationEvaluationEntity.getUserId();
|
||||
if(instructorId != null){
|
||||
NotificationReq notificationreq = createNotificationReq(notificationTypeEnum.getValue(), placeHolders,instructorId);
|
||||
|
||||
Long instructorId = applicationEvaluationEntity.getUserId();
|
||||
ApplicationEntity application = applicationService.validateApplication(applicationEvaluationEntity.getApplicationId());
|
||||
if (instructorId != null) {
|
||||
NotificationReq notificationreq = createNotificationReq(notificationTypeEnum.getValue(), placeHolders, instructorId, application.getUserWithCompany(),
|
||||
listOf(application.getCompanyId()));
|
||||
sendNotification(notificationreq);
|
||||
}
|
||||
}
|
||||
public void sendNotificationToSuperUser(ApplicationEntity application,Map<String,String> placeHolders,NotificationTypeEnum notificationTypeEnum) {
|
||||
List<UserEntity> user=userRepository.findByRoleEntity_RoleTypeAndHubId(RoleStatusEnum.ROLE_SUPER_ADMIN.getValue(), application.getHubId());
|
||||
UserEntity userEntity1=user.get(0);
|
||||
if(userEntity1 != null) {
|
||||
NotificationReq notificationreq = createNotificationReq(notificationTypeEnum.getValue(), placeHolders,userEntity1.getId());
|
||||
public void sendNotificationToSuperUser(ApplicationEntity application, Map<String, String> placeHolders, NotificationTypeEnum notificationTypeEnum) {
|
||||
|
||||
List<UserEntity> user = userRepository.findByRoleEntity_RoleTypeAndHubId(RoleStatusEnum.ROLE_SUPER_ADMIN.getValue(), application.getHubId());
|
||||
UserEntity userEntity1 = user.get(0);
|
||||
if (userEntity1 != null) {
|
||||
NotificationReq notificationreq = createNotificationReq(notificationTypeEnum.getValue(), placeHolders, userEntity1.getId(), application.getUserWithCompany(),
|
||||
listOf(application.getCompanyId()));
|
||||
sendNotification(notificationreq);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Long> getAllCompanyIdsForUser(Long userId) {
|
||||
|
||||
return userWithCompanyRepository.findActiveCompanyIdsByUserId(userId);
|
||||
}
|
||||
|
||||
public NotificationResponse getNotificationById(Long id) {
|
||||
|
||||
NotificationEntity notificationEntity = validateNotificationEntity(id);
|
||||
NotificationResponse notificationReq=convertNotificationEntityToNotificationResponse(notificationEntity);
|
||||
return notificationReq;
|
||||
return convertNotificationEntityToNotificationResponse(notificationEntity);
|
||||
}
|
||||
|
||||
private NotificationEntity validateNotificationEntity(Long id) {
|
||||
NotificationEntity notificationEntity=notificationRepository.findByIdAndIsDeletedFalse(id);
|
||||
if(notificationEntity ==null){
|
||||
|
||||
NotificationEntity notificationEntity = notificationRepository.findByIdAndIsDeletedFalse(id);
|
||||
if (notificationEntity == null) {
|
||||
throw new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.NOTIFICATION_NOT_FOUND));
|
||||
}
|
||||
return notificationEntity;
|
||||
}
|
||||
|
||||
public List<NotificationResponse> getNotificationByUserId(Long userId, Long companyId, List<NotificationEnum> statuses) {
|
||||
|
||||
List<NotificationEntity> notificationEntities = notificationRepository.findByUserIdAndIsDeletedFalse(userId);
|
||||
UserWithCompanyEntity userWithCompany=null;
|
||||
List<String> statusStrings=new ArrayList<>();
|
||||
if(companyId!=null){
|
||||
userWithCompany=companyDao.validateUserWithCompny(userId,companyId);
|
||||
UserWithCompanyEntity userWithCompany = null;
|
||||
List<String> statusStrings = new ArrayList<>();
|
||||
if (companyId != null) {
|
||||
userWithCompany = companyDao.validateUserWithCompny(userId, companyId);
|
||||
}
|
||||
|
||||
if (statuses != null ) {
|
||||
statusStrings = statuses.stream()
|
||||
.map(NotificationEnum::name) // Convert enum to its name as String
|
||||
if (statuses != null) {
|
||||
statusStrings = statuses.stream().map(NotificationEnum::name) // Convert enum to its name as String
|
||||
.toList();
|
||||
notificationEntities = notificationRepository.findByUserIdAndIsDeletedFalseAndStatusIn(userId,statusStrings);
|
||||
notificationEntities = notificationRepository.findByUserIdAndIsDeletedFalseAndStatusIn(userId, statusStrings);
|
||||
|
||||
if(userWithCompany != null){
|
||||
if (userWithCompany != null) {
|
||||
notificationEntities = notificationRepository.findByUserIdAndUserWithCompanyIdAndIsDeletedFalseAndStatusIn(userId, userWithCompany.getId(), statusStrings);
|
||||
}
|
||||
}
|
||||
|
||||
List<NotificationResponse> notificationReq= notificationEntities.stream()
|
||||
.map(this::convertNotificationEntityToNotificationResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return notificationReq;
|
||||
return notificationEntities.stream().map(this::convertNotificationEntityToNotificationResponse).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public NotificationResponse convertNotificationEntityToNotificationResponse(NotificationEntity entity) {
|
||||
if (entity == null) {
|
||||
return null; // Handle null entity gracefully
|
||||
}
|
||||
public NotificationResponse updateNotificationStatus(Long id, NotificationEnum status) {
|
||||
|
||||
NotificationResponse response = new NotificationResponse();
|
||||
response.setId(entity.getId());
|
||||
response.setUserId(entity.getUserId());
|
||||
response.setMessage(entity.getMessage());
|
||||
response.setNotificationType(entity.getNotificationType());
|
||||
response.setStatus(entity.getStatus());
|
||||
response.setCreatedDate(entity.getCreatedDate());
|
||||
response.setUpdatedDate(entity.getUpdatedDate());
|
||||
response.setRedirectUrl(entity.getRedirectLink());
|
||||
response.setCompanyId(entity.getUserWithCompanyId());
|
||||
|
||||
return response;
|
||||
}
|
||||
public NotificationResponse updateNotificationStatus(Long id,NotificationEnum status){
|
||||
NotificationEntity notificationEntity=validateNotificationEntity(id);
|
||||
if(notificationEntity.getStatus().equals(status.getValue())){
|
||||
throw new CustomValidationException(Status.BAD_REQUEST,Translator.toLocale(GepafinConstant.NOTIFICATION_ALREADY_IN_THAT_STATE));
|
||||
NotificationEntity notificationEntity = validateNotificationEntity(id);
|
||||
if (notificationEntity.getStatus().equals(status.getValue())) {
|
||||
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.NOTIFICATION_ALREADY_IN_THAT_STATE));
|
||||
}
|
||||
notificationEntity.setStatus(status.getValue());
|
||||
notificationEntity.setUpdatedDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now()));
|
||||
@@ -229,8 +243,9 @@ public class NotificationDao {
|
||||
return convertNotificationEntityToNotificationResponse(notificationEntity);
|
||||
}
|
||||
|
||||
public void deleteNotification(Long id){
|
||||
NotificationEntity notificationEntity=validateNotificationEntity(id);
|
||||
public void deleteNotification(Long id) {
|
||||
|
||||
NotificationEntity notificationEntity = validateNotificationEntity(id);
|
||||
notificationEntity.setIsDeleted(true);
|
||||
notificationRepository.save(notificationEntity);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user