All files / libs/lti/shared/src/lib get-granted-shares.ts

91.83% Statements 45/49
88.23% Branches 15/17
100% Functions 1/1
91.83% Lines 45/49

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 501x 1x 1x 1x 1x 2x 2x 2x 2x 3x     3x 3x 3x 3x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x     3x 3x 3x 3x 1x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x  
import { type LtiGrantOutcomeKpiRatios, type LtiParticipantGrants } from '@amalia/lti/types';
 
import { getEarnedRatioForKpi } from './get-earned-ratio-for-kpi';
 
export const getGrantedShares = (grants: Pick<LtiParticipantGrants, 'grants' | 'plan'>) => {
  const grantedShares = grants.grants.map((g) => g.nbShares).reduce((acc, curr) => acc + curr, 0);
 
  const outcomeKpiRatios = grants.plan.kpis.reduce(
    (acc, curr) => {
      if (acc === null) {
        return null;
      }
 
      const kpiValue = grants.plan.kpiValues?.[curr.id] ?? null;
 
      if (kpiValue === null) {
        return null;
      }
 
      return {
        ...acc,
        [curr.id]: getEarnedRatioForKpi(curr, kpiValue),
      };
    },
    {} as LtiGrantOutcomeKpiRatios | null,
  );
 
  const performance = grants.plan.kpis.reduce(
    (acc, curr) => {
      if (acc === null) {
        return null;
      }
 
      const earnedRatioForKpi = outcomeKpiRatios?.[curr.id];
 
      return earnedRatioForKpi === undefined || earnedRatioForKpi === null
        ? null
        : acc + earnedRatioForKpi * curr.weight;
    },
    0 as number | null,
  );
 
  return {
    grantedShares,
    earnedShares: performance === null ? null : Math.floor(performance * grantedShares),
    performance,
    outcomeKpiRatios,
  };
};