All files / src/flow/nodes split_by_resthook.ts

98.36% Statements 120/122
89.18% Branches 33/37
100% Functions 3/3
98.36% Lines 120/122

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 12399x 99x 99x 1x 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   2x 1x 2x 2x 2x 1x 1x 2x 2x 1x 1x 2x 2x 2x 2x 99x 3x 3x 3x   3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 3x 3x 3x 99x 99x 99x 99x 99x 99x 99x 103x 103x 103x 99x 99x 99x 102x 102x 102x 99x 99x 99x 100x 100x 100x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 100x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x  
import { ACTION_GROUPS, FormData, NodeConfig, FlowTypes } from '../types';
import { CallResthook, Node } from '../../store/flow-definition';
import { generateUUID, createSuccessFailureRouter } from '../../utils';
import { html } from 'lit';
import { resultNameField } from './shared';
import { renderClamped, validateWith } from '../utils';
 
export const split_by_resthook: NodeConfig = {
  type: 'split_by_resthook',
  name: 'Call Resthook',
  group: ACTION_GROUPS.services,
  flowTypes: [FlowTypes.VOICE, FlowTypes.MESSAGE, FlowTypes.BACKGROUND],
  showAsAction: true,
  form: {
    resthook: {
      type: 'select',
      label: 'Resthook',
      required: true,
      searchable: true,
      clearable: false,
      placeholder: 'Select a resthook...',
      endpoint: '/api/v2/resthooks.json',
      valueKey: 'resthook',
      nameKey: 'resthook',
      helpText: 'Select the resthook to call'
    },
    result_name: resultNameField
  },
  layout: ['resthook', 'result_name'],
  validate: validateWith((formData, errors) => {
    if (!formData.resthook || formData.resthook.length === 0) {
      errors.resthook = 'A resthook is required';
    }
  }),
  render: (node: Node) => {
    const callResthookAction = node.actions?.find(
      (action) => action.type === 'call_resthook'
    ) as CallResthook;
    const resthook = callResthookAction?.resthook || 'Configure resthook';
    return html` <div class="body">${renderClamped(resthook, resthook)}</div> `;
  },
  toFormData: (node: Node) => {
    // extract data from the existing node structure
    const callResthookAction = node.actions?.find(
      (action) => action.type === 'call_resthook'
    ) as CallResthook;
 
    return {
      uuid: node.uuid,
      resthook: callResthookAction?.resthook
        ? [
            {
              resthook: callResthookAction.resthook
            }
          ]
        : [],
      result_name: node.router?.result_name || ''
    };
  },
  fromFormData: (formData: FormData, originalNode: Node): Node => {
    // get resthook selection
    const resthookSelection =
      Array.isArray(formData.resthook) && formData.resthook.length > 0
        ? formData.resthook[0]
        : null;
 
    if (!resthookSelection) {
      return originalNode;
    }
 
    // find existing call_resthook action to preserve its UUID
    const existingCallResthookAction = originalNode.actions?.find(
      (action) => action.type === 'call_resthook'
    );
    const callResthookUuid = existingCallResthookAction?.uuid || generateUUID();
 
    // create call_resthook action
    const callResthookAction: CallResthook = {
      type: 'call_resthook',
      uuid: callResthookUuid,
      resthook: resthookSelection.resthook
    };
 
    // create categories and exits for Success and Failure
    const existingCategories = originalNode.router?.categories || [];
    const existingExits = originalNode.exits || [];
    const existingCases = originalNode.router?.cases || [];
 
    const { router, exits } = createSuccessFailureRouter(
      '@webhook.json.status',
      {
        type: 'has_text',
        arguments: []
      },
      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 the complete node
    return {
      uuid: originalNode.uuid,
      actions: [callResthookAction],
      router: finalRouter,
      exits: exits
    };
  },
 
  // Localization support for categories
  localizable: 'categories',
  nonTranslatableCategories: 'all'
};