All files / src/flow/nodes split_by_scheme.ts

100% Statements 123/123
81.25% Branches 26/32
100% Functions 3/3
100% Lines 123/123

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 12499x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 1881x 1881x 99x 99x 99x 99x 48x 353x 48x 48x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 3x 2x 3x 2x 2x 2x 3x 3x 99x 3x 3x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 4x 3x 3x 3x 99x 99x 99x 99x 99x 99x 107x 99x 99x 99x 99x 99x 99x 100x 99x 99x 99x 99x 99x 99x 99x 120x 120x 120x 120x 120x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x  
import { SPLIT_GROUPS, FormData, NodeConfig, FlowTypes } from '../types';
import { Node, Case } from '../../store/flow-definition.d';
import { SCHEMES, validateWith } from '../utils';
import {
  resultNameField,
  nodeOptionsAccordionSimple,
  buildCategoriesExitsCases,
  appendOtherCategory
} from './shared';
 
const getSchemeOptions = () => {
  return SCHEMES.map((scheme) => ({
    value: scheme.scheme,
    name: scheme.name
  }));
};
 
const getSchemeName = (scheme: string) =>
  SCHEMES.find((s) => s.scheme === scheme)?.name || scheme;
 
export const split_by_scheme: NodeConfig = {
  type: 'split_by_scheme',
  name: 'Split by URN Type',
  group: SPLIT_GROUPS.split,
  flowTypes: [FlowTypes.VOICE, FlowTypes.MESSAGE, FlowTypes.BACKGROUND],
  form: {
    schemes: {
      type: 'select',
      label: 'Channel Types',
      helpText:
        "The contact's URN is the address they used to reach you such as their phone number or a Facebook ID. Select which URN types to split by.",
      required: true,
      options: getSchemeOptions(),
      multi: true,
      searchable: true,
      placeholder: 'Select the channels to split by...'
    },
    result_name: resultNameField
  },
  layout: ['schemes', nodeOptionsAccordionSimple],
  validate: validateWith((formData, errors) => {
    if (
      !formData.schemes ||
      !Array.isArray(formData.schemes) ||
      formData.schemes.length === 0
    ) {
      errors.schemes = 'At least one channel type is required';
    }
  }),
  toFormData: (node: Node) => {
    const schemes: string[] = [];
 
    if (node.router?.cases) {
      node.router.cases.forEach((c: Case) => {
        if (c.type === 'has_only_phrase' && c.arguments?.length > 0) {
          schemes.push(c.arguments[0]);
        }
      });
    }
 
    return {
      uuid: node.uuid,
      schemes: schemes.map((scheme) => ({
        value: scheme,
        name: getSchemeName(scheme)
      })),
      result_name: node.router?.result_name || ''
    };
  },
  fromFormData: (formData: FormData, originalNode: Node): Node => {
    const selectedSchemes = (formData.schemes || [])
      .filter((scheme: any) => scheme)
      .map((scheme: any) =>
        typeof scheme === 'string' ? scheme : scheme.value
      );
 
    const existingCategories = originalNode.router?.categories || [];
    const existingExits = originalNode.exits || [];
    const existingCases = originalNode.router?.cases || [];
 
    const { categories, exits, cases } = buildCategoriesExitsCases(
      selectedSchemes.map((scheme) => ({
        name: getSchemeName(scheme),
        case: {
          type: 'has_only_phrase',
          arguments: [scheme]
        }
      })),
      existingCategories,
      existingExits,
      existingCases
    );
 
    const defaultCategoryUuid = appendOtherCategory(
      categories,
      exits,
      existingCategories,
      existingExits,
      selectedSchemes.map(getSchemeName)
    );
 
    return {
      uuid: originalNode.uuid,
      actions: originalNode.actions || [],
      router: {
        type: 'switch',
        cases,
        categories,
        default_category_uuid: defaultCategoryUuid,
        operand: '@(urn_parts(contact.urn).scheme)',
        result_name: formData.result_name || ''
      },
      exits
    };
  },
  router: {
    type: 'switch',
    operand: '@(urn_parts(contact.urn).scheme)'
  },
 
  // Localization support for categories
  localizable: 'categories'
};