use of org.motechproject.mots.domain.AssignedModules in project mots by motech-implementations.
the class ModuleAssignmentServiceTest method assignModulesToDistrictShouldThrowIfIvrIdIsNotSet.
@Test(expected = ModuleAssignmentException.class)
public void assignModulesToDistrictShouldThrowIfIvrIdIsNotSet() {
CommunityHealthWorker chw = new CommunityHealthWorkerDataBuilder().withIvrId(null).build();
AssignedModules assignedModules = new AssignedModulesDataBuilder().withChw(chw).build();
when(communityHealthWorkerRepository.findByCommunityFacilityChiefdomDistrictIdAndSelected(any(), any())).thenReturn(Collections.singletonList(chw));
when(assignedModulesRepository.findByHealthWorkerId(eq(chw.getId()))).thenReturn(Optional.of(assignedModules));
moduleAssignmentService.assignModulesToDistrict(districtAssignmentDto);
}
use of org.motechproject.mots.domain.AssignedModules in project mots by motech-implementations.
the class ModuleAssignmentServiceTest method shouldAssignModulesToDistrict.
@Test
public void shouldAssignModulesToDistrict() throws Exception {
when(userService.getUserByUserName(eq(user.getUsername()))).thenReturn(user);
when(communityHealthWorkerRepository.findByCommunityFacilityChiefdomDistrictIdAndSelected(any(), any())).thenReturn(Collections.singletonList(CHW));
moduleAssignmentService.assignModulesToDistrict(districtAssignmentDto);
ArgumentCaptor<DistrictAssignmentLog> districtAssignmentLogCaptor = ArgumentCaptor.forClass(DistrictAssignmentLog.class);
verify(districtAssignmentLogRepository, times(2)).save(districtAssignmentLogCaptor.capture());
List<DistrictAssignmentLog> districtAssignmentLogs = districtAssignmentLogCaptor.getAllValues();
final Set<Module> passedModules = new HashSet<>(Arrays.asList(MODULE_2, MODULE_3));
assertTrue(districtAssignmentLogs.stream().allMatch(l -> passedModules.contains(l.getModule())));
for (DistrictAssignmentLog log : districtAssignmentLogs) {
assertEquals(DISTRICT, log.getDistrict());
assertEquals(districtAssignmentDto.getStartDate(), log.getStartDate().toString());
assertEquals(districtAssignmentDto.getEndDate(), log.getEndDate().toString());
assertEquals(user, log.getOwner());
}
ArgumentCaptor<AssignedModules> assignedModulesCaptor = ArgumentCaptor.forClass(AssignedModules.class);
verify(assignedModulesRepository).save(assignedModulesCaptor.capture());
final Set<Module> allModules = new HashSet<>(Arrays.asList(MODULE_1, MODULE_2, MODULE_3));
assertEquals(allModules, assignedModulesCaptor.getValue().getModules());
verify(ivrService).addSubscriberToGroups(eq(CHW.getIvrId()), eq(Collections.singletonList(IVR_GROUP)));
verify(moduleProgressService).createModuleProgresses(any(), eq(Collections.singleton(MODULE_3)));
}
use of org.motechproject.mots.domain.AssignedModules in project mots by motech-implementations.
the class ModuleAssignmentService method assignModules.
/**
* Assign modules to CHW.
* @param assignedModules modules assigned for CHW
*/
@SuppressWarnings("checkstyle:variabledeclarationusagedistance")
@Transactional
@PreAuthorize(RoleNames.HAS_ASSIGN_MODULES_ROLE)
public void assignModules(AssignedModules assignedModules) {
AssignedModules existingAssignedModules = getAssignedModules(assignedModules.getHealthWorker().getId());
CommunityHealthWorker assignedModulesChw = existingAssignedModules.getHealthWorker();
Set<Module> oldModules = new HashSet<>(existingAssignedModules.getModules());
existingAssignedModules.setModules(assignedModules.getModules());
repository.save(existingAssignedModules);
entityManager.flush();
entityManager.refresh(existingAssignedModules);
Set<Module> newModules = new HashSet<>(existingAssignedModules.getModules());
Set<Module> modulesToAdd = getModulesToAdd(oldModules, newModules);
Set<Module> modulesToRemove = getModulesToRemove(oldModules, newModules);
validateModulesToUnassign(assignedModulesChw, modulesToRemove);
String ivrId = assignedModulesChw.getIvrId();
if (ivrId == null) {
throw new ModuleAssignmentException("Could not assign module to CHW, because CHW has empty IVR id");
}
try {
ivrService.addSubscriberToGroups(ivrId, getIvrGroupsFromModules(modulesToAdd));
ivrService.removeSubscriberFromGroups(ivrId, getIvrGroupsFromModules(modulesToRemove));
} catch (IvrException ex) {
String message = "Could not assign or delete module for CHW, " + "because of IVR module assignment error.\n\n" + ex.getClearVotoInfo();
throw new ModuleAssignmentException(message, ex);
}
moduleProgressService.removeModuleProgresses(assignedModulesChw, modulesToRemove);
moduleProgressService.createModuleProgresses(assignedModulesChw, modulesToAdd);
}
use of org.motechproject.mots.domain.AssignedModules in project mots by motech-implementations.
the class ModuleAssignmentService method unassignOldModuleVersion.
private void unassignOldModuleVersion(Module oldModule) {
List<AssignedModules> assignedModulesList = repository.findByModules_id(oldModule.getId());
if (assignedModulesList != null && !assignedModulesList.isEmpty()) {
assignedModulesList.forEach(assignedModules -> {
assignedModules.unassignModule(oldModule);
String ivrGroup = oldModule.getIvrGroup();
String chwIvrId = assignedModules.getHealthWorker().getIvrId();
try {
ivrService.removeSubscriberFromGroups(chwIvrId, Collections.singletonList(ivrGroup));
} catch (IvrException ex) {
throw new ModuleAssignmentException("Could not unassign old module version, " + "because of IVR module assignment error.\n\n" + ex.getClearVotoInfo(), ex);
}
});
repository.save(assignedModulesList);
}
}
use of org.motechproject.mots.domain.AssignedModules in project mots by motech-implementations.
the class ModuleAssignmentService method assignModulesToDistrict.
/**
* Creates DistrictAssignmentLog for district assignment,
* and assigns modules to each CHW from a district.
* @param assignmentDto dto with district id, list of modules assigned to it
* and start and end dates
*/
@Transactional
@PreAuthorize(RoleNames.HAS_ASSIGN_MODULES_ROLE)
public void assignModulesToDistrict(DistrictAssignmentDto assignmentDto) {
List<CommunityHealthWorker> communityHealthWorkers = communityHealthWorkerRepository.findByCommunityFacilityChiefdomDistrictIdAndSelected(UUID.fromString(assignmentDto.getDistrictId()), true);
Set<Module> newChwModules = new HashSet<>();
String userName = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
User currentUser = userService.getUserByUserName(userName);
for (String moduleId : assignmentDto.getModules()) {
Module moduleToAssign = moduleRepository.findById(UUID.fromString(moduleId)).orElseThrow(() -> new EntityNotFoundException("No module found for Id: {0}", moduleId));
newChwModules.add(moduleToAssign);
assignmentLogRepository.save(new DistrictAssignmentLog(districtRepository.findOne(UUID.fromString(assignmentDto.getDistrictId())), LocalDate.parse(assignmentDto.getStartDate()), LocalDate.parse(assignmentDto.getEndDate()), moduleToAssign, currentUser));
}
for (CommunityHealthWorker chw : communityHealthWorkers) {
AssignedModules existingAssignedModules = getAssignedModules(chw.getId());
Set<Module> oldModules = existingAssignedModules.getModules();
Set<Module> modulesToAdd = getModulesToAdd(oldModules, newChwModules);
existingAssignedModules.getModules().addAll(modulesToAdd);
repository.save(existingAssignedModules);
entityManager.flush();
entityManager.refresh(existingAssignedModules);
String ivrId = existingAssignedModules.getHealthWorker().getIvrId();
if (ivrId == null) {
throw new ModuleAssignmentException("Could not assign module to CHW, because CHW has empty IVR id");
}
try {
ivrService.addSubscriberToGroups(ivrId, getIvrGroupsFromModules(modulesToAdd));
} catch (IvrException ex) {
String message = "Could not assign or delete module for CHW, " + "because of IVR module assignment error.\n\n" + ex.getClearVotoInfo();
throw new ModuleAssignmentException(message, ex);
}
moduleProgressService.createModuleProgresses(chw, modulesToAdd);
}
}
Aggregations