All files / src/flow/nodes split_by_expression.ts

98.24% Statements 112/114
52.17% Branches 12/23
100% Functions 2/2
98.24% Lines 112/114

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 103 104 105 106 107 108 109 110 111 112 113 114 115132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 8x 1x 1x 132x 132x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 8x 14x 14x 14x 14x 14x 132x 132x 2x 2x 2x 2x 2x 2x 2x     132x 132x 132x 132x 132x 132x 131x 132x 132x 132x 132x 130x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x  
import { SPLIT_GROUPS, FormData, NodeConfig, FlowTypes } from '../types';
import { Node } from '../../store/flow-definition';
import { createRulesRouter } from '../../utils';
import {
  getWaitForResponseOperators,
  operatorsToSelectOptions,
  getOperatorConfig
} from '../operators';
import {
  resultNameField,
  localizeRulesField,
  localizeCategoriesField,
  nodeOptionsAccordion
} from './shared';
import {
  createRulesArrayConfig,
  extractUserRules,
  casesToFormRules
} from './shared-rules';
import { validateWith } from '../utils';
 
export const split_by_expression: NodeConfig = {
  type: 'split_by_expression',
  name: 'Split by Expression',
  group: SPLIT_GROUPS.split,
  flowTypes: [FlowTypes.VOICE, FlowTypes.MESSAGE, FlowTypes.BACKGROUND],
  dialogSize: 'large',
  form: {
    operand: {
      type: 'text',
      label: 'Expression',
      helpText: 'The expression to evaluate and split on',
      required: true,
      evaluated: true,
      maxLength: 10000,
      placeholder: '@fields.age'
    },
    rules: createRulesArrayConfig(
      operatorsToSelectOptions(getWaitForResponseOperators()),
      ''
    ),
    result_name: resultNameField,
    localizeRules: localizeRulesField,
    localizeCategories: localizeCategoriesField
  },
  layout: ['operand', 'rules', nodeOptionsAccordion],
  validate: validateWith((formData, errors) => {
    if (!formData.operand || formData.operand.trim() === '') {
      errors.operand = 'Expression is required';
    }
  }),
  toFormData: (node: Node, nodeUI?: any) => {
    // Extract rules from router cases using shared function
    const rules = casesToFormRules(node);
 
    return {
      uuid: node.uuid,
      operand: node.router?.operand || '@input.text',
      rules: rules,
      result_name: node.router?.result_name || '',
      localizeRules: nodeUI?.config?.localizeRules || false,
      localizeCategories: nodeUI?.config?.localizeCategories || false
    };
  },
  toUIConfig: (formData: FormData) => {
    const config: Record<string, any> = {};
    config.localizeRules = !!formData.localizeRules;
    config.localizeCategories = formData.result_name
      ? !!formData.localizeCategories
      : false;
    return config;
  },
  fromFormData: (formData: FormData, originalNode: Node): Node => {
    // Get user rules using shared extraction function
    const userRules = extractUserRules(formData);
 
    // Get operand from form data
    const operand = formData.operand?.trim() || '@input.text';

    // Get existing router data for preservation
    const existingCategories = originalNode.router?.categories || [];
    const existingExits = originalNode.exits || [];
    const existingCases = originalNode.router?.cases || [];
 
    // Create router and exits using existing data when possible
    const { router, exits } = createRulesRouter(
      operand,
      userRules,
      getOperatorConfig,
      existingCategories,
      existingExits,
      existingCases
    );
 
    // Build final router with result_name
    const finalRouter: any = {
      ...router
    };
 
    // Only set result_name if provided
    if (formData.result_name && formData.result_name.trim() !== '') {
      finalRouter.result_name = formData.result_name.trim();
    }
 
    return {
      ...originalNode,
      router: finalRouter,
      exits: exits
    };
  },
 
  // Localization support for categories
  localizable: 'categories'
};