All files / src/utils/conditionals handleRuleDefaults.ts

0% Statements 0/24
0% Branches 0/6
0% Functions 0/4
0% Lines 0/22

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                                                                                             
import { RuleType } from "../../components/common/Conditions/types/RuleTypes.ts";
import { additionalStateSelector } from "../../store/features/drawers/drawersSlice.ts";
import { currentPlaybookSelector } from "../../store/features/playbook/playbookSlice.ts";
import { store } from "../../store/index.ts";
import handleDefaults from "./defaults/handleDefaults.ts";
import { handleRelationRuleChange } from "./handleRelationRuleChange.ts";
import { bashCommandOutputOptions } from "./typeOptions/bash.ts";
import { RuleTypes } from "./types/ruleTypes.ts";
 
type ValueType = {
  key: string;
  value: any;
};
 
function handleRuleDefaults(rule: any, conditionIndex: number, condition: any) {
  const { id } = additionalStateSelector(store.getState());
  const currentPlaybook = currentPlaybookSelector(store.getState());
  const relations = currentPlaybook?.step_relations ?? [];
  const edgeIndex = relations?.findIndex((e) => e.id === id);
  let values: ValueType[] = [];
  const keyValue = condition?.type?.toLowerCase();
 
  const setValue = (key: string, value: any) => {
    handleRelationRuleChange(
      `${keyValue}.${key}`,
      value,
      edgeIndex,
      conditionIndex,
      RuleType.RULE,
    );
  };
 
  switch (rule.type) {
    case RuleTypes.GREP:
    case RuleTypes.NO_GREP:
    case RuleTypes.GREP_COUNT:
      values = handleDefaults(rule.type, bashCommandOutputOptions);
      break;
    default:
      return;
  }
 
  values.forEach((v) => setValue(v.key, v.value));
}
 
export default handleRuleDefaults;