@@ -4,11 +4,22 @@ import Settings from '../entities/Settings';
44import db from '../utils/db' ;
55import { getAccessToken , generateAuthorizeUrl } from '../utils/bb2' ;
66import { getBenefitData } from './Data' ;
7- import { getLoggedInUser } from 'src/utils/user' ;
87import logger from '@shared/Logger' ;
8+ import { clearBB2Data , getLoggedInUser } from 'src/utils/user' ;
9+
10+ const BENE_DENIED_ACCESS = 'access_denied' ;
11+
912
1013export async function authorizationCallback ( req : Request , res : Response ) {
1114 try {
15+
16+ if ( req . query . error === BENE_DENIED_ACCESS ) {
17+ const loggedInUser = getLoggedInUser ( db ) ;
18+ // clear all saved claims data since the bene has denied access for the application
19+ clearBB2Data ( loggedInUser ) ;
20+ loggedInUser . errors . push ( BENE_DENIED_ACCESS ) ;
21+ throw new Error ( 'Beneficiary denied application access to their data' ) ;
22+ }
1223
1324 if ( ! req . query . code ) {
1425 throw new Error ( 'Response was missing access code' ) ;
@@ -19,31 +30,39 @@ export async function authorizationCallback(req: Request, res: Response) {
1930
2031 // this gets the token from Medicare.gov once the 'user' authenticates their Medicare.gov account
2132 const response = await getAccessToken ( req . query . code ?. toString ( ) , req . query . state ?. toString ( ) ) ;
22- const authToken = new AuthorizationToken ( response . data ) ;
23-
24- /* DEVELOPER NOTES:
25- * This is where you would most likely place some type of
26- * persistence service/functionality to store the token along with
27- * the application user identifiers
28- */
29-
30- // Here we are grabbing the mocked 'user' for our application
31- // to be able to store the access token for that user
32- // thereby linking the 'user' of our sample applicaiton with their Medicare.gov account
33- // providing access to their Medicare data to our sample application
34- const loggedInUser = getLoggedInUser ( db ) ;
35- loggedInUser . authToken = authToken ;
3633
34+ const loggedInUser = getLoggedInUser ( db ) ;
3735
38- /* DEVELOPER NOTES:
39- * Here we will use the token to get the EoB data for the mocked 'user' of the sample application
40- * then to save trips to the BB2 API we will store it in the mocked db with the mocked 'user'
41- *
42- * You could also request data for the Patient endpoint and/or the Coverage endpoint here
43- * using similar functionality
44- */
45- const eobData = await getBenefitData ( req , res ) ;
46- loggedInUser . eobData = eobData ;
36+ if ( response . status === 200 ) {
37+ const authToken = new AuthorizationToken ( response . data ) ;
38+
39+ /* DEVELOPER NOTES:
40+ * This is where you would most likely place some type of
41+ * persistence service/functionality to store the token along with
42+ * the application user identifiers
43+ */
44+
45+ // Here we are grabbing the mocked 'user' for our application
46+ // to be able to store the access token for that user
47+ // thereby linking the 'user' of our sample applicaiton with their Medicare.gov account
48+ // providing access to their Medicare data to our sample application
49+ loggedInUser . authToken = authToken ;
50+
51+
52+ /* DEVELOPER NOTES:
53+ * Here we will use the token to get the EoB data for the mocked 'user' of the sample application
54+ * then to save trips to the BB2 API we will store it in the mocked db with the mocked 'user'
55+ *
56+ * You could also request data for the Patient endpoint and/or the Coverage endpoint here
57+ * using similar functionality
58+ */
59+ const eobData = await getBenefitData ( req , res ) ;
60+ loggedInUser . eobData = eobData ;
61+ }
62+ else {
63+ // send generic error message to FE
64+ loggedInUser . eobData = JSON . parse ( '{"message": "Unable to load EOB Data - authorization failed."}' ) ;
65+ }
4766
4867 } catch ( e ) {
4968 /* DEVELOPER NOTES:
0 commit comments