Skip to content

Commit f6d4d7d

Browse files
Refactor get_and_update_user to reduce BFD calls, and to ensure fhir_ids are always updated if DB values differ from what BFD returns
1 parent b95785b commit f6d4d7d

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

apps/fhir/bluebutton/utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,6 @@ def prepend_q(pass_params):
410410
return pass_params
411411

412412

413-
# BB2-4166-TODO - if tests are failing this could be related
414413
def dt_patient_reference(user, version):
415414
"""Get Patient Reference from Crosswalk for user"""
416415

@@ -428,7 +427,6 @@ def crosswalk_patient_id(user, version):
428427
logger.debug("\ncrosswalk_patient_id User:%s" % user)
429428
try:
430429
patient = Crosswalk.objects.get(user=user)
431-
# BB2-4166-TODO - if tests are failing this could be related
432430
if patient.fhir_id(version):
433431
return patient.fhir_id(version)
434432

apps/fhir/server/tests/test_authentication.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from rest_framework import exceptions
77
from apps.fhir.bluebutton.exceptions import UpstreamServerException
88
from apps.test import BaseApiTest
9-
from ..authentication import match_fhir_id
9+
from apps.fhir.server.authentication import match_fhir_id
1010
from apps.versions import Versions
11-
from .responses import responses
11+
from apps.fhir.server.tests.responses import responses
1212

1313
from hhs_oauth_server.settings.base import MOCK_FHIR_ENDPOINT_HOSTNAME, MOCK_FHIR_V3_ENDPOINT_HOSTNAME
1414

@@ -17,9 +17,8 @@ def mock_fhir_url(version):
1717
return MOCK_FHIR_ENDPOINT_HOSTNAME if version in [1, 2] else MOCK_FHIR_V3_ENDPOINT_HOSTNAME
1818

1919

20-
# MOCK_FHIR_PATH = "/v1/fhir/Patient/"
2120
def mock_fhir_path(version):
22-
return f"/v{version}/fhir/Patient"
21+
return f'/v{version}/fhir/Patient'
2322

2423

2524
class TestAuthentication(BaseApiTest):
@@ -41,10 +40,7 @@ def setUp(self):
4140
self.request = self.factory.get('http://localhost:8000/mymedicare/sls-callback')
4241
self.request.session = self.client.session
4342

44-
# The mock uses data from responses.py. We need to have mock data for a given MBI/FHIR ID
45-
# for each version of the API we want to test this way. We can use the v2 test data for
46-
# v3 because we're just asking "do you have a valid us-mbi query string?", and usnig that to then
47-
# look up the mock response.
43+
# The mock uses data from responses.py
4844
@classmethod
4945
def create_fhir_mock(cls, hicn_response_key, mbi_response_key, version=Versions.NOT_AN_API_VERSION):
5046
@urlmatch(netloc=mock_fhir_url(version), path=mock_fhir_path(version), method='POST')

apps/mymedicare_cb/models.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
MAX_HICN_HASH_LENGTH = 64
1919
MAX_MBI_LENGTH = 11
20+
FHIR_ID_VERSIONS_TO_CHECK = [2, 3]
2021

2122

2223
class BBMyMedicareCallbackCrosswalkCreateException(APIException):
@@ -68,25 +69,27 @@ def get_and_update_user(slsx_client: OAuth2ConfigSLSx, request):
6869
# If the lookup for the requested version fails, raise the exception
6970
# This is wrapped in the case that if the requested version fails, match_fhir_id
7071
# will still bubble up UpstreamServerException
71-
for supported_version in Versions.supported_versions():
72+
for supported_version in FHIR_ID_VERSIONS_TO_CHECK:
7273
try:
7374
fhir_id, hash_lookup_type = match_fhir_id(
7475
mbi=slsx_client.mbi,
7576
hicn_hash=hicn_hash,
7677
request=request,
7778
version=supported_version,
78-
7979
)
8080
versioned_fhir_ids[supported_version] = fhir_id
8181
except UpstreamServerException as e:
8282
if supported_version == version:
8383
raise e
8484

