All files / src/flow/actions start_session.ts

99.23% Statements 258/260
70% Branches 35/50
100% Functions 6/6
99.23% Lines 258/260

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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 26198x 5x 5x 5x 5x 5x 5x 5x 5x 5x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 14x 1x 1x 14x 14x 2x 1x 14x 12x 2x 2x 2x 2x 10x 10x 10x 20x 20x 20x 20x 10x 12x 12x 12x 12x 12x 12x 12x 10x 2x 2x 2x 2x 2x 2x 2x 10x 10x 1x 1x 1x 1x 1x 1x 14x 14x 1x 1x 1x 1x 1x 1x 14x 98x 22x 22x 22x 22x 19x 19x 19x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 31x 22x 22x 22x 22x 22x 22x 22x 18x 22x 30x 30x 30x 30x 30x 30x 30x 22x 22x 5x 5x 5x 22x 22x 22x 22x 22x 22x 22x   98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 14x 14x 14x   14x 14x 14x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 14x 14x 14x 14x 14x 14x 14x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 10x 10x 10x 10x 10x 10x 10x 6x 6x 6x 6x 6x 6x 10x 1x 10x 2x 1x 1x 1x 10x 10x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 99x 98x 98x 98x 98x 98x 97x 98x 98x 98x 98x  
import { html } from 'lit-html';
import {
  ActionConfig,
  ACTION_GROUPS,
  FormData,
  ValidationResult,
  FlowTypes
} from '../types';
import { Node, StartSession } from '../../store/flow-definition';
import {
  renderMixedList,
  renderFlowLinks,
  renderHighlightedText
} from '../utils';
import { Icon } from '../../Icons';
import { CustomEventType } from '../../interfaces';
 
