Skip to content

Commit

Permalink
avniproject/avni-webapp#1217 | Create reset sync records for applicab…
Browse files Browse the repository at this point in the history
…le users when location parent is updated
  • Loading branch information
1t5j0y committed Jun 3, 2024
1 parent c3877e6 commit e37792f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ default Optional<AddressLevel> findByTitleLineageIgnoreCase(String locationTitle
List<AddressLevel> findByIsVoidedFalseAndTitleIgnoreCaseContains(String title);

@Query(value = "select id from address_level where lineage ~ cast(:lquery as lquery)", nativeQuery = true)
List<Long> getAllChildrenLocationsIds(@Param("lquery") String lquery);
List<Long> getAllChildrenLocationsIds(@Param("lquery") String lquery); //actually returns List<Integer>

@Query(value = "select * from address_level where lineage ~ cast(:lquery as lquery)", nativeQuery = true)
List<AddressLevel> getAllChildLocations(@Param("lquery") String lquery);

@Query(value = "select * from virtual_catchment_address_mapping_table where catchment_id = :catchmentId", nativeQuery = true)
List<VirtualCatchmentProjection> getVirtualCatchmentsForCatchmentId(@Param("catchmentId") Long catchmentId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ public class LocationService implements ScopeAwareService<AddressLevel> {
private final OrganisationRepository organisationRepository;
private final LocationRepository locationRepository;
private final LocationMappingRepository locationMappingRepository;
private final ResetSyncService resetSyncService;
private final Logger logger;

@Autowired
public LocationService(LocationRepository locationRepository, AddressLevelTypeRepository addressLevelTypeRepository, OrganisationRepository organisationRepository, LocationMappingRepository locationMappingRepository) {
public LocationService(LocationRepository locationRepository, AddressLevelTypeRepository addressLevelTypeRepository, OrganisationRepository organisationRepository, LocationMappingRepository locationMappingRepository, ResetSyncService resetSyncService) {
this.locationRepository = locationRepository;
this.addressLevelTypeRepository = addressLevelTypeRepository;
this.organisationRepository = organisationRepository;
this.locationMappingRepository = locationMappingRepository;
this.resetSyncService = resetSyncService;
this.logger = LoggerFactory.getLogger(this.getClass());
}

Expand Down Expand Up @@ -170,6 +172,7 @@ public AddressLevel update(LocationEditContract locationEditContract, Long id) {
updateLocationMapping(location, locationEditContract);
location.setLineage(updateLineage(lineage, oldParentId, newParentId));
location.setParent(locationRepository.findOne(newParentId));
resetSyncService.recordLocationParentChange(location, oldParentId);
}

location.setTitle(locationEditContract.getTitle());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.avni.server.service;

import org.avni.server.dao.IndividualRepository;
import org.avni.server.dao.ResetSyncRepository;
import org.avni.server.dao.SubjectTypeRepository;
import org.avni.server.dao.UserRepository;
import org.avni.server.application.projections.BaseProjection;
import org.avni.server.application.projections.LocationProjection;
import org.avni.server.application.projections.VirtualCatchmentProjection;
import org.avni.server.dao.*;
import org.avni.server.domain.*;
import org.avni.server.util.JsonObjectUtil;
import org.avni.server.web.request.CatchmentContract;
Expand All @@ -27,13 +27,15 @@ public class ResetSyncService {
private final UserRepository userRepository;
private final IndividualRepository individualRepository;
private final SubjectTypeRepository subjectTypeRepository;
private final LocationRepository locationRepository;

@Autowired
public ResetSyncService(ResetSyncRepository resetSyncRepository, UserRepository userRepository, IndividualRepository individualRepository, SubjectTypeRepository subjectTypeRepository) {
public ResetSyncService(ResetSyncRepository resetSyncRepository, UserRepository userRepository, IndividualRepository individualRepository, SubjectTypeRepository subjectTypeRepository, LocationRepository locationRepository) {
this.resetSyncRepository = resetSyncRepository;
this.userRepository = userRepository;
this.individualRepository = individualRepository;
this.subjectTypeRepository = subjectTypeRepository;
this.locationRepository = locationRepository;
}

public void recordCatchmentChange(Catchment savedCatchment, CatchmentContract request) {
Expand All @@ -60,6 +62,30 @@ public void recordSyncAttributeChange(SubjectType savedSubjectType, SubjectTypeC
}
}

public void recordLocationParentChange(AddressLevel addressLevel, Long oldParentId) {
String lquery = "*.".concat(addressLevel.getLineage()).concat(".*");
List<AddressLevel> allChildLocations = locationRepository.getAllChildLocations(lquery);
List<Long> allChildLocationIds = allChildLocations.stream().map(AddressLevel::getId).collect(Collectors.toList());
if (individualRepository.hasSubjectsInLocations(allChildLocationIds)) {
List<LocationProjection> parentLocations = locationRepository.getParents(addressLevel.getParentUuid());
List<LocationProjection> oldParentParentLocations = locationRepository.getParents(locationRepository.findById(oldParentId).get().getUuid());
parentLocations.addAll(oldParentParentLocations);
List<VirtualCatchmentProjection> virtualCatchmentsForAddressLevelIds = locationRepository.getVirtualCatchmentsForAddressLevelIds(parentLocations.stream().map(BaseProjection::getId).collect(Collectors.toList()));
List<ResetSync> resetSyncRecords = new ArrayList<>();
userRepository.findByCatchment_IdInAndIsVoidedFalse(virtualCatchmentsForAddressLevelIds
.stream()
.map(VirtualCatchmentProjection::getCatchment_id)
.collect(Collectors.toList()))
.forEach(user -> {
ResetSync resetSync = buildNewResetSync();
resetSync.setUser(user);
resetSyncRecords.add(resetSync);
});

if (!resetSyncRecords.isEmpty()) resetSyncRepository.saveAll(resetSyncRecords);
}
}

private boolean anySyncAttributeChanged(SubjectType savedSubjectType, SubjectTypeContractWeb request) {
return isChanged(savedSubjectType.getSyncRegistrationConcept1(), request.getSyncRegistrationConcept1()) ||
isChanged(savedSubjectType.getSyncRegistrationConcept2(), request.getSyncRegistrationConcept2()) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ public class LocationServiceTest {
private AddressLevelTypeRepository addressLevelTypeRepository;
@Mock
private LocationRepository locationRepository;
@Mock
private ResetSyncService resetSyncService;

private LocationService locationService;

@Before
public void before() {
initMocks(this);
locationService = new LocationService(locationRepository, addressLevelTypeRepository, organisationRepository, locationMappingRepository);
locationService = new LocationService(locationRepository, addressLevelTypeRepository, organisationRepository, locationMappingRepository, resetSyncService);
}

@Test
Expand Down

0 comments on commit e37792f

Please sign in to comment.