All files / libs/amalia-lang/formula/parsing/src/lib/formula-parser itemsGenerators.ts

93.93% Statements 124/132
100% Branches 11/11
85.71% Functions 6/7
93.93% Lines 124/132

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 1331x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 44x 44x 44x 1x 1x 1x 1x 1x 1x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 1x 1x                 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x  
import { last } from 'lodash-es';
import { sha1 } from 'object-hash';
 
import { VariableType } from '@amalia/amalia-lang/tokens/types';
import {
  ComputedItemTypes,
  type ComputedFunctionResult,
  type ComputedRule,
  type ComputedVariable,
  type FilterDataset,
  type MetricsDataset,
  type QuotaDataset,
} from '@amalia/payout-calculation/types';
import { type Rule } from '@amalia/payout-definition/plans/types';
 
export const sha2 = (seeds: (object | string)[]) =>
  seeds
    .filter(Boolean)
    .map((e) => (typeof e === 'object' ? sha1(e) : e))
    .join(':');
 
export class ItemsGenerators {
  // -------------------- COMPUTED ITEMS --------------------
 
  private static generateComputedVariable(
    computedItem: Pick<
      ComputedVariable,
      'customObjectMachineName' | 'relationLevel' | 'type' | 'variableMachineName' | 'variableType'
    >,
    parentIds: string[],
    // Needed to avoid doubles in object variables.
    parentDatasetId?: string,
  ): ComputedVariable {
    const compositeKey = [
      computedItem.type.charAt(0),
      computedItem.variableType.charAt(0),
      computedItem.variableMachineName,
      computedItem.customObjectMachineName,
      computedItem.relationLevel,
      parentDatasetId && last(parentDatasetId.split(':')),
    ].filter(Boolean);
 
    return {
      ...computedItem,
      id: sha2(compositeKey),
      value: undefined,
      parentIds,
    };
  }
 
  public static generateGlobalVariable(name: string, objectName: VariableType, parentIds: string[]) {
    return ItemsGenerators.generateComputedVariable(
      {
        type: ComputedItemTypes.VARIABLE,
        variableMachineName: name,
        variableType: objectName,
      },
      parentIds,
    );
  }
 
  public static generateObjectVariable(
    variableMachineName: string,
    objectName: string,
    parentIds: string[],
    parentDatasetId?: string,
    relationLevel?: string,
  ) {
    return ItemsGenerators.generateComputedVariable(
      {
        type: ComputedItemTypes.VARIABLE,
        variableMachineName,
        variableType: VariableType.object,
        customObjectMachineName: objectName,
        relationLevel: relationLevel || undefined,
      },
      parentIds,
      parentDatasetId,
    );
  }
 
  public static generateComputedFunctionResult(
    computedItem: Pick<ComputedFunctionResult, 'args' | 'function' | 'type'>,
    parentIds: string[],
  ): ComputedFunctionResult {
    const compositeKey = [
      computedItem.type.charAt(0),
      (computedItem as ComputedFunctionResult).function,
      (computedItem as ComputedFunctionResult).args,
    ].filter(Boolean);
 
    return {
      ...computedItem,
      id: sha2(compositeKey),
      value: undefined,
      parentIds,
    };
  }
 
  public static generateComputedRule(rule: Rule): ComputedRule {
    return {
      id: `R:${rule.machineName}`,
      type: ComputedItemTypes.RULE,
      ruleMachineName: rule.machineName,
      parentIds: [],
      value: undefined,
    };
  }
 
  // -------------------- DATASETS --------------------
 
  public static generateFilterDataset(
    dataset: Pick<
      FilterDataset | MetricsDataset | QuotaDataset,
      'customObjectDefinition' | 'filterMachineName' | 'type'
    >,
    parentIds: string[],
  ): FilterDataset | MetricsDataset | QuotaDataset {
    const compositeKey = [
      dataset.type.charAt(0),
      dataset.customObjectDefinition.machineName,
      dataset.filterMachineName,
    ];
 
    return {
      ...dataset,
      id: sha2(compositeKey),
      parentIds,
      computedItems: [],
    };
  }
}