All files / src/utils environmentHelper.js

95.29% Statements 81/85
93.18% Branches 82/88
100% Functions 13/13
98.76% Lines 80/81

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233      19x     9x 9x 9x             332x 332x       160x 160x         4x 1x     3x 3x       416x 1x     415x   380x 329x 328x       6x   322x     49x 18x     31x 7x     24x 3x     21x           14x     7x       1x       4x   4x 2x           2x         2x 1x     2x       66x   66x 2x     64x 63x     1x       4x   4x 2x                           2x                         2x 1x             2x       18x   18x 5x     13x 5x             8x 5x           3x       29x 29x   29x 13x   16x 8x     8x 4x             4x   8x 6x     16x 10x     16x       47x   47x 40x     7x 1x     6x 5x     1x    
import { ROLES } from '../constants/roles';
import { ENVIRONMENTS } from '../store/types';
 
const ENV_TYPE_OVERRIDE_KEY = 'DEV_ENV_TYPE_OVERRIDE';
 
export function setEnvironmentTypeOverride(envType) {
  Iif (typeof window === 'undefined') return;
  if (envType) {
    window.localStorage.setItem(ENV_TYPE_OVERRIDE_KEY, envType);
  } else E{
    window.localStorage.removeItem(ENV_TYPE_OVERRIDE_KEY);
  }
}
 
export function getEnvironmentTypeOverride() {
  Iif (typeof window === 'undefined') return null;
  return window.localStorage.getItem(ENV_TYPE_OVERRIDE_KEY);
}
 
export function clearEnvironmentTypeOverride() {
  Iif (typeof window === 'undefined') return;
  window.localStorage.removeItem(ENV_TYPE_OVERRIDE_KEY);
}
 
export function isLocalEnvironment() {
  // SSR safety check
  if (typeof window === 'undefined') {
    return false;
  }
 
  const { origin } = window.location;
  return origin.includes('localhost') || origin.includes('127.0.0.1');
}
 
export function getEnvironmentType() {
  if (typeof window === 'undefined') {
    return 'unknown';
  }
 
  const { origin } = window.location;
 
  if (origin.includes('localhost') || origin.includes('127.0.0.1')) {
    const override = getEnvironmentTypeOverride();
    if (
      override
      && ['local', 'staging', 'perf', 'production'].includes(override)
    ) {
      return override;
    }
    return 'local';
  }
 
  if (origin.includes('staging.devops.fds.com')) {
    return 'staging';
  }
 
  if (origin.includes('perf.devops.fds.com')) {
    return 'perf';
  }
 
  if (origin.includes('.cloudrts.net')) {
    return 'production';
  }
 
  if (
    origin.includes('.fds.com')
    && !origin.includes('staging')
    && !origin.includes('perf')
    && !origin.includes('localhost')
  ) {
    return 'production';
  }
 
  return 'unknown';
}
 
export function getAvailableEnvironmentTypes() {
  return ['local', 'staging', 'perf', 'production'];
}
 
export function getAvailableEnvironmentOptions() {
  const envType = getEnvironmentType();
 
  if (envType === 'production' || envType === 'perf') {
    return [
      { displayValue: 'PROD BCOM', value: ENVIRONMENTS.PROD_BCOM },
      { displayValue: 'PROD MCOM', value: ENVIRONMENTS.PROD_MCOM },
    ];
  }
 
  const options = [
    { displayValue: 'QA BCOM', value: ENVIRONMENTS.QA_BCOM },
    { displayValue: 'QA MCOM', value: ENVIRONMENTS.QA_MCOM },
  ];
 
  if (envType === 'local') {
    options.unshift({ displayValue: 'Local', value: ENVIRONMENTS.LOCAL });
  }
 
  return options;
}
 
