All files / app/assets/javascripts/api groups_api.js

75.86% Statements 22/29
30% Branches 3/10
45.45% Functions 5/11
75.86% Lines 22/29

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        105x 105x 105x 105x 105x 105x   105x                                                                   1x   1x       1x   1x     105x 1x 1x   1x     105x 37x 37x   37x     105x 1x 1x    
import { DEFAULT_PER_PAGE } from '~/api';
import axios from '../lib/utils/axios_utils';
import { buildApiUrl } from './api_utils';
 
const GROUP_PATH = '/api/:version/groups/:id';
const GROUPS_PATH = '/api/:version/groups.json';
const GROUP_MEMBERS_PATH = '/api/:version/groups/:id/members';
const GROUP_ALL_MEMBERS_PATH = '/api/:version/groups/:id/members/all';
const DESCENDANT_GROUPS_PATH = '/api/:version/groups/:id/descendant_groups';
const GROUP_TRANSFER_LOCATIONS_PATH = 'api/:version/groups/:id/transfer_locations';
 
const axiosGet = (url, query, options, callback, axiosOptions = {}) => {
  return axios
    .get(url, {
      params: {
        search: query,
        per_page: DEFAULT_PER_PAGE,
        ...options,
      },
      ...axiosOptions,
    })
    .then(({ data, headers }) => {
      callback(data);
 
      return { data, headers };
    });
};
 
export function getGroups(query, options, callback = () => {}, axiosOptions = {}) {
  const url = buildApiUrl(GROUPS_PATH);
  return axiosGet(url, query, options, callback, axiosOptions);
}
 
export function getDescendentGroups(
  parentGroupId,
  query,
  options,
  callback = () => {},
  axiosOptions = {},
) {
  const url = buildApiUrl(DESCENDANT_GROUPS_PATH.replace(':id', parentGroupId));
  return axiosGet(url, query, options, callback, axiosOptions);
}
 
export function updateGroup(groupId, data = {}) {
  const url = buildApiUrl(GROUP_PATH).replace(':id', groupId);
 
  return axios.put(url, data);
}
 
export function deleteGroup(groupId) {
  const url = buildApiUrl(GROUP_PATH).replace(':id', groupId);
 
  return axios.delete(url);
}
 
export const getGroupTransferLocations = (groupId, params = {}) => {
  const url = buildApiUrl(GROUP_TRANSFER_LOCATIONS_PATH).replace(':id', groupId);
  const defaultParams = { per_page: DEFAULT_PER_PAGE };
 
  return axios.get(url, { params: { ...defaultParams, ...params } });
};
 
export const getGroupMembers = (groupId, inherited = false) => {
  const path = inherited ? GROUP_ALL_MEMBERS_PATH : GROUP_MEMBERS_PATH;
  const url = buildApiUrl(path).replace(':id', groupId);
 
  return axios.get(url);
};
 
export const createGroup = (params) => {
  const url = buildApiUrl(GROUPS_PATH);
  return axios.post(url, params);
};