All files / src/flow/actions add_contact_groups.ts

72.58% Statements 45/62
80% Branches 4/5
60% Functions 3/5
72.58% Lines 45/62

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 63132x 2x 2x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 11x 11x 2x 11x 11x 11x 11x 11x 132x 132x 17x 17x 17x 17x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x   132x 132x                                 132x 132x 132x  
import { html } from 'lit-html';
import { ActionConfig, ACTION_GROUPS, FormData, FlowTypes } from '../types';
import { Node, AddToGroup } from '../../store/flow-definition';
import { renderGroupLinks } from '../utils';
 
export const add_contact_groups: ActionConfig = {
  name: 'Add to Group',
  group: ACTION_GROUPS.contacts,
  flowTypes: [FlowTypes.VOICE, FlowTypes.MESSAGE, FlowTypes.BACKGROUND],
  render: (_node: Node, action: AddToGroup) => {
    return html`<div>${renderGroupLinks(action.groups, 'group')}</div>`;
  },
 
  // Form-level transformations - default 1:1 mapping for this case
  toFormData: (action: AddToGroup) => {
    return {
      groups: action.groups || null,
      uuid: action.uuid
    };
  },
  form: {
    groups: {
      type: 'select',
      label: 'Groups',
      helpText: 'Select the groups to add the contact to',
      required: true,
      options: [],
      multi: true,
      searchable: true,
      endpoint: '/api/v2/groups.json',
      valueKey: 'uuid',
      nameKey: 'name',
      placeholder: 'Search for groups...',
      shouldExclude: (option: any) => !!option.query,
      allowCreate: true,
      createArbitraryOption: (input: string, options: any[]) => {
        // Check if a label with this name already exists
        const existing = options.find(
          (option) =>
            option.name.toLowerCase().trim() === input.toLowerCase().trim()
        );
        if (!existing && input.trim()) {
          return {
            name: input.trim(),
            arbitrary: true
          };
        }
        return null;
      }
    }
  },
  fromFormData: (formData: FormData): AddToGroup => {
    return {
      uuid: formData.uuid,
      type: 'add_contact_groups',
      groups: (formData.groups || []).map((g: any) => ({
        uuid: g.uuid,
        name: g.name
      }))
    };
  }
};