85+
bfd_fhir_id_v2 = versioned_fhir_ids[Versions.V2]
86+
bfd_fhir_id_v3 = versioned_fhir_ids[Versions.V3]
87+
8588
log_dict = {
8689
'type': 'mymedicare_cb:get_and_update_user',
8790
'subject': slsx_client.user_id,
88-
'fhir_id_v2': versioned_fhir_ids.get(Versions.V2, None),
89-
'fhir_id_v3': versioned_fhir_ids.get(Versions.V3, None),
91+
'fhir_id_v2': bfd_fhir_id_v2,
92+
'fhir_id_v3': bfd_fhir_id_v3,
9093
'hicn_hash': slsx_client.hicn_hash,
9194
'hash_lookup_type': hash_lookup_type,
9295
'crosswalk': {},
@@ -104,7 +107,12 @@ def get_and_update_user(slsx_client: OAuth2ConfigSLSx, request):
104107
hicn_updated = True
105108

106109
update_fhir_id = False
107-
if user.crosswalk.fhir_id(Versions.V2) == '' or user.crosswalk.fhir_id(Versions.V3) == '':
110+
if (
111+
user.crosswalk.fhir_id(Versions.V2) == ''
112+
or user.crosswalk.fhir_id(Versions.V3) == ''
113+
or user.crosswalk.fhir_id(Versions.V2) != bfd_fhir_id_v2
114+
or user.crosswalk.fhir_id(Versions.V3) != bfd_fhir_id_v3
115+
):
108116
update_fhir_id = True
109117
# Update Crosswalk if the user_mbi is null, but we have an mbi value from SLSx or
110118
# if the saved user_mbi value is different than what SLSx has
@@ -119,8 +127,8 @@ def get_and_update_user(slsx_client: OAuth2ConfigSLSx, request):
119127
'crosswalk_before': {
120128
'id': user.crosswalk.id,
121129
'user_hicn_hash': user.crosswalk.user_hicn_hash,
122-
'fhir_id_v2': versioned_fhir_ids.get(Versions.V2, None),
123-
'fhir_id_v3': versioned_fhir_ids.get(Versions.V3, None),
130+
'fhir_id_v2': bfd_fhir_id_v2,
131+
'fhir_id_v3': bfd_fhir_id_v3,
124132
'user_id_type': user.crosswalk.user_id_type,
125133
},
126134
})
@@ -129,8 +137,8 @@ def get_and_update_user(slsx_client: OAuth2ConfigSLSx, request):
129137
# Archive to audit crosswalk changes
130138
ArchivedCrosswalk.create(user.crosswalk)
131139
if update_fhir_id:
132-
user.crosswalk.fhir_id_v2 = versioned_fhir_ids.get(Versions.V2)
133-
user.crosswalk.fhir_id_v3 = versioned_fhir_ids.get(Versions.V3)
140+
user.crosswalk.fhir_id_v2 = bfd_fhir_id_v2
141+
user.crosswalk.fhir_id_v3 = bfd_fhir_id_v3
134142
# Update crosswalk per changes
135143
user.crosswalk.user_id_type = hash_lookup_type
136144
user.crosswalk.user_hicn_hash = slsx_client.hicn_hash
@@ -147,8 +155,8 @@ def get_and_update_user(slsx_client: OAuth2ConfigSLSx, request):
147155
'crosswalk': {
148156
'id': user.crosswalk.id,
149157
'user_hicn_hash': user.crosswalk.user_hicn_hash,
150-
'fhir_id_v2': versioned_fhir_ids.get(Versions.V2, None),
151-
'fhir_id_v3': versioned_fhir_ids.get(Versions.V3, None),
158+
'fhir_id_v2': bfd_fhir_id_v2,
159+
'fhir_id_v3': bfd_fhir_id_v3,
152160
'user_id_type': user.crosswalk.user_id_type,
153161
},
154162
})
@@ -160,8 +168,8 @@ def get_and_update_user(slsx_client: OAuth2ConfigSLSx, request):
160168

161169
user = create_beneficiary_record(
162170
slsx_client,
163-
fhir_id_v2=versioned_fhir_ids.get(Versions.V2, None),
164-
fhir_id_v3=versioned_fhir_ids.get(Versions.V3, None),
171+
fhir_id_v2=bfd_fhir_id_v2,
172+
fhir_id_v3=bfd_fhir_id_v3,
165173
user_id_type=hash_lookup_type,
166174
request=request
167175
)
@@ -175,8 +183,8 @@ def get_and_update_user(slsx_client: OAuth2ConfigSLSx, request):
175183
'crosswalk': {
176184
'id': user.crosswalk.id,
177185
'user_hicn_hash': user.crosswalk.user_hicn_hash,
178-
'fhir_id_v2': versioned_fhir_ids.get(Versions.V2, None),
179-
'fhir_id_v3': versioned_fhir_ids.get(Versions.V3, None),
186+
'fhir_id_v2': bfd_fhir_id_v2,
187+
'fhir_id_v3': bfd_fhir_id_v3,
180188
'user_id_type': user.crosswalk.user_id_type,
181189
},
182190
})

0 commit comments

Comments
 (0)