Skip to content

Commit

Permalink
avniproject/avni-webapp#1347 | Fix valid location type validation for…
Browse files Browse the repository at this point in the history
… location edit csv upload
  • Loading branch information
1t5j0y committed Nov 21, 2024
1 parent fe17645 commit 0e03621
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.List;
import java.util.Optional;

import static org.springframework.util.ObjectUtils.nullSafeEquals;

@Component
public class BulkLocationEditor extends BulkLocationModifier {
private final LocationService locationService;
Expand Down Expand Up @@ -45,11 +47,14 @@ public void editLocation(Row row, List<String> allErrorMsgs) {
allErrorMsgs.add(String.format("Provided new location parent does not exist in Avni. Please add it or check for spelling mistakes and ensure space between two locations - '%s'", newLocationParentTitleLineage));
throw new RuntimeException(String.join(", ", allErrorMsgs));
}
AddressLevelType currentParentType = null;
AddressLevel currentParent = existingLocationAddressLevel.get().getParentLocation();
if (currentParent != null) {currentParentType = currentParent.getType();}
if (!newLocationParentAddressLevel.getType().equals(currentParentType)) {
allErrorMsgs.add(String.format("Only parent of location type \'%s\' is allowed for %s.", currentParentType.getName(), existingLocationAddressLevel.get().getTitle()));

AddressLevelType allowedParentType = existingLocationAddressLevel.get().getType().getParent();
if (!nullSafeEquals(allowedParentType, newLocationParentAddressLevel.getType())) {
if (allowedParentType != null) {
allErrorMsgs.add(String.format("Only parent of location type \'%s\' is allowed for %s.", allowedParentType.getName(), existingLocationAddressLevel.get().getTitle()));
} else {
allErrorMsgs.add(String.format("No parent is allowed for %s since it is a top level location.", existingLocationAddressLevel.get().getTitle()));
}
throw new RuntimeException(String.join(", ", allErrorMsgs));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ public class BulkLocationEditorIntegrationTest extends BaseCSVImportTest {
@Override
public void setUp() throws Exception {
TestDataSetupService.TestOrganisationData organisationData = testDataSetupService.setupOrganisation();
AddressLevelType block = new AddressLevelTypeBuilder().name("Block").level(2d).withUuid(UUID.randomUUID()).build();
AddressLevelType district = new AddressLevelTypeBuilder().name("District").level(3d).withUuid(UUID.randomUUID()).build();
AddressLevelType state = new AddressLevelTypeBuilder().name("State").level(4d).withUuid(UUID.randomUUID()).build();
testDataSetupService.saveLocationTypes(Arrays.asList(block, district, state));
testDataSetupService.saveLocationTypes(Collections.singletonList(state));
AddressLevelType district = new AddressLevelTypeBuilder().name("District").level(3d).parent(state).withUuid(UUID.randomUUID()).build();
testDataSetupService.saveLocationTypes(Collections.singletonList(district));
AddressLevelType block = new AddressLevelTypeBuilder().name("Block").level(2d).parent(district).withUuid(UUID.randomUUID()).build();
testDataSetupService.saveLocationTypes(Collections.singletonList(block));
Concept codedConcept = testConceptService.createCodedConcept("Coded Concept", "Answer 1", "Answer 2");
Concept textConcept = testConceptService.createConcept("Text Concept", ConceptDataType.Text);
Form locationForm = new TestFormBuilder()
Expand Down Expand Up @@ -200,13 +202,20 @@ public void shouldEdit() {
verifyExists("Bihar", "District2", "Block21"),
verifyNotExists("Bihar, District2, Block21Town"));

// change to parent at a different level from current parent's level
// change to parent of a different type from allowed parent's type
failure(header("Location with full hierarchy", "New location name", "Parent location with full hierarchy", "GPS coordinates"),
dataRow("Bihar, District2, Block21", " Block21Town", "Bihar", "23.45,43.85"),
error("Only parent of location type 'District' is allowed for Block21."),
verifyExists("Bihar", "District2", "Block21"),
verifyNotExists("Bihar, District2, Block21Town"));

// attempt to change root level location to an invalid parent
failure(header("Location with full hierarchy", "Parent location with full hierarchy"),
dataRow("Bihar", "Bihar, District2"),
error("No parent is allowed for Bihar since it is a top level location."),
verifyExists("Bihar"),
verifyNotExists("District2, Bihar"));

treatAsDescriptor(header("Location with full hierarchy", "New location name", "Parent location with full hierarchy", "GPS coordinates"),
dataRow("Can be found from Admin -> Locations -> Click Export. Used to specify which location's fields need to be updated. mandatory field",
"Enter new name here ONLY if it needs to be updated",
Expand Down

0 comments on commit 0e03621

Please sign in to comment.