All files / src/flow/actions add_contact_urn.ts

100% Statements 91/91
73.33% Branches 11/15
100% Functions 4/4
100% Lines 91/91

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 92132x 1x 1x 1x 1x 1x 1x 1x 1x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 20x 20x 20x 20x 20x 20x 20x 1x 20x 20x 20x 20x 20x 20x 20x 132x 39x 39x 39x 39x 39x 39x 39x 38x 38x 39x 132x 132x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 132x 132x 132x 132x 132x 132x 132x 132x 132x 2508x 2508x 2508x 2508x 2508x 132x 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,
  ValidationResult,
  FlowTypes
} from '../types';
import { Node, AddContactUrn } from '../../store/flow-definition';
import { SCHEMES, renderClamped, renderHighlightedText } from '../utils';
 
export const add_contact_urn: ActionConfig = {
  name: 'Add URN',
  group: ACTION_GROUPS.contacts,
  flowTypes: [FlowTypes.VOICE, FlowTypes.MESSAGE, FlowTypes.BACKGROUND],
  render: (_node: Node, action: AddContactUrn) => {
    const schemeObj = SCHEMES.find((s) => s.scheme === action.scheme);
    const friendlyScheme = schemeObj?.path || action.scheme;
    return renderClamped(
      html`Add ${friendlyScheme} ${renderHighlightedText(action.path, true)}`,
      `Add ${friendlyScheme} ${action.path}`
    );
  },
 
  toFormData: (action: AddContactUrn) => {
    const schemeObj = SCHEMES.find((s) => s.scheme === action.scheme);
    return {
      uuid: action.uuid,
      scheme: schemeObj
        ? [{ name: schemeObj.path, value: action.scheme }]
        : null,
      path: action.path || ''
    };
  },
 
  fromFormData: (formData: FormData): AddContactUrn => {
    // Extract scheme value from select format
    const schemeValue =
      Array.isArray(formData.scheme) && formData.scheme.length > 0
        ? formData.scheme[0].value
        : 'tel';
 
    return {
      uuid: formData.uuid,
      type: 'add_contact_urn',
      scheme: schemeValue,
      path: formData.path || ''
    };
  },
 
  form: {
    scheme: {
      type: 'select',
      label: 'URN Type',
      helpText: 'Select the type of URN to add to the contact',
      required: true,
      searchable: false,
      multi: false,
      options: SCHEMES.map((scheme) => ({
        name: scheme.path,
        value: scheme.scheme
      }))
    },
    path: {
      type: 'text',
      label: 'URN Value',
      helpText: 'Enter the URN value (e.g., phone number, Facebook ID, etc.)',
      required: true,
      maxLength: 1000,
      placeholder: 'Enter the URN value...',
      evaluated: true
    }
  },
 
  validate: (formData: FormData): ValidationResult => {
    const errors: { [key: string]: string } = {};
 
    if (!formData.scheme || formData.scheme.length === 0) {
      errors.scheme = 'URN type is required';
    }
 
    if (!formData.path || formData.path.trim() === '') {
      errors.path = 'URN value is required';
    }
 
    return {
      valid: Object.keys(errors).length === 0,
      errors
    };
  }
};