All files / ee/app/assets/javascripts/audit_events utils.js

100% Statements 37/37
100% Branches 16/16
100% Functions 17/17
100% Lines 33/33

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                    8x 6x 20x       8x 69x     8x             3x                 8x 19x 19x 19x   19x                       19x   2x 2x   1x 1x   16x     19x     8x 140x   140x   163x                   84x       8x 55x 55x   55x 5x     50x 28x 28x 28x        
import { parsePikadayDate, pikadayToString } from '~/lib/utils/datetime_utility';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import {
  AVAILABLE_TOKEN_TYPES,
  AUDIT_FILTER_CONFIGS,
  ENTITY_TYPES,
  createBlankHeader,
} from './constants';
import { parseUsername, displayUsername } from './token_utils';
 
export const getTypeFromEntityType = (entityType) => {
  return AUDIT_FILTER_CONFIGS.find(
    ({ entityType: configEntityType }) => configEntityType === entityType,
  )?.type;
};
 
export const getEntityTypeFromType = (type) => {
  return AUDIT_FILTER_CONFIGS.find(({ type: configType }) => configType === type)?.entityType;
};
 
export const parseAuditEventSearchQuery = ({
  created_after: createdAfter,
  created_before: createdBefore,
  entity_type: entityType,
  entity_username: entityUsername,
  author_username: authorUsername,
  ...restOfParams
}) => ({
  ...restOfParams,
  created_after: createdAfter ? parsePikadayDate(createdAfter) : null,
  created_before: createdBefore ? parsePikadayDate(createdBefore) : null,
  entity_type: getTypeFromEntityType(entityType),
  entity_username: displayUsername(entityUsername),
  author_username: displayUsername(authorUsername),
});
 
export const createAuditEventSearchQuery = ({ filterValue, startDate, endDate, sortBy }) => {
  const entityValue = filterValue.find((value) => AVAILABLE_TOKEN_TYPES.includes(value.type));
  const entityType = getEntityTypeFromType(entityValue?.type);
  const filterData = entityValue?.value.data;
 
  const params = {
    created_after: startDate ? pikadayToString(startDate) : null,
    created_before: endDate ? pikadayToString(endDate) : null,
    sort: sortBy,
    entity_type: entityType,
    entity_id: null,
    entity_username: null,
    author_username: null,
    // When changing the search parameters, we should be resetting to the first page
    page: null,
  };
 
  switch (entityType) {
    case ENTITY_TYPES.USER:
      params.entity_username = parseUsername(filterData);
      break;
    case ENTITY_TYPES.AUTHOR:
      params.author_username = parseUsername(filterData);
      break;
    default:
      params.entity_id = filterData;
  }
 
  return params;
};
 
export const mapItemHeadersToFormData = (item, settings = {}) => {
  const headers = item?.headers?.nodes || [];
 
  return (
    headers
      .map(({ id, key, value, active }) => ({
        ...createBlankHeader(),
        id,
        name: key,
        value,
        active,
        ...settings,
      }))
      // Sort the headers so they appear in the order they were created
      // The GraphQL endpoint returns them in the reverse order of this
      .sort((a, b) => getIdFromGraphQLId(a.id) - getIdFromGraphQLId(b.id))
  );
};
 
export const mapAllMutationErrors = (mutations, name) => {
  return Promise.allSettled(mutations).then((results) => {
    const rejected = results.filter((r) => r.status === 'rejected').map((r) => r.reason);
 
    if (rejected.length > 0) {
      throw rejected[0];
    }
 
    return results
      .filter((r) => r.status === 'fulfilled')
      .map((r) => r.value.data[name].errors)
      .reduce((r, errors) => r.concat(errors), [])
      .filter(Boolean);
  });
};