All files / lib/packages/shared-utils/package-actions getAvailableActions.ts

100% Statements 41/41
87.5% Branches 14/16
100% Functions 23/23
100% Lines 24/24

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          215x       985x   985x   985x 131x     985x 1116x 1116x   10270x         985x 1116x 131x     985x 1116x 315x 985x 985x 1x 2x         985x 11820x   985x 982x   985x    
import { Action, CognitoUserAttributes, opensearch } from "shared-types";
 
import { PackageCheck } from "../package-check";
import rules from "./rules";
 
export const getAvailableActions = (
  user: CognitoUserAttributes,
  result: opensearch.main.Document,
) => {
  const allActions: Action[][] = [];
 
  const allMembers = [result];
 
  if (result.appkChildren) {
    allMembers.push(...result.appkChildren.map((el) => el._source));
  }
 
  allMembers.forEach((element) => {
    const checks = PackageCheck(element);
    allActions.push([
      ...(checks.isWaiver || checks.isSpa
        ? rules.filter((r) => r.check(checks, user)).map((r) => r.action)
        : []),
    ]);
  });
 
  let commonActions = allActions.reduce((acc, currentActions, index) => {
    if (index === 0) return currentActions;
    return acc.filter((action: Action) => currentActions.includes(action));
  }, []);
 
  const allRaiRequestedDates = allMembers
    .filter((member) => (member as opensearch.main.SeatoolDocument)?.raiRequestedDate !== undefined)
    .map((member) => (member as opensearch.main.SeatoolDocument)?.raiRequestedDate);
  const isRaiRequestedDateIdentical = allRaiRequestedDates.every((date, _, arr) => date === arr[0]);
  if (!isRaiRequestedDateIdentical) {
    const actionsToRemove = [Action.RESPOND_TO_RAI, Action.WITHDRAW_RAI];
    commonActions = commonActions.filter((action: any) => !actionsToRemove.includes(action));
  }
 
  // We are sorting the actions based on the value they are in the order of the enum.
  // If we want to update this order just move the values around.
  const actionOrder = Object.values(Action);
  const orderMap = new Map(actionOrder.map((action, index) => [action, index]));
 
  const sortedActions = commonActions.sort(
    (a, b) => (orderMap.get(a) ?? Infinity) - (orderMap.get(b) ?? Infinity),
  );
  return sortedActions;
};