All files / lib/packages/shared-utils package-check.ts

100% Statements 16/16
100% Branches 17/17
100% Functions 7/7
100% Lines 15/15

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    283x 7008x   283x 12871x           283x                             1804x           1804x           3400x     1804x                                 7841x                     1804x       1804x                             1804x     2x     1804x                                        
import { Action, ActionType, Authority, FullUser, opensearch, SEATOOL_STATUS } from "shared-types";
 
const checkAuthority = (authority: Authority | string | null, validAuthorities: string[]) =>
  !authority ? false : validAuthorities.includes(authority.toLowerCase());
 
const checkStatus = (seatoolStatus: string, authorized: string | string[]) =>
  typeof authorized === "string"
    ? seatoolStatus === authorized
    : authorized.includes(seatoolStatus);
 
/** A object of booleans and methods handling common conditions
 * for business logic. */
export const PackageCheck = ({
  seatoolStatus,
  raiRequestedDate,
  raiReceivedDate,
  raiWithdrawnDate,
  raiWithdrawEnabled,
  authority,
  actionType,
  // appkParentId,
  // appkParent,
  initialIntakeNeeded,
  submissionDate,
  leadAnalystName,
  locked,
}: opensearch.main.Document) => {
  const secondClockStatuses = [
    SEATOOL_STATUS.PENDING,
    SEATOOL_STATUS.PENDING_APPROVAL,
    SEATOOL_STATUS.PENDING_CONCURRENCE,
  ];
 
  const planChecks = {
    isSpa: checkAuthority(authority, [Authority.MED_SPA, Authority.CHIP_SPA]),
    isWaiver: checkAuthority(authority, [Authority["1915b"], Authority["1915c"]]),
    isAppk: false,
    isAppkChild: false,
    /** Keep excess methods to a minimum with `is` **/
    authorityIs: (validAuthorities: string[]) => checkAuthority(authority, validAuthorities),
    hasCpoc: !!leadAnalystName,
  };
  const statusChecks = {
    /** Is in any of our pending statuses, sans Pending-RAI **/
    isInActivePendingStatus: checkStatus(seatoolStatus, [
      ...secondClockStatuses,
      SEATOOL_STATUS.PENDING_OFF_THE_CLOCK,
    ]),
    /** Is in a second clock status and RAI has been received **/
    isInSecondClock:
      !planChecks.authorityIs([Authority.CHIP_SPA]) &&
      checkStatus(seatoolStatus, secondClockStatuses) &&
      !!raiRequestedDate &&
      !!raiReceivedDate,
    /** Is in any status except Package Withdrawn **/
    isNotWithdrawn: !checkStatus(seatoolStatus, SEATOOL_STATUS.WITHDRAWN),
    /** Added for elasticity, but common checks should always bubble up as
     * object attributes! **/
    hasStatus: (authorizedStatuses: string | string[]) =>
      checkStatus(seatoolStatus, authorizedStatuses),
    /** Is Placeholder Status is a check that is used to determine if the record is in one of the in between status while waiting on seatool intake */
    isPlaceholderStatus: [
      SEATOOL_STATUS.RAI_RESPONSE_WITHDRAW_REQUESTED,
      SEATOOL_STATUS.SUBMITTED,
      SEATOOL_STATUS.WITHDRAW_REQUESTED,
    ].includes(seatoolStatus),
    /** If submission date exists */
    hasSubmissionDate: submissionDate !== undefined,
    isLocked: locked,
  };
  const subStatusChecks = {
    /** Is in any of our pending statuses, sans Pending-RAI **/
    needsIntake: initialIntakeNeeded,
  };
  const raiChecks = {
    /** There is an RAI and it does not have a response **/
    hasRequestedRai: !!raiRequestedDate && !raiReceivedDate,
    /** There is an RAI **/
    hasLatestRai: !!raiRequestedDate,
    /** There is an RAI, it has a response, and it has not been withdrawn **/
    hasRaiResponse: !!raiRequestedDate && !!raiReceivedDate && !raiWithdrawnDate,
    /** Latest RAI has a response and/or has been withdrawn **/
    hasCompletedRai: !!raiRequestedDate && !!raiReceivedDate,
    /** Latest RAI has a response and/or has been withdrawn **/
    hasRaiWithdrawal: !!raiWithdrawnDate,
    /** RAI Withdraw has been enabled **/
    hasEnabledRaiWithdraw: raiWithdrawEnabled,
  };
 
  const actionTypeChecks = {
    isInitialOrRenewal: actionType === "New" || actionType === "Renew",
    isTempExtension: actionType === "Extend",
    actionIs: (action: ActionType) => actionType === action,
  };
 
  return {
    ...planChecks,
    ...statusChecks,
    ...subStatusChecks,
    ...raiChecks,
    ...actionTypeChecks,
  };
};
 
export type IPackageCheck = ReturnType<typeof PackageCheck>;
 
export type ActionRule = {
  action: Action;
  check: (
    checker: IPackageCheck,
    user: FullUser,
    /** Keep excess parameters to a minimum **/
    ...any: any[]
  ) => boolean;
};