-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(package-actions): Refactor logic for
getPackageActions
(#275)
* Refactor action rules * Tidy up latestRai stuff * Tidy up latestRai stuff * Fix 'err is any' ts issue * Fix logic for disable rai response withdraw * Type jiu jitsu * Fix med spa gate logic * Refactoring how we check planType * Oops, wrong allowed plan type * Add status check helper object * Type update * Remove action type from spa details * Reduce load on check process * Add jsdocs for convenience * small change * Remove unused utility * PlanChek -> PlanTypeCheck * Only instantiate ActionAvailabilityCheck once
- Loading branch information
Kevin Haube
authored
Dec 21, 2023
1 parent
9045759
commit aed336c
Showing
28 changed files
with
245 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,61 @@ | ||
import { OsMainSourceItem } from "./opensearch"; | ||
import { CognitoUserAttributes } from "./user"; | ||
import { getLatestRai } from "shared-utils"; | ||
import { SEATOOL_STATUS } from "./statusHelper"; | ||
|
||
export enum Action { | ||
ISSUE_RAI = "issue-rai", | ||
RESPOND_TO_RAI = "respond-to-rai", | ||
ENABLE_RAI_WITHDRAW = "enable-rai-withdraw", | ||
WITHDRAW_PACKAGE = "withdraw-package", | ||
DISABLE_RAI_WITHDRAW = "disable-rai-withdraw", | ||
ISSUE_RAI = "issue-rai", | ||
WITHDRAW_RAI = "withdraw-rai", | ||
RESPOND_TO_RAI = "respond-to-rai", | ||
WITHDRAW_PACKAGE = "withdraw-package", | ||
} | ||
|
||
const checkStatus = (seatoolStatus: string, authorized: string | string[]) => | ||
typeof authorized === "string" | ||
? seatoolStatus === authorized | ||
: authorized.includes(seatoolStatus); | ||
|
||
export const ActionAvailabilityCheck = ({ | ||
seatoolStatus, | ||
rais, | ||
raiWithdrawEnabled, | ||
}: OsMainSourceItem) => { | ||
const latestRai = getLatestRai(rais); | ||
return { | ||
/** Is in any of our pending statuses, sans Pending-RAI **/ | ||
isInActivePendingStatus: checkStatus(seatoolStatus, [ | ||
SEATOOL_STATUS.PENDING, | ||
SEATOOL_STATUS.PENDING_OFF_THE_CLOCK, | ||
SEATOOL_STATUS.PENDING_APPROVAL, | ||
SEATOOL_STATUS.PENDING_CONCURRENCE, | ||
]), | ||
/** Latest RAI is requested and status is Pending-RAI **/ | ||
hasRequestedRai: | ||
latestRai?.status === "requested" && | ||
checkStatus(seatoolStatus, SEATOOL_STATUS.PENDING_RAI), | ||
/** Latest RAI is not null **/ | ||
hasLatestRai: latestRai !== null, | ||
/** Latest RAI has been responded to **/ | ||
hasRaiResponse: latestRai?.status === "received", | ||
/** RAI Withdraw has been enabled **/ | ||
hasEnabledRaiWithdraw: raiWithdrawEnabled, | ||
/** 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), | ||
}; | ||
}; | ||
|
||
export type ActionRule = { | ||
action: Action; | ||
check: ( | ||
checker: ReturnType<typeof ActionAvailabilityCheck>, | ||
user: CognitoUserAttributes, | ||
/** Keep excess parameters to a minimum **/ | ||
...any: any[] | ||
) => boolean; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export enum PlanType { | ||
MED_SPA = "medicaid spa", | ||
CHIP_SPA = "chip spa", | ||
} | ||
|
||
const checkPlan = (planType: PlanType | null, validPlanTypes: PlanType[]) => | ||
!planType | ||
? false | ||
: validPlanTypes.includes(planType.toLowerCase() as PlanType); | ||
|
||
export const PlanTypeCheck = (planType: PlanType | null) => ({ | ||
isSpa: checkPlan(planType, [PlanType.MED_SPA, PlanType.CHIP_SPA]), | ||
isWaiver: checkPlan(planType, []), | ||
/** Keep excess methods to a minimum with `is` **/ | ||
is: (validPlanTypes: PlanType[]) => checkPlan(planType, validPlanTypes), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
src/packages/shared-utils/package-actions/getAvailableActions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { | ||
ActionAvailabilityCheck, | ||
CognitoUserAttributes, | ||
OsMainSourceItem, | ||
PlanTypeCheck, | ||
PlanType, | ||
} from "../../shared-types"; | ||
import rules from "./rules"; | ||
|
||
export const getAvailableActions = ( | ||
user: CognitoUserAttributes, | ||
result: OsMainSourceItem | ||
) => { | ||
const actionChecker = ActionAvailabilityCheck(result); | ||
return PlanTypeCheck(result.planType).is([PlanType.MED_SPA]) | ||
? rules.filter((r) => r.check(actionChecker, user)).map((r) => r.action) | ||
: []; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { | ||
Action, | ||
ActionAvailabilityCheck, | ||
ActionRule, | ||
SEATOOL_STATUS, | ||
} from "../../shared-types"; | ||
import { isCmsUser, isStateUser } from "../user-helper"; | ||
|
||
const arIssueRai: ActionRule = { | ||
action: Action.ISSUE_RAI, | ||
check: (checker, user) => | ||
checker.isInActivePendingStatus && | ||
(!checker.hasLatestRai || checker.hasRequestedRai) && | ||
isCmsUser(user), | ||
}; | ||
|
||
const arRespondToRai: ActionRule = { | ||
action: Action.RESPOND_TO_RAI, | ||
check: (checker, user) => | ||
checker.hasStatus(SEATOOL_STATUS.PENDING_RAI) && | ||
checker.hasRequestedRai && | ||
isStateUser(user), | ||
}; | ||
|
||
const arEnableWithdrawRaiResponse: ActionRule = { | ||
action: Action.ENABLE_RAI_WITHDRAW, | ||
check: (checker, user) => | ||
checker.isNotWithdrawn && | ||
checker.hasRaiResponse && | ||
!checker.hasEnabledRaiWithdraw && | ||
isCmsUser(user), | ||
}; | ||
|
||
const arDisableWithdrawRaiResponse: ActionRule = { | ||
action: Action.DISABLE_RAI_WITHDRAW, | ||
check: (checker, user) => | ||
checker.isNotWithdrawn && | ||
checker.hasRaiResponse && | ||
checker.hasEnabledRaiWithdraw && | ||
isCmsUser(user), | ||
}; | ||
|
||
const arWithdrawRaiResponse: ActionRule = { | ||
action: Action.WITHDRAW_RAI, | ||
check: (checker, user) => | ||
checker.isInActivePendingStatus && | ||
checker.hasRaiResponse && | ||
checker.hasEnabledRaiWithdraw && | ||
isStateUser(user), | ||
}; | ||
|
||
const arWithdrawPackage: ActionRule = { | ||
action: Action.WITHDRAW_PACKAGE, | ||
check: (checker, user) => | ||
checker.isInActivePendingStatus && isStateUser(user), | ||
}; | ||
|
||
export default [ | ||
arIssueRai, | ||
arRespondToRai, | ||
arEnableWithdrawRaiResponse, | ||
arDisableWithdrawRaiResponse, | ||
arWithdrawRaiResponse, | ||
arWithdrawPackage, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,34 @@ | ||
|
||
export const getLatestRai = (rais: any) => { | ||
const keys = Object.keys(rais); | ||
if (keys.length === 0) { | ||
return null; | ||
} else { | ||
const maxKey = keys.reduce((max, key) => Math.max(max, Number(key)), -Infinity); | ||
return { | ||
key: maxKey, | ||
status: getRaiStatus(rais[maxKey]), | ||
value: rais[maxKey], | ||
}; | ||
} | ||
export type LatestRai = { | ||
key: number; | ||
status: RaiStatus; | ||
value: any; | ||
}; | ||
export const getLatestRai = (rais: any | undefined): LatestRai | null => { | ||
if (!rais || Object.keys(rais).length === 0) { | ||
// No keys = no rai entries | ||
return null; | ||
} else { | ||
const maxKey = Object.keys(rais).reduce( | ||
(max, key) => Math.max(max, Number(key)), | ||
-Infinity | ||
); | ||
return { | ||
key: maxKey, | ||
status: getRaiStatus(rais[maxKey]), | ||
value: rais[maxKey], | ||
}; | ||
} | ||
}; | ||
|
||
export const getRaiStatus = (rai: any) => { | ||
if(rai.withdrawnDate) { | ||
return "withdrawn" | ||
} else if(rai.receivedDate) { | ||
return "received" | ||
} else if (rai.requestedDate) { | ||
return "requested" | ||
} else { | ||
return "unknown" | ||
} | ||
} | ||
if (rai.withdrawnDate) { | ||
return "withdrawn"; | ||
} else if (rai.receivedDate) { | ||
return "received"; | ||
} else if (rai.requestedDate) { | ||
return "requested"; | ||
} else { | ||
return "unknown"; | ||
} | ||
}; | ||
export type RaiStatus = ReturnType<typeof getRaiStatus>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.