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

HLM-4062: count api changes #534

Merged
merged 24 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d080035
HLM-3376: review comments commit
kanishq-egov Oct 12, 2023
c8c4a5f
Merge branch 'stock-transfer-parties-HLM-3372' into hlm-3376-reviewco…
kanishq-egov Oct 14, 2023
b6f2a51
HLM-3069: side effect code comments, code refactor
kanishq-egov Oct 15, 2023
01b8d76
Merge branch 'stock-transfer-parties-HLM-3372' into hlm-3376-reviewco…
kanishq-egov Oct 15, 2023
2c7e49a
HLM-3376: code review comments and code refactoring
kanishq-egov Oct 16, 2023
a4c9abf
Merge remote-tracking branch 'origin/dev' into hlm-3376-reviewcomments
kanishq-egov Oct 18, 2023
e185c4b
updated the common-models version to 1.0.10, and updated in dependent…
kanishq-egov Oct 18, 2023
b55cffe
HLM-3376 : Added additional field in side effect, referral.
kanishq-egov Oct 19, 2023
b53877a
HLM-3376: missing column fix
kanishq-egov Oct 19, 2023
afa0ee5
HLM-3372: constants type changed
kanishq-egov Oct 19, 2023
088ae72
HLM-3376: removed not used validators
kanishq-egov Oct 19, 2023
0a20eb6
code refactor and code comments
kanishq-egov Oct 19, 2023
2eda751
hlm-3376: added test cases
kanishq-egov Oct 20, 2023
6325466
hlm-3376: referralmanagement context in test cases
kanishq-egov Oct 20, 2023
d96a892
hlm-3376: changed parameters for find by id
kanishq-egov Oct 20, 2023
76c71d0
HLM-3372: typo fix
kanishq-egov Oct 20, 2023
e56cb45
hlm-3376: persister changes, removed invalid parameters
kanishq-egov Oct 20, 2023
d1e4418
hlm-3372: added changes as per code review, removed unused properties
kanishq-egov Oct 20, 2023
297a028
hlm-3376: recipient validator for faciliy not working fix
kanishq-egov Oct 23, 2023
efc7cae
HLM-3376: throwing exception on invalid recipient type
kanishq-egov Oct 23, 2023
251dcd9
HLM-4062: added count api changes for household
kanishq-egov Oct 23, 2023
f0295a8
HLM-4062: Updated findbyid references
kanishq-egov Oct 23, 2023
1dc788f
Merge branch 'hlm-3376-reviewcomments' into hlm-4062-count-api
kanishq-egov Oct 23, 2023
c3c2ee3
HLM-4062: Updated pom.xml of household
kanishq-egov Oct 23, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions docs/health-api-specs/contracts/referral-management.yml
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,8 @@ definitions:
type: array
items:
type: string
additionalFields:
$ref: '#/definitions/additionalFields'
isDeleted:
$ref: '#/definitions/isDeleted'
rowVersion:
Expand Down Expand Up @@ -663,17 +665,17 @@ definitions:
minLength: 2
maxLength: 64
description: Project Beneficiary Client Reference Id
referringById:
referrerId:
type: string
minLength: 2
maxLength: 64
description: Worker Id that is referring the Beneficiary
referredToId:
recipientId:
type: string
minLength: 2
maxLength: 64
description: Individual or Facility Id whom the Beneficiary is referred to.
referredToType:
recipientType:
type: string
description: Individual or Facility
reasons:
Expand All @@ -682,6 +684,8 @@ definitions:
type: string
sideEffect:
$ref: '#/definition/SideEffect'
additionalFields:
$ref: '#/definitions/additionalFields'
isDeleted:
$ref: '#/definitions/isDeleted'
rowVersion:
Expand Down
2 changes: 1 addition & 1 deletion health-services/household/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.10-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Map<HouseholdMember, List<Error>> validate(HouseholdMemberBulkRequest hou
List<String> houseHoldIds = getIdList(householdMembers, idMethod);

log.info("finding valid household ids from household service");
List<Household> validHouseHoldIds = householdService.findById(houseHoldIds, columnName, false);
List<Household> validHouseHoldIds = householdService.findById(houseHoldIds, columnName, false).getY();

log.info("getting unique household ids from valid household ids");
Set<String> uniqueHoldIds = getSet(validHouseHoldIds, columnName == "id" ? "getId": "getClientReferenceId");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.egov.common.data.query.builder.SelectQueryBuilder;
import org.egov.common.data.query.exception.QueryBuilderException;
import org.egov.common.data.repository.GenericRepository;
import org.egov.common.ds.Tuple;
import org.egov.common.models.household.Household;
import org.egov.common.producer.Producer;
import org.egov.household.repository.rowmapper.HouseholdRowMapper;
Expand Down Expand Up @@ -40,7 +41,7 @@ protected HouseholdRepository(Producer producer,
super(producer, namedParameterJdbcTemplate, redisTemplate, selectQueryBuilder, householdRowMapper, Optional.of("household"));
}

public List<Household> findById(List<String> ids, String columnName, Boolean includeDeleted) {
public Tuple<Long, List<Household>> findById(List<String> ids, String columnName, Boolean includeDeleted) {
List<Household> objFound = findInCache(ids).stream()
.filter(entity -> entity.getIsDeleted().equals(includeDeleted))
.collect(Collectors.toList());
Expand All @@ -50,7 +51,7 @@ public List<Household> findById(List<String> ids, String columnName, Boolean inc
.map(obj -> (String) ReflectionUtils.invokeMethod(idMethod, obj))
.collect(Collectors.toList()));
if (ids.isEmpty()) {
return objFound;
return new Tuple<>(Long.valueOf(objFound.size()), objFound);
}
}

Expand All @@ -61,12 +62,14 @@ public List<Household> findById(List<String> ids, String columnName, Boolean inc
Map<String, Object> paramMap = new HashMap();
paramMap.put("ids", ids);

Long totalCount = constructTotalCountCTEAndReturnResult(query, paramMap);

objFound.addAll(this.namedParameterJdbcTemplate.query(query, paramMap, this.rowMapper));
putInCache(objFound);
return objFound;
return new Tuple<>(totalCount, objFound);
}

public List<Household> find(HouseholdSearch searchObject, Integer limit, Integer offset, String tenantId, Long lastChangedSince, Boolean includeDeleted) throws QueryBuilderException {
public Tuple<Long, List<Household>> find(HouseholdSearch searchObject, Integer limit, Integer offset, String tenantId, Long lastChangedSince, Boolean includeDeleted) throws QueryBuilderException {
String query = "SELECT *, a.id as aid,a.tenantid as atenantid, a.clientreferenceid as aclientreferenceid FROM household h LEFT JOIN address a ON h.addressid = a.id";
Map<String, Object> paramsMap = new HashMap<>();
List<String> whereFields = GenericQueryBuilder.getFieldsWithCondition(searchObject, QueryFieldChecker.isNotNull, paramsMap);
Expand All @@ -82,13 +85,16 @@ public List<Household> find(HouseholdSearch searchObject, Integer limit, Integer
if (lastChangedSince != null) {
query = query + "and lastModifiedTime>=:lastModifiedTime ";
}
query = query + "ORDER BY h.id ASC LIMIT :limit OFFSET :offset";
paramsMap.put("tenantId", tenantId);
paramsMap.put("isDeleted", includeDeleted);
paramsMap.put("lastModifiedTime", lastChangedSince);

Long totalCount = constructTotalCountCTEAndReturnResult(query, paramsMap);

query = query + "ORDER BY h.id ASC LIMIT :limit OFFSET :offset";
paramsMap.put("limit", limit);
paramsMap.put("offset", offset);
return this.namedParameterJdbcTemplate.query(query, paramsMap, this.rowMapper);
return new Tuple<>(totalCount, this.namedParameterJdbcTemplate.query(query, paramsMap, this.rowMapper));
}

/**
Expand All @@ -102,9 +108,9 @@ public List<Household> find(HouseholdSearch searchObject, Integer limit, Integer
*
* Fetch all the household which falls under the radius provided using longitude and latitude provided.
*/
public List<Household> findByRadius(HouseholdSearch searchObject, Integer limit, Integer offset, String tenantId, Boolean includeDeleted) throws QueryBuilderException {
public Tuple<Long, List<Household>> findByRadius(HouseholdSearch searchObject, Integer limit, Integer offset, String tenantId, Boolean includeDeleted) throws QueryBuilderException {
String query = searchCriteriaWaypointQuery +
"SELECT * FROM (SELECT h.*, a.*, " + calculateDistanceFromTwoWaypointsFormulaQuery + " \n" +
"SELECT * FROM (SELECT h.*, a.*, a.id as aid,a.tenantid as atenantid, a.clientreferenceid as aclientreferenceid, " + calculateDistanceFromTwoWaypointsFormulaQuery + " \n" +
"FROM public.household h LEFT JOIN public.address a ON h.addressid = a.id AND h.tenantid = a.tenantid, cte_search_criteria_waypoint cte_scw ";
Map<String, Object> paramsMap = new HashMap<>();
List<String> whereFields = GenericQueryBuilder.getFieldsWithCondition(searchObject, QueryFieldChecker.isNotNull, paramsMap);
Expand All @@ -117,14 +123,22 @@ public List<Household> findByRadius(HouseholdSearch searchObject, Integer limit,
}
query = query + " ) AS rt ";
query = query + " WHERE distance < :distance ";
query = query + " ORDER BY distance ASC LIMIT :limit OFFSET :offset ";
paramsMap.put("s_latitude", searchObject.getLatitude());
paramsMap.put("s_longitude", searchObject.getLongitude());
paramsMap.put("tenantId", tenantId);
paramsMap.put("isDeleted", includeDeleted);
paramsMap.put("distance", searchObject.getSearchRadius());
Long totalCount = constructTotalCountCTEAndReturnResult(query, paramsMap);
query = query + " ORDER BY distance ASC LIMIT :limit OFFSET :offset ";
paramsMap.put("limit", limit);
paramsMap.put("offset", offset);
return this.namedParameterJdbcTemplate.query(query, paramsMap, this.rowMapper);
return new Tuple<>(totalCount, this.namedParameterJdbcTemplate.query(query, paramsMap, this.rowMapper));
}

private Long constructTotalCountCTEAndReturnResult(String query, Map<String, Object> paramsMap) {
String cteQuery = "WITH result_cte AS ("+query+"), totalCount_cte AS (SELECT COUNT(*) AS totalRows FROM result_cte) select * from totalCount_cte";
return this.namedParameterJdbcTemplate.query(cteQuery, paramsMap, resultSet -> {
return resultSet.getLong("totalRows");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void enrichHousehold(List<HouseholdMember> householdMembers) {
log.info("getting houseHoldIds for householdMembers");
List<String> houseHoldIds = getIdList(householdMembers, idMethod);
log.info("finding households from householdService with ids: {}", houseHoldIds);
List<Household> householdList = householdService.findById(houseHoldIds, columnName, false);
List<Household> householdList = householdService.findById(houseHoldIds, columnName, false).getY();
log.info("getting method for householdList with columnName: {}", columnName);
Method householdMethod = getIdMethod(householdList, columnName);
log.info("getting Map of households");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,38 +108,34 @@ public List<Household> create(HouseholdBulkRequest request, boolean isBulk) {
return request.getHouseholds();
}

public List<Household> search(HouseholdSearch householdSearch, Integer limit, Integer offset, String tenantId,
public Tuple<Long, List<Household>> search(HouseholdSearch householdSearch, Integer limit, Integer offset, String tenantId,
Long lastChangedSince, Boolean includeDeleted) {

String idFieldName = getIdFieldName(householdSearch);
if (isSearchByIdOnly(householdSearch, idFieldName)) {
List<String> ids = (List<String>) ReflectionUtils.invokeMethod(getIdMethod(Collections
.singletonList(householdSearch)),
householdSearch);
List<Household> households = householdRepository.findById(ids,
idFieldName, includeDeleted).stream()
Tuple<Long, List<Household>> householdsTuple = householdRepository.findById(ids,
idFieldName, includeDeleted);
List<Household> households = householdsTuple.getY().stream()
.filter(lastChangedSince(lastChangedSince))
.filter(havingTenantId(tenantId))
.filter(includeDeleted(includeDeleted))
.collect(Collectors.toList());
log.info("households found for search by id, size: {}", households.size());
return households;
}
if(isProximityBasedSearch(householdSearch)) {
try {
List<Household> households = householdRepository.findByRadius(householdSearch, limit, offset, tenantId, includeDeleted);
log.info("households found for search, size: {}", households.size());
return households;
} catch (QueryBuilderException e) {
log.error("error occurred while searching households", e);
throw new CustomException("ERROR_IN_QUERY", e.getMessage());
}
return new Tuple<>(householdsTuple.getX(), households);
}
try {
List<Household> households = householdRepository.find(householdSearch, limit, offset,
tenantId, lastChangedSince, includeDeleted);
log.info("households found for search, size: {}", households.size());
return households;
new Tuple<>(null, Collections.emptyList());
Tuple<Long, List<Household>> householdsTuple;
if(isProximityBasedSearch(householdSearch)) {
householdsTuple = householdRepository.findByRadius(householdSearch, limit, offset, tenantId, includeDeleted);
} else {
householdsTuple = householdRepository.find(householdSearch, limit, offset, tenantId, lastChangedSince, includeDeleted);
}
log.info("households found for search, size: {}", householdsTuple.getY().size());
return householdsTuple;
} catch (QueryBuilderException e) {
log.error("error occurred while searching households", e);
throw new CustomException("ERROR_IN_QUERY", e.getMessage());
Expand Down Expand Up @@ -209,13 +205,13 @@ public List<Household> delete(HouseholdBulkRequest request, boolean isBulk) {
return request.getHouseholds();
}

public List<Household> findById(List<String> houseHoldIds, String columnName, boolean includeDeleted){
public Tuple<Long, List<Household>> findById(List<String> houseHoldIds, String columnName, boolean includeDeleted){
log.info("finding Households by Ids: {} with columnName: {} and includeDeleted: {}",
houseHoldIds, columnName, includeDeleted);
log.info("started finding Households by Ids");
List<Household> households = householdRepository.findById(houseHoldIds, columnName, includeDeleted);
log.info("finished finding Households by Ids. Found {} Households", households.size());
return households;
Tuple<Long, List<Household>> householdsTuple = householdRepository.findById(houseHoldIds, columnName, includeDeleted);
log.info("finished finding Households by Ids. Found {} Households", householdsTuple.getY().size());
return householdsTuple;
}

public void putInCache(List<Household> households) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Map<Household, List<Error>> validate(HouseholdBulkRequest request) {
if (!eMap.isEmpty()) {
List<String> entityIds = new ArrayList<>(eMap.keySet());
List<Household> existingEntities = householdRepository.findById(entityIds,
getIdFieldName(idMethod), false);
getIdFieldName(idMethod), false).getY();
List<Household> nonExistentEntities = checkNonExistentEntities(eMap,
existingEntities, idMethod);
nonExistentEntities.forEach(task -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Map<Household, List<Error>> validate(HouseholdBulkRequest request) {
if (!eMap.isEmpty()) {
List<String> entityIds = new ArrayList<>(eMap.keySet());
List<Household> existingEntities = repository.findById(entityIds,
getIdFieldName(idMethod), false);
getIdFieldName(idMethod), false).getY();
List<Household> entitiesWithMismatchedRowVersion =
getEntitiesWithMismatchedRowVersion(eMap, existingEntities, idMethod);
entitiesWithMismatchedRowVersion.forEach(individual -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiParam;
import org.egov.common.contract.response.ResponseInfo;
import org.egov.common.ds.Tuple;
import org.egov.common.models.household.Household;
import org.egov.common.models.household.HouseholdBulkRequest;
import org.egov.common.models.household.HouseholdBulkResponse;
Expand Down Expand Up @@ -204,9 +205,9 @@ public ResponseEntity<HouseholdBulkResponse> householdV1SearchPost(@ApiParam(val
@ApiParam(value = "epoch of the time since when the changes on the object should be picked up. Search results from this parameter should include both newly created objects since this time as well as any modified objects since this time. This criterion is included to help polling clients to get the changes in system since a last time they synchronized with the platform. ") @Valid @RequestParam(value = "lastChangedSince", required = false) Long lastChangedSince,
@ApiParam(value = "Used in search APIs to specify if (soft) deleted records should be included in search results.", defaultValue = "false") @Valid @RequestParam(value = "includeDeleted", required = false, defaultValue = "false") Boolean includeDeleted) {

List<Household> households = householdService.search(request.getHousehold(), limit, offset, tenantId, lastChangedSince, includeDeleted);
Tuple<Long, List<Household>> tuple = householdService.search(request.getHousehold(), limit, offset, tenantId, lastChangedSince, includeDeleted);
HouseholdBulkResponse response = HouseholdBulkResponse.builder().responseInfo(ResponseInfoFactory
.createResponseInfo(request.getRequestInfo(), true)).households(households).build();
.createResponseInfo(request.getRequestInfo(), true)).totalCount(tuple.getX()).households(tuple.getY()).build();

return ResponseEntity.status(HttpStatus.OK).body(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ void shouldOnlySearchByIdIfOnlyIdIsPresent() throws QueryBuilderException {
HouseholdSearchRequest householdSearchRequest = HouseholdSearchRequest.builder()
.requestInfo(RequestInfoTestBuilder.builder().withCompleteRequestInfo().build())
.household(HouseholdSearch.builder().id(Collections.singletonList("some-id")).build()).build();
when(householdRepository.findById(anyList(), eq("id"), anyBoolean()))
when(householdRepository.findById(anyList(), eq("id"), anyBoolean()).getY())
.thenReturn(Collections.emptyList());

householdService.search(householdSearchRequest.getHousehold(), 10, 0, "default",
null, false);

verify(householdRepository, times(1))
.findById(anyList(), eq("id"), anyBoolean());
.findById(anyList(), eq("id"), anyBoolean()).getY();
}

@Test
Expand All @@ -55,7 +55,7 @@ void shouldOnlySearchByClientReferenceIdIfOnlyClientReferenceIdIsPresent() throw
HouseholdSearchRequest householdSearchRequest = HouseholdSearchRequest.builder()
.requestInfo(RequestInfoTestBuilder.builder().withCompleteRequestInfo().build())
.household(HouseholdSearch.builder().clientReferenceId(Collections.singletonList("some-id")).build()).build();
when(householdRepository.findById(anyList(), eq("clientReferenceId"), anyBoolean()))
when(householdRepository.findById(anyList(), eq("clientReferenceId"), anyBoolean()).getY())
.thenReturn(Collections.emptyList());

householdService.search(householdSearchRequest.getHousehold(), 10, 0, "default",
Expand All @@ -73,7 +73,7 @@ void shouldNotCallFindByIfIfMoreParametersAreAvailable() throws QueryBuilderExce
.household(HouseholdSearch.builder().id(Collections.singletonList("someid"))
.clientReferenceId(Collections.singletonList("some-id")).build()).build();
when(householdRepository.find(any(HouseholdSearch.class), anyInt(),
anyInt(), anyString(), anyLong(), anyBoolean())).thenReturn(Collections.emptyList());
anyInt(), anyString(), anyLong(), anyBoolean()).getY()).thenReturn(Collections.emptyList());

householdService.search(householdSearchRequest.getHousehold(), 10, 0,
"default", 0L, false);
Expand All @@ -90,7 +90,7 @@ void shouldCallFindIfMoreParametersAreAvailable() throws QueryBuilderException {
.household(HouseholdSearch.builder().id(Collections.singletonList("someid"))
.clientReferenceId(Collections.singletonList("some-id")).build()).build();
when(householdRepository.find(any(HouseholdSearch.class), anyInt(),
anyInt(), anyString(), anyLong(), anyBoolean())).thenReturn(Collections.emptyList());
anyInt(), anyString(), anyLong(), anyBoolean()).getY()).thenReturn(Collections.emptyList());

householdService.search(householdSearchRequest.getHousehold(), 10, 0,
"default", 0L, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private void mockHouseholdFindIds() {
any(List.class),
any(String.class),
any(Boolean.class)
)).thenReturn(
).getY()).thenReturn(
Collections.singletonList(
Household.builder().id("some-household-id").clientReferenceId("some-client-ref-id").build())
);
Expand Down
Loading