Advertisement
Guest User

Untitled

a guest
Apr 4th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. flowApp.controller('userController',['$scope', 'blockUI', 'adminService', '$window','gettextCatalog', function($scope, blockUI, adminService, $window, gettextCatalog) {
  2.  
  3.     $scope.selUsrObj = null;
  4.     $scope.mgrSrchTxt=$scope.deptSrchTxt=$scope.locSrchTxt=null;
  5.     $scope.uniqueUsrType = null;
  6.     $scope.disablePermission = false;
  7.     $scope.disableActivate = false;
  8.     $scope.allLoaded = false;
  9.     $scope.depSearchList = [];
  10.     $scope.locSearchList = [];
  11.     $scope.depObjSearchList = [];
  12.     $scope.locObjSearchList = [];
  13.     $scope.search = {data:'', selected_user_type:'2'};
  14.     $scope.userList = [];
  15.     $scope.usrIds = [];
  16.  
  17.     $scope.listUsers = function(type, page, srchTxt, userType){
  18.         if ($scope.pageType != type || userType!=null) {
  19.             $scope.usrIds = [];
  20.             $scope.userList = [];
  21.             $scope.search.selected_user_type = userType;
  22.         }else{
  23.             $scope.search.selected_user_type='2';
  24.         }
  25.  
  26.         var data = {};
  27.  
  28.         if ($scope.menuName=='User Requests' || $scope.menuName=='Creator Requests'){
  29.             data['Requester'] = $scope.menuName.split(' ')[0];
  30.         }else{
  31.             if(userType){
  32.                 var data = {Inactive:userType}
  33.             }else {
  34.                 var data = type == "Active" ? {Active: 1} : {Inactive: 1}
  35.             }
  36.         }
  37.  
  38.         $scope.pageType = type;
  39.         $scope.pageNumber = page;
  40.  
  41.         if(srchTxt) {
  42.             data["Search"] = srchTxt;
  43.             $('#search-admin-data').css('opacity', 1);
  44.         }
  45.  
  46.         adminService.getModelData('User', page, $scope.PAGE_SIZE, data)
  47.             .success(function (data) {
  48.                 $scope.userList = $scope.userList.concat(data["data"]);
  49.                 $scope.totalCount = data["total_count"];
  50.                 $('.admin-list-container').css('opacity', 1);
  51.             })
  52.             .error(function (xmlr, status, err) {
  53.                 Materialize.toast(gettextCatalog.getString('Unable to get users list'), 4000, 'red');
  54.             });
  55.     }
  56.  
  57.     $scope.listUsers($scope.menuName.split(' ')[0], 1);
  58.  
  59.     $scope.toggleUsrSelection = function (usrId) {
  60.         var idx = $scope.usrIds.indexOf(usrId);
  61.         // is currently selected
  62.         if (idx > -1) {
  63.             $scope.usrIds.splice(idx, 1);
  64.         }
  65.  
  66.         // is newly selected
  67.         else {
  68.           $scope.usrIds.push(usrId);
  69.         }
  70.         if($scope.usrIds.length>1){
  71.             $scope.mgrSrchTxt=$scope.getUnique('Manager');
  72.             $scope.deptSrchTxt=$scope.getUnique('Department');
  73.             $scope.locSrchTxt=$scope.getUnique('Location');
  74.             $scope.uniqueUsrType=$scope.getUnique('UserType');
  75.         }
  76.         $scope.disablePermission=$scope.isCrtrLicnseExcds();
  77.         $scope.disableActivate = $scope.isUsrLicnseExcds();
  78.     };
  79.  
  80.     $scope.fetchUser = function(usrId){
  81.         return _.find($scope.userList, function(usr){return usr.Id === usrId;});
  82.     }
  83.  
  84.  
  85.     $scope.updateUser = function(key, val){
  86.         var userList = [];
  87.         for (var ids in $scope.usrIds){
  88.             var param = {};
  89.             param[key] = val;
  90.             param['Id'] = $scope.usrIds[ids];
  91.             userList.push(param);
  92.         }
  93.         var data = {'user_list':JSON.stringify(userList)}
  94.         $scope.batchUserUpdate(data, key+" updated");
  95.     }
  96.  
  97.     $scope.removeUsers = function(data){
  98.         var usrIds = _.pluck(JSON.parse(data["user_list"]), "Id")
  99.         $scope.userList = _.reject($scope.userList, function(d){ return $.inArray(d.Id, usrIds)!=-1; });
  100.     }
  101.  
  102.     $scope.batchUserUpdate = function(data, key, updateCount){
  103.         var updateList = data;
  104.         adminService.updateUserList(data)
  105.             .success(function (result){
  106.                 if(key == 'User deactivated' || key=='User activated'){
  107.                     $scope.removeUsers(data)
  108.                 }else{
  109.                     $scope.userList = [];
  110.                     $scope.listUsers($scope.pageType, $scope.pageNumber, $scope.search.data);
  111.                 }
  112.                 if (updateCount) {
  113.                     $scope.loadUsersCount();
  114.                     $scope.updateToWizard(updateList,key);
  115.                 }
  116.                 $scope.selUsrObj = $scope.fetchUser($scope.usrIds[0]);
  117.                 Materialize.toast(gettextCatalog.getString(key+" successfully"), 4000, 'green');
  118.             })
  119.             .error(function(error){
  120.                 Materialize.toast(gettextCatalog.getString('Unable to update the users'), 7000, 'red');
  121.             });
  122.     }
  123.  
  124.     $scope.getDepList = function(searchText){
  125.         var searchLimit = 1000;
  126.         var data = {SearchBy:searchText}
  127.         adminService.getModelData("Department", 1, searchLimit, data)
  128.             .then(function(result){
  129.                $scope.depObjSearchList = result.data["data"];
  130.                $scope.depSearchList = _.map(result.data["data"], function(obj) {
  131.                     return obj.Name
  132.                 });
  133.             });
  134.     }
  135.  
  136.     $scope.getLocList = function(searchText){
  137.         var searchLimit = 1000;
  138.         var data = {SearchBy:searchText}
  139.         adminService.getModelData("Location", 1, searchLimit, data)
  140.             .then(function(result){
  141.                $scope.locObjSearchList = result.data["data"];
  142.                $scope.locSearchList = _.map(result.data["data"], function(obj) {
  143.                     return obj.Name
  144.                 });
  145.             });
  146.     }
  147.  
  148.     $scope.updateMgr = function (val) {
  149.         $scope.updateUser('Manager', val);
  150.     }
  151.  
  152.     $scope.updateDep = function (val) {
  153.         var dep = _.find($scope.depObjSearchList, function(dept){return dept.Name === val;});
  154.         $scope.updateUser('Department', dep.Id);
  155.     }
  156.  
  157.     $scope.updateLoc = function (val) {
  158.         var loc = _.find($scope.locObjSearchList, function(l){return l.Name === val;});
  159.         $scope.updateUser('Location', loc.Id);
  160.     }
  161.  
  162.     $scope.getUnique = function (key){
  163.         var selectedUsers = [];
  164.         for(var ids in $scope.usrIds){
  165.             var selectedUser=$scope.fetchUser($scope.usrIds[ids]);
  166.             selectedUsers.push(selectedUser);
  167.         }
  168.         var value=_.unique(_.pluck(selectedUsers, key));
  169.         return value.length==1?value[0]:null;
  170.     }
  171.  
  172.     $scope.actDeactUser = function(menuName){
  173.         var userList = [];
  174.  
  175.         if (menuName == 'Inactive Users'){
  176.             if ($scope.isUsrLicnseExcds()==true){
  177.                 Materialize.toast(gettextCatalog.getString('License exceeded'), 4000, 'red');
  178.                 return;
  179.             }
  180.         }
  181.         /*license check validated so proceeds activation
  182.         * No need to check license when deactivate users
  183.         * */
  184.         for (var ids in $scope.usrIds){
  185.             var param = {};
  186.             param['Id'] = $scope.usrIds[ids];
  187.             param['RequestUser'] = menuName=='Active Users'?'Reject':'Approved';
  188.             param['RequestCreator'] = 'Reject';
  189.             param['mail_args']={
  190.               'Request':key=menuName=='Active Users'?'User deactivated':'User activated'
  191.             }
  192.             userList.push(param);
  193.         }
  194.  
  195.         var data = {'user_list':JSON.stringify(userList)}
  196.         $scope.batchUserUpdate(data, key=menuName=='Active Users'?'User deactivated':'User activated', true);
  197.         $scope.usrIds = [];
  198.     }
  199.  
  200.     $scope.accessRequest = function(usrId, action, mailArgs){
  201. /*        jQuery(dao).osInvoke('admin.items', "invoke", {actionName: "flow.services.mailService.invoke", caption: 'sendMailNotification',
  202.             data:{'mailType':args['Request'], 'mailid':args['mailid'], 'fullName':args['fullName']}})*/
  203.  
  204.         if (mailArgs["Request"] == 'UserAccessApproved'){
  205.             if ($scope.company.UserLicenses+$scope.company.CreatorLicenses <= $scope.countDict.User+$scope.countDict.Creator==true){
  206.                 Materialize.toast(gettextCatalog.getString('User License exceeded'), 4000, 'red');
  207.                 return;
  208.             }
  209.         }
  210.  
  211.         if (mailArgs["Request"] == 'CreatorAccessApproved'){
  212.             if ($scope.company.CreatorLicenses <= $scope.countDict.Creator==true){
  213.                 Materialize.toast(gettextCatalog.getString('Creator License exceeded'), 4000, 'red');
  214.                 return;
  215.             }
  216.         }
  217.  
  218.         var param = action;
  219.         param['Id'] = usrId;
  220.         param['mail_args']=mailArgs
  221.         var data = {'user_list':JSON.stringify([param])}
  222.         $scope.batchUserUpdate(data, key=mailArgs["Request"], true);
  223.  
  224.     }
  225.  
  226.     $scope.isUsrLicnseExcds = function(){
  227.         /*
  228.             * returns true if license exceeded with user selection
  229.             * return false if license is not exceeded then can proceed activation
  230.         */
  231.         return $scope.company.UserLicenses+$scope.company.CreatorLicenses < $scope.countDict.User+$scope.usrIds.length+$scope.countDict.Creator;
  232.     }
  233.  
  234.     $scope.isCrtrLicnseExcds = function(){
  235.         /*
  236.             * returns true if license exceeded with creator selection
  237.             * return false if license is not exceeded then can proceed creator activation
  238.         */
  239.         return $scope.company.CreatorLicenses < $scope.countDict.Creator+$scope.usrIds.length;
  240.     }
  241.  
  242.     $scope.updatePermission = function(perm){
  243.         var userList = [];
  244.         for (var ids in $scope.usrIds){
  245.             var param = {};
  246.             param['Id'] = $scope.usrIds[ids];
  247.             param['RequestCreator'] = perm!='User'?'Approved':'Reject';
  248.             param['IsAdmin'] = perm=='Admin'?'1':'0';
  249.             userList.push(param);
  250.         }
  251.         var data = {'user_list':JSON.stringify(userList)}
  252.         $scope.batchUserUpdate(data, key="User permission updated", true);
  253.  
  254.     }
  255.  
  256.     $scope.importUsers = function(){
  257.         blockUI.start();
  258.         blockUI.message('Importing Users');
  259.  
  260.         var uploadOptions = {
  261.             "managerValidation": false,
  262.             'groupImport': false,
  263.             "deptValidation": false,
  264.             "locValidation": false,
  265.             "removeNonExist": false
  266.         };
  267.  
  268.         var options = {
  269.             url: "/" + _APPLICATION_ID + "/1/admin/import/users",
  270.             "dataType": "json",
  271.             "fileElementId": 'userCSV',
  272.             "data": uploadOptions,
  273.             "success": function (result, status) {
  274.                 if (result["success"]) {
  275.  
  276.                     Materialize.toast(gettextCatalog.getString('Users Imported Successfully'), 4000, 'green');
  277.  
  278.                     $scope.loadUsersCount();
  279.  
  280.                     if(result["data"]["Manager"]==true||result["data"]["Department"]||result["data"]["Location"]){
  281.                         $scope.triggerValidation(result["data"]);
  282.  
  283.                     }else{
  284.                         /*Materialize.toast(result['success'], 4000, 'green');*/
  285.                         $scope.showMenu('Inactive Users');
  286.                     }
  287.                     blockUI.stop();
  288.                 }else if (result["error"]) {
  289.                     Materialize.toast(result["error"], 4000, "red");
  290.                     $scope.$apply(function(){blockUI.stop();});
  291.                 }else if(result["Error"]){
  292.                     Materialize.toast(result["ErrorMessage"], 4000, "red");
  293.                     $scope.$apply(function(){blockUI.stop();});
  294.                 }
  295.             },
  296.             "error": function (xmlr, status, e) {
  297.                 if(xmlr.responseText.search('success')>-1){
  298.                     Materialize.toast(gettextCatalog.getString('Import Completed'), 4000, 'green');
  299.                     blockUI.stop();
  300.                 }else{
  301.                     Materialize.toast(gettextCatalog.getString("The import is failed, please contact support"), 4000, 'red');
  302.                     Materialize.toast(xmlr.responseText, 4000, 'red');
  303.                     blockUI.stop();
  304.                 }
  305.             }
  306.         };
  307.  
  308.         $.ajaxFileUpload(options);
  309.     }
  310.  
  311.     $scope.triggerValidation = function(chkValue){
  312.             $('.collapsible').collapsible();
  313.             $('#ImportValidation').show();
  314.             if(chkValue["Department"] == true){
  315.                     $('#validate-Department').css('display', 'inline');
  316.                     $scope.validateNonExist({"managerValidation": false, "groupImport":false,
  317.                         "deptValidation": true, "locValidation": false, "removeNonExist":false }, 'Department');
  318.             }else{
  319.                 $('#collapse-Department .progress').hide();
  320.                 $('#validate-Department').html('No Errors found in Department');
  321.             }
  322.             if(chkValue["Manager"] == true){
  323.                     $('#validate-Manager').css('display', 'inline');
  324.                     $scope.validateNonExist({"managerValidation": true, "groupImport":false,
  325.                         "deptValidation": false, "locValidation": false, "removeNonExist":false} , 'Manager');
  326.             }else{
  327.                 $('#collapse-Manager .progress').hide();
  328.                 $('#validate-Manager').html('No Errors found in Manager');
  329.             }
  330.             if(chkValue["Location"] == true){
  331.                     $('#validate-Location').css('display', 'inline');
  332.                     $scope.validateNonExist({"managerValidation": false, "groupImport":false,
  333.                         "deptValidation": false, "locValidation": true, "removeNonExist":false }, 'Location');
  334.             }else{
  335.                 $('#collapse-Location .progress').hide();
  336.                 $('#validate-Location').html('No Errors found in Location');
  337.             }
  338.     }
  339.  
  340.     $scope.validateNonExist = function(dataValue, alias){
  341.         adminService.importUsers(dataValue)
  342.             .success(function(result){
  343.                 if(result["success"]) {
  344.                     $('#validate-'+alias).html(alias+" Errors");
  345.                     $('#collapse-'+alias+' .progress').hide();
  346.                     $('#validate-'+alias).css("display","block");
  347.                     $.each(result['data'], function (index, value) {
  348.                        $('#collapse-'+alias).append("<li style='margin-left: 20px;padding:5px'>The "+alias+" '"+ value[alias.toLowerCase()] + "' for '" +
  349.                            value['userid'] + "' not exist in system</li>");
  350.                     });
  351.                     if ($('#collapse-'+alias).children().length == 0){
  352.                         $('#validate-'+alias).html(getI18NMsg('No Errors found in', 'Admin')+' '+getI18NMsg(alias, 'Admin'));
  353.                     }
  354.                     }else if(result["error"]){
  355.                         Materialize.toast(gettextCatalog.getString("Validating imported data failed, please contact support"), 4000, 'red');
  356.                     }
  357.             })
  358.             .error(function(data){
  359.                 Materialize.toast(gettextCatalog.getString('Unable to validate Imported data in KiSSFLOW. Please contact support.'), 4000, 'red');
  360.             });
  361.     }
  362.  
  363.     $scope.removeNonExist = function(){
  364.         var dataValue = {"managerValidation": false, "groupImport":false, "deptValidation": false, "locValidation": false, "removeNonExist":true}
  365.  
  366.         adminService.importUsers(dataValue)
  367.             .success(function(result){
  368.                 if(result["success"]) {
  369.                     Materialize.toast(gettextCatalog.getString('Erorr Data Removed, Redirected to Inactive user\'s list'), 4000, 'green');
  370.                     $scope.showMenu('Inactive Users');
  371.                 }
  372.             })
  373.             .error(function(data){
  374.                 Materialize.toast(gettextCatalog.getString('Unable to validate Imported data in KiSSFLOW. Please contact support.'), 4000, 'red');
  375.             });
  376.     }
  377.  
  378.     $scope.getNextUsers = function(menuName){
  379.         /*console.info(menuName);*/
  380.         if($('.admin-list-container').scrollTop() + $('.admin-list-container').innerHeight() >= $('.admin-list-container')[0].scrollHeight) {
  381.             var to = $scope.totalCount<$scope.PAGE_SIZE*$scope.pageNumber?$scope.totalCount:$scope.PAGE_SIZE*$scope.pageNumber;
  382.             if (to<$scope.totalCount){
  383.                 $scope.pageNumber+=1;
  384.                 $scope.listUsers($scope.menuName.split(' ')[0], $scope.pageNumber, $scope.search.data);
  385.             }
  386.         }
  387.     }
  388.  
  389.     $scope.searchUsers = function(searchTxt){
  390.         $scope.userList = [];
  391.         $scope.usrIds = [];
  392.         if($scope.pageType=='Inactive')
  393.             $scope.search.selected_user_type='1';
  394.         $scope.listUsers($scope.pageType, 1, searchTxt, $scope.search.selected_user_type);        
  395.     }
  396.  
  397.     $scope.showSearchInput =function() {
  398.         $('.searchWrapper .searchInput').toggleClass('selectedtext');
  399.         if($('.searchWrapper i').hasClass('icons8-Delete')){
  400.             $('#search-admin-data').val('');
  401.             $scope.searchUsers();
  402.         }
  403.         $('.searchWrapper i').toggleClass('icons8-Delete');
  404.         $('.searchWrapper i').toggleClass('icons8-Search');
  405.         $('.searchWrapper input').focus();
  406.     }
  407.  
  408.     $scope.updateToWizard = function(updateList,key) {
  409.         var user_list =JSON.parse(updateList["user_list"]);
  410.  
  411.         /*Checking to send activation mail or not
  412.         * If needs to send getting activation mail content*/
  413.         var preference = JSON.parse(_COMPANY.Preference || "{}");
  414.         var sendActivationMail = false;
  415.         var activationMailContent = '';
  416.  
  417.         if('ActivateAccount' in preference){
  418.             adminService.sendMailOnActivation(data)
  419.             .success(function(data) {
  420.                 sendActivationMail = true;
  421.                 activationMailContent = data;
  422.             })
  423.             .error(function(xmlr, status, err){
  424.                 blockUI.stop();
  425.                 Materialize.toast(gettextCatalog.getString("Update user is failed, please contact support"), 4000, 'red');
  426.             });
  427.         }
  428.  
  429.         for(var usr in user_list){
  430.             /*console.info(user_list[usr]);*/
  431.             var usr = user_list[usr];
  432.             var user_type = usr["IsAdmin"] >= 1?4:usr["RequestCreator"] == "Approved"?3:usr["RequestUser"] == "Approved"?2:1
  433.             blockUI.start();
  434.  
  435.             var data = {'email_id': usr.Id, 'app_id': _APPLICATION_ID,
  436.                         'invited_by': _USER_ID, 'UserType': user_type, 'wiz_host': _WIZ_HOST}
  437.  
  438.             if(sendActivationMail == true){
  439.                 data['ActivationMail'] = true;
  440.                 data['activationMailContent'] = activationMailContent;
  441.             }
  442.  
  443.             adminService.addUserAccount(data)
  444.                 .success(function(dataObj){
  445.                     if(key=="User activated")
  446.  
  447.                     if(dataObj["textStatus"]==500 && dataObj["errorThrown"]){
  448.                         Materialize.toast(gettextCatalog.getString("Something went wrong, please refresh the page and try again, " +
  449.                             "if the problem still persist,please conatct support"), 4000, 'red');
  450.                     }
  451.                     if (dataObj["errorMessage"]){
  452.                         var msg = $(dataObj["errorMessage"].replace(/\&lt;/g, '<').replace(/\&gt;/g, '>')).text();
  453.                         Materialize.toast(msg,6000,'red');
  454.                     }else{
  455.                         blockUI.stop();
  456.  
  457.                     }
  458.                 })
  459.                 .error(function(xmlr, status, err){
  460.                     blockUI.stop();
  461.                     Materialize.toast(gettextCatalog.getString("Update user is failed, please contact support"), 4000, 'red');
  462.                 });
  463.  
  464.         }
  465.     }
  466.  
  467.     $scope.transferPrivilege = function(val){
  468.         if(val==_USER_ID){
  469.             Materialize.toast(gettextCatalog.getString('You cannot transfer to the same user'), 4000, 'red');
  470.             return;
  471.         }
  472.         $('#transferAdminModal').openModal();
  473.     }
  474.  
  475.     $scope.doTransfer = function(){
  476.         var val = $('#transfer_privilege').val();
  477.         $('#transferAdminModal').closeModal();
  478.         $scope.saveNewAdminToWizard(val, val);
  479.     }
  480.  
  481.  
  482.     $scope.saveNewAdminToWizard = function(user_id, full_name){
  483.         /******************** Updating the UserType in Wizard****************/
  484.         /******************** Sharing the process in Wizard****************/
  485.         blockUI.start();
  486.  
  487.         var data = {'invited_by':_USER_ID, 'email_id':_USER_ID,'new_admin':user_id,'app_id':_APPLICATION_ID, 'new_admin_name':full_name}
  488.  
  489.         adminService.transferAccount(data)
  490.             .success(function(dataObj){
  491.                 if(dataObj["textStatus"]==500 && dataObj["errorThrown"])
  492.                     {
  493.                         Materialize.toast(gettextCatalog.getString("Something went wrong, please refresh the page and try again, " +
  494.                             "if the problem still persist,please conatct support"), 4000, "red");
  495.                         return;
  496.                     }
  497.                     if (dataObj["errorMessage"])
  498.                     {
  499.                         Materialize.toast(dataObj["errorMessage"], 4000, "red");
  500.                         return;
  501.                     }else{
  502.                         blockUI.stop();
  503.                         $scope.removeUpdateAdmin(user_id);
  504.                         Materialize.toast(gettextCatalog.getString('Selected user updated as Admin'), 4000, 'green');
  505.                     }
  506.             })
  507.             .error(function(xmlr, status, err){
  508.                 blockUI.stop();
  509.                 Materialize.toast(gettextCatalog.getString('Unable to change admin privileges in KiSSFLOW. Please contact support.'), 4000, 'red');
  510.             });
  511.  
  512.     }
  513.  
  514.     $scope.removeUpdateAdmin = function(newAdmin){
  515.         var userList = [{"Id":newAdmin, "IsAdmin":1, "RequestCreator":"Approved"}];
  516.         var dict = {};
  517.         dict['IsAdmin']=0;
  518.         dict['RequestUser']='Approved';
  519.         dict['RequestCreator']='Reject';
  520.         dict["Id"] = _USER_ID;
  521.         userList.push(dict)
  522.         var data = {'user_list':JSON.stringify(userList)}
  523.         $scope.batchUserUpdate(data, key="New admin added and Current admin privileges removed", true);
  524.         $window.location.reload();
  525.     }
  526.  
  527.  
  528.     $scope.resetPassword = function(){
  529.        for (var ids in $scope.usrIds) {
  530.            var data = {"emailId": $scope.usrIds[ids]};
  531.            adminService.resetPassword(data)
  532.                .success(function (data) {
  533.                    Materialize.toast(gettextCatalog.getString('Reset password mail sent'), 4000, 'green');
  534.                })
  535.                .error(function (xmlr, status, err) {
  536.                    Materialize.toast(gettextCatalog.getString('Unable to send reset password mail'), 4000, 'red');
  537.                });
  538.        }
  539.     }
  540.  
  541.  
  542. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement