use of org.motechproject.mots.exception.IvrException in project mots by motech-implementations.
the class ModuleAssignmentServiceTest method assignModulesToDistrictShouldThrowCustomExceptionIfIvrServiceThrow.
@Test(expected = ModuleAssignmentException.class)
public void assignModulesToDistrictShouldThrowCustomExceptionIfIvrServiceThrow() throws Exception {
when(communityHealthWorkerRepository.findByCommunityFacilityChiefdomDistrictIdAndSelected(any(), any())).thenReturn(Collections.singletonList(CHW));
doThrow(new IvrException("message")).when(ivrService).addSubscriberToGroups(any(), any());
moduleAssignmentService.assignModulesToDistrict(districtAssignmentDto);
}
use of org.motechproject.mots.exception.IvrException in project mots by motech-implementations.
the class CommunityHealthWorkerService method saveHealthWorker.
/**
* Update CHW and IVR Subscriber.
* @param chw CHW to update
* @return saved CHW
*/
@PreAuthorize(RoleNames.HAS_CHW_WRITE_ROLE)
public CommunityHealthWorker saveHealthWorker(CommunityHealthWorker chw) {
String ivrId = chw.getIvrId();
String phoneNumber = chw.getPhoneNumber();
String name = chw.getCombinedName();
Language preferredLanguage = chw.getPreferredLanguage();
try {
ivrService.updateSubscriber(ivrId, phoneNumber, name, preferredLanguage);
} catch (IvrException ex) {
String message = "Could not update CHW, because of IVR subscriber update error. \n\n" + ex.getClearVotoInfo();
throw new ChwException(message, ex);
}
return healthWorkerRepository.save(chw);
}
use of org.motechproject.mots.exception.IvrException in project mots by motech-implementations.
the class ModuleAssignmentServiceTest method assignModulesShouldThrowCustomExceptionIfIvrServiceThrow.
@Test(expected = ModuleAssignmentException.class)
public void assignModulesShouldThrowCustomExceptionIfIvrServiceThrow() throws Exception {
when(moduleProgressService.getModuleProgress(any(), any())).thenReturn(getModuleProgress(ProgressStatus.NOT_STARTED));
doThrow(new IvrException("message")).when(ivrService).addSubscriberToGroups(any(), any());
moduleAssignmentService.assignModules(newAssignedModules);
}
use of org.motechproject.mots.exception.IvrException in project mots by motech-implementations.
the class IvrService method sendVotoRequest.
private <T> T sendVotoRequest(String url, MultiValueMap<String, String> params, ParameterizedTypeReference<T> responseType, HttpMethod method) throws IvrException {
params.add(API_KEY, ivrApiKey);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url).queryParams(params);
HttpEntity<?> request = new HttpEntity<>(headers);
try {
ResponseEntity<T> responseEntity = restTemplate.exchange(builder.build().toString(), method, request, responseType);
return responseEntity.getBody();
} catch (RestClientResponseException ex) {
String responseBodyJson = ex.getResponseBodyAsString();
String responseMessage = responseBodyJson;
String clearVotoInfo = "";
try {
VotoResponseDto<String> votoResponse = mapper.readValue(responseBodyJson, new TypeReference<VotoResponseDto<String>>() {
});
if (votoResponse.getMessage() != null) {
responseMessage = votoResponse.getMessage();
clearVotoInfo = "Invalid IVR service response: " + responseMessage;
}
} catch (IOException e) {
responseMessage = responseBodyJson;
}
String message = "Invalid IVR service response: " + ex.getRawStatusCode() + " " + ex.getStatusText() + ", Response body: " + responseMessage;
throw new IvrException(message, ex, clearVotoInfo);
} catch (RestClientException ex) {
String message = "Error occurred when sending request to IVR service: " + ex.getMessage();
throw new IvrException(message, ex);
}
}
use of org.motechproject.mots.exception.IvrException 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);
}
Aggregations