Notification Code.
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
import net.gepafin.tendermanagement.entities.NotificationEntity;
|
||||
import net.gepafin.tendermanagement.enums.NotificationEnum;
|
||||
import net.gepafin.tendermanagement.model.request.NotificationReq;
|
||||
import net.gepafin.tendermanagement.repositories.NotificationRepository;
|
||||
import net.gepafin.tendermanagement.repositories.NotificationTypeRepository;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class NotificationDao {
|
||||
|
||||
@Autowired
|
||||
private SimpMessagingTemplate messagingTemplate;
|
||||
|
||||
@Autowired
|
||||
private NotificationRepository notificationRepository;
|
||||
|
||||
@Autowired
|
||||
private NotificationTypeRepository notificationTypeRepository;
|
||||
|
||||
public NotificationReq sendNotification(NotificationReq notificationReq) {
|
||||
|
||||
// Ensure userId is properly set in notificationReq if not already
|
||||
Long userId = notificationReq.getUserId();
|
||||
if (userId == null) {
|
||||
log.error("User ID is missing in the notification request.");
|
||||
return null;
|
||||
}
|
||||
NotificationEntity notificationEntity = saveNotification(notificationReq);
|
||||
log.info("Sending notification to user {} with content: {}", userId, notificationReq.getMessage());
|
||||
List<Long> companyIds = notificationReq.getCompanyIds();
|
||||
|
||||
if (companyIds.isEmpty()) {
|
||||
sendToUser(userId, notificationEntity);
|
||||
} else {
|
||||
sendToCompanies(userId, companyIds, notificationEntity);
|
||||
}
|
||||
|
||||
return convertToNotificationReq(notificationEntity);
|
||||
}
|
||||
|
||||
private NotificationEntity saveNotification(NotificationReq notificationReq) {
|
||||
|
||||
NotificationEntity notificationEntity = convertToNotificationEntity(notificationReq);
|
||||
return notificationRepository.save(notificationEntity);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private void sendToCompanies(Long userId, List<Long> companyIds, NotificationEntity notificationEntity) {
|
||||
// Send notification to each company provided in the companyIds list
|
||||
companyIds.forEach(companyId -> {
|
||||
String companyChannel = Utils.createChannelForUserAndCompany(userId, companyId);
|
||||
log.info("Channel for User and Company {}, {}", userId, companyChannel);
|
||||
messagingTemplate.convertAndSend(companyChannel, notificationEntity);
|
||||
});
|
||||
}
|
||||
|
||||
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 NotificationEntity convertToNotificationEntity(NotificationReq notificationReq) {
|
||||
|
||||
NotificationEntity notificationEntity = new NotificationEntity();
|
||||
String message = notificationReq.getMessage();
|
||||
notificationEntity.setNotificationType(notificationReq.getNotificationType());
|
||||
notificationEntity.setUserId(notificationReq.getUserId());
|
||||
notificationEntity.setStatus(NotificationEnum.UNREAD.getValue());
|
||||
notificationEntity.setIsDeleted(Boolean.FALSE);
|
||||
notificationEntity.setMessage(message);
|
||||
return notificationEntity;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user