|
| 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 app = express(); |
| 11 | + |
| 12 | +const bb = new BlueButton(); |
| 13 | +const authData = bb.generateAuthData(); |
| 14 | + |
| 15 | +// This is where medicare.gov beneficiary associated |
| 16 | +// with the current logged in app user, |
| 17 | +// in real app, this could be the app specific |
| 18 | +// account management system |
| 19 | + |
| 20 | +const loggedInUser: User = { |
| 21 | +}; |
| 22 | + |
| 23 | +// AuthorizationToken holds access grant info: |
| 24 | +// access token, expire in, expire at, token type, scope, refreh token, etc. |
| 25 | +// it is associated with current logged in user in real app, |
| 26 | +// check SDK js docs for more details. |
| 27 | + |
| 28 | +let authToken: AuthorizationToken; |
| 29 | + |
| 30 | +// auth flow: response with URL to redirect to Medicare.gov beneficiary login |
| 31 | +app.get("/api/authorize/authurl", (req: Request, res: Response) => { |
| 32 | + res.send(bb.generateAuthorizeUrl(authData)); |
| 33 | +}); |
| 34 | + |
| 35 | +// auth flow: oauth2 call back |
| 36 | +app.get("/api/bluebutton/callback", async (req: Request, res: Response) => { |
| 37 | + if (typeof req.query.error === "string") { |
| 38 | + res.json({ message: req.query.error }); |
| 39 | + } else { |
| 40 | + if ( |
| 41 | + typeof req.query.code === "string" && |
| 42 | + typeof req.query.state === "string" |
| 43 | + ) { |
| 44 | + // let results; |
| 45 | + try { |
| 46 | + authToken = await bb.getAuthorizationToken( |
| 47 | + authData, |
| 48 | + req.query.code, |
| 49 | + req.query.state |
| 50 | + ); |
| 51 | + // data flow: after access granted |
| 52 | + // the app logic can fetch the beneficiary's data in app specific ways: |
| 53 | + // e.g. download EOB periodically etc. |
| 54 | + // access token can expire, SDK automatically refresh access token when that happens. |
| 55 | + const eobResults = await bb.getExplanationOfBenefitData(authToken); |
| 56 | + authToken = eobResults.token; // in case authToken got refreshed during fhir call |
| 57 | + // const patientResults = await bb.getPatientData(authToken); |
| 58 | + // authToken = patientResults.token; |
| 59 | + // const coverageResults = await bb.getCoverageData(authToken); |
| 60 | + // authToken = coverageResults.token; |
| 61 | + // const profileResults = await bb.getProfileData(authToken); |
| 62 | + // authToken = profileResults.token; |
| 63 | + |
| 64 | + // nav pages if needed for eob, patient, coverage |
| 65 | + // client code can preemptively refresh tokens by calling refreshAuthToken(authToken) |
| 66 | + // console.log( |
| 67 | + // "============= preemptively do oauth token refresh before fetch EOB =================" |
| 68 | + // ); |
| 69 | + |
| 70 | + // console.log("============= authToken ================="); |
| 71 | + |
| 72 | + // authToken = await bb.refreshAuthToken(authToken); |
| 73 | + |
| 74 | + // console.log(authToken); |
| 75 | + |
| 76 | + loggedInUser.authToken = authToken; |
| 77 | + // console.log("============= EOB PAGES ================="); |
| 78 | + |
| 79 | + loggedInUser.eobData = eobResults.response?.data; |
| 80 | + // const eobs = await bb.getPages(eobbundle, authToken); |
| 81 | + // for (let i = 0; i < eobs.pages.length; i++) { |
| 82 | + // fs.writeFileSync(`eob_p${i}.json`, JSON.stringify(eobs.pages[i])); |
| 83 | + // } |
| 84 | + |
| 85 | + // authToken = eobs.token; |
| 86 | + |
| 87 | + // console.log("=============PATIENT================="); |
| 88 | + // const ptbundle = patientResults.response?.data; |
| 89 | + // const pts = await bb.getPages(ptbundle, authToken); |
| 90 | + // authToken = pts.token; |
| 91 | + |
| 92 | + // console.log("=============COVERAGE================="); |
| 93 | + // const coveragebundle = coverageResults.response?.data; |
| 94 | + // const coverages = await bb.getPages(coveragebundle, authToken); |
| 95 | + // authToken = coverages.token; |
| 96 | + |
| 97 | + // console.log("=============PROFILE================="); |
| 98 | + // const pfbundle = profileResults.response?.data; |
| 99 | + // const pfs = await bb.getPages(pfbundle, authToken); |
| 100 | + // authToken = pfs.token; |
| 101 | + |
| 102 | + // results = { |
| 103 | + // eob: eobs.pages, |
| 104 | + // patient: pts.pages, |
| 105 | + // coverage: coverages.pages, |
| 106 | + // profile: pfs.pages, |
| 107 | + // }; |
| 108 | + } catch (e) { |
| 109 | + console.log(e); |
| 110 | + } |
| 111 | + // res.json(results); |
| 112 | + } else { |
| 113 | + //res.json({ message: "Missing AC in callback." }); |
| 114 | + console.log("Missing AC in callback."); |
| 115 | + } |
| 116 | + } |
| 117 | + const fe_redirect_url = |
| 118 | + process.env.SELENIUM_TESTS ? 'http://client:3000' : 'http://localhost:3000'; |
| 119 | + res.redirect(fe_redirect_url); |
| 120 | +}); |
| 121 | + |
| 122 | +// data flow: front end fetch eob |
| 123 | +app.get("/api/data/benefit", async (req: Request, res: Response) => { |
| 124 | + if (loggedInUser.eobData) { |
| 125 | + res.json(loggedInUser.eobData); |
| 126 | + } |
| 127 | +}); |
| 128 | + |
| 129 | +const port = 3001; |
| 130 | +app.listen(port, () => { |
| 131 | + console.log(`[server]: Server is running at https://localhost:${port}`); |
| 132 | +}); |
0 commit comments