All files / src/flow/nodes split_by_ticket.ts

85.23% Statements 127/149
26.66% Branches 4/15
25% Functions 1/4
85.23% Lines 127/149

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 150133x 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 133x 133x 133x 133x 133x 133x 133x     133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x 133x                                       133x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 133x 133x 133x 133x 133x 132x 133x 133x 133x 133x 133x 133x 132x 132x 132x 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  
import { ACTION_GROUPS, FormData, NodeConfig, FlowTypes } from '../types';
import { Node, OpenTicket } from '../../store/flow-definition';
import { generateUUID, createSuccessFailureRouter } from '../../utils';
import { html } from 'lit';
 
export const split_by_ticket: NodeConfig = {
  type: 'split_by_ticket',
  name: 'Open Ticket',
  group: ACTION_GROUPS.trigger,
  flowTypes: [FlowTypes.VOICE, FlowTypes.MESSAGE, FlowTypes.BACKGROUND],
  showAsAction: true,
  form: {
    topic: {
      type: 'select',
      label: 'Topic',
      required: true,
      placeholder: 'Select a topic',
      options: [],
      endpoint: '/api/v2/topics.json',
      valueKey: 'uuid',
      nameKey: 'name',
      maxWidth: '200px'
    },
    assignee: {
      type: 'select',
      label: 'Assignee',
      required: false,
      placeholder: 'Select an agent (optional)',
      endpoint: '/api/v2/users.json',
      valueKey: 'uuid',
      getName: (item: {
        first_name?: string;
        last_name?: string;
        name?: string;
      }) => {
        return item.name || [item.first_name, item.last_name].join(' ');
      },
      clearable: true
    },
    note: {
      type: 'textarea',
      label: 'Note',
      required: false,
      evaluated: true,
      placeholder: 'Enter a note for the ticket (optional)',
      maxLength: 10000,
      minHeight: 100
    }
  },
  layout: [{ type: 'row', items: ['topic', 'assignee'] }, 'note'],
  render: (node: Node) => {
    const openTicketAction = node.actions?.find(
      (action) => action.type === 'open_ticket'
    ) as OpenTicket;
    return html`
      <div class="body">
        ${openTicketAction?.topic?.name || 'Configure ticket'}
      </div>
    `;
  },
  toFormData: (node: Node) => {
    // Extract data from the existing node structure
    const openTicketAction = node.actions?.find(
      (action) => action.type === 'open_ticket'
    ) as any;

    return {
      uuid: node.uuid,
      topic: openTicketAction?.topic
        ? [
            {
              uuid: openTicketAction.topic.uuid,
              name: openTicketAction.topic.name
            }
          ]
        : [],
      assignee: openTicketAction?.assignee
        ? [
            {
              uuid: openTicketAction.assignee.uuid,
              name: openTicketAction.assignee.name
            }
          ]
        : [],
      note: openTicketAction?.note || ''
    };
  },
  fromFormData: (formData: FormData, originalNode: Node): Node => {
    // Find existing open_ticket action to preserve its UUID
    const existingOpenTicketAction = originalNode.actions?.find(
      (action) => action.type === 'open_ticket'
    );
    const openTicketUuid = existingOpenTicketAction?.uuid || generateUUID();
 
    // Create open_ticket action
    const openTicketAction: OpenTicket = {
      type: 'open_ticket',
      uuid: openTicketUuid,
      topic:
        formData.topic && formData.topic.length > 0
          ? {
              uuid: formData.topic[0].uuid,
              name: formData.topic[0].name
            }
          : undefined,
      assignee:
        formData.assignee && formData.assignee.length > 0
          ? {
              uuid: formData.assignee[0].uuid,
              name:
                formData.assignee[0].name ||
                [
                  formData.assignee[0].first_name,
                  formData.assignee[0].last_name
                ].join(' ')
            }
          : undefined,
      note: formData.note || ''
    };
 
    // 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(
      '@locals._new_ticket',
      {
        type: 'has_text',
        arguments: []
      },
      existingCategories,
      existingExits,
      existingCases
    );
 
    // Return the complete node
    return {
      uuid: originalNode.uuid,
      actions: [openTicketAction],
      router: router,
      exits: exits
    };
  },
 
  // Localization support for categories
  localizable: 'categories',
  nonTranslatableCategories: 'all'
};