theforeman/foreman_remote_execution

View on GitHub
webpack/JobWizard/steps/HostsAndInputs/buildHostQuery.js

Summary

Maintainability
A
45 mins
Test Coverage
export const buildHostQuery = (selected, search) => {
  const { hosts, hostCollections, hostGroups } = selected;
  const hostsSearch = `id ^ (${hosts.map(({ id }) => id).join(',')})`;
  const hostCollectionsSearch = `host_collection_id ^ (${hostCollections
    .map(({ id }) => id)
    .join(',')})`;
  const hostGroupsSearch = `hostgroup_id ^ (${hostGroups
    .map(({ id }) => id)
    .join(',')})`;
  const queryParts = [
    hosts.length ? hostsSearch : false,
    hostCollections.length ? hostCollectionsSearch : false,
    hostGroups.length ? hostGroupsSearch : false,
    search.length ? search : false,
  ].filter(Boolean);

  if (queryParts.length === 0) {
    return 'name=a AND name=b';
  }
  if (queryParts.length === 1) {
    return queryParts[0] || 'name=a AND name=b';
  }
  return queryParts.map(p => `(${p})`).join(' or ') || 'name=a AND name=b';
};