export function getDefaultEnvironment() {
  const envType = getEnvironmentType();
 
  if (envType === 'production' || envType === 'perf') {
    return ENVIRONMENTS.PROD_BCOM;
  }
 
  if (envType === 'local') {
    return ENVIRONMENTS.LOCAL;
  }
 
  return ENVIRONMENTS.QA_BCOM;
}
 
export function getEnvironmentTileOptions() {
  const envType = getEnvironmentType();
 
  if (envType === 'production' || envType === 'perf') {
    return [
      {
        id: ENVIRONMENTS.PROD_BCOM,
        text: ENVIRONMENTS.PROD_BCOM,
        subText: 'Bloom Production Environment',
      },
      {
        id: ENVIRONMENTS.PROD_MCOM,
        text: ENVIRONMENTS.PROD_MCOM,
        subText: "Macy's Production Environment",
      },
    ];
  }
 
  const options = [
    {
      id: ENVIRONMENTS.QA_BCOM,
      text: ENVIRONMENTS.QA_BCOM,
      subText: 'Bloom Environment',
    },
    {
      id: ENVIRONMENTS.QA_MCOM,
      text: ENVIRONMENTS.QA_MCOM,
      subText: "Macy's Environment",
    },
  ];
 
  if (envType === 'local') {
    options.push({
      id: ENVIRONMENTS.LOCAL,
      text: ENVIRONMENTS.LOCAL,
      subText: 'Local Development',
    });
  }
 
  return options;
}
 
export function canCopyExperiment(userRoles = []) {
  const envType = getEnvironmentType();
 
  if (envType === 'local') {
    return true;
  }
 
  if (envType === 'staging') {
    return (
      userRoles.includes(ROLES.ADMIN)
      || userRoles.includes(ROLES.RELEASE_ENGINEER)
      || userRoles.includes(ROLES.PRODUCT_MANAGER)
    );
  }
 
  if (envType === 'production') {
    return (
      userRoles.includes(ROLES.ADMIN)
      || userRoles.includes(ROLES.RELEASE_ENGINEER)
    );
  }
 
  return false;
}
 
export function getCopyTargetEnvironments(userRoles = [], sourceEnv = null) {
  const envType = getEnvironmentType();
  let allowedEnvs = [];
 
  if (envType === 'local') {
    return [ENVIRONMENTS.LOCAL];
  }
  if (envType === 'staging') {
    const isAdminOrRE = userRoles.includes(ROLES.ADMIN)
      || userRoles.includes(ROLES.RELEASE_ENGINEER);
 
    if (isAdminOrRE) {
      allowedEnvs = [
        ENVIRONMENTS.QA_BCOM,
        ENVIRONMENTS.QA_MCOM,
        ENVIRONMENTS.PROD_BCOM,
        ENVIRONMENTS.PROD_MCOM,
      ];
    } else {
      allowedEnvs = [ENVIRONMENTS.QA_BCOM, ENVIRONMENTS.QA_MCOM];
    }
  } else if (envType === 'perf' || envType === 'production') {
    allowedEnvs = [ENVIRONMENTS.PROD_BCOM, ENVIRONMENTS.PROD_MCOM];
  }
 
  if (sourceEnv) {
    allowedEnvs = allowedEnvs.filter((env) => env !== sourceEnv);
  }
 
  return allowedEnvs;
}
 
export function getCreateEnvironments() {
  const envType = getEnvironmentType();
 
  if (envType === 'local') {
    return [ENVIRONMENTS.LOCAL, ENVIRONMENTS.QA_BCOM, ENVIRONMENTS.QA_MCOM];
  }
 
  if (envType === 'staging') {
    return [ENVIRONMENTS.QA_BCOM, ENVIRONMENTS.QA_MCOM];
  }
 
  if (envType === 'perf' || envType === 'production') {
    return [ENVIRONMENTS.PROD_BCOM, ENVIRONMENTS.PROD_MCOM];
  }
 
  return [ENVIRONMENTS.QA_BCOM, ENVIRONMENTS.QA_MCOM];
}