-
Notifications
You must be signed in to change notification settings - Fork 21
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-1254-Adding household type for communal living facility #1124
base: dev
Are you sure you want to change the base?
Changes from all commits
d2be605
c6333fc
ef5e631
a3d1a5c
6061423
56b3e53
efe2657
8c5753f
6e9f55c
563145c
8064857
ef08a5c
36fdc15
113e897
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.egov.household.validators.household; | ||
|
||
import org.egov.common.models.Error; | ||
import org.egov.common.models.household.HouseHoldType; | ||
import org.egov.common.models.household.Household; | ||
import org.egov.common.models.household.HouseholdBulkRequest; | ||
import org.egov.common.validator.Validator; | ||
import org.egov.household.config.HouseholdConfiguration; | ||
import org.egov.tracer.model.CustomException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.egov.common.utils.CommonUtils.populateErrorDetails; | ||
|
||
public class HCommunityTypeValidator implements Validator<HouseholdBulkRequest, Household> { | ||
private final HouseholdConfiguration configuration; | ||
|
||
@Autowired | ||
public HCommunityTypeValidator(HouseholdConfiguration configuration) { | ||
this.configuration = configuration; | ||
} | ||
@Override | ||
public Map<Household, List<Error>> validate(HouseholdBulkRequest request) { | ||
HashMap<Household, List<Error>> errorDetailsMap = new HashMap<>(); | ||
if (configuration.isHouseholdTypeSameValidation()) { | ||
// validate if request contains households of different householdTypes | ||
List<Household> communityHouseholds = request.getHouseholds() | ||
.stream() | ||
.filter(household -> household.getHouseholdType() != null && | ||
household.getHouseholdType().equals(HouseHoldType.COMMUNITY)) | ||
.toList(); | ||
|
||
if (!CollectionUtils.isEmpty(communityHouseholds) && | ||
request.getHouseholds().size() != communityHouseholds.size()) { | ||
communityHouseholds.forEach(household -> { | ||
Error error = Error.builder() | ||
.errorMessage("Community and Family household cannot be in same request") | ||
.errorCode("COMMUNITY_AND_FAMILY_HOUSEHOLD_IN_SAME_REQUEST") | ||
.type(Error.ErrorType.NON_RECOVERABLE) | ||
.exception(new CustomException("COMMUNITY_AND_FAMILY_HOUSEHOLD_IN_SAME_REQUEST", "Community and Family household cannot be in same request")) | ||
.build(); | ||
// Populate error details for the household | ||
populateErrorDetails(household, error, errorDetailsMap); | ||
}); | ||
} | ||
} | ||
return errorDetailsMap; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.egov.household.validators.household; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.egov.common.models.Error; | ||
import org.egov.common.models.household.HouseHoldType; | ||
import org.egov.common.models.household.Household; | ||
import org.egov.common.models.household.HouseholdBulkRequest; | ||
import org.egov.common.validator.Validator; | ||
import org.egov.household.config.HouseholdConfiguration; | ||
import org.egov.tracer.model.CustomException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.egov.common.utils.CommonUtils.populateErrorDetails; | ||
|
||
@Component | ||
@Order(value = 1) | ||
@Slf4j | ||
public class HCommunityValidator implements Validator<HouseholdBulkRequest, Household> { | ||
|
||
private final HouseholdConfiguration configuration; | ||
|
||
@Autowired | ||
public HCommunityValidator(HouseholdConfiguration configuration) { | ||
this.configuration = configuration; | ||
} | ||
|
||
@Override | ||
public Map<Household, List<Error>> validate(HouseholdBulkRequest request) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validation should be added to check no normal household is there for community household create request and vice versa. and this should be enabled or disabled by config There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
HashMap<Household, List<Error>> errorDetailsMap = new HashMap<>(); | ||
List<Household> communityHouseholds = request.getHouseholds() | ||
.stream() | ||
.filter(household -> household.getHouseholdType() != null && | ||
household.getHouseholdType().equals(HouseHoldType.COMMUNITY)) | ||
.toList(); | ||
|
||
if (!CollectionUtils.isEmpty(communityHouseholds) && | ||
request.getRequestInfo().getUserInfo().getRoles() | ||
.stream() | ||
.noneMatch(role -> role.getCode().equals(configuration.getCommunityHouseholdCreatorRoleCode()))) { | ||
communityHouseholds.forEach(household -> { | ||
Error error = Error.builder() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error goes to error queue, make sure error is thrown in single api |
||
.errorMessage("User doesn't have permission to create/update community household") | ||
.errorCode("COMMUNITY_USER_ACCESS_DENIED") | ||
.type(Error.ErrorType.NON_RECOVERABLE) | ||
.exception(new CustomException("COMMUNITY_USER_ACCESS_DENIED", "User doesn't have permission to create/update community household")) | ||
.build(); | ||
// Populate error details for the household | ||
populateErrorDetails(household, error, errorDetailsMap); | ||
}); | ||
} | ||
return errorDetailsMap; | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package org.egov.household.validators.household; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.egov.common.models.Error; | ||
import org.egov.common.models.household.Household; | ||
import org.egov.common.models.household.HouseholdBulkRequest; | ||
import org.egov.common.models.household.HouseholdSearch; | ||
import org.egov.common.validator.Validator; | ||
import org.egov.household.repository.HouseholdRepository; | ||
import org.egov.tracer.model.CustomException; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.egov.common.utils.CommonUtils.*; | ||
import static org.egov.common.utils.CommonUtils.notHavingErrors; | ||
|
||
@Slf4j | ||
@Component | ||
@Order(value = 3) | ||
public class HHouseholdTypeChangeValidator implements Validator<HouseholdBulkRequest, Household> { | ||
|
||
private final HouseholdRepository householdRepository; | ||
|
||
public HHouseholdTypeChangeValidator(HouseholdRepository householdRepository) { | ||
this.householdRepository = householdRepository; | ||
} | ||
|
||
@Override | ||
public Map<Household, List<Error>> validate(HouseholdBulkRequest request) { | ||
// Map to hold household entities and their error details | ||
Map<Household, List<Error>> errorDetailsMap = new HashMap<>(); | ||
// Get the list of household entities from the request | ||
List<Household> entities = request.getHouseholds(); | ||
// Map to store entities by their IDs | ||
Map<String, Household> eMap = entities.stream().filter(notHavingErrors()).collect(Collectors.toMap(Household::getId, household -> household)); | ||
// Lists to store IDs and client reference IDs | ||
List<String> idList = new ArrayList<>(); | ||
List<String> clientReferenceIdList = new ArrayList<>(); | ||
// Extract IDs and client reference IDs from household entities | ||
entities.forEach(household -> { | ||
idList.add(household.getId()); | ||
clientReferenceIdList.add(household.getClientReferenceId()); | ||
}); | ||
// Check if the entity map is not empty | ||
if (!eMap.isEmpty()) { | ||
// Create a search object for querying existing entities | ||
HouseholdSearch householdSearch = HouseholdSearch.builder() | ||
.clientReferenceId(clientReferenceIdList) | ||
.id(idList) | ||
.build(); | ||
|
||
List<Household> existingEntities; | ||
try { | ||
// Query the repository to find existing entities | ||
existingEntities = householdRepository.find(householdSearch, entities.size(), 0, | ||
entities.get(0).getTenantId(), null, false).getResponse(); | ||
} catch (Exception e) { | ||
// Handle query builder exception | ||
log.error("Search failed for Household with error: {}", e.getMessage(), e); | ||
throw new CustomException("HOUSEHOLD_SEARCH_FAILED", "Search Failed for Household, " + e.getMessage()); | ||
} | ||
// Check for non-existent entities | ||
List<Household> entitiesWithHouseholdTypeChange = changeInHouseholdType(eMap, | ||
existingEntities); | ||
// Populate error details for non-existent entities | ||
entitiesWithHouseholdTypeChange.forEach(entity -> { | ||
Error error = Error.builder().errorMessage("Household Type change").errorCode("HOUSEHOLD_TYPE_CHANGE") | ||
.type(Error.ErrorType.NON_RECOVERABLE) | ||
.exception(new CustomException("HOUSEHOLD_TYPE_CHANGE", "Household Type change")).build(); | ||
populateErrorDetails(entity, error, errorDetailsMap); | ||
}); | ||
} | ||
|
||
return errorDetailsMap; | ||
} | ||
|
||
|
||
private List<Household> changeInHouseholdType(Map<String, Household> eMap, | ||
List<Household> existingEntities) { | ||
List<Household> entitiesWithHouseholdTypeChange = new ArrayList<>(); | ||
|
||
for (Household existingEntity : existingEntities) { | ||
if (eMap.containsKey(existingEntity.getId())) { | ||
if (!existingEntity.getHouseholdType().equals(eMap.get(existingEntity.getId()).getHouseholdType())) { | ||
entitiesWithHouseholdTypeChange.add(eMap.get(existingEntity.getId())); | ||
} | ||
} | ||
} | ||
return entitiesWithHouseholdTypeChange; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ALTER TABLE HOUSEHOLD ADD COLUMN IF NOT EXISTS householdType character varying(64); | ||
UPDATE HOUSEHOLD SET householdType = 'FAMILY' WHERE householdType IS NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please sit with Yashita and get her enum changes merged to the same version of the library
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
She has added the same changes in data model package for apk