All files / src/flow/nodes wait_for_dial.ts

100% Statements 172/172
75.6% Branches 31/41
100% Functions 1/1
100% Lines 172/172

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 173133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 399x 399x 399x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 7x 7x 8x 8x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 132x 133x 133x 133x 133x 133x 152x 152x 162x 152x 144x 144x 144x 144x 144x 144x 144x 144x 132x 132x 152x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 133x 147x 147x 147x 147x 147x 147x 150x 150x 150x 150x 150x 150x 150x 147x 147x 147x 147x 147x 147x 147x 147x 147x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 134x 134x 134x 134x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x  
import { SPLIT_GROUPS, FormData, NodeConfig, FlowTypes } from '../types';
import { Node, Category, Exit } from '../../store/flow-definition';
import { generateUUID } from '../../utils';
import { resultNameField } from './shared';
 
const DIAL_CATEGORIES = ['Answered', 'No Answer', 'Busy', 'Failed'];
const DIAL_CASES = [
  { type: 'has_only_text', arguments: ['answered'], categoryName: 'Answered' },
  {
    type: 'has_only_text',
    arguments: ['no_answer'],
    categoryName: 'No Answer'
  },
  { type: 'has_only_text', arguments: ['busy'], categoryName: 'Busy' }
];
 
export const wait_for_dial: NodeConfig = {
  type: 'wait_for_dial',
  name: 'Redirect Call',
  group: SPLIT_GROUPS.wait,
  flowTypes: [FlowTypes.VOICE],
  router: {
    type: 'switch',
    defaultCategory: 'Failed',
    rules: DIAL_CASES.map((c) => ({
      type: c.type as any,
      arguments: c.arguments,
      categoryName: c.categoryName
    }))
  },
  form: {
    phone: {
      type: 'text',
      label: 'Phone Number',
      required: true,
      evaluated: true,
      maxLength: 1000,
      placeholder: 'Phone number or expression'
    },
    dial_limit_seconds: {
      type: 'text',
      label: 'Dial Limit (seconds)',
      required: false,
      placeholder: '60'
    },
    call_limit_seconds: {
      type: 'text',
      label: 'Call Limit (seconds)',
      required: false,
      placeholder: '7200'
    },
    result_name: resultNameField
  },
  layout: [
    'phone',
    {
      type: 'row',
      items: ['dial_limit_seconds', 'call_limit_seconds']
    },
    'result_name'
  ],
  toFormData: (node: Node) => {
    const wait = node.router?.wait;
    return {
      uuid: node.uuid,
      phone: wait?.phone || '',
      dial_limit_seconds: wait?.dial_limit_seconds
        ? String(wait.dial_limit_seconds)
        : '',
      call_limit_seconds: wait?.call_limit_seconds
        ? String(wait.call_limit_seconds)
        : '',
      result_name: node.router?.result_name || ''
    };
  },
  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 dial outcome
    for (const catName of DIAL_CATEGORIES) {
      const existing = existingCategories.find(
        (c: Category) => c.name === catName
      );
 
      if (existing) {
        categories.push(existing);
        const existingExit = existingExits.find(
          (e: Exit) => e.uuid === existing.exit_uuid
        );
        exits.push(
          existingExit || {
            uuid: existing.exit_uuid,
            destination_uuid: null
          }
        );
      } else {
        const exitUuid = generateUUID();
        categories.push({
          uuid: generateUUID(),
          name: catName,
          exit_uuid: exitUuid
        });
        exits.push({ uuid: exitUuid, destination_uuid: null });
      }
    }
 
    // Build cases for non-default categories
    for (const dialCase of DIAL_CASES) {
      const category = categories.find((c) => c.name === dialCase.categoryName);
      if (!category) continue;
 
      const existingCase = existingCases.find(
        (c: any) =>
          c.type === dialCase.type && c.arguments?.[0] === dialCase.arguments[0]
      );
 
      cases.push({
        uuid: existingCase?.uuid || generateUUID(),
        type: dialCase.type,
        arguments: dialCase.arguments,
        category_uuid: category.uuid
      });
    }
 
    const failedCategory = categories.find((c) => c.name === 'Failed');
 
    // Build wait config
    const phone = (formData.phone || '').trim();
    const dialLimit = parseInt(formData.dial_limit_seconds, 10);
    const callLimit = parseInt(formData.call_limit_seconds, 10);
 
    const waitConfig: any = {
      type: 'dial',
      phone
    };
 
    if (!isNaN(dialLimit) && dialLimit > 0) {
      waitConfig.dial_limit_seconds = dialLimit;
    }
 
    if (!isNaN(callLimit) && callLimit > 0) {
      waitConfig.call_limit_seconds = callLimit;
    }
 
    const router: any = {
      type: 'switch',
      operand: '@(default(resume.dial.status, ""))',
      default_category_uuid: failedCategory?.uuid,
      cases,
      categories,
      wait: waitConfig
    };
 
    if (formData.result_name && formData.result_name.trim() !== '') {
      router.result_name = formData.result_name.trim();
    }
 
    return {
      ...originalNode,
      router,
      exits
    };
  },
  localizable: 'categories',
  nonTranslatableCategories: 'all'
};