remove null checks for call and put some other validation

This commit is contained in:
rajesh
2024-08-28 17:44:20 +05:30
parent 88e019350a
commit 75db09eb12
15 changed files with 134 additions and 104 deletions

View File

@@ -83,5 +83,6 @@ public class GepafinConstant {
public static final String INVALID_STATUS_CHANGE_FROM_DRAFT = "invalid.status.change.from.draft";
public static final String STATUS_CANNOT_BE_CHANGED = "status.cannot.be.changed";
public static final String INVALID_STATUS_TRANSITION = "invalid.status.transition";
public static final String PUBLISHED_CALL_NOT_UPDATE = "published.call.not.update";
}

View File

@@ -1,6 +1,5 @@
package net.gepafin.tendermanagement.dao;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
@@ -8,7 +7,6 @@ import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import net.gepafin.tendermanagement.enums.UserStatusEnum;
import net.gepafin.tendermanagement.model.response.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -78,8 +76,10 @@ public class CallDao {
private UserService userService;
public CallResponse createCallStep1(CreateCallRequestStep1 createCallRequest, Long userId) {
UserEntity userEntity = userService.validateUser(userId);
createCallRequest.setRegionId(userEntity.getRoleEntity().getRegion().getId());
CallEntity callEntity = convertToCallEntity(createCallRequest);
convertToFaqEntities(createCallRequest.getFaq(), callEntity, userId);
convertToFaqEntities(createCallRequest.getFaq(), callEntity, userEntity);
convertLookUpDataEntities(createCallRequest.getAimedTo(), callEntity,
LookUpDataTypeEnum.AIMED_TO);
// createCallResponseBean = assembleCreateCallResponseBean(callEntity, Collections.emptyList(),
@@ -94,7 +94,7 @@ public class CallDao {
public CallEntity convertToCallEntity(CreateCallRequestStep1 createCallRequest) {
CallEntity callEntity = new CallEntity();
validateCallEntity(createCallRequest);
// validateCallEntity(createCallRequest);
RegionEntity region = regionRepository.findById(createCallRequest.getRegionId())
.orElseThrow(() -> new ResourceNotFoundException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.REGION_NOT_FOUND)));
@@ -102,8 +102,13 @@ public class CallDao {
callEntity.setName(createCallRequest.getName());
callEntity.setDescriptionShort(createCallRequest.getDescriptionShort());
callEntity.setDescriptionLong(createCallRequest.getDescriptionLong());
callEntity.setStartDate(createCallRequest.getStartDate());
callEntity.setEndDate(createCallRequest.getEndDate());
List<LocalDateTime> dates = createCallRequest.getDates();
if(dates!=null) {
if(dates.size()>1) {
callEntity.setStartDate(dates.get(0));
callEntity.setEndDate(dates.get(1));
}
}
callEntity.setStatus(CallStatusEnum.DRAFT.getValue());
callEntity.setAmountMax(createCallRequest.getAmountMax());
callEntity.setAmount(createCallRequest.getAmountMax());
@@ -194,20 +199,19 @@ public class CallDao {
return documentEntity;
}
public List<FaqEntity> convertToFaqEntities(List<FaqReq> faqReqList, CallEntity callEntity, Long userId) {
public List<FaqEntity> convertToFaqEntities(List<FaqReq> faqReqList, CallEntity callEntity, UserEntity userEntity) {
if (faqReqList == null) {
return null;
}
List<FaqEntity> faqEntities = faqReqList.stream().map(req -> convertToFaqEntity(req, callEntity, userId))
List<FaqEntity> faqEntities = faqReqList.stream().map(req -> convertToFaqEntity(req, callEntity, userEntity))
.collect(Collectors.toList());
faqRepository.saveAll(faqEntities);
return faqEntities;
}
public FaqEntity convertToFaqEntity(FaqReq faqReq, CallEntity callEntity, Long userId) {
public FaqEntity convertToFaqEntity(FaqReq faqReq, CallEntity callEntity, UserEntity userEntity) {
FaqEntity faqEntity = new FaqEntity();
validateFaqEntity(faqReq.getQuestion());
UserEntity userEntity = userService.validateUser(userId);
faqEntity.setUser(userEntity);
faqEntity.setIsVisible(true);
if (faqReq.getIsVisible() != null) {
@@ -246,32 +250,38 @@ public class CallDao {
}
}
public void validateCallEntity(CreateCallRequestStep1 createCallRequest) {
if (createCallRequest.getRegionId() == null) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.REGION_NOT_FOUND_MSG));
}
if (createCallRequest.getAmount().compareTo(BigDecimal.ZERO) <= 0
|| createCallRequest.getAmountMax().compareTo(BigDecimal.ZERO) <= 0) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.AMOUNT_GREATER_THAN_ZERO_MSG));
}
if (createCallRequest.getStartDate().toLocalDate().isBefore(LocalDate.now())
|| createCallRequest.getEndDate().toLocalDate().isBefore(LocalDate.now()) || createCallRequest
.getStartDate().toLocalDate().isAfter(createCallRequest.getEndDate().toLocalDate())) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.INVALID_DATE_MSG));
}
}
// public void validateCallEntity(CreateCallRequestStep1 createCallRequest) {
// if (createCallRequest.getRegionId() == null) {
// throw new CustomValidationException(Status.VALIDATION_ERROR,
// Translator.toLocale(GepafinConstant.REGION_NOT_FOUND_MSG));
// }
//
// if (createCallRequest.getAmount().compareTo(BigDecimal.ZERO) <= 0
// || createCallRequest.getAmountMax().compareTo(BigDecimal.ZERO) <= 0) {
// throw new CustomValidationException(Status.VALIDATION_ERROR,
// Translator.toLocale(GepafinConstant.AMOUNT_GREATER_THAN_ZERO_MSG));
// }
// if (createCallRequest.getStartDate().toLocalDate().isBefore(LocalDate.now())
// || createCallRequest.getEndDate().toLocalDate().isBefore(LocalDate.now()) || createCallRequest
// .getStartDate().toLocalDate().isAfter(createCallRequest.getEndDate().toLocalDate())) {
// throw new CustomValidationException(Status.VALIDATION_ERROR,
// Translator.toLocale(GepafinConstant.INVALID_DATE_MSG));
// }
//
// }
public CallResponse convertToCallResponseBean(CallEntity callEntity) {
CallResponse createCallResponseBean = new CallResponse();
createCallResponseBean.setId(callEntity.getId());
createCallResponseBean.setName(callEntity.getName());
createCallResponseBean.setStartDate(callEntity.getStartDate());
createCallResponseBean.setEndDate(callEntity.getEndDate());
List<LocalDateTime> dates = new ArrayList<>();
if(callEntity.getStartDate() != null) {
dates.add(callEntity.getStartDate());
}
if(callEntity.getStartDate() != null) {
dates.add(callEntity.getEndDate());
}
createCallResponseBean.setDates(dates);
createCallResponseBean.setDescriptionShort(callEntity.getDescriptionShort());
createCallResponseBean.setDescriptionLong(callEntity.getDescriptionLong());
createCallResponseBean.setStatus(CallStatusEnum.valueOf(callEntity.getStatus()));
@@ -353,6 +363,9 @@ public class CallDao {
public List<LookUpDataResponse> convertLookUpDataEntities(List<LookUpDataReq> lookUpData, CallEntity callEntity,
LookUpDataEntity.LookUpDataTypeEnum type) {
if(lookUpData == null) {
return null;
}
List<LookUpDataEntity> lookUpDataEntities = lookUpData.stream()
.map(req -> convertLookUpDataRequestIntoLookUpDataEntity(req, type)).collect(Collectors.toList());
@@ -418,7 +431,7 @@ public class CallDao {
CallEntity callEntity = callRepository.findById(callId)
.orElseThrow(() -> new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.CALL_NOT_FOUND)));
validateUpdate(callEntity);
setIfUpdated(callEntity::getThreshold, callEntity::setThreshold, createCallRequest.getThreshold());
callRepository.save(callEntity);
convertToEvaluationCriteriaEntities(createCallRequest.getCriteria(), callEntity, LookUpDataTypeEnum.EVALUATION_CRITERIA);
@@ -442,11 +455,24 @@ public class CallDao {
return createCallResponseBean;
}
private void validateUpdate(CallEntity callEntity) {
if(callEntity.getStatus().equals(CallStatusEnum.PUBLISH.getValue())) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.PUBLISHED_CALL_NOT_UPDATE));
}
}
public void isValidDateRange(UpdateCallRequestStep1 updateCallRequest, CallEntity callEntity) {
LocalDate startDate = updateCallRequest.getStartDate() != null ? updateCallRequest.getStartDate().toLocalDate()
List<LocalDateTime> dates = updateCallRequest.getDates();
LocalDate startDate = (dates != null && dates.size() > 0 && dates.get(0) != null)
? dates.get(0).toLocalDate()
: null;
LocalDate endDate = updateCallRequest.getEndDate() != null ? updateCallRequest.getEndDate().toLocalDate()
LocalDate endDate = (dates != null && dates.size() > 1 && dates.get(1) != null)
? dates.get(1).toLocalDate()
: null;
Boolean isValid = true;
if (startDate != null && endDate != null && startDate.isAfter(endDate)) {
isValid = false;
@@ -473,8 +499,18 @@ public class CallDao {
updateCallRequest.getDescriptionShort());
setIfUpdated(callEntity::getDescriptionLong, callEntity::setDescriptionLong,
updateCallRequest.getDescriptionLong());
setIfUpdated(callEntity::getStartDate, callEntity::setStartDate, updateCallRequest.getStartDate());
setIfUpdated(callEntity::getEndDate, callEntity::setEndDate, updateCallRequest.getEndDate());
List<LocalDateTime> dates=updateCallRequest.getDates();
if (dates != null && dates.size()>1) {
if (dates.size() > 0) {
setIfUpdated(callEntity::getStartDate, callEntity::setStartDate, dates.get(0));
}
if (dates.size() > 1) {
setIfUpdated(callEntity::getEndDate, callEntity::setEndDate, dates.get(1));
}
}
// setIfUpdated(callEntity::getStartDate, callEntity::setStartDate, updateCallRequest.getStartDate());
// setIfUpdated(callEntity::getEndDate, callEntity::setEndDate, updateCallRequest.getEndDate());
setIfUpdated(callEntity::getAmount, callEntity::setAmount, updateCallRequest.getAmount());
setIfUpdated(callEntity::getAmountMax, callEntity::setAmountMax, updateCallRequest.getAmountMax());
setIfUpdated(callEntity::getDocumentationRequested, callEntity::setDocumentationRequested,
@@ -657,7 +693,7 @@ public class CallDao {
}
break;
case PUBLISH:
case EXPIRE:
case EXPIRED:
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.STATUS_CANNOT_BE_CHANGED));
default:

View File

@@ -9,7 +9,7 @@ import net.gepafin.tendermanagement.model.request.FaqReq;
import net.gepafin.tendermanagement.model.response.FaqResponseBean;
import net.gepafin.tendermanagement.repositories.CallRepository;
import net.gepafin.tendermanagement.repositories.FaqRepository;
import net.gepafin.tendermanagement.repositories.UserRepository;
import net.gepafin.tendermanagement.service.UserService;
import net.gepafin.tendermanagement.util.DateTimeUtil;
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
@@ -25,7 +25,7 @@ public class FaqDao {
private FaqRepository faqRepository;
@Autowired
private UserRepository userRepository;
private UserService userService;
@Autowired
private CallDao callDao;
@@ -38,7 +38,8 @@ public class FaqDao {
CallEntity callEntity = callRepository.findById(callId)
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
Translator.toLocale(GepafinConstant.CALL_NOT_FOUND)));
entity = callDao.convertToFaqEntity(faqRequest, callEntity, userId);
UserEntity userEntity = userService.validateUser(userId);
entity = callDao.convertToFaqEntity(faqRequest, callEntity, userEntity);
faqRepository.save(entity);
return convertFaqEntityToResponseBean(entity);
}
@@ -81,9 +82,7 @@ public class FaqDao {
private void updateFaqEntity(FaqEntity faqEntity, FaqReq faqReq, Long userId, CallEntity callEntity) {
faqEntity.setQuestion(faqReq.getQuestion());
UserEntity userEntity = userRepository.findById(userId)
.orElseThrow(() -> new ResourceNotFoundException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.LOOK_UP_DATA_NOT_VALID_MSG)));
UserEntity userEntity = userService.validateUser(userId);
faqEntity.setUser(userEntity);
faqEntity.setIsVisible(true);
if (faqReq.getIsVisible() != null) {

View File

@@ -17,7 +17,7 @@ import java.time.LocalDateTime;
@Builder
public class CallEntity extends BaseEntity {
@Column(name = "NAME", nullable = false, length = 255)
@Column(name = "NAME", length = 255)
private String name;
@Column(name = "DESCRIPTION_SHORT", columnDefinition = "TEXT")
@@ -26,23 +26,23 @@ public class CallEntity extends BaseEntity {
@Column(name = "DESCRIPTION_LONG", columnDefinition = "TEXT")
private String descriptionLong;
@Column(name = "START_DATE", nullable = false)
@Column(name = "START_DATE")
private LocalDateTime startDate;
@Column(name = "END_DATE", nullable = false)
@Column(name = "END_DATE")
private LocalDateTime endDate;
@Column(name = "STATUS", nullable = false, length = 255)
@Column(name = "STATUS", length = 255)
private String status;
@ManyToOne
@JoinColumn(name = "REGION_ID", nullable = false, foreignKey = @ForeignKey(name = "fk_region_call"))
@JoinColumn(name = "REGION_ID", foreignKey = @ForeignKey(name = "fk_region_call"))
private RegionEntity region;
@Column(name = "AMOUNT", nullable = false)
@Column(name = "AMOUNT")
private BigDecimal amount;
@Column(name = "AMOUNT_MAX", nullable = false)
@Column(name = "AMOUNT_MAX")
private BigDecimal amountMax;
@Column(name = "CONTACT_INFO", columnDefinition = "TEXT")
@@ -51,7 +51,7 @@ public class CallEntity extends BaseEntity {
@Column(name = "SUBMISSION_METHOD", columnDefinition = "TEXT")
private String submissionMethod;
@Column(name = "THRESHOLD", nullable = false)
@Column(name = "THRESHOLD")
private Long threshold;
@Column(name="DOCUMENTATION_REQUESTED",columnDefinition = "TEXT")

View File

@@ -6,7 +6,7 @@ public enum CallStatusEnum {
DRAFT("DRAFT"),
PUBLISH("PUBLISH"),
EXPIRE("EXPIRED"),
EXPIRED("EXPIRED"),
READY_TO_PUBLISH("READY_TO_PUBLISH");
private String value;

View File

@@ -3,42 +3,27 @@ package net.gepafin.tendermanagement.model.request;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
public class CreateCallRequestStep1 {
@NotEmpty
private String name;
@NotEmpty
private String descriptionShort;
@NotEmpty
private String descriptionLong;
@NotNull
private LocalDateTime startDate;
private List<LocalDateTime> dates;
@NotNull
private LocalDateTime endDate;
@NotNull
private Long regionId;
@NotNull
private BigDecimal amount;
@NotNull
private BigDecimal amountMax;
@NotNull
private List<LookUpDataReq> aimedTo;
@NotEmpty
private String documentationRequested;
private Boolean confidi;

View File

@@ -2,7 +2,6 @@ package net.gepafin.tendermanagement.model.request;
import java.util.List;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
@@ -10,10 +9,8 @@ public class CreateCallRequestStep2 {
private Long threshold;
@NotNull
private List<EvaluationCriteriaReq> criteria;
@NotNull
private List<LookUpDataReq> checkList;
private List<DocumentReq> docs;

View File

@@ -9,31 +9,20 @@ import lombok.Data;
@Data
public class UpdateCallRequestStep1 {
private String name;
private String descriptionShort;
private String descriptionLong;
private LocalDateTime startDate;
private LocalDateTime endDate;
private List<LocalDateTime> dates;
private BigDecimal amount;
private BigDecimal amountMax;
private List<LookUpDataReq> aimedTo;
private String documentationRequested;
private Boolean confidi;

View File

@@ -18,9 +18,7 @@ public class CallResponse {
private String descriptionLong;
private LocalDateTime startDate;
private LocalDateTime endDate;
private List<LocalDateTime> dates;
private CallStatusEnum status;

View File

@@ -1,7 +1,13 @@
package net.gepafin.tendermanagement.service.impl;
import java.time.LocalDate;
import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.model.response.CallResponse;
import net.gepafin.tendermanagement.util.FieldValidator;
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
public class CallValidatorServiceImpl {
@@ -11,8 +17,8 @@ public class CallValidatorServiceImpl {
.notNull(response.getName(), "name")
.notNull(response.getDescriptionShort(), "descriptionShort")
.notNull(response.getDescriptionLong(), "descriptionLong")
.notNull(response.getStartDate(), "startDate")
.notNull(response.getEndDate(), "endDate")
.notNull(response.getDates().get(0), "startDate")
.notNull(response.getDates().get(1), "endDate")
.notNull(response.getStatus(), "status")
.notNull(response.getRegionId(), "regionId")
.notNull(response.getAmount(), "amount")
@@ -26,6 +32,13 @@ public class CallValidatorServiceImpl {
.notEmpty(response.getImages(), "images")
.notEmpty(response.getCheckList(), "checkList")
.validate();
if (response.getDates().get(0).toLocalDate().isBefore(LocalDate.now())
|| response.getDates().get(1).toLocalDate().isBefore(LocalDate.now()) ||
response.getDates().get(0).toLocalDate().isAfter(response.getDates().get(1).toLocalDate())) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.INVALID_DATE_MSG));
}
}
}

View File

@@ -3,8 +3,6 @@ package net.gepafin.tendermanagement.web.rest.api;
import java.util.List;
import net.gepafin.tendermanagement.enums.CallStatusEnum;
import net.gepafin.tendermanagement.enums.UserStatusEnum;
import net.gepafin.tendermanagement.model.response.UserResponseBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
@@ -46,7 +44,7 @@ public interface CallApi {
@Parameter(description = "Call request object", required = true)
@Valid @RequestBody CreateCallRequestStep1 createCallRequest);
@Operation(summary = "Api to create call step 2",
@Operation(summary = "Api to update call step 2",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {

View File

@@ -3,8 +3,6 @@ package net.gepafin.tendermanagement.web.rest.api.impl;
import java.util.List;
import net.gepafin.tendermanagement.enums.CallStatusEnum;
import net.gepafin.tendermanagement.enums.UserStatusEnum;
import net.gepafin.tendermanagement.model.response.UserResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

View File

@@ -442,4 +442,15 @@
</addColumn>
</changeSet>
<changeSet id="27-08-2024_3" author="Rajesh Khore">
<dropNotNullConstraint tableName="call" columnName="name"/>
<dropNotNullConstraint tableName="call" columnName="start_date"/>
<dropNotNullConstraint tableName="call" columnName="end_date"/>
<dropNotNullConstraint tableName="call" columnName="status"/>
<dropNotNullConstraint tableName="call" columnName="region_id"/>
<dropNotNullConstraint tableName="call" columnName="amount"/>
<dropNotNullConstraint tableName="call" columnName="amount_max"/>
</changeSet>
</databaseChangeLog>

View File

@@ -55,6 +55,9 @@ status.same.error=Status is already set.
invalid.status.change.from.draft=Status cannot be changed to READY_TO_PUBLISH or PUBLISH from DRAFT.
status.cannot.be.changed=Status cannot be changed.
invalid.status.transition=Invalid status transition.
published.call.not.update=Published call cannot be updated.
# Login-related messages
login.successfully=Login successfully.

View File

@@ -55,6 +55,8 @@ status.same.error=Lo stato
invalid.status.change.from.draft=Lo stato non può essere cambiato in READY_TO_PUBLISH o PUBLISH da DRAFT.
status.cannot.be.changed=Lo stato non può essere cambiato.
invalid.status.transition=Transizione di stato non valida.
published.call.not.update=Il bando pubblicato non può essere aggiornato.
# Login-related messages
login.successfully=Accesso effettuato con successo.