diff --git a/components/home/search/search-controller.js b/components/home/search/search-controller.js index ad47f7bda..a44dcddb0 100644 --- a/components/home/search/search-controller.js +++ b/components/home/search/search-controller.js @@ -204,7 +204,7 @@ trackerCapture.controller('SearchController',function( //Open TEI if unique and in same search scope and in selected org unit if(gridData.rows.own.length ===1 && res.callingScope === res.resultScope && searchGroup.uniqueGroup){ $scope.searching = null; - openTei(gridData.rows.own[0]); + openTei(gridData.rows.own[0], [$scope.selectedOrgUnit.id]); return; } } else if(rowsCnt > 0){ @@ -230,14 +230,18 @@ trackerCapture.controller('SearchController',function( } } - var openTei = function(tei, fromAudit){ + var openTei = function(tei, programOwners, fromAudit){ if($scope.searchingForRelatedTei) { $rootScope.$broadcast('assignRelationshipTei', tei); } else { - $location.path('/dashboard').search({tei: tei.id, - program: $scope.base.selectedProgramForSearch ? $scope.base.selectedProgramForSearch.id: null, - ou: $scope.selectedOrgUnit.id, - fromAudit: fromAudit}); + AccessUtils.withinUserHierarchy(programOwners[0].ownerOrgUnit).then(function(response) { + $location.path('/dashboard').search({tei: tei.id, + program: $scope.base.selectedProgramForSearch ? $scope.base.selectedProgramForSearch.id: null, + ou: response ? programOwners[0].ownerOrgUnit : $scope.selectedOrgUnit.id, + fromAudit: fromAudit}); + }).catch(function(error) { + console.log("error opening TEI from search modal: ", error); + }); } } @@ -385,7 +389,7 @@ trackerCapture.controller('SearchController',function( $scope.openTei = function(tei){ if(internalService.base.selectedProgramForSearch && internalService.base.selectedProgramForSearch.id){ TEIService.getWithProgramData(tei.id, internalService.base.selectedProgramForSearch.id, internalService.base.optionSets, internalService.base.attributesById).then(function(resultTei){ - $modalInstance.close({ action: "OPENTEI", tei: tei, fromAudit: true}); + $modalInstance.close({ action: "OPENTEI", tei: tei, programOwners: resultTei.programOwners, fromAudit: true}); }, function(error){ if(error && !error.auditDismissed && error.data && error.data.message){ var headerText = $translate.instant('open_tei_error'); @@ -444,7 +448,7 @@ trackerCapture.controller('SearchController',function( var def = $q.defer(); def.resolve(); if(res.action === "OPENTEI"){ - openTei(res.tei, res.fromAudit); + openTei(res.tei, res.programOwners, res.fromAudit); }else if(res.action === "OPENREGISTRATION") { var registrationPrefill = getRegistrationPrefill(searchGroup); diff --git a/scripts/services.js b/scripts/services.js index 11a79ff88..65751892b 100644 --- a/scripts/services.js +++ b/scripts/services.js @@ -3311,7 +3311,7 @@ i } } }) -.service('AccessUtils', function($q, TCStorageService){ +.service('AccessUtils', function($http, $q, TCStorageService){ this.anyWritable = function(accessKeyValuePair){ if(accessKeyValuePair){ @@ -3346,6 +3346,12 @@ i }); return writable; } + + this.withinUserHierarchy = function(ou) { + return $http.get(DHIS2URL + '/organisationUnits?withinUserHierarchy=true&fields=id&filter=id:eq:' + ou).then(function(response) { + return response.data.organisationUnits.length !== 0; + }); + } }) .service('TCOrgUnitService', function($q, $rootScope, TCStorageService, OrgUnitFactory){ this.get = function(uid) {