Added API for getting notifications by compnayId and userId.

This commit is contained in:
piyushkag
2024-12-28 12:19:39 +05:30
parent 5d32e19dd4
commit 8074d5d73d
6 changed files with 48 additions and 3 deletions

View File

@@ -61,6 +61,9 @@ public class NotificationDao {
@Autowired
private ApplicationService applicationService;
@Autowired
private UserDao userDao;
public NotificationResponse sendNotification(NotificationReq notificationReq) {
// Ensure userId is properly set in notificationReq if not already
@@ -249,4 +252,20 @@ public class NotificationDao {
notificationEntity.setIsDeleted(true);
notificationRepository.save(notificationEntity);
}
public List<NotificationResponse> getNotificationByCompanyIdAndUserId(Long userId, Long companyId) {
companyDao.validateCompany(companyId);
userDao.validateUser(userId);
UserWithCompanyEntity userWithCompanyData = userWithCompanyRepository.findByUserIdAndCompanyIdAndIsDeletedFalseForNotification(userId, companyId);
if (userWithCompanyData == null) {
throw new CustomValidationException(Status.BAD_REQUEST, GepafinConstant.USER_MUST_BE_ASSOCIATED_WITH_COMPANY);
}
List<NotificationEntity> notifications = notificationRepository.findByUserWithCompanyIdAndUserIdAndIsDeletedFalse(userWithCompanyData.getId(),
userWithCompanyData.getUserId());
return notifications.stream().map(this::convertNotificationEntityToNotificationResponse).toList();
}
}

View File

@@ -1,6 +1,7 @@
package net.gepafin.tendermanagement.repositories;
import net.gepafin.tendermanagement.entities.NotificationEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
@@ -14,4 +15,6 @@ public interface NotificationRepository extends JpaRepository<NotificationEntity
List<NotificationEntity> findByUserIdAndUserWithCompanyIdAndIsDeletedFalseAndStatusIn(Long userId, Long userWithCompanyId, List<String> statuses);
List<NotificationEntity> findByUserIdAndIsDeletedFalseAndStatusIn(Long userId, List<String> statuses);
List<NotificationEntity> findByUserWithCompanyIdAndUserIdAndIsDeletedFalse(Long userWithCompanyId, Long userId);
}

View File

@@ -17,4 +17,7 @@ public interface NotificationService {
public NotificationResponse updateNotificationStatus(HttpServletRequest request, Long id, NotificationEnum status);
public void deleteNotification(HttpServletRequest request, Long id);
public List<NotificationResponse> getNotificationsByCompanyIdAndUserId(Long userId, Long companyId);
}

View File

@@ -55,4 +55,8 @@ public class NotificationServiceImpl implements NotificationService {
return;
}
@Override
public List<NotificationResponse> getNotificationsByCompanyIdAndUserId(Long userId, Long companyId) {
return notificationDao.getNotificationByCompanyIdAndUserId(userId, companyId);
}
}

View File

@@ -83,6 +83,18 @@ public interface NotificationApi {
ResponseEntity<Response<Void>> deleteNotification(HttpServletRequest request,
@Parameter(description = "The notification id", required = true) @PathVariable(value = "id", required = true) Long id);
@Operation(summary = "API to get notifications by user ID and company ID", responses = { @ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.UNAUTHORIZED_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) })
@GetMapping(value = "/user/{userId}/company/{companyId}/notifications", produces = "application/json")
ResponseEntity<Response<List<NotificationResponse>>> getNotificationsByUserIdAndCompanyId(HttpServletRequest request,
@Parameter(description = "The user id", required = true) @PathVariable(value = "userId") Long userId,
@Parameter(description = "The company ID", required = true) @PathVariable(value = "companyId") Long companyId);
}

View File

@@ -4,7 +4,6 @@ import jakarta.servlet.http.HttpServletRequest;
import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.enums.NotificationEnum;
import net.gepafin.tendermanagement.enums.NotificationTypeEnum;
import net.gepafin.tendermanagement.model.request.NotificationReq;
import net.gepafin.tendermanagement.model.response.NotificationResponse;
import net.gepafin.tendermanagement.model.util.Response;
@@ -19,8 +18,6 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import static org.hibernate.internal.util.collections.CollectionHelper.listOf;
@RestController
@RequestMapping("${openapi.gepafin.base-path:/v1/notification}")
public class NotificationApiController implements NotificationApi {
@@ -67,4 +64,11 @@ public class NotificationApiController implements NotificationApi {
return ResponseEntity.status(HttpStatus.OK).body(new Response<>(null, Status.SUCCESS, Translator.toLocale(GepafinConstant.NOTIFICATION_DELETED_SUCCESSFULLY)));
}
@Override
public ResponseEntity<Response<List<NotificationResponse>>> getNotificationsByUserIdAndCompanyId(HttpServletRequest request,Long userId, Long companyId) {
List<NotificationResponse> notificationResponses = notificationService.getNotificationsByCompanyIdAndUserId(userId, companyId);
return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(notificationResponses, Status.SUCCESS, Translator.toLocale(GepafinConstant.NOTIFICATION_FETCHED_SUCCESSFULLY)));
}
}