Search in sources :

Example 11 with AssignedModules

use of org.motechproject.mots.domain.AssignedModules in project mots by motech-implementations.

the class CommunityHealthWorkerService method selectHealthWorker.

/**
 * Select CHW, create IVR Subscriber and assign it to CHW. Initiate empty AssignedModules
 * instance for selected CHW.
 * @param healthWorker CHW to be selected
 * @return saved CHW
 */
@PreAuthorize(RoleNames.HAS_CHW_WRITE_ROLE)
public CommunityHealthWorker selectHealthWorker(CommunityHealthWorker healthWorker) {
    if (healthWorker.getSelected()) {
        throw new ChwException("Could not select CHW, because already selected");
    }
    healthWorker.setSelected(true);
    String phoneNumber = healthWorker.getPhoneNumber();
    String name = healthWorker.getCombinedName();
    Language preferredLanguage = healthWorker.getPreferredLanguage();
    try {
        String ivrId = ivrService.createSubscriber(phoneNumber, name, preferredLanguage);
        healthWorker.setIvrId(ivrId);
    } catch (IvrException ex) {
        String message = "Could not select CHW, because of IVR subscriber creation error. \n\n" + ex.getClearVotoInfo();
        throw new ChwException(message, ex);
    }
    healthWorkerRepository.save(healthWorker);
    AssignedModules emptyAssignedModulesInstance = new AssignedModules(healthWorker);
    assignedModulesRepository.save(emptyAssignedModulesInstance);
    return healthWorker;
}
Also used : AssignedModules(org.motechproject.mots.domain.AssignedModules) ChwException(org.motechproject.mots.exception.ChwException) IvrException(org.motechproject.mots.exception.IvrException) Language(org.motechproject.mots.domain.enums.Language) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

AssignedModules (org.motechproject.mots.domain.AssignedModules)11 CommunityHealthWorker (org.motechproject.mots.domain.CommunityHealthWorker)5 IvrException (org.motechproject.mots.exception.IvrException)5 Test (org.junit.Test)4 ModuleAssignmentException (org.motechproject.mots.exception.ModuleAssignmentException)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 HashSet (java.util.HashSet)3 Module (org.motechproject.mots.domain.Module)3 AssignedModulesDataBuilder (org.motechproject.mots.testbuilder.AssignedModulesDataBuilder)3 CommunityHealthWorkerDataBuilder (org.motechproject.mots.testbuilder.CommunityHealthWorkerDataBuilder)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 CourseModule (org.motechproject.mots.domain.CourseModule)2 DistrictAssignmentLog (org.motechproject.mots.domain.DistrictAssignmentLog)2 User (org.motechproject.mots.domain.security.User)2 EntityNotFoundException (org.motechproject.mots.exception.EntityNotFoundException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1