diff --git a/docs/health-api-specs/contracts/stock.yml b/docs/health-api-specs/contracts/stock.yml index d2df236940d..2ee5512df51 100644 --- a/docs/health-api-specs/contracts/stock.yml +++ b/docs/health-api-specs/contracts/stock.yml @@ -504,6 +504,7 @@ definitions: - LOST_IN_TRANSIT - DAMAGED_IN_STORAGE - DAMAGED_IN_TRANSIT + description: This field accepts values from enum and if an invalid value is provided, it will default to null. wayBillNumber: type: string minLength: 2 diff --git a/health-services/libraries/health-services-models/pom.xml b/health-services/libraries/health-services-models/pom.xml index 6dc9eca800b..7d9b777292b 100644 --- a/health-services/libraries/health-services-models/pom.xml +++ b/health-services/libraries/health-services-models/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.egov.common health-services-models - 1.0.14-SNAPSHOT + 1.0.15-SNAPSHOT 8 8 @@ -28,6 +28,11 @@ digit-models 1.0.0-SNAPSHOT + + org.egov.services + tracer + 2.1.4-SNAPSHOT + diff --git a/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/core/validator/CustomIntegerDeserializer.java b/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/core/validator/CustomIntegerDeserializer.java new file mode 100644 index 00000000000..b5cb00eac27 --- /dev/null +++ b/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/core/validator/CustomIntegerDeserializer.java @@ -0,0 +1,43 @@ +package org.egov.common.models.core.validator; + +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import org.egov.tracer.model.CustomException; + +// Custom deserializer for Integer values +public class CustomIntegerDeserializer extends StdDeserializer { + + public CustomIntegerDeserializer() { + this(null); + } + + public CustomIntegerDeserializer(Class vc) { + super(vc); + } + + @Override + public Integer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { + + // Read the JSON tree from the parser + JsonNode node = jsonParser.getCodec().readTree(jsonParser); + System.out.println(node.toString()); + if(node.asLong() > Integer.MAX_VALUE){ + throw new CustomException("INVALID_INPUT","Value must be an Integer"); + } + + // Parse the quantity as an integer + int quantity = node.asInt(); + + // Check if the parsed quantity matches the original string representation + if ((double) quantity != Double.parseDouble(node.asText())) { + throw new CustomException("INVALID_INPUT", "Quantity must be an integer"); + } + + // Return the parsed quantity + return quantity; + } +} diff --git a/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/referralmanagement/hfreferral/HFReferral.java b/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/referralmanagement/hfreferral/HFReferral.java index 76aaa84009e..330b437df95 100644 --- a/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/referralmanagement/hfreferral/HFReferral.java +++ b/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/referralmanagement/hfreferral/HFReferral.java @@ -1,6 +1,5 @@ package org.egov.common.models.referralmanagement.hfreferral; -import java.util.List; import javax.validation.Valid; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @@ -13,7 +12,6 @@ import lombok.Data; import lombok.NoArgsConstructor; import org.egov.common.models.project.AdditionalFields; -import org.egov.common.models.referralmanagement.sideeffect.SideEffect; @Data @NoArgsConstructor diff --git a/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/stock/ReferenceIdType.java b/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/stock/ReferenceIdType.java new file mode 100644 index 00000000000..7b5943c3493 --- /dev/null +++ b/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/stock/ReferenceIdType.java @@ -0,0 +1,32 @@ +package org.egov.common.models.stock; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonValue; + +@JsonIgnoreProperties(ignoreUnknown = true) +public enum ReferenceIdType { + PROJECT("PROJECT"), + OTHER("OTHER"); + private String value; + + ReferenceIdType(String value) { + this.value = value; + } + + @Override + @JsonValue + public String toString() { + return String.valueOf(value); + } + + @JsonCreator + public static ReferenceIdType fromValue(String text) { + for (ReferenceIdType b : ReferenceIdType.values()) { + if (String.valueOf(b.value).equals(text)) { + return b; + } + } + return null; + } +} diff --git a/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/stock/SenderReceiverType.java b/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/stock/SenderReceiverType.java new file mode 100644 index 00000000000..e5b1924574a --- /dev/null +++ b/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/stock/SenderReceiverType.java @@ -0,0 +1,34 @@ +package org.egov.common.models.stock; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonValue; + +@JsonIgnoreProperties(ignoreUnknown = true) +public enum SenderReceiverType { + WAREHOUSE("WAREHOUSE"), + + STAFF("STAFF"); + + private String value; + + SenderReceiverType(String value) { + this.value = value; + } + + @Override + @JsonValue + public String toString() { + return String.valueOf(value); + } + + @JsonCreator + public static SenderReceiverType fromValue(String text) { + for (SenderReceiverType b : SenderReceiverType.values()) { + if (String.valueOf(b.value).equals(text)) { + return b; + } + } + return null; + } +} diff --git a/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/stock/Stock.java b/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/stock/Stock.java index a08748daf49..c53164b370c 100644 --- a/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/stock/Stock.java +++ b/health-services/libraries/health-services-models/src/main/java/org/egov/common/models/stock/Stock.java @@ -1,21 +1,21 @@ package org.egov.common.models.stock; import javax.validation.Valid; +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; -import org.springframework.validation.annotation.Validated; - import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; - import digit.models.coremodels.AuditDetails; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Builder.Default; import lombok.Data; import lombok.NoArgsConstructor; +import org.springframework.validation.annotation.Validated; /** * Stock @@ -51,6 +51,8 @@ public class Stock { @JsonProperty("quantity") @NotNull + @Min(value = 1, message = "Minimum value cannot be less than 1") + @Max(value = Integer.MAX_VALUE, message = "Value exceeds maximum allowable limit") private Integer quantity; /* project id in-case of health */ @@ -58,12 +60,13 @@ public class Stock { private String referenceId; @JsonProperty("referenceIdType") - @Size(min=2, max=64) - private String referenceIdType; + @NotNull(message = "referenceIdType must be PROJECT or OTHER") + @Valid + private ReferenceIdType referenceIdType; // transaction fields @JsonProperty("transactionType") - @NotNull + @NotNull(message = "transactionType must be either RECEIVED or DISPATCHED") @Valid private TransactionType transactionType; @@ -77,9 +80,9 @@ public class Stock { private String senderId; @JsonProperty("senderType") - @NotNull - @Size(min=2, max=64) - private String senderType; + @NotNull(message = "Sender Type can be either WAREHOUSE or STAFF") + @Valid + private SenderReceiverType senderType; @JsonProperty("receiverId") @NotNull @@ -87,9 +90,9 @@ public class Stock { private String receiverId; @JsonProperty("receiverType") - @NotNull - @Size(min=2, max=64) - private String receiverType; + @NotNull(message = "Receiver Type can be either WAREHOUSE or STAFF") + @Valid + private SenderReceiverType receiverType; @JsonProperty("wayBillNumber") @Size(min = 2, max = 200) diff --git a/health-services/stock/pom.xml b/health-services/stock/pom.xml index 36a6ee1f830..1ccbcf4a3e4 100644 --- a/health-services/stock/pom.xml +++ b/health-services/stock/pom.xml @@ -49,7 +49,7 @@ org.egov.common health-services-models - 1.0.9-SNAPSHOT + 1.0.15-SNAPSHOT diff --git a/health-services/stock/src/main/java/org/egov/stock/config/MainConfiguration.java b/health-services/stock/src/main/java/org/egov/stock/config/MainConfiguration.java index e0d61ae0682..b067e1cb2ab 100644 --- a/health-services/stock/src/main/java/org/egov/stock/config/MainConfiguration.java +++ b/health-services/stock/src/main/java/org/egov/stock/config/MainConfiguration.java @@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; +import com.fasterxml.jackson.databind.module.SimpleModule; +import org.egov.common.models.core.validator.CustomIntegerDeserializer; import org.egov.tracer.config.TracerConfiguration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -42,7 +44,12 @@ public void initialize() { @Bean public ObjectMapper objectMapper(){ - return new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).setTimeZone(TimeZone.getTimeZone(timeZone)); + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + SimpleModule module = new SimpleModule(); + module.addDeserializer(Integer.class, new CustomIntegerDeserializer()); + objectMapper.registerModule(module); + return objectMapper.setTimeZone(TimeZone.getTimeZone(timeZone)); } @Bean diff --git a/health-services/stock/src/main/java/org/egov/stock/repository/rowmapper/StockRowMapper.java b/health-services/stock/src/main/java/org/egov/stock/repository/rowmapper/StockRowMapper.java index 89471581084..d011f3301a4 100644 --- a/health-services/stock/src/main/java/org/egov/stock/repository/rowmapper/StockRowMapper.java +++ b/health-services/stock/src/main/java/org/egov/stock/repository/rowmapper/StockRowMapper.java @@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import digit.models.coremodels.AuditDetails; import org.egov.common.models.stock.AdditionalFields; +import org.egov.common.models.stock.ReferenceIdType; +import org.egov.common.models.stock.SenderReceiverType; import org.egov.common.models.stock.Stock; import org.egov.common.models.stock.TransactionReason; import org.egov.common.models.stock.TransactionType; @@ -33,6 +35,10 @@ public Stock mapRow(ResultSet resultSet, int i) throws SQLException { .lastModifiedTime(resultSet.getLong("clientLastModifiedTime")) .lastModifiedBy(resultSet.getString("clientLastModifiedBy")) .build(); + Long dateOfEntry = resultSet.getLong("dateOfEntry"); + if(resultSet.wasNull()){ + dateOfEntry = null; + } return Stock.builder() .id(resultSet.getString("id")) .clientReferenceId(resultSet.getString("clientReferenceId")) @@ -41,20 +47,20 @@ public Stock mapRow(ResultSet resultSet, int i) throws SQLException { .quantity(resultSet.getInt("quantity")) .wayBillNumber(resultSet.getString("wayBillNumber")) .referenceId(resultSet.getString("referenceId")) - .referenceIdType(resultSet.getString("referenceIdType")) + .referenceIdType(ReferenceIdType.fromValue(resultSet.getString("referenceIdType"))) .transactionType(TransactionType.fromValue(resultSet.getString("transactionType"))) .transactionReason(TransactionReason.fromValue(resultSet.getString("transactionReason"))) .senderId(resultSet.getString("senderId")) - .senderType(resultSet.getString("senderType")) + .senderType(SenderReceiverType.fromValue(resultSet.getString("senderType"))) .receiverId(resultSet.getString("receiverId")) - .receiverType(resultSet.getString("receiverType")) + .receiverType(SenderReceiverType.fromValue(resultSet.getString("receiverType"))) .additionalFields(resultSet.getString("additionalDetails") == null ? null : objectMapper .readValue(resultSet.getString("additionalDetails"), AdditionalFields.class)) .auditDetails(auditDetails) .clientAuditDetails(clientAuditDetails) .rowVersion(resultSet.getInt("rowVersion")) .isDeleted(resultSet.getBoolean("isDeleted")) - .dateOfEntry(resultSet.getLong("dateOfEntry")) + .dateOfEntry(dateOfEntry) .build(); } catch (JsonProcessingException e) { throw new SQLException(e); diff --git a/health-services/stock/src/main/java/org/egov/stock/service/FacilityService.java b/health-services/stock/src/main/java/org/egov/stock/service/FacilityService.java index 068a26a99fb..5d93a0ecfdd 100644 --- a/health-services/stock/src/main/java/org/egov/stock/service/FacilityService.java +++ b/health-services/stock/src/main/java/org/egov/stock/service/FacilityService.java @@ -25,6 +25,7 @@ import org.egov.common.models.project.ProjectFacilityBulkResponse; import org.egov.common.models.project.ProjectFacilitySearch; import org.egov.common.models.project.ProjectFacilitySearchRequest; +import org.egov.common.models.stock.SenderReceiverType; import org.egov.common.models.stock.Stock; import org.egov.common.models.stock.StockReconciliation; import org.egov.stock.config.StockConfiguration; @@ -97,10 +98,10 @@ public Map> validateProjectFacilityMappings(List ent Stock stock = (Stock) entity; - if (stock.getSenderType().equalsIgnoreCase(WAREHOUSE)) { + if (SenderReceiverType.WAREHOUSE.equals(stock.getSenderType())) { facilityIds.add(stock.getSenderId()); } - if (stock.getReceiverType().equalsIgnoreCase(WAREHOUSE)) { + if (SenderReceiverType.WAREHOUSE.equals(stock.getReceiverType())) { facilityIds.add(stock.getReceiverId()); } } diff --git a/health-services/stock/src/main/java/org/egov/stock/service/StockService.java b/health-services/stock/src/main/java/org/egov/stock/service/StockService.java index 8bbbd72c065..7876d60ac63 100644 --- a/health-services/stock/src/main/java/org/egov/stock/service/StockService.java +++ b/health-services/stock/src/main/java/org/egov/stock/service/StockService.java @@ -34,6 +34,7 @@ import org.egov.stock.validator.stock.SProductVariantIdValidator; import org.egov.stock.validator.stock.SReferenceIdValidator; import org.egov.stock.validator.stock.SRowVersionValidator; +import org.egov.stock.validator.stock.SSenderIdReceiverIdEqualsValidator; import org.egov.stock.validator.stock.SUniqueEntityValidator; import org.egov.stock.validator.stock.StocktransferPartiesValidator; import org.egov.stock.web.models.StockSearchRequest; @@ -57,6 +58,7 @@ public class StockService { private final Predicate> isApplicableForCreate = validator -> validator.getClass().equals(SProductVariantIdValidator.class) + || validator.getClass().equals(SSenderIdReceiverIdEqualsValidator.class) || validator.getClass().equals(StocktransferPartiesValidator.class) || validator.getClass().equals(SReferenceIdValidator.class); @@ -68,11 +70,13 @@ public class StockService { || validator.getClass().equals(SRowVersionValidator.class) || validator.getClass().equals(SUniqueEntityValidator.class) || validator.getClass().equals(SReferenceIdValidator.class) + || validator.getClass().equals(SSenderIdReceiverIdEqualsValidator.class) || validator.getClass().equals(StocktransferPartiesValidator.class); private final Predicate> isApplicableForDelete = validator -> validator.getClass().equals(SNonExistentValidator.class) - || validator.getClass().equals(SNullIdValidator.class); + || validator.getClass().equals(SNullIdValidator.class) + || validator.getClass().equals(SRowVersionValidator.class); public StockService(StockRepository stockRepository, List> validators, StockConfiguration configuration, StockEnrichmentService enrichmentService) { this.stockRepository = stockRepository; diff --git a/health-services/stock/src/main/java/org/egov/stock/util/ValidatorUtil.java b/health-services/stock/src/main/java/org/egov/stock/util/ValidatorUtil.java index b39ad93699d..b46431fe018 100644 --- a/health-services/stock/src/main/java/org/egov/stock/util/ValidatorUtil.java +++ b/health-services/stock/src/main/java/org/egov/stock/util/ValidatorUtil.java @@ -21,6 +21,7 @@ import org.egov.common.contract.request.RequestInfo; import org.egov.common.ds.Tuple; import org.egov.common.models.Error; +import org.egov.common.models.stock.SenderReceiverType; import org.egov.common.models.stock.Stock; import org.egov.common.models.stock.StockReconciliation; import org.egov.common.service.UserService; @@ -64,17 +65,13 @@ public static Map> validateFacilityIds(R request, Map - * @param - * @param stockRequest + * Non-generic method used for validating sender/receiver (parties) against facility or staff based on the type + * + * @param requestInfo * @param errorDetailsMap - * @param validEntities - * @param getId + * @param validStockEntities * @param facilityService + * @param userService * @return */ public static Map> validateStockTransferParties(RequestInfo requestInfo, @@ -94,17 +91,14 @@ public static Map> validateStockTransferParties(RequestInf } /** - * validates the list of party-ids (facility and staff) against the respective - * APIs and enriches the invalid ids list for both parties - * - * @param - * @param stockRequest + * Validates the list of party-ids (facility and staff) against the respective APIs and enriches the invalid ids list for both parties. + * + * @param requestInfo * @param errorDetailsMap * @param validStockEntities * @param facilityService - * @param facilityIds - * @param InvalidStaffId - * @param invalidFacilityIds + * @param userService + * @return A tuple containing lists of invalid facility ids and invalid staff ids */ @SuppressWarnings("unchecked") private static Tuple, List> validateAndEnrichInvalidPartyIds(RequestInfo requestInfo, @@ -158,18 +152,18 @@ private static void enrichFaciltyAndStaffIdsFromStock(List validStockEnti for (Stock stock : validStockEntities) { - if (stock.getSenderType().equalsIgnoreCase(WAREHOUSE)) { + if (SenderReceiverType.WAREHOUSE.equals(stock.getSenderType()) && stock.getSenderId() != null) { facilityIds.add(stock.getSenderId()); - } - if (stock.getSenderType().equalsIgnoreCase(STAFF)) { + } else if (SenderReceiverType.STAFF.equals(stock.getSenderType()) && stock.getSenderId() != null) { staffIds.add(stock.getSenderId()); } - if (stock.getReceiverType().equalsIgnoreCase(WAREHOUSE)) { + + if (SenderReceiverType.WAREHOUSE.equals(stock.getReceiverType()) && stock.getReceiverId() != null) { facilityIds.add(stock.getReceiverId()); - } - if (stock.getReceiverType().equalsIgnoreCase(STAFF)) { + } else if (SenderReceiverType.STAFF.equals(stock.getReceiverType()) && stock.getReceiverId() != null) { staffIds.add(stock.getReceiverId()); } + } } @@ -179,7 +173,7 @@ private static void enrichFaciltyAndStaffIdsFromStock(List validStockEnti * * @param errorDetailsMap * @param validStockEntities - * @param InvalidStaffId + * @param invalidStaffIds * @param invalidFacilityIds */ @SuppressWarnings("unchecked") @@ -195,16 +189,16 @@ private static void enrichErrorMapFromInvalidPartyIds(Map> er String senderId = stock.getSenderId(); String recieverId = stock.getReceiverId(); - if ((stock.getSenderType().equalsIgnoreCase(WAREHOUSE) && invalidFacilityIds.contains(senderId)) + if ((SenderReceiverType.WAREHOUSE.equals(stock.getSenderType()) && invalidFacilityIds.contains(senderId)) - || (stock.getSenderType().equalsIgnoreCase(STAFF) && invalidStaffIds.contains(senderId))) { + || (SenderReceiverType.STAFF.equals(stock.getSenderType()) && invalidStaffIds.contains(senderId))) { getIdForErrorFromMethod(errorDetailsMap, (T) stock, senderIdMethod); } - if ((stock.getReceiverType().equalsIgnoreCase(WAREHOUSE) && invalidFacilityIds.contains(recieverId)) + if ((SenderReceiverType.WAREHOUSE.equals(stock.getReceiverType()) && invalidFacilityIds.contains(recieverId)) - || (stock.getReceiverType().equalsIgnoreCase(STAFF) && invalidStaffIds.contains(recieverId))) { + || (SenderReceiverType.STAFF.equals(stock.getReceiverType()) && invalidStaffIds.contains(recieverId))) { getIdForErrorFromMethod(errorDetailsMap, (T) stock, recieverIdMethod); } @@ -232,7 +226,7 @@ private static void getIdForErrorFromMethod(Map> errorDetails * @param request * @param errorDetailsMap * @param validEntities - * @param getId + * @param getReferenceId * @param facilityService * @return */ @@ -279,11 +273,11 @@ private static void enrichErrorForStock(List validEntities, List facilityIds = ProjectFacilityMappingOfIds.get(stock.getReferenceId()); if (!CollectionUtils.isEmpty(facilityIds)) { - if (stock.getSenderType().equalsIgnoreCase("WAREHOUSE") && !facilityIds.contains(senderId)) { + if (SenderReceiverType.WAREHOUSE.equals(stock.getSenderType()) && !facilityIds.contains(senderId)) { populateErrorForStock(stock, senderId, errorDetailsMap); } - if (stock.getReceiverType().equalsIgnoreCase("WAREHOUSE") && !facilityIds.contains(receiverId)) + if (SenderReceiverType.WAREHOUSE.equals(stock.getReceiverType()) && !facilityIds.contains(receiverId)) populateErrorForStock(stock, receiverId, errorDetailsMap); } else { populateErrorForStock(stock, senderId + " and " + receiverId, errorDetailsMap); diff --git a/health-services/stock/src/main/java/org/egov/stock/validator/stock/SReferenceIdValidator.java b/health-services/stock/src/main/java/org/egov/stock/validator/stock/SReferenceIdValidator.java index 47a17106727..ceab1dfb887 100644 --- a/health-services/stock/src/main/java/org/egov/stock/validator/stock/SReferenceIdValidator.java +++ b/health-services/stock/src/main/java/org/egov/stock/validator/stock/SReferenceIdValidator.java @@ -2,6 +2,8 @@ import lombok.extern.slf4j.Slf4j; import org.egov.common.models.Error; +import org.egov.common.models.stock.ReferenceIdType; +import org.egov.common.models.stock.SenderReceiverType; import org.egov.common.models.stock.Stock; import org.egov.common.models.stock.StockBulkRequest; import org.egov.common.validator.Validator; @@ -37,11 +39,11 @@ public Map> validate(StockBulkRequest request) { List validEntities = request.getStock().stream() .filter(notHavingErrors()) - .filter(entity -> PROJECT.equals(entity.getReferenceIdType())) + .filter(entity -> ReferenceIdType.PROJECT.equals(entity.getReferenceIdType())) .collect(Collectors.toList()); - long countOfWareHouseInStock = request.getStock().stream().filter(stock -> - stock.getReceiverType().equalsIgnoreCase("WAREHOUSE") || stock.getSenderType().equalsIgnoreCase("WAREHOUSE") + long countOfWareHouseInStock = request.getStock().stream().filter(stock -> + SenderReceiverType.WAREHOUSE.equals(stock.getReceiverType()) || SenderReceiverType.WAREHOUSE.equals(stock.getSenderType()) ).count(); if(countOfWareHouseInStock == 0) return errorDetailsMap; diff --git a/health-services/stock/src/main/java/org/egov/stock/validator/stock/SSenderIdReceiverIdEqualsValidator.java b/health-services/stock/src/main/java/org/egov/stock/validator/stock/SSenderIdReceiverIdEqualsValidator.java new file mode 100644 index 00000000000..4285d90d26d --- /dev/null +++ b/health-services/stock/src/main/java/org/egov/stock/validator/stock/SSenderIdReceiverIdEqualsValidator.java @@ -0,0 +1,75 @@ +package org.egov.stock.validator.stock; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import lombok.extern.slf4j.Slf4j; +import org.egov.common.models.Error; +import org.egov.common.models.stock.Stock; +import org.egov.common.models.stock.StockBulkRequest; +import org.egov.common.validator.Validator; +import org.egov.tracer.model.CustomException; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import static org.egov.common.utils.CommonUtils.populateErrorDetails; + +/** + * Validator class to check if senderId and receiverId are equal in a list of Stock entities. + */ +@Component +@Order(value = 6) +@Slf4j +public class SSenderIdReceiverIdEqualsValidator implements Validator { + + /** + * Validates the list of Stock entities to ensure that senderId and receiverId are not equal. + * + * @param stockBulkRequest The bulk request containing a list of Stock entities. + * @return A map containing Stock entities with corresponding error details for entities with equal senderId and receiverId. + */ + @Override + public Map> validate(StockBulkRequest stockBulkRequest) { + Map> errorDetailsMap = new HashMap<>(); + List entities = stockBulkRequest.getStock(); + List invalidEntities = new ArrayList<>(); + log.info("validating whether sender id and receiver id are same"); + + // Iterate through each Stock entity in the list + entities.forEach(stock -> { + // Check if senderId and receiverId are equal using helper method + if (areSenderAndReceiverEqual(stock)) { + // If equal, add the entity to the list of invalid entities + invalidEntities.add(stock); + + // Create an error object for the entity + Error error = Error.builder() + .errorMessage("Sender Id and Receiver Id cannot be the same") + .errorCode("SENDER_RECEIVER_CANNOT_BE_EQUAL") + .type(Error.ErrorType.NON_RECOVERABLE) + .exception(new CustomException("SENDER_RECEIVER_CANNOT_BE_EQUAL", "Sender Id and Receiver Id cannot be the same")) + .build(); + + // Populate error details for the entity + populateErrorDetails(stock, error, errorDetailsMap); + } + }); + + return errorDetailsMap; + } + + /** + * Helper method to check if senderId and receiverId are equal. + * + * @param stock The Stock entity to check. + * @return True if senderId and receiverId are equal, false otherwise. + */ + private boolean areSenderAndReceiverEqual(Stock stock) { + return stock.getSenderType() == stock.getReceiverType() + && stock.getReceiverId() != null + && stock.getSenderId() != null + && stock.getReceiverId().equals(stock.getSenderId()); + } +} diff --git a/health-services/stock/src/test/java/org/egov/stock/TestConfiguration.java b/health-services/stock/src/test/java/org/egov/stock/TestConfiguration.java index 0d8d9758688..d507efd67c1 100644 --- a/health-services/stock/src/test/java/org/egov/stock/TestConfiguration.java +++ b/health-services/stock/src/test/java/org/egov/stock/TestConfiguration.java @@ -1,5 +1,11 @@ package org.egov.stock; +import java.util.TimeZone; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; +import org.egov.common.models.core.validator.CustomIntegerDeserializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.kafka.core.KafkaTemplate; @@ -13,4 +19,14 @@ public class TestConfiguration { public KafkaTemplate kafkaTemplate() { return mock(KafkaTemplate.class); } + + @Bean + public ObjectMapper objectMapper(){ + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + SimpleModule module = new SimpleModule(); + module.addDeserializer(Integer.class, new CustomIntegerDeserializer()); + objectMapper.registerModule(module); + return objectMapper.setTimeZone(TimeZone.getTimeZone("UTC")); + } } \ No newline at end of file diff --git a/health-services/stock/src/test/java/org/egov/stock/helper/StockTestBuilder.java b/health-services/stock/src/test/java/org/egov/stock/helper/StockTestBuilder.java index 90960baf5a3..fd83333e42e 100644 --- a/health-services/stock/src/test/java/org/egov/stock/helper/StockTestBuilder.java +++ b/health-services/stock/src/test/java/org/egov/stock/helper/StockTestBuilder.java @@ -1,6 +1,8 @@ package org.egov.stock.helper; import org.egov.common.helper.AuditDetailsTestBuilder; +import org.egov.common.models.stock.ReferenceIdType; +import org.egov.common.models.stock.SenderReceiverType; import org.egov.common.models.stock.Stock; import org.egov.common.models.stock.TransactionReason; import org.egov.common.models.stock.TransactionType; @@ -26,13 +28,16 @@ public StockTestBuilder withStock() { this.builder .senderId("sender-id") .receiverId("receiver-id") - .productVariantId("pv-id").quantity(0) + .productVariantId("pv-id") + .quantity(1) .referenceId("reference-id") - .referenceIdType("PROJECT").rowVersion(1).tenantId("default") + .referenceIdType(ReferenceIdType.PROJECT) + .rowVersion(1) + .tenantId("default") .transactionType(TransactionType.DISPATCHED) .transactionReason(TransactionReason.RECEIVED) - .senderType("WAREHOUSE") - .receiverType("STAFF") + .senderType(SenderReceiverType.WAREHOUSE) + .receiverType(SenderReceiverType.STAFF) .hasErrors(false) .isDeleted(Boolean.FALSE) .auditDetails(AuditDetailsTestBuilder.builder().withAuditDetails().build());