Skip to content

Commit

Permalink
Hlm 3372 enhance inventory flow backend fixes (#623)
Browse files Browse the repository at this point in the history
* HLM-3372: added changes required to fix quantity, Sender Receiver enum

* HLM-3372: Sender and Receiver id validator

* HLM-3372: updated all reference for SenderType and Receiver Type enum

* HLM-3372: stock model updated, removed size annotations from referenceidtype enum field

* HLM-3372: Min validation added for integer type of quantity

* HLM-3372: test cases updated

* HLM-5004 Added max value and decimal condition for quantity in stock, added component and order annotation for SSenderIdReceiverIdEqualsValidator

* HLM-5004 Custom JsonDeserializer validator IntegerValidator added in health-services-models

* hlm-5004 added custom exception and a custom exception handler to handle the integer validator exception

* hlm-5004 optimized imports and added code comments

* hlm-5004 CustomIntegerSerializer added and unnecessary validators removed

* hlm-5004 Registered the CustomIntegerDeserializer with objectMapper for Integer class

* hlm-5004 Removed line of code that was removing all the invalid entities from the list in SSenderIdReceiverIdEqualsValidator

* hlm-5004 changes in test configurations and optimized imports

* hlm-5004 added row version validator for stock delete

* hlm-5004 dateOfEntry field was handled in StockRowMapper to return null if no value is present and description was added to stock contact for transactionReason

* updated pom.xml for health campaign models

* Revert "updated pom.xml for health campaign models"

This reverts commit 035c787.

---------

Co-authored-by: syed-egov <[email protected]>
  • Loading branch information
kanishq-egov and syed-egov authored Feb 23, 2024
1 parent dee30a1 commit fe8c32b
Show file tree
Hide file tree
Showing 17 changed files with 288 additions and 62 deletions.
1 change: 1 addition & 0 deletions docs/health-api-specs/contracts/stock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion health-services/libraries/health-services-models/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.egov.common</groupId>
<artifactId>health-services-models</artifactId>
<version>1.0.14-SNAPSHOT</version>
<version>1.0.15-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
Expand All @@ -28,6 +28,11 @@
<artifactId>digit-models</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.egov.services</groupId>
<artifactId>tracer</artifactId>
<version>2.1.4-SNAPSHOT</version>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> {

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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -51,19 +51,22 @@ 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 */
@JsonProperty("referenceId")
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;

Expand All @@ -77,19 +80,19 @@ 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
@Size(min=2, max=64)
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)
Expand Down
2 changes: 1 addition & 1 deletion health-services/stock/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<dependency>
<groupId>org.egov.common</groupId>
<artifactId>health-services-models</artifactId>
<version>1.0.9-SNAPSHOT</version>
<version>1.0.15-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"))
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,10 +98,10 @@ public <T> Map<String, List<String>> validateProjectFacilityMappings(List<T> 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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -57,6 +58,7 @@ public class StockService {

private final Predicate<Validator<StockBulkRequest, Stock>> isApplicableForCreate =
validator -> validator.getClass().equals(SProductVariantIdValidator.class)
|| validator.getClass().equals(SSenderIdReceiverIdEqualsValidator.class)
|| validator.getClass().equals(StocktransferPartiesValidator.class)
|| validator.getClass().equals(SReferenceIdValidator.class);

Expand All @@ -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<Validator<StockBulkRequest, Stock>> 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<Validator<StockBulkRequest, Stock>> validators, StockConfiguration configuration, StockEnrichmentService enrichmentService) {
this.stockRepository = stockRepository;
Expand Down
Loading

0 comments on commit fe8c32b

Please sign in to comment.