<filename>node_modules/apiconnect/node_modules/apim-ui/dist/apim/users/usersFilters.js
/********************************************************* {COPYRIGHT-TOP} ***
 * Licensed Materials - Property of IBM
 * 5725-Z22, 5725-Z63, 5725-U33, 5725-Z63
 *
 * (C) Copyright IBM Corporation 2015, 2017
 *
 * All Rights Reserved.
 * US Government Users Restricted Rights - Use, duplication or disclosure
 * restricted by GSA ADP Schedule Contract with IBM Corp.
 ********************************************************** {COPYRIGHT-END} **/
// Node module: apim-ui

'use strict';

/* Filters */

var users = angular.module('apim.users');

users.filter('contactSupport', function() {
  return function(user, sbsToken) {
    if (!window.isSaaS || !sbsToken) return "https://www.ibm.com/support/entry/portal/product/websphere/ibm_api_connect?productContext=567241173";
    if (sbsToken && sbsToken.billedEnvironment && sbsToken.billedEnvironment == "Bluemix") {
      return "https://www.ibm.com/mysupport/s/?language=en_US&login=bluemix";
    } else {
      if (sbsToken.partNumber && sbsToken.partNumber.toLowerCase().indexOf("trial") > 0) {
        return "https://developer.ibm.com/answers/questions/ask/?community=api&tags=apimsaas";
      } else if (sbsToken.url && sbsToken.url.toLowerCase().indexOf("ds1") > 0) {
        return "https://ibm-sandbox.parature.com/ics/support/mylogin.asp?deptID=5383&login=apimanagement";
      } else {
        return "https://www.ibm.com/mysupport/s/?language=en_US&login=apimanagement";
      }
    }
  };
});

users.filter('productDocumentation', ['$rootScope', function($rootScope) {
  return function(user, sbsToken) {
    // if we're in the CMC
    if(user.context == "cmc") return "http://www.ibm.com/support/knowledgecenter/SSMNED_5.0.0/com.ibm.apic.cmc.doc/con_cmc_overview.html";
    // else if we're SaaS
    if (window.isSaaS) return "http://www.ibm.com/support/knowledgecenter/SSFS6T/com.ibm.apic.apionprem.doc/APIonPrem_gettingstarted.html";
    // else if we're offline
    if ($rootScope.offlineMode) return "http://www.ibm.com/support/knowledgecenter/SSMNED_5.0.0/com.ibm.apic.toolkit.doc/capim_cli_overview.html";
    // else assume we're API Manager on-premise
    return "http://www.ibm.com/support/knowledgecenter/SSMNED_5.0.0/com.ibm.apic.apionprem.doc/APIonPrem_gettingstarted.html";
  };
}]);

users.filter('productForum', function() {
  return function(sbsToken) {
    // if we're SaaS
    if (window.isSaaS) return "https://developer.ibm.com/answers/tags/apimsaas/?community=api";
    // else assume we're API Manager on-premise
    return "https://developer.ibm.com/answers/smartspace/apimanagement/";
  };
});

users.filter('profileLabel', ['translateFilter', function(translateFilter) {
  return function(user) {
    if (user.idpSummary && user.idpSummary.capabilities) {
      if (!user.idpSummary.capabilities.updateProfile) return translateFilter('viewProfile');
    }
    return translateFilter('manageProfile');
  };
}]);

users.filter('userDisplayName', function() {
  return function(user) {
    if (!user) return "";
    if (user.firstName && user.lastName) return user.firstName + " " + user.lastName;
    return user.name;
  };
});

users.filter('notOwnerRole', function() {
  return function(roles) {
    if (!roles) return;
    return roles.filter(function(role) {
      return (role.name !== "owner" && role.name !== "cloudowner");
    });
  };
});

users.filter('usersQuery', function() {
  return function (users, query) {
    if(!users) return users;
    if(!query) return users;
    var term = query.toLowerCase();
    return users.filter(function(user) {
      return (user.user.name && user.user.name.toLowerCase().indexOf(term) > -1) ||
        (user.user.email && user.user.email.toLowerCase().indexOf(term) > -1) ||
        (user.user.firstName && user.user.firstName.toLowerCase().indexOf(term) > -1) ||
        (user.user.lastName && user.user.lastName.toLowerCase().indexOf(term) > -1) ||
        (user.user.status && user.user.status.toLowerCase().indexOf(term) > -1);
    });
  };
});

users.filter('lastLoginForUser', ['translateFilter', 'sinceFilter', function(translateFilter, sinceFilter) {
  return function(user) {
    if (user.lastLoginTime) return sinceFilter(user.lastLoginTime);
    if (user.status == "active") return translateFilter('userLoginNever');
    return translateFilter('pending');
  };
}]);

users.filter('usersByScope', function() {
  return function (users, scopeType) {
    if(!users) return users;
    if(!scopeType) return users;
    return users.filter(function(user) {
      if (!user.scope) return true;
      if (user.scope == scopeType) return true;
      return false;
    });
  };
});

users.filter('locationForUser', function() {
  return function(user, $scope, usersScope) {
    if (user.envId) {
      var len = $scope.environments.length;
      for (var i=0;i<len;i++) {
        var env = $scope.environments[i];
        if (env.id == user.envId) {
          return env.name;
        }
      } // end for
    }
    return $scope.orgName;
  };
});

users.filter('catalogNameFromChild', function() {
  return function(childMember, $scope) {
    if (childMember.envId) {
      var len = $scope.environments.length;
      for (var i=0;i<len;i++) {
        var env = $scope.environments[i];
        if (env.id == childMember.envId) {
          if (childMember.scope == "space") {
            // dive deeper to get space name
            var spaceLen = env.spaces.length;
            for (var j=0;j<spaceLen;j++) {
              var space = env.spaces[j];
              if (space.id == childMember.spaceId) {
                return space.title;
              }
            } // end for
          } else {
            return env.name;
          }
        }
      } // end for
    }
    return "";
  };
});

