All files / src/flow/nodes split_by_groups.ts

90.29% Statements 121/134
69.23% Branches 18/26
50% Functions 1/2
90.29% Lines 121/134

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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 13599x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x                           99x 99x 99x 99x 99x 3x 3x 2x 2x 3x 2x 2x 99x 99x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 99x 99x 99x 99x 99x 99x 99x 99x 103x 103x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 103x 103x 103x 103x 103x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x  
import { SPLIT_GROUPS, FormData, NodeConfig, FlowTypes } from '../types';
import { Node, Case } from '../../store/flow-definition.d';
import { generateUUID } from '../../utils';
import { validateWith } from '../utils';
import {
  resultNameField,
  nodeOptionsAccordionSimple,
  buildCategoriesExitsCases,
  appendOtherCategory
} from './shared';
 
export const split_by_groups: NodeConfig = {
  type: 'split_by_groups',
  name: 'Split by Group',
  group: SPLIT_GROUPS.split,
  flowTypes: [FlowTypes.VOICE, FlowTypes.MESSAGE, FlowTypes.BACKGROUND],
  form: {
    groups: {
      type: 'select',
      label: 'Groups',
      helpText:
        'Select the groups to split contacts by. Contacts will be routed based on their group membership.',
      required: true,
      options: [],
      multi: true,
      searchable: true,
      endpoint: '/api/v2/groups.json',
      valueKey: 'uuid',
      nameKey: 'name',
      placeholder: 'Search for groups...',
      allowCreate: true,
      createArbitraryOption: (input: string, options: any[]) => {
        // Check if a group with this name already exists
        const existing = options.find(
          (option) =>
            option.name.toLowerCase().trim() === input.toLowerCase().trim()
        );
        if (!existing && input.trim()) {
          return {
            name: input.trim(),
            arbitrary: true
          };
        }
        return null;
      }
    },
    result_name: resultNameField
  },
  layout: ['groups', nodeOptionsAccordionSimple],
  validate: validateWith((formData, errors) => {
    if (
      !formData.groups ||
      !Array.isArray(formData.groups) ||
      formData.groups.length === 0
    ) {
      errors.groups = 'At least one group is required';
    }
  }),
  toFormData: (node: Node) => {
    // Extract groups from the existing node structure
    const groups: { uuid: string; name: string }[] = [];
 
    if (node.router?.cases) {
      node.router.cases.forEach((c: Case) => {
        if (c.type === 'has_group' && c.arguments?.length >= 2) {
          groups.push({
            uuid: c.arguments[0],
            name: c.arguments[1]
          });
        }
      });
    }
 
    return {
      uuid: node.uuid,
      groups: groups,
      result_name: node.router?.result_name || ''
    };
  },
  fromFormData: (formData: FormData, originalNode: Node): Node => {
    const selectedGroups = (formData.groups || [])
      .filter((group: any) => group?.uuid || group?.arbitrary)
      .map((group: any) => ({
        uuid: group.uuid || generateUUID(),
        name: group.name
      }));
 
    const existingCategories = originalNode.router?.categories || [];
    const existingExits = originalNode.exits || [];
    const existingCases = originalNode.router?.cases || [];
 
    const { categories, exits, cases } = buildCategoriesExitsCases(
      selectedGroups.map((group) => ({
        name: group.name,
        case: {
          type: 'has_group',
          arguments: [group.uuid, group.name]
        }
      })),
      existingCategories,
      existingExits,
      existingCases
    );
 
    const defaultCategoryUuid = appendOtherCategory(
      categories,
      exits,
      existingCategories,
      existingExits,
      selectedGroups.map((g) => g.name)
    );
 
    return {
      uuid: originalNode.uuid,
      actions: originalNode.actions || [],
      router: {
        type: 'switch',
        cases,
        categories,
        default_category_uuid: defaultCategoryUuid,
        operand: '@contact.groups',
        result_name: formData.result_name || ''
      },
      exits
    };
  },
  router: {
    type: 'switch',
    operand: '@contact.groups'
  },
 
  // Localization support for categories
  localizable: 'categories'
};