Created endpoint for get,update,delete

This commit is contained in:
Piyush
2024-12-23 19:28:37 +05:30
parent d7e2e35746
commit db48cf9502
11 changed files with 279 additions and 19 deletions

View File

@@ -1,28 +1,38 @@
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;
import net.gepafin.tendermanagement.entities.ApplicationEntity;
import net.gepafin.tendermanagement.entities.ApplicationEvaluationEntity;
import net.gepafin.tendermanagement.entities.NotificationEntity;
import net.gepafin.tendermanagement.entities.NotificationTypeEntity;
import net.gepafin.tendermanagement.entities.UserEntity;
import net.gepafin.tendermanagement.entities.UserWithCompanyEntity;
import net.gepafin.tendermanagement.enums.NotificationEnum;
import net.gepafin.tendermanagement.enums.NotificationTypeEnum;
import net.gepafin.tendermanagement.enums.RoleStatusEnum;
import net.gepafin.tendermanagement.model.request.NotificationReq;
import net.gepafin.tendermanagement.model.response.NotificationResponse;
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.util.DateTimeUtil;
import net.gepafin.tendermanagement.util.Utils;
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
@Slf4j
@@ -43,7 +53,10 @@ public class NotificationDao {
@Autowired
private UserRepository userRepository;
public NotificationReq sendNotification(NotificationReq notificationReq) {
@Autowired
private CompanyDao companyDao;
public NotificationResponse sendNotification(NotificationReq notificationReq) {
// Ensure userId is properly set in notificationReq if not already
Long userId = notificationReq.getUserId();
@@ -55,13 +68,13 @@ public class NotificationDao {
log.info("Sending notification to user {} with content: {}", userId, notificationReq.getMessage());
List<Long> companyIds = notificationReq.getCompanyIds();
if (companyIds.isEmpty()) {
if (companyIds==null || companyIds.isEmpty()) {
sendToUser(userId, notificationEntity);
} else {
sendToCompanies(userId, companyIds, notificationEntity);
}
return convertToNotificationReq(notificationEntity);
return convertNotificationEntityToNotificationResponse(notificationEntity);
}
private NotificationEntity saveNotification(NotificationReq notificationReq) {
@@ -147,4 +160,78 @@ public class NotificationDao {
sendNotification(notificationreq);
}
}
public NotificationResponse getNotificationById(Long id) {
NotificationEntity notificationEntity = validateNotificationEntity(id);
NotificationResponse notificationReq=convertNotificationEntityToNotificationResponse(notificationEntity);
return notificationReq;
}
private NotificationEntity validateNotificationEntity(Long id) {
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);
}
if (statuses != null ) {
statusStrings = statuses.stream()
.map(NotificationEnum::name) // Convert enum to its name as String
.toList();
notificationEntities = notificationRepository.findByUserIdAndIsDeletedFalseAndStatusIn(userId,statusStrings);
if(userWithCompany != null){
notificationEntities = notificationRepository.findByUserIdAndUserWithCompanyIdAndIsDeletedFalseAndStatusIn(userId, userWithCompany.getId(), statusStrings);
}
}
List<NotificationResponse> notificationReq= notificationEntities.stream()
.map(this::convertNotificationEntityToNotificationResponse)
.collect(Collectors.toList());
return notificationReq;
}
public NotificationResponse convertNotificationEntityToNotificationResponse(NotificationEntity entity) {
if (entity == null) {
return null; // Handle null entity gracefully
}
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.setStatus(status.getValue());
notificationEntity.setUpdatedDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now()));
notificationRepository.save(notificationEntity);
return convertNotificationEntityToNotificationResponse(notificationEntity);
}
public void deleteNotification(Long id){
NotificationEntity notificationEntity=validateNotificationEntity(id);
notificationEntity.setIsDeleted(true);
notificationRepository.save(notificationEntity);
}
}