Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HCMPRE-878: Assess Stock Service for various issues #1037

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,31 @@ public class Constants {

public static String GET_REQUEST_INFO = "getRequestInfo";

public static String NO_PROJECT_FACILITY_MAPPING_EXISTS = "NO_PROJECT_FACILITY_MAPPING_EXISTS";
//Stock Reconciliation Error Code
public static String SR_CREATE_VALIDATION_ERROR = "STOCK_RECONCILIATION_CREATE_REQUEST_VALIDATION_ERROR";

public static String SR_PRODUCT_VARIANT_ID_VALIDATION_ERROR = "STOCK_RECONCILIATION_PRODUCTVARIANTID_VALIDATOR_PRODUCTVARIANTID_FETCH_FAILED";

public static String SR_UPDATE_VALIDATION_ERROR = "STOCK_RECONCILIATION_UPDATE_REQUEST_VALIDATION_ERROR";

public static String SR_VALIDATOR_STOCK_SEARCH_FAILED = "STOCK_RECONCILIATION_NONEXISTENTVALIDATOR_STOCK_SEARCH_FAILED";

public static String SR_DELETE_VALIDATION_ERROR = "STOCK_RECONCILIATION_DELETE_REQUEST_VALIDATION_ERROR";

public static String SR_REFERENCEID_VALIDATION_ERROR = "STOCK_RECONCILIATION_REFERENCEID_VALIDATOR_FACILITY_PROJECTFACILITY_MAPPING_FAILED";

//Stock Error Code
public static String S_CREATE_VALIDATION_ERROR = "STOCK_CREATE_REQUEST_VALIDATION_ERROR";

public static String S_PRODUCT_VARIANT_ID_VALIDATION_ERROR = "STOCK_PRODUCTVARIANTID_VALIDATOR_PRODUCTVARIANTID_FETCH_FAILED";

public static String S_SENDER_RECEIVER_ID_EQUALS_VALIDATION_ERROR = "STOCK_SENDER_RECEIVER_ID_EQUALS_VALIDATION_FAILED";

public static String S_UPDATE_VALIDATION_ERROR = "STOCK_UPDATE_REQUEST_VALIDATION_ERROR";

public static String S_VALIDATION_STOCK_SEARCH_FAILED = "STOCK_NONEXISTENT_VALIDATOR_STOCK_SEARCH_FAILED";

public static String S_DELETE_VALIDATION_ERROR = "STOCK_DELETE_REQUEST_VALIDATION_ERROR";

public static String S_REFERENCEID_VALIDATION_ERROR = "STOCK_REFERENCEID_VALIDATOR_FACILITY_PROJECTFACILITY_MAPPING_FAILED";
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.egov.stock.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@
import static org.egov.common.utils.CommonUtils.lastChangedSince;
import static org.egov.common.utils.CommonUtils.populateErrorDetails;
import static org.egov.common.utils.CommonUtils.validate;
import static org.egov.stock.Constants.GET_STOCK_RECONCILIATION;
import static org.egov.stock.Constants.SET_STOCK_RECONCILIATION;
import static org.egov.stock.Constants.VALIDATION_ERROR;
import static org.egov.stock.Constants.*;


@Service
Expand Down Expand Up @@ -97,7 +95,7 @@ public List<StockReconciliation> create(StockReconciliationBulkRequest request,
log.info("starting create method for stock reconciliation");

Tuple<List<StockReconciliation>, Map<StockReconciliation, ErrorDetails>> tuple = validate(validators,
isApplicableForCreate, request, SET_STOCK_RECONCILIATION, GET_STOCK_RECONCILIATION, VALIDATION_ERROR,
isApplicableForCreate, request, SET_STOCK_RECONCILIATION, GET_STOCK_RECONCILIATION, SR_CREATE_VALIDATION_ERROR,
isBulk);

Map<StockReconciliation, ErrorDetails> errorDetailsMap = tuple.getY();
Expand All @@ -113,7 +111,7 @@ public List<StockReconciliation> create(StockReconciliationBulkRequest request,
populateErrorDetails(request, errorDetailsMap, validEntities, exception, SET_STOCK_RECONCILIATION);
}

handleErrors(errorDetailsMap, isBulk, VALIDATION_ERROR);
handleErrors(errorDetailsMap, isBulk, SR_CREATE_VALIDATION_ERROR);
log.info("completed create method for stock reconciliation");
return validEntities;
}
Expand All @@ -129,7 +127,7 @@ public StockReconciliation update(StockReconciliationRequest request) {
public List<StockReconciliation> update(StockReconciliationBulkRequest request, boolean isBulk) {
log.info("starting update method for stock reconciliation");
Tuple<List<StockReconciliation>, Map<StockReconciliation, ErrorDetails>> tuple = validate(validators,
isApplicableForUpdate, request, SET_STOCK_RECONCILIATION, GET_STOCK_RECONCILIATION, VALIDATION_ERROR,
isApplicableForUpdate, request, SET_STOCK_RECONCILIATION, GET_STOCK_RECONCILIATION, SR_UPDATE_VALIDATION_ERROR,
isBulk);

Map<StockReconciliation, ErrorDetails> errorDetailsMap = tuple.getY();
Expand All @@ -145,7 +143,7 @@ public List<StockReconciliation> update(StockReconciliationBulkRequest request,
populateErrorDetails(request, errorDetailsMap, validEntities, exception, SET_STOCK_RECONCILIATION);
}

handleErrors(errorDetailsMap, isBulk, VALIDATION_ERROR);
handleErrors(errorDetailsMap, isBulk, SR_UPDATE_VALIDATION_ERROR);

log.info("completed update method for stock reconciliation");
return validEntities;
Expand All @@ -162,7 +160,7 @@ public StockReconciliation delete(StockReconciliationRequest request) {
public List<StockReconciliation> delete(StockReconciliationBulkRequest request, boolean isBulk) {
log.info("starting delete method for stock reconciliation");
Tuple<List<StockReconciliation>, Map<StockReconciliation, ErrorDetails>> tuple = validate(validators,
isApplicableForDelete, request, SET_STOCK_RECONCILIATION, GET_STOCK_RECONCILIATION, VALIDATION_ERROR,
isApplicableForDelete, request, SET_STOCK_RECONCILIATION, GET_STOCK_RECONCILIATION, SR_DELETE_VALIDATION_ERROR,
isBulk);

Map<StockReconciliation, ErrorDetails> errorDetailsMap = tuple.getY();
Expand All @@ -178,7 +176,7 @@ public List<StockReconciliation> delete(StockReconciliationBulkRequest request,
populateErrorDetails(request, errorDetailsMap, validEntities, exception, SET_STOCK_RECONCILIATION);
}

handleErrors(errorDetailsMap, isBulk, VALIDATION_ERROR);
handleErrors(errorDetailsMap, isBulk, SR_DELETE_VALIDATION_ERROR);
log.info("completed delete method for stock reconciliation");
return validEntities;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@
import static org.egov.common.utils.CommonUtils.lastChangedSince;
import static org.egov.common.utils.CommonUtils.populateErrorDetails;
import static org.egov.common.utils.CommonUtils.validate;
import static org.egov.stock.Constants.GET_STOCK;
import static org.egov.stock.Constants.SET_STOCK;
import static org.egov.stock.Constants.VALIDATION_ERROR;
import static org.egov.stock.Constants.*;


@Service
Expand Down Expand Up @@ -98,7 +96,7 @@ public Stock create(StockRequest request) {
public List<Stock> create(StockBulkRequest request, boolean isBulk) {
log.info("starting create method for stock");
Tuple<List<Stock>, Map<Stock, ErrorDetails>> tuple = validate(validators,
isApplicableForCreate, request, SET_STOCK, GET_STOCK, VALIDATION_ERROR,
isApplicableForCreate, request, SET_STOCK, GET_STOCK, S_CREATE_VALIDATION_ERROR,
isBulk);
Map<Stock, ErrorDetails> errorDetailsMap = tuple.getY();
List<Stock> validEntities = tuple.getX();
Expand All @@ -113,7 +111,7 @@ public List<Stock> create(StockBulkRequest request, boolean isBulk) {
populateErrorDetails(request, errorDetailsMap, validEntities, exception, SET_STOCK);
}

handleErrors(errorDetailsMap, isBulk, VALIDATION_ERROR);
handleErrors(errorDetailsMap, isBulk, S_CREATE_VALIDATION_ERROR);
log.info("completed create method for stock");
return validEntities;
}
Expand All @@ -128,7 +126,7 @@ public Stock update(StockRequest request) {
public List<Stock> update(StockBulkRequest request, boolean isBulk) {
log.info("starting update method for stock");
Tuple<List<Stock>, Map<Stock, ErrorDetails>> tuple = validate(validators,
isApplicableForUpdate, request, SET_STOCK, GET_STOCK, VALIDATION_ERROR,
isApplicableForUpdate, request, SET_STOCK, GET_STOCK, S_UPDATE_VALIDATION_ERROR,
isBulk);
Map<Stock, ErrorDetails> errorDetailsMap = tuple.getY();
List<Stock> validEntities = tuple.getX();
Expand All @@ -143,7 +141,7 @@ public List<Stock> update(StockBulkRequest request, boolean isBulk) {
populateErrorDetails(request, errorDetailsMap, validEntities, exception, SET_STOCK);
}

handleErrors(errorDetailsMap, isBulk, VALIDATION_ERROR);
handleErrors(errorDetailsMap, isBulk, S_UPDATE_VALIDATION_ERROR);
log.info("completed update method for stock");
return validEntities;
}
Expand All @@ -158,7 +156,7 @@ public Stock delete(StockRequest request) {
public List<Stock> delete(StockBulkRequest request, boolean isBulk) {
log.info("starting delete method for stock");
Tuple<List<Stock>, Map<Stock, ErrorDetails>> tuple = validate(validators,
isApplicableForDelete, request, SET_STOCK, GET_STOCK, VALIDATION_ERROR,
isApplicableForDelete, request, SET_STOCK, GET_STOCK, S_DELETE_VALIDATION_ERROR,
isBulk);
Map<Stock, ErrorDetails> errorDetailsMap = tuple.getY();
List<Stock> validEntities = tuple.getX();
Expand All @@ -173,7 +171,7 @@ public List<Stock> delete(StockBulkRequest request, boolean isBulk) {
populateErrorDetails(request, errorDetailsMap, validEntities, exception, SET_STOCK);
}

handleErrors(errorDetailsMap, isBulk, VALIDATION_ERROR);
handleErrors(errorDetailsMap, isBulk, S_DELETE_VALIDATION_ERROR);
log.info("completed delete method for stock");
return validEntities;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
import static org.egov.common.utils.CommonUtils.notHavingErrors;
import static org.egov.common.utils.CommonUtils.populateErrorDetails;
import static org.egov.common.utils.ValidatorUtils.getErrorForNonExistentRelatedEntity;
import static org.egov.stock.Constants.GET_REQUEST_INFO;
import static org.egov.stock.Constants.NO_PROJECT_FACILITY_MAPPING_EXISTS;
import static org.egov.stock.Constants.*;

public class ValidatorUtil {

Expand Down Expand Up @@ -68,21 +67,21 @@ public static <R, T> Map<T, List<Error>> validateFacilityIds(R request, Map<T, L
*
* @param requestInfo
* @param errorDetailsMap
* @param validStockEntities
* @param stockEntities
* @param facilityService
* @param userService
* @return
*/
public static <R, T> Map<T, List<Error>> validateStockTransferParties(RequestInfo requestInfo,
Map<T, List<Error>> errorDetailsMap, List<Stock> validStockEntities, FacilityService facilityService,
Map<T, List<Error>> errorDetailsMap, List<Stock> stockEntities, FacilityService facilityService,
UserService userService) {

if (!validStockEntities.isEmpty()) {
if (!stockEntities.isEmpty()) {

Tuple<List<String>, List<String>> tupleOfInvalidStaffIdsAndFacilityIds = validateAndEnrichInvalidPartyIds(
requestInfo, errorDetailsMap, validStockEntities, facilityService, userService);
requestInfo, errorDetailsMap, stockEntities, facilityService, userService);

enrichErrorMapFromInvalidPartyIds(errorDetailsMap, validStockEntities,
enrichErrorMapFromInvalidPartyIds(errorDetailsMap, stockEntities,
tupleOfInvalidStaffIdsAndFacilityIds.getX(), tupleOfInvalidStaffIdsAndFacilityIds.getY());

}
Expand All @@ -94,32 +93,32 @@ public static <R, T> Map<T, List<Error>> validateStockTransferParties(RequestInf
*
* @param requestInfo
* @param errorDetailsMap
* @param validStockEntities
* @param stockEntities
* @param facilityService
* @param userService
* @return A tuple containing lists of invalid facility ids and invalid staff ids
*/
@SuppressWarnings("unchecked")
private static <T> Tuple<List<String>, List<String>> validateAndEnrichInvalidPartyIds(RequestInfo requestInfo,
Map<T, List<Error>> errorDetailsMap, List<Stock> validStockEntities, FacilityService facilityService,
Map<T, List<Error>> errorDetailsMap, List<Stock> stockEntities, FacilityService facilityService,
UserService userService) {

List<String> facilityIds = new ArrayList<>();
List<String> staffIds = new ArrayList<>();

enrichFaciltyAndStaffIdsFromStock(validStockEntities, facilityIds, staffIds);
enrichFaciltyAndStaffIdsFromStock(stockEntities, facilityIds, staffIds);

// copy all of party identifiers into invalid list
List<String> invalidStaffIds = new ArrayList<>(staffIds);
List<String> invalidFacilityIds = new ArrayList<>(facilityIds);

String tenantId = getTenantId(validStockEntities);
String tenantId = getTenantId(stockEntities);

// validate and remove valid identifiers from invalidStaffIds
validateAndEnrichStaffIds(requestInfo, userService, staffIds, invalidStaffIds);

// validate and remove valid identifiers from invalidfacilityIds
List<String> validFacilityIds = facilityService.validateFacilityIds(facilityIds, (List<T>) validStockEntities,
List<String> validFacilityIds = facilityService.validateFacilityIds(facilityIds, (List<T>) stockEntities,
tenantId, errorDetailsMap, requestInfo);
invalidFacilityIds.removeAll(validFacilityIds);

Expand Down Expand Up @@ -314,9 +313,9 @@ private static <T> void populateErrorForStock(Stock stock, String facilityId, Ma
String errorMessage = String.format("No mapping exists for project id: %s & facility id: %s",
stock.getReferenceId(), facilityId);

Error error = Error.builder().errorMessage(errorMessage).errorCode(NO_PROJECT_FACILITY_MAPPING_EXISTS)
Error error = Error.builder().errorMessage(errorMessage).errorCode(S_REFERENCEID_VALIDATION_ERROR)
.type(Error.ErrorType.NON_RECOVERABLE)
.exception(new CustomException(NO_PROJECT_FACILITY_MAPPING_EXISTS, errorMessage)).build();
.exception(new CustomException(S_REFERENCEID_VALIDATION_ERROR, errorMessage)).build();
populateErrorDetails((T) stock, error, errorDetailsMap);
}

Expand All @@ -342,9 +341,9 @@ private static <T> void populateErrorForStockReconciliation(StockReconciliation
String errorMessage = String.format("No mapping exists for project id: %s & facility id: %s",
stockReconciliation.getReferenceId(), stockReconciliation.getFacilityId());

Error error = Error.builder().errorMessage(errorMessage).errorCode(NO_PROJECT_FACILITY_MAPPING_EXISTS)
Error error = Error.builder().errorMessage(errorMessage).errorCode(SR_REFERENCEID_VALIDATION_ERROR)
.type(Error.ErrorType.NON_RECOVERABLE)
.exception(new CustomException(NO_PROJECT_FACILITY_MAPPING_EXISTS, errorMessage)).build();
.exception(new CustomException(SR_REFERENCEID_VALIDATION_ERROR, errorMessage)).build();
populateErrorDetails((T) stockReconciliation, error, errorDetailsMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Map<Stock, List<Error>> validate(StockBulkRequest request) {

// Extract client reference IDs from Stock entities that do not already have errors
List<String> clientReferenceIdList = entities.stream()
.filter(notHavingErrors()) // Exclude entities with existing errors
.filter(entity -> StringUtils.hasText(entity.getClientReferenceId())) // Ensure client reference ID is not empty
.map(Stock::getClientReferenceId) // Map entities to their client reference IDs
.collect(Collectors.toList()); // Collect IDs into a list

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.egov.common.utils.CommonUtils.notHavingErrors;
import static org.egov.common.utils.CommonUtils.populateErrorDetails;
import static org.egov.common.utils.ValidatorUtils.getErrorForIsDelete;

Expand All @@ -25,10 +23,10 @@ public class SIsDeletedValidator implements Validator<StockBulkRequest, Stock> {
public Map<Stock, List<Error>> validate(StockBulkRequest request) {
HashMap<Stock, List<Error>> errorDetailsMap = new HashMap<>();
log.info("validating is deleted stock");
List<Stock> validEntities = request.getStock().stream().filter(notHavingErrors()).collect(Collectors.toList());
validEntities.stream().filter(Stock::getIsDeleted).forEach(individual -> {
List<Stock> entities = request.getStock();
entities.stream().filter(Stock::getIsDeleted).forEach(individual -> {
Error error = getErrorForIsDelete();
log.info("validation failed for stock is deleted: {} with error: {}", validEntities, error);
log.info("validation failed for stock is deleted: {} with error: {}", entities, error);
populateErrorDetails(individual, error, errorDetailsMap);
});
log.info("stock is deleted validation completed successfully, total errors: "+errorDetailsMap.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,13 @@
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import static org.egov.common.utils.CommonUtils.checkNonExistentEntities;
import static org.egov.common.utils.CommonUtils.getIdToObjMap;
import static org.egov.common.utils.CommonUtils.getMethod;
import static org.egov.common.utils.CommonUtils.getObjClass;
import static org.egov.common.utils.CommonUtils.notHavingErrors;
import static org.egov.common.utils.CommonUtils.populateErrorDetails;
import static org.egov.common.utils.CommonUtils.*;
import static org.egov.common.utils.ValidatorUtils.getErrorForNonExistentEntity;
import static org.egov.stock.Constants.GET_ID;
import static org.egov.stock.Constants.*;

@Component
@Slf4j
@Order(2)
@Order(3)
public class SNonExistentValidator implements Validator<StockBulkRequest, Stock> {

private final StockRepository stockRepository;
Expand All @@ -42,6 +37,7 @@ public SNonExistentValidator(StockRepository stockRepository) {
public Map<Stock, List<Error>> validate(StockBulkRequest request) {
Map<Stock, List<Error>> errorDetailsMap = new HashMap<>();
List<Stock> entities = request.getStock();

log.info("validating non existent stock");
Class<?> objClass = getObjClass(entities);
Method idMethod = getMethod(GET_ID, objClass);
Expand Down Expand Up @@ -69,7 +65,7 @@ public Map<Stock, List<Error>> validate(StockBulkRequest request) {
} catch (Exception e) {
// Handle query builder exception
log.error("Search failed for Stock with error: {}", e.getMessage(), e);
throw new CustomException("STOCK_SEARCH_FAILED", "Search Failed for Stock, " + e.getMessage());
throw new CustomException(S_VALIDATION_STOCK_SEARCH_FAILED, "Search Failed for Stock, " + e.getMessage());
}
List<Stock> nonExistentEntities = checkNonExistentEntities(eMap,
existingEntities, idMethod);
Expand All @@ -82,4 +78,4 @@ public Map<Stock, List<Error>> validate(StockBulkRequest request) {
log.info("stock non existent validation completed successfully, total errors: "+errorDetailsMap.size());
return errorDetailsMap;
}
}
}
Loading
Loading