export const start_session: ActionConfig = {
  name: 'Start Flow',
  group: ACTION_GROUPS.broadcast,
  flowTypes: [FlowTypes.VOICE, FlowTypes.MESSAGE, FlowTypes.BACKGROUND],
  render: (_node: Node, action: StartSession) => {
    let recipientsDisplay = html``;
    if (action.create_contact) {
      recipientsDisplay = html`Create a new contact`;
    } else if ((action as any).contact_query) {
      recipientsDisplay = html`${renderHighlightedText(
        (action as any).contact_query,
        true
      )}`;
    } else {
      const recipients = [
        ...(action.groups || []).map((g) => ({
          name: g.name,
          icon: Icon.group,
          uuid: g.uuid,
          eventType: CustomEventType.GroupClicked
        })),
        ...(action.contacts || []).map((c) => ({
          name: c.name,
          icon: Icon.contacts,
          uuid: c.uuid,
          eventType: CustomEventType.ContactClicked
        })),
        ...(action.legacy_vars || []).map((v) => ({
          name: v,
          icon: Icon.contacts,
          content: renderHighlightedText(v, true)
        }))
      ];
      recipientsDisplay = html`${renderMixedList(recipients)}`;
    }
 
    return html`
      <div>
        <div>${recipientsDisplay}</div>
        <div style="margin-top: 0.5em">
          ${renderFlowLinks([action.flow], 'flow')}
        </div>
      </div>
    `;
  },
 
  toFormData: (action: StartSession) => {
    const extendedAction = action as StartSession & {
      contact_query?: string;
      exclusions?: { in_a_flow?: boolean };
    };
 
    // Determine start type based on action properties
    let startTypeValue = 'manual';
    if (action.create_contact) {
      startTypeValue = 'create';
    } else if (extendedAction.contact_query) {
      startTypeValue = 'query';
    }
 
    // Map value to full option object for proper display
    const startTypeOptions = [
      { value: 'manual', name: 'Select recipients manually' },
      { value: 'query', name: 'Select a contact with a query' },
      { value: 'create', name: 'Create a new contact' }
    ];
    const startType =
      startTypeOptions.find((opt) => opt.value === startTypeValue) ||
      startTypeOptions[0];
 
    return {
      flow: action.flow ? [action.flow] : null,
      recipients: [
        ...(action.contacts || []).map((c) => ({
          id: c.uuid,
          name: c.name,
          type: 'contact'
        })),
        ...(action.groups || []).map((g) => ({
          id: g.uuid,
          name: g.name,
          type: 'group'
        })),
        ...(action.legacy_vars || []).map((v) => ({
          id: v,
          name: v,
          type: 'expression'
        }))
      ],
      startType: [startType],
      contactQuery: extendedAction.contact_query || '',
      skipContactsInFlow: extendedAction.exclusions?.in_a_flow || false,
      uuid: action.uuid
    };
  },
 
  form: {
    flow: {
      type: 'select',
      label: 'Flow',
      helpText: 'Select the flow to start',
      required: true,
      searchable: true,
      endpoint: '/api/v2/flows.json',
      valueKey: 'uuid',
      nameKey: 'name',
      placeholder: 'Select a flow...'
    },
    startType: {
      type: 'select',
      label: 'Start Type',
      helpText: 'How should contacts be selected?',
      required: true,
      options: [
        { value: 'manual', name: 'Select recipients manually' },
        { value: 'query', name: 'Select a contact with a query' },
        { value: 'create', name: 'Create a new contact' }
      ]
    },
    recipients: {
      type: 'select',
      label: 'Recipients',
      helpText: 'Select who should be started in the flow',
      multi: true,
      searchable: true,
      endpoint: '/contact/omnibox/?types=gc',
      queryParam: 'search',
      valueKey: 'id',
      nameKey: 'name',
      placeholder: 'Search for contacts or groups...',
      expressions: 'session',
      conditions: {
        visible: (formData: FormData) => {
          const startType = formData.startType?.[0]?.value;
          return startType === 'manual';
        }
      }
    },
    contactQuery: {
      type: 'text',
      evaluated: true,
      label: 'Contact Query',
      helpText: 'Only one matching contact will be started',
      placeholder: 'household_id = @fields.household_id',
      conditions: {
        visible: (formData: FormData) => {
          const startType = formData.startType?.[0]?.value;
          return startType === 'query';
        }
      }
    },
    skipContactsInFlow: {
      type: 'checkbox',
      label: 'Skip contacts currently in a flow',
      helpText: 'Avoid interrupting a contact who is already in a flow'
    }
  },
 
  layout: [
    'flow',
    'startType',
    'recipients',
    'contactQuery',
    'skipContactsInFlow'
  ],
 
  validate: (formData: FormData): ValidationResult => {
    const errors: { [key: string]: string } = {};
 
    const startType = formData.startType?.[0]?.value;
 
    // Check if manual selection has recipients
    if (
      startType === 'manual' &&
      (!formData.recipients || formData.recipients.length === 0)
    ) {
      errors.recipients = 'At least one contact or group must be selected';
    }
 
    // Check if query has a query string
    if (
      startType === 'query' &&
      (!formData.contactQuery || !formData.contactQuery.trim())
    ) {
      errors.contactQuery = 'Contact query is required';
    }
 
    return {
      valid: Object.keys(errors).length === 0,
      errors
    };
  },
 
  fromFormData: (formData: FormData): StartSession => {
    const action: StartSession & {
      contact_query?: string;
      exclusions?: { in_a_flow: boolean };
    } = {
      uuid: formData.uuid,
      type: 'start_session',
      flow: {
        uuid: formData.flow[0].uuid || formData.flow[0].value,
        name: formData.flow[0].name
      },
      groups: [],
      contacts: []
    };
 
    // Get the start type value from array
    const startTypeValue = formData.startType[0].value;
 
    // Handle different start types
    if (startTypeValue === 'create') {
      action.create_contact = true;
    } else if (startTypeValue === 'query') {
      action.contact_query = formData.contactQuery || '';
    } else {
      // Manual selection - separate contacts, groups, and legacy vars
      const recipients = formData.recipients || [];
      action.contacts = recipients
        .filter(
          (r: any) => r.type === 'contact' || (!r.type && !r.expression && r.id)
        )
        .map((c: any) => ({ uuid: c.id, name: c.name }));
      action.groups = recipients
        .filter((r: any) => r.type === 'group')
        .map((g: any) => ({ uuid: g.id, name: g.name }));
      const legacy_vars = recipients
        .filter((r: any) => r.type === 'expression' || r.expression)
        .map((e: any) => e.value || e.name || e.id);
      if (legacy_vars.length > 0) {
        action.legacy_vars = legacy_vars;
      }
    }
 
    // Add exclusions if set
    if (formData.skipContactsInFlow) {
      action.exclusions = { in_a_flow: true };
    }
 
    return action;
  }
};