summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/feature_flags/store/helpers.js
blob: 300709f2771403496fcf4ca2da4095c8920facd6 (plain)
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
import { ROLLOUT_STRATEGY_GITLAB_USER_LIST, NEW_VERSION_FLAG } from '../constants';

const mapStrategyScopesToRails = (scopes) =>
  scopes.length === 0
    ? [{ environment_scope: '*' }]
    : scopes.map((s) => ({
        id: s.id,
        _destroy: s.shouldBeDestroyed,
        environment_scope: s.environmentScope,
      }));

const mapStrategyScopesToView = (scopes) =>
  scopes.map((s) => ({
    id: s.id,
    // eslint-disable-next-line no-underscore-dangle
    shouldBeDestroyed: Boolean(s._destroy),
    environmentScope: s.environment_scope,
  }));

const mapStrategiesParametersToViewModel = (params) => {
  if (params.userIds) {
    return { ...params, userIds: params.userIds.split(',').join(', ') };
  }
  return params;
};

export const mapStrategiesToViewModel = (strategiesFromRails) =>
  (strategiesFromRails || []).map((s) => ({
    id: s.id,
    name: s.name,
    parameters: mapStrategiesParametersToViewModel(s.parameters),
    userList: s.user_list,
    // eslint-disable-next-line no-underscore-dangle
    shouldBeDestroyed: Boolean(s._destroy),
    scopes: mapStrategyScopesToView(s.scopes),
  }));

const mapStrategiesParametersToRails = (params) => {
  if (params.userIds) {
    return { ...params, userIds: params.userIds.replace(/\s*,\s*/g, ',') };
  }
  return params;
};

const mapStrategyToRails = (strategy) => {
  const mappedStrategy = {
    id: strategy.id,
    name: strategy.name,
    _destroy: strategy.shouldBeDestroyed,
    scopes_attributes: mapStrategyScopesToRails(strategy.scopes || []),
    parameters: mapStrategiesParametersToRails(strategy.parameters),
  };

  if (strategy.name === ROLLOUT_STRATEGY_GITLAB_USER_LIST) {
    mappedStrategy.user_list_id = strategy.userList.id;
  }
  return mappedStrategy;
};

export const mapStrategiesToRails = (params) => ({
  operations_feature_flag: {
    name: params.name,
    description: params.description,
    active: params.active,
    strategies_attributes: (params.strategies || []).map(mapStrategyToRails),
    version: NEW_VERSION_FLAG,
  },
});