All files / src/features/experiments/utils experimentEndDateLimit.js

100% Statements 15/15
94.73% Branches 18/19
100% Functions 6/6
100% Lines 14/14

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    5x 5x     44x       21x       18x     29x                     359x 37x                     349x             344x 5x 5x 5x 5x    
import { ROLES } from '../../../constants/roles';
 
export const PROD_ENVS = ['PROD-MCOM', 'PROD-BCOM'];
export const MAX_DAYS_NON_ADMIN = 42;
 
export function isProdEnv(env) {
  return PROD_ENVS.includes(env);
}
 
export function isAdmin(userRoles) {
  return Array.isArray(userRoles) && userRoles.includes(ROLES.ADMIN);
}
 
export function hasFullyRampedTreatment(recipes) {
  return (
    Array.isArray(recipes)
    && recipes.some(
      (r) => r && r.recipeType === 'Treatment' && Number(r.weight) === 100,
    )
  );
}
 
export function shouldCapEndDate({
  env,
  userRoles,
  recipes,
  enabled = true,
} = {}) {
  if (enabled !== true) return false;
  return (
    isProdEnv(env) && !isAdmin(userRoles) && hasFullyRampedTreatment(recipes)
  );
}
 
export function getMaxEndDate(
  {
    env, userRoles, recipes, enabled = true,
  } = {},
  now = new Date(),
) {
  if (
    !shouldCapEndDate({
      env,
      userRoles,
      recipes,
      enabled,
    })
  ) return null;
  const max = new Date(now);
  max.setHours(0, 0, 0, 0);
  max.setDate(max.getDate() + MAX_DAYS_NON_ADMIN);
  return max;
}