All files / libs/payout-calculation/statements/shared/src/lib/highlighted-kpis index.ts

60.66% Statements 91/150
88.23% Branches 30/34
42.85% Functions 3/7
60.66% Lines 91/150

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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 1511x 1x 1x 1x 1x 4x 4x 4x 4x 4x 10x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 5x 5x 3x 3x 3x 8x 8x 8x 2x 8x 8x 2x 8x 8x 8x 8x 8x 8x 1x 1x 2x 2x 2x 1x 8x 8x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 4x 4x 4x 8x 8x 8x 4x 1x 1x 1x           1x 1x                               1x 1x             1x 1x 1x                                                                    
import { type ComputedStatement, type ComputedStatementSummary, type Statement } from '@amalia/core/types';
import { ComputedItemTypes, type ComputedVariable, type ComputeEngineResult } from '@amalia/payout-calculation/types';
import { HighlightedKpiIdentifier, type ComputedHighlightedKpi } from '@amalia/payout-definition/plans/types';
 
export const getComputedVariableInStatement = <TValue extends ComputeEngineResult = ComputeEngineResult>(
  computedStatement: ComputedStatement | ComputedStatementSummary,
  variableMachineName: string,
): ComputedVariable<TValue> | undefined =>
  computedStatement.computedObjects.find(
    (co): co is ComputedVariable<TValue> =>
      co.type === ComputedItemTypes.VARIABLE && co.variableMachineName === variableMachineName,
  );
 
const evaluateHighlightedKpiForIdentifier = (
  computedStatement: ComputedStatement | ComputedStatementSummary,
  identifier: HighlightedKpiIdentifier,
): ComputedHighlightedKpi | null => {
  // Grab definition of the KPI in the plan.
  const highlightedKpiDefinition = (computedStatement.definitions.plan?.highlightedKpis ?? []).find(
    (kpi) => kpi.identifier === identifier,
  );
 
  if (!highlightedKpiDefinition) {
    return null;
  }
 
  // Grab variable definition for the variables.
  // Variables are indexed by machineName instead of ids.
  const variableDefinitions = Object.values(computedStatement.definitions.variables || {});
  const variableDefinition = variableDefinitions.find((v) => v.id === highlightedKpiDefinition.variableId);
  const minVariableDefinition = highlightedKpiDefinition.minimumVariableId
    ? variableDefinitions.find((v) => v.id === highlightedKpiDefinition.minimumVariableId)
    : null;
  const maxVariableDefinition = highlightedKpiDefinition.maximumVariableId
    ? variableDefinitions.find((v) => v.id === highlightedKpiDefinition.maximumVariableId)
    : null;
 
  // Grab the variable in the computed statement.
  const variable =
    variableDefinition && getComputedVariableInStatement<number>(computedStatement, variableDefinition.machineName);
  if (!variable) {
    return null;
  }
 
  // Get min and max.
  const minVariable = minVariableDefinition
    ? getComputedVariableInStatement<number>(computedStatement, minVariableDefinition.machineName)
    : null;
  const maxVariable = maxVariableDefinition
    ? getComputedVariableInStatement<number>(computedStatement, maxVariableDefinition.machineName)
    : null;
 
  // If we can't find them, assume it's a percentage and use 0 and 1.
  const currentValue = variable.value ?? null;
  const minimumValue = minVariable?.value ?? 0;
  const maximumValue = maxVariable?.value ?? 1;
 
  // Evaluate result.
  const progress = (((currentValue || 0) - minimumValue) / (maximumValue - minimumValue)) * 100;
 
  return {
    format: variableDefinition.format,
    minimumValue,
    maximumValue,
    currentValue,
    maximumVariableLabel: maxVariableDefinition?.name || null,
    minimumVariableLabel: minVariableDefinition?.name || null,
    variableLabel: variableDefinition.name,
    progress,
  };
};
 
export const evaluateHighlightedKpisProgress = (
  computedStatement: ComputedStatement | ComputedStatementSummary,
): Record<HighlightedKpiIdentifier, ComputedHighlightedKpi | null> =>
  Object.values(HighlightedKpiIdentifier).reduce<Record<HighlightedKpiIdentifier, ComputedHighlightedKpi | null>>(
    (acc, currentIdentifier) => {
      acc[currentIdentifier] = evaluateHighlightedKpiForIdentifier(computedStatement, currentIdentifier);
      return acc;
    },
    {} as Record<HighlightedKpiIdentifier, ComputedHighlightedKpi | null>,
  );
 
export const getStatementKpis = (statement: Statement, isForecast: boolean = false) => {
  const summary =
    isForecast && statement.forecast?.resultSummary ? statement.forecast.resultSummary : statement.resultSummary;

  return summary ? evaluateHighlightedKpisProgress(summary) : undefined;
};
 
const aggregateKpi = (
  accKpi: { kpi: ComputedHighlightedKpi | null; count: number },
  kpiToAggregate?: ComputedHighlightedKpi | null,
) => {
  if (kpiToAggregate) {
    if (accKpi.kpi) {
      accKpi.kpi.currentValue ??= 0;
      accKpi.kpi.currentValue += kpiToAggregate.currentValue ?? 0;
      accKpi.kpi.progress += kpiToAggregate.progress;
    } else {
      accKpi.kpi = kpiToAggregate;
    }

    accKpi.count++;
  }
};
 
const getKpiAverage = (kpi: ComputedHighlightedKpi | null, count: number): ComputedHighlightedKpi | null =>
  kpi
    ? {
        ...kpi,
        currentValue: (kpi.currentValue ?? 0) / count,
        progress: kpi.progress / count,
      }
    : null;
 
export const getStatementsAverageKpis = (
  /** Statements of the same plan. */
  statements: Statement[],
  /** Is in forecast mode. */
  isForecast: boolean = false,
): Record<HighlightedKpiIdentifier, ComputedHighlightedKpi | null> => {
  const kpisAcc = statements.reduce<
    Record<HighlightedKpiIdentifier, { kpi: ComputedHighlightedKpi | null; count: number }>
  >(
    (acc, statement) => {
      const statementKpis = getStatementKpis(statement, isForecast);

      aggregateKpi(acc[HighlightedKpiIdentifier.PRIMARY], statementKpis?.[HighlightedKpiIdentifier.PRIMARY]);
      aggregateKpi(acc[HighlightedKpiIdentifier.SECONDARY], statementKpis?.[HighlightedKpiIdentifier.SECONDARY]);

      return acc;
    },
    {
      [HighlightedKpiIdentifier.PRIMARY]: { kpi: null, count: 0 },
      [HighlightedKpiIdentifier.SECONDARY]: { kpi: null, count: 0 },
    },
  );

  return {
    [HighlightedKpiIdentifier.PRIMARY]: getKpiAverage(
      kpisAcc[HighlightedKpiIdentifier.PRIMARY].kpi,
      kpisAcc[HighlightedKpiIdentifier.PRIMARY].count,
    ),
    [HighlightedKpiIdentifier.SECONDARY]: getKpiAverage(
      kpisAcc[HighlightedKpiIdentifier.SECONDARY].kpi,
      kpisAcc[HighlightedKpiIdentifier.SECONDARY].count,
    ),
  };
};