|
| 1 | +import express, { Request, Response } from "express"; |
| 2 | +import { AuthorizationToken, BlueButton } from "cms-bluebutton-sdk"; |
| 3 | + |
| 4 | +interface User { |
| 5 | + authToken?: AuthorizationToken, |
| 6 | + eobData?: any, |
| 7 | + errors?: string[] |
| 8 | +} |
| 9 | + |
| 10 | +const BENE_DENIED_ACCESS = "access_denied" |
| 11 | +const FE_MSG_ACCESS_DENIED = "Beneficiary denied app access to their data" |
| 12 | +const ERR_QUERY_EOB = "Error when querying the patient's EOB!" |
| 13 | +const ERR_MISSING_AUTH_CODE = "Response was missing access code!" |
| 14 | +const ERR_MISSING_STATE = "State is required when using PKCE" |
| 15 | + |
| 16 | +const app = express(); |
| 17 | + |
| 18 | +const bb = new BlueButton(); |
| 19 | +const authData = bb.generateAuthData(); |
| 20 | + |
| 21 | +// This is where medicare.gov beneficiary associated |
| 22 | +// with the current logged in app user, |
| 23 | +// in real app, this could be the app specific |
| 24 | +// account management system |
| 25 | + |
| 26 | +const loggedInUser: User = { |
| 27 | +}; |
| 28 | + |
| 29 | +// helper to clean up cached eob data |
| 30 | +function clearBB2Data() { |
| 31 | + loggedInUser.authToken = undefined; |
| 32 | + loggedInUser.eobData = {}; |
| 33 | +} |
| 34 | + |
| 35 | +// AuthorizationToken holds access grant info: |
| 36 | +// access token, expire in, expire at, token type, scope, refreh token, etc. |
| 37 | +// it is associated with current logged in user in real app, |
| 38 | +// check SDK js docs for more details. |
| 39 | + |
| 40 | +let authToken: AuthorizationToken; |
| 41 | + |
| 42 | +// auth flow: response with URL to redirect to Medicare.gov beneficiary login |
| 43 | +app.get("/api/authorize/authurl", (req: Request, res: Response) => { |
| 44 | + res.send(bb.generateAuthorizeUrl(authData)); |
| 45 | +}); |
| 46 | + |
| 47 | +// auth flow: oauth2 call back |
| 48 | +app.get("/api/bluebutton/callback", (req: Request, res: Response) => { |
| 49 | + (async (req: Request, res: Response) => { |
| 50 | + if (typeof req.query.error === "string") { |
| 51 | + // clear all cached claims eob data since the bene has denied access |
| 52 | + // for the application |
| 53 | + clearBB2Data(); |
| 54 | + let errMsg = req.query.error; |
| 55 | + if (req.query.error === BENE_DENIED_ACCESS) { |
| 56 | + errMsg = FE_MSG_ACCESS_DENIED; |
| 57 | + } |
| 58 | + loggedInUser.eobData = {"message": errMsg}; |
| 59 | + process.stdout.write(errMsg + '\n'); |
| 60 | + } else { |
| 61 | + if ( |
| 62 | + typeof req.query.code === "string" && |
| 63 | + typeof req.query.state === "string" |
| 64 | + ) { |
| 65 | + try { |
| 66 | + authToken = await bb.getAuthorizationToken( |
| 67 | + authData, |
| 68 | + req.query.code, |
| 69 | + req.query.state |
| 70 | + ); |
| 71 | + // data flow: after access granted |
| 72 | + // the app logic can fetch the beneficiary's data in app specific ways: |
| 73 | + // e.g. download EOB periodically etc. |
| 74 | + // access token can expire, SDK automatically refresh access token when that happens. |
| 75 | + const eobResults = await bb.getExplanationOfBenefitData(authToken); |
| 76 | + authToken = eobResults.token; // in case authToken got refreshed during fhir call |
| 77 | + |
| 78 | + loggedInUser.authToken = authToken; |
| 79 | + |
| 80 | + loggedInUser.eobData = eobResults.response?.data; |
| 81 | + } catch (e) { |
| 82 | + loggedInUser.eobData = {}; |
| 83 | + process.stdout.write(ERR_QUERY_EOB + '\n'); |
| 84 | + process.stdout.write("Exception: " + e + '\n'); |
| 85 | + } |
| 86 | + } else { |
| 87 | + clearBB2Data(); |
| 88 | + process.stdout.write(ERR_MISSING_AUTH_CODE + '\n'); |
| 89 | + process.stdout.write("OR" + '\n'); |
| 90 | + process.stdout.write(ERR_MISSING_STATE + '\n'); |
| 91 | + process.stdout.write("AUTH CODE: " + req.query.code + '\n'); |
| 92 | + process.stdout.write("STATE: " + req.query.state + '\n'); |
| 93 | + } |
| 94 | + } |
| 95 | + const fe_redirect_url = |
| 96 | + process.env.SELENIUM_TESTS ? 'http://client:3000' : 'http://localhost:3000'; |
| 97 | + res.redirect(fe_redirect_url); |
| 98 | + } |
| 99 | + )(req, res); |
| 100 | +}); |
| 101 | + |
| 102 | +// data flow: front end fetch eob |
| 103 | +app.get("/api/data/benefit", (req: Request, res: Response) => { |
| 104 | + if (loggedInUser.eobData) { |
| 105 | + res.json(loggedInUser.eobData); |
| 106 | + } |
| 107 | +}); |
| 108 | + |
| 109 | +const port = 3001; |
| 110 | +app.listen(port, () => { |
| 111 | + process.stdout.write(`[server]: Server is running at https://localhost:${port}`); |
| 112 | + process.stdout.write("\n"); |
| 113 | +}); |
0 commit comments