All files / src/flow/actions remove_contact_groups.ts

100% Statements 81/81
92.85% Branches 13/14
100% Functions 5/5
100% Lines 81/81

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 82132x 2x 2x 2x 2x 2x 2x 2x 2x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 12x 12x 2x 2x 10x 10x 12x 1x 1x 12x 12x 18x 18x 18x 18x 18x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 12x 12x 12x 132x 132x 132x 132x 132x 132x 132x 132x 132x 10x 10x 10x 10x 8x 10x 2x 2x 2x 10x 10x 10x 10x 132x 132x 132x 132x  
import { html } from 'lit-html';
import {
  ActionConfig,
  ACTION_GROUPS,
  FormData,
  ValidationResult,
  FlowTypes
} from '../types';
import { Node, RemoveFromGroup } from '../../store/flow-definition';
import { renderGroupLinks } from '../utils';
 
export const remove_contact_groups: ActionConfig = {
  name: 'Remove from Group',
  group: ACTION_GROUPS.contacts,
  flowTypes: [FlowTypes.VOICE, FlowTypes.MESSAGE, FlowTypes.BACKGROUND],
  render: (_node: Node, action: RemoveFromGroup) => {
    if (action.all_groups) {
      return html`<div>Remove from all groups</div>`;
    }
    return html`<div>${renderGroupLinks(action.groups, 'group')}</div>`;
  },
  toFormData: (action: RemoveFromGroup) => {
    return {
      uuid: action.uuid,
      all_groups: action.all_groups || false,
      groups: action.groups || null
    };
  },
  form: {
    groups: {
      type: 'select',
      label: 'Groups',
      helpText: 'Select the groups to remove the contact from',
      options: [],
      multi: true,
      searchable: true,
      endpoint: '/api/v2/groups.json',
      valueKey: 'uuid',
      nameKey: 'name',
      placeholder: 'Search for groups...',
      conditions: {
        visible: (formData) => !formData.all_groups
      }
    },
    all_groups: {
      type: 'checkbox',
      label: 'Remove from All Groups',
      helpText:
        'Check this to remove the contact from all groups instead of specific ones'
    }
  },
  validate: (formData: FormData): ValidationResult => {
    const errors: { [key: string]: string } = {};
 
    if (
      !formData.all_groups &&
      (!formData.groups || formData.groups.length === 0)
    ) {
      errors.groups =
        'At least one group must be selected or check "Remove from All Groups"';
    }
 
    return {
      valid: Object.keys(errors).length === 0,
      errors
    };
  },
  fromFormData: (formData: FormData): RemoveFromGroup => {
    return {
      uuid: formData.uuid,
      type: 'remove_contact_groups',
      groups: formData.all_groups
        ? []
        : (formData.groups || []).map((g: any) => ({
            uuid: g.uuid,
            name: g.name
          })),
      all_groups: formData.all_groups || false
    };
  }
};