Added user action log and versioning for communication.

This commit is contained in:
piyushkag
2024-11-26 12:47:03 +05:30
parent 4f68d09568
commit 7d04e4ab58
7 changed files with 99 additions and 14 deletions

View File

@@ -5,14 +5,19 @@ import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.entities.ApplicationAmendmentRequestEntity;
import net.gepafin.tendermanagement.entities.CommunicationEntity;
import net.gepafin.tendermanagement.enums.VersionActionTypeEnum;
import net.gepafin.tendermanagement.model.request.CommunicationRequestBean;
import net.gepafin.tendermanagement.model.request.VersionHistoryRequest;
import net.gepafin.tendermanagement.model.response.ApplicationAmendmentResponse;
import net.gepafin.tendermanagement.model.response.CommunicationResponseBean;
import net.gepafin.tendermanagement.repositories.CommunicationRepository;
import net.gepafin.tendermanagement.service.ApplicationAmendmentRequestService;
import net.gepafin.tendermanagement.util.LoggingUtil;
import net.gepafin.tendermanagement.util.Utils;
import net.gepafin.tendermanagement.util.Validator;
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -34,24 +39,41 @@ public class CommunicationDao {
@Autowired
private Validator validator;
@Autowired
private LoggingUtil loggingUtil;
@Autowired
private HttpServletRequest request;
public CommunicationResponseBean addCommentToAmendmentRequest(HttpServletRequest request, CommunicationRequestBean communicationReq, Long amendmentId) {
log.info("Adding communication request...");
CommunicationEntity communicationEntity = convertToCommunicationCommentEntity(communicationReq, amendmentId);
communicationEntity = communicationRepository.save(communicationEntity);
/** This code is responsible for adding a version history log for the "adding comment to amendment request" operation. **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.INSERT).oldData(null).newData(communicationEntity).build());
log.info("Added comment: {}", communicationEntity);
return convertToCommunicationResponseBean(communicationEntity);
}
public String deleteComment(Long amendmentId, Long commentId) {
CommunicationEntity data = communicationRepository.findById(commentId)
CommunicationEntity data = communicationRepository.findByIdAndIsDeletedFalse(commentId)
.orElseThrow(() -> new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.COMMENT_NOT_FOUND)));
//cloned for old commentData
CommunicationEntity oldComment = Utils.getClonedEntityForData(data);
if (!data.getApplicationAmendmentRequest().getId().equals(amendmentId)) {
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.INVALID_AMENDMENT_FOR_COMMENT));
}
data.setIsDeleted(true);
communicationRepository.save(data);
data = communicationRepository.save(data);
/** This code is responsible for adding a version history log for the "soft deleting comment to amendment request" operation. **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.SOFT_DELETE).oldData(oldComment).newData(data).build());
return "Deleted Comment Successfully.";
}
@@ -70,6 +92,9 @@ public class CommunicationDao {
log.info("Updating communication comment...");
CommunicationEntity existingComment = communicationRepository.findById(commentId)
.orElseThrow(() -> new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.COMMENT_NOT_FOUND)));
//cloned for old data for communication
CommunicationEntity oldCommentData = Utils.getClonedEntityForData(existingComment);
if (!existingComment.getApplicationAmendmentRequest().getId().equals(amendmentId)) {
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.COMMENT_NOT_ASSOCIATE_WITH_AMENDMENT_ID_ERROR_MSG));
}
@@ -78,6 +103,10 @@ public class CommunicationDao {
existingComment.setCommentedDate(LocalDateTime.now());
existingComment = communicationRepository.save(existingComment);
log.info("Updated Comment: {}", existingComment);
/** This code is responsible for adding a version history log for the "updating comment to amendment request" operation. **/
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldCommentData).newData(existingComment).build());
return convertToCommunicationResponseBean(existingComment);
}
@@ -92,6 +121,7 @@ public class CommunicationDao {
response.setTitle(entity.getCommunicationTitle());
response.setSenderUserId(entity.getSenderUserId());
response.setReceiverUserId(entity.getReceiverUserId());
response.setId(entity.getId());
return response;
}