Skip to content

Commit 19e94c6

Browse files
committed
Revert "Refactor to not update the resource"
Change in requirements - we now want to update both identifier_pk and the identifier within the resource This reverts commit a782fa2.
1 parent 838c8f7 commit 19e94c6

File tree

3 files changed

+138
-25
lines changed

3 files changed

+138
-25
lines changed

lambdas/recordforwarder/src/repository/fhir_batch_repository.py

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717
)
1818
from common.models.utils.generic_utils import get_nhs_number
1919

20-
TPP_V2_SUPPLIER_IDENTIFIER_SYSTEM = "YGA"
21-
TPP_V5_SUPPLIER_IDENTIFIER_SYSTEM = "https://tpp-uk.com/Id/ve/vacc"
22-
EMIS_V2_SUPPLIER_IDENTIFIER_SYSTEM = "YGJ"
23-
EMIS_V5_SUPPLIER_IDENTIFIER_SYSTEM = "https://emishealth.com/identifiers/vacc"
24-
2520

2621
def create_table(region_name="eu-west-2"):
2722
table_name = os.environ["DYNAMODB_TABLE_NAME"]
@@ -104,8 +99,7 @@ def create_immunization(
10499
immunization["id"] = new_id
105100
attr = RecordAttributes(immunization, vax_type, supplier_system, 0)
106101

107-
identifier_pk = self._get_identifier_pk_from_immunization(immunization)
108-
query_response = _query_identifier(table, "IdentifierGSI", "IdentifierPK", identifier_pk, is_present)
102+
query_response = _query_identifier(table, "IdentifierGSI", "IdentifierPK", attr.identifier, is_present)
109103

110104
if query_response is not None:
111105
raise IdentifierDuplicationError(identifier=attr.identifier)
@@ -117,7 +111,7 @@ def create_immunization(
117111
"PatientPK": attr.patient_pk,
118112
"PatientSK": attr.patient_sk,
119113
"Resource": json.dumps(attr.resource, use_decimal=True),
120-
"IdentifierPK": identifier_pk,
114+
"IdentifierPK": attr.identifier,
121115
"Operation": "CREATE",
122116
"Version": attr.version,
123117
"SupplierSystem": attr.supplier,
@@ -146,10 +140,10 @@ def update_immunization(
146140
table: any,
147141
is_present: bool,
148142
) -> dict:
149-
identifier_pk = self._get_identifier_pk_from_immunization(immunization)
150-
query_response = _query_identifier(table, "IdentifierGSI", "IdentifierPK", identifier_pk, is_present)
143+
identifier = self._identifier_response(immunization)
144+
query_response = _query_identifier(table, "IdentifierGSI", "IdentifierPK", identifier, is_present)
151145
if query_response is None:
152-
raise ResourceNotFoundError(resource_type="Immunization", resource_id=identifier_pk)
146+
raise ResourceNotFoundError(resource_type="Immunization", resource_id=identifier)
153147
old_id, version = self._get_id_version(query_response)
154148
deleted_at_required, update_reinstated, is_reinstate = self._get_record_status(query_response)
155149

@@ -174,10 +168,10 @@ def delete_immunization(
174168
table: any,
175169
is_present: bool,
176170
) -> dict:
177-
identifier_pk = self._get_identifier_pk_from_immunization(immunization)
178-
query_response = _query_identifier(table, "IdentifierGSI", "IdentifierPK", identifier_pk, is_present)
171+
identifier = self._identifier_response(immunization)
172+
query_response = _query_identifier(table, "IdentifierGSI", "IdentifierPK", identifier, is_present)
179173
if query_response is None:
180-
raise ResourceNotFoundError(resource_type="Immunization", resource_id=identifier_pk)
174+
raise ResourceNotFoundError(resource_type="Immunization", resource_id=identifier)
181175
try:
182176
now_timestamp = int(time.time())
183177
imms_id = self._get_pk(query_response)
@@ -213,19 +207,9 @@ def _handle_dynamo_response(response, imms_id):
213207
raise UnhandledResponseError(message="Non-200 response from dynamodb", response=response)
214208

215209
@staticmethod
216-
def _get_identifier_pk_from_immunization(immunization: any):
210+
def _identifier_response(immunization: any):
217211
system_id = immunization["identifier"][0]["system"]
218212
system_value = immunization["identifier"][0]["value"]
219-
220-
# The below checks can be safely removed once DPS carries out it's data migration to update legacy
221-
# identifiers as it should become redundant. However, it may be worth keeping in case legacy format identifiers
222-
# are received for some reason. Please see issue VED-904 for more information.
223-
if system_id == TPP_V2_SUPPLIER_IDENTIFIER_SYSTEM:
224-
system_id = TPP_V5_SUPPLIER_IDENTIFIER_SYSTEM
225-
226-
if system_id == EMIS_V2_SUPPLIER_IDENTIFIER_SYSTEM:
227-
system_id = EMIS_V5_SUPPLIER_IDENTIFIER_SYSTEM
228-
229213
return f"{system_id}#{system_value}"
230214

231215
@staticmethod

lambdas/recordforwarder/src/service/fhir_batch_service.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,30 @@
44

55
IMMUNIZATION_VALIDATOR = ImmunizationValidator()
66

7+
TPP_V2_SUPPLIER_IDENTIFIER_SYSTEM = "YGA"
8+
TPP_V5_SUPPLIER_IDENTIFIER_SYSTEM = "https://tpp-uk.com/Id/ve/vacc"
9+
EMIS_V2_SUPPLIER_IDENTIFIER_SYSTEM = "YGJ"
10+
EMIS_V5_SUPPLIER_IDENTIFIER_SYSTEM = "https://emishealth.com/identifiers/vacc"
11+
12+
13+
def uplift_legacy_identifier(immunization: dict):
14+
# This code the above constants can be safely removed once DPS carries out it's data migration to update legacy
15+
# identifiers as it should become redundant. However, it may be worth keeping in case legacy format identifiers are
16+
# received for some reason. Please see issue VED-904 for more information.
17+
identifier = immunization.get("identifier")
18+
19+
if identifier is None or len(identifier) == 0:
20+
# Return here to allow validation to raise appropriate error
21+
return
22+
23+
identifier_system = immunization.get("identifier")[0].get("system")
24+
25+
if identifier_system == TPP_V2_SUPPLIER_IDENTIFIER_SYSTEM:
26+
immunization["identifier"][0]["system"] = TPP_V5_SUPPLIER_IDENTIFIER_SYSTEM
27+
28+
if identifier_system == EMIS_V2_SUPPLIER_IDENTIFIER_SYSTEM:
29+
immunization["identifier"][0]["system"] = EMIS_V5_SUPPLIER_IDENTIFIER_SYSTEM
30+
731

832
class ImmunizationBatchService:
933
def __init__(
@@ -28,6 +52,9 @@ def create_immunization(
2852
the record in the database.
2953
"""
3054

55+
# TODO: Remove after DPS data migration to new identifiers
56+
uplift_legacy_identifier(immunization)
57+
3158
try:
3259
self.validator.validate(immunization)
3360
except (ValueError, MandatoryError) as error:
@@ -49,6 +76,9 @@ def update_immunization(
4976
the record in the database.
5077
"""
5178

79+
# TODO: Remove after DPS data migration to new identifiers
80+
uplift_legacy_identifier(immunization)
81+
5282
try:
5383
self.validator.validate(immunization)
5484
except (ValueError, MandatoryError) as error:
@@ -70,4 +100,7 @@ def delete_immunization(
70100
the record in the database.
71101
"""
72102

103+
# TODO: Remove after DPS data migration to new identifiers
104+
uplift_legacy_identifier(immunization)
105+
73106
return self.immunization_repo.delete_immunization(immunization, supplier_system, vax_type, table, is_present)

lambdas/recordforwarder/tests/service/test_fhir_batch_service.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,38 @@ def test_create_immunization_post_validation_error(self):
9797
self.assertTrue(expected_msg in error.exception.message)
9898
self.mock_repo.create_immunization.assert_not_called()
9999

100+
def test_create_immunization_uplifts_legacy_identifier_system_tpp(self):
101+
"""it should recognise a legacy TPP identifier system and use the new style identifier"""
102+
103+
immunisation = create_covid_immunization_dict_no_id()
104+
immunisation["identifier"][0]["system"] = "YGA"
105+
106+
_ = self.service.create_immunization(
107+
immunization=immunisation,
108+
supplier_system="test_supplier",
109+
vax_type="test_vax",
110+
table=self.mock_table,
111+
is_present=True,
112+
)
113+
114+
self.assertEqual(immunisation["identifier"][0]["system"], "https://tpp-uk.com/Id/ve/vacc")
115+
116+
def test_create_immunization_uplifts_legacy_identifier_system_emis(self):
117+
"""it should recognise a legacy EMIS identifier system and use the new style identifier"""
118+
119+
immunisation = create_covid_immunization_dict_no_id()
120+
immunisation["identifier"][0]["system"] = "YGJ"
121+
122+
_ = self.service.create_immunization(
123+
immunization=immunisation,
124+
supplier_system="test_supplier",
125+
vax_type="test_vax",
126+
table=self.mock_table,
127+
is_present=True,
128+
)
129+
130+
self.assertEqual(immunisation["identifier"][0]["system"], "https://emishealth.com/identifiers/vacc")
131+
100132

101133
class TestUpdateImmunizationBatchService(TestFhirBatchServiceBase):
102134
def setUp(self):
@@ -169,6 +201,38 @@ def test_update_immunization_post_validation_error(self):
169201
self.assertTrue(expected_msg in error.exception.message)
170202
self.mock_repo.update_immunization.assert_not_called()
171203

204+
def test_update_immunization_uplifts_legacy_identifier_system_tpp(self):
205+
"""it should recognise a legacy TPP identifier system and use the new style identifier"""
206+
207+
immunisation = create_covid_immunization_dict_no_id()
208+
immunisation["identifier"][0]["system"] = "YGA"
209+
210+
_ = self.service.update_immunization(
211+
immunization=immunisation,
212+
supplier_system="test_supplier",
213+
vax_type="test_vax",
214+
table=self.mock_table,
215+
is_present=True,
216+
)
217+
218+
self.assertEqual(immunisation["identifier"][0]["system"], "https://tpp-uk.com/Id/ve/vacc")
219+
220+
def test_update_immunization_uplifts_legacy_identifier_system_emis(self):
221+
"""it should recognise a legacy EMIS identifier system and use the new style identifier"""
222+
223+
immunisation = create_covid_immunization_dict_no_id()
224+
immunisation["identifier"][0]["system"] = "YGJ"
225+
226+
_ = self.service.update_immunization(
227+
immunization=immunisation,
228+
supplier_system="test_supplier",
229+
vax_type="test_vax",
230+
table=self.mock_table,
231+
is_present=True,
232+
)
233+
234+
self.assertEqual(immunisation["identifier"][0]["system"], "https://emishealth.com/identifiers/vacc")
235+
172236

173237
class TestDeleteImmunizationBatchService(unittest.TestCase):
174238
def setUp(self):
@@ -194,6 +258,38 @@ def test_delete_immunization_valid(self):
194258
)
195259
self.assertEqual(result, imms_id)
196260

261+
def test_delete_immunization_uplifts_legacy_identifier_system_tpp(self):
262+
"""it should recognise a legacy TPP identifier system and use the new style identifier"""
263+
264+
immunisation = create_covid_immunization_dict_no_id()
265+
immunisation["identifier"][0]["system"] = "YGA"
266+
267+
_ = self.service.delete_immunization(
268+
immunization=immunisation,
269+
supplier_system="test_supplier",
270+
vax_type="test_vax",
271+
table=self.mock_table,
272+
is_present=True,
273+
)
274+
275+
self.assertEqual(immunisation["identifier"][0]["system"], "https://tpp-uk.com/Id/ve/vacc")
276+
277+
def test_delete_immunization_uplifts_legacy_identifier_system_emis(self):
278+
"""it should recognise a legacy EMIS identifier system and use the new style identifier"""
279+
280+
immunisation = create_covid_immunization_dict_no_id()
281+
immunisation["identifier"][0]["system"] = "YGJ"
282+
283+
_ = self.service.delete_immunization(
284+
immunization=immunisation,
285+
supplier_system="test_supplier",
286+
vax_type="test_vax",
287+
table=self.mock_table,
288+
is_present=True,
289+
)
290+
291+
self.assertEqual(immunisation["identifier"][0]["system"], "https://emishealth.com/identifiers/vacc")
292+
197293

198294
if __name__ == "__main__":
199295
unittest.main()

0 commit comments

Comments
 (0)