All files / src/flow/nodes wait_for_menu.ts

97.77% Statements 220/225
71.73% Branches 33/46
66.66% Functions 2/3
97.77% Lines 220/225

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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 1431x 1431x 1431x 1431x 132x 132x 132x 132x 132x 132x 132x 132x 1320x 1320x 1320x 1320x 1320x 1320x 1320x 1320x 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 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 11x 11x 11x 11x 21x 21x 11x 11x 11x 11x 11x 5x 5x 5x 5x 5x 5x 5x 5x 5x   132x 132x         132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 181x 181x 141x 141x 141x 140x 140x 140x 140x 136x 136x 136x 136x 140x 136x 136x 136x 136x 136x 131x 131x 131x 131x 140x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 181x 141x 141x 140x 140x 140x 140x 140x 141x 141x 141x 181x 181x 181x 181x 181x 181x 132x 132x 132x 132x 132x 132x 132x 133x 133x 138x 138x 138x 133x 133x 131x 131x 131x 133x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 132x 132x 132x 132x 132x 132x 132x  
import { SPLIT_GROUPS, FormData, NodeConfig, FlowTypes } from '../types';
import { Node, Category, Exit } from '../../store/flow-definition';
import { generateUUID } from '../../utils';
import {
  resultNameField,
  localizeRulesField,
  localizeCategoriesField,
  nodeOptionsAccordion
} from './shared';
 
// Menu digits in display order: 1-9 then 0
const MENU_DIGITS = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
 
function digitFieldKey(digit: string): string {
  return `digit_${digit}`;
}
 
export const wait_for_menu: NodeConfig = {
  type: 'wait_for_menu',
  name: 'Wait for Menu',
  group: SPLIT_GROUPS.wait,
  flowTypes: [FlowTypes.VOICE],
  form: {
    ...Object.fromEntries(
      MENU_DIGITS.map((digit) => [
        digitFieldKey(digit),
        {
          type: 'text' as const,
          required: false,
          placeholder: '',
          flavor: 'xsmall' as const
        }
      ])
    ),
    result_name: resultNameField,
    localizeRules: localizeRulesField,
    localizeCategories: localizeCategoriesField
  },
  layout: [
    {
      type: 'row' as const,
      items: ['digit_1', 'digit_2', 'digit_3'],
      gap: '2rem',
      marginBottom: '0.5rem',
      inlineLabels: { digit_1: '1', digit_2: '2', digit_3: '3' }
    },
    {
      type: 'row' as const,
      items: ['digit_4', 'digit_5', 'digit_6'],
      gap: '2rem',
      marginBottom: '0.5rem',
      inlineLabels: { digit_4: '4', digit_5: '5', digit_6: '6' }
    },
    {
      type: 'row' as const,
      items: ['digit_7', 'digit_8', 'digit_9'],
      gap: '2rem',
      marginBottom: '0.5rem',
      inlineLabels: { digit_7: '7', digit_8: '8', digit_9: '9' }
    },
    {
      type: 'row' as const,
      items: [
        { type: 'spacer' as const },
        'digit_0',
        { type: 'spacer' as const }
      ],
      gap: '2rem',
      inlineLabels: { digit_0: '0' }
    },
    nodeOptionsAccordion
  ],
  toFormData: (node: Node, nodeUI?: any) => {
    const formData: FormData = {
      uuid: node.uuid,
      result_name: node.router?.result_name || ''
    };
 
    // Initialize all digit fields as empty
    for (const digit of MENU_DIGITS) {
      formData[digitFieldKey(digit)] = '';
    }
 
    // Fill in category names from cases
    if (node.router?.cases && node.router?.categories) {
      for (const case_ of node.router.cases) {
        if (case_.type === 'has_number_eq' && case_.arguments?.[0]) {
          const digit = case_.arguments[0];
          const category = node.router.categories.find(
            (cat: Category) => cat.uuid === case_.category_uuid
          );
          if (category && MENU_DIGITS.includes(digit)) {
            formData[digitFieldKey(digit)] = category.name;
          }
        }
      }
    }
 
    formData.localizeRules = nodeUI?.config?.localizeRules || false;
    formData.localizeCategories = nodeUI?.config?.localizeCategories || false;
    return formData;
  },
  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 => {
    const existingCategories = originalNode.router?.categories || [];
    const existingExits = originalNode.exits || [];
    const existingCases = originalNode.router?.cases || [];

    const categories: Category[] = [];
    const exits: Exit[] = [];
    const cases: any[] = [];
 
    // Build categories and cases for each filled digit
    for (const digit of MENU_DIGITS) {
      const categoryName = (formData[digitFieldKey(digit)] || '').trim();
      if (!categoryName) continue;
 
      // Check if a category with this name already exists in our new list
      let category = categories.find((c) => c.name === categoryName);
 
      if (!category) {
        // Try to find existing category with same name to preserve UUIDs
        const existingCat = existingCategories.find(
          (c: Category) => c.name === categoryName
        );
 
        if (existingCat) {
          category = existingCat;
          const existingExit = existingExits.find(
            (e: Exit) => e.uuid === existingCat.exit_uuid
          );
          categories.push(category);
          exits.push(
            existingExit || {
              uuid: existingCat.exit_uuid,
              destination_uuid: null
            }
          );
        } else {
          const exitUuid = generateUUID();
          category = {
            uuid: generateUUID(),
            name: categoryName,
            exit_uuid: exitUuid
          };
          categories.push(category);
          exits.push({ uuid: exitUuid, destination_uuid: null });
        }
      }
 
      // Find existing case for this digit to preserve UUID
      const existingCase = existingCases.find(
        (c: any) => c.type === 'has_number_eq' && c.arguments?.[0] === digit
      );
 
      cases.push({
        uuid: existingCase?.uuid || generateUUID(),
        type: 'has_number_eq',
        arguments: [digit],
        category_uuid: category.uuid
      });
    }
 
    // Add "Other" default category
    const existingOther = existingCategories.find(
      (c: Category) => c.name === 'Other'
    );
 
    let otherCategory: Category;
    if (existingOther) {
      otherCategory = existingOther;
      const existingExit = existingExits.find(
        (e: Exit) => e.uuid === existingOther.exit_uuid
      );
      exits.push(
        existingExit || {
          uuid: existingOther.exit_uuid,
          destination_uuid: null
        }
      );
    } else {
      const exitUuid = generateUUID();
      otherCategory = {
        uuid: generateUUID(),
        name: 'Other',
        exit_uuid: exitUuid
      };
      exits.push({ uuid: exitUuid, destination_uuid: null });
    }
    categories.push(otherCategory);
 
    const router: any = {
      type: 'switch',
      operand: '@input.text',
      default_category_uuid: otherCategory.uuid,
      cases,
      categories,
      wait: {
        type: 'msg',
        hint: {
          type: 'digits',
          count: 1
        }
      }
    };
 
    if (formData.result_name && formData.result_name.trim() !== '') {
      router.result_name = formData.result_name.trim();
    }
 
    return {
      ...originalNode,
      router,
      exits
    };
  },
  localizable: 'categories'
};