Skip to content

Commit a782fa2

Browse files
committed
Refactor to not update the resource
1 parent bed5535 commit a782fa2

File tree

3 files changed

+25
-138
lines changed

3 files changed

+25
-138
lines changed

lambdas/recordforwarder/src/repository/fhir_batch_repository.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
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+
2025

2126
def create_table(region_name="eu-west-2"):
2227
table_name = os.environ["DYNAMODB_TABLE_NAME"]
@@ -99,7 +104,8 @@ def create_immunization(
99104
immunization["id"] = new_id
100105
attr = RecordAttributes(immunization, vax_type, supplier_system, 0)
101106

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

104110
if query_response is not None:
105111
raise IdentifierDuplicationError(identifier=attr.identifier)
@@ -111,7 +117,7 @@ def create_immunization(
111117
"PatientPK": attr.patient_pk,
112118
"PatientSK": attr.patient_sk,
113119
"Resource": json.dumps(attr.resource, use_decimal=True),
114-
"IdentifierPK": attr.identifier,
120+
"IdentifierPK": identifier_pk,
115121
"Operation": "CREATE",
116122
"Version": attr.version,
117123
"SupplierSystem": attr.supplier,
@@ -140,10 +146,10 @@ def update_immunization(
140146
table: any,
141147
is_present: bool,
142148
) -> dict:
143-
identifier = self._identifier_response(immunization)
144-
query_response = _query_identifier(table, "IdentifierGSI", "IdentifierPK", identifier, is_present)
149+
identifier_pk = self._get_identifier_pk_from_immunization(immunization)
150+
query_response = _query_identifier(table, "IdentifierGSI", "IdentifierPK", identifier_pk, is_present)
145151
if query_response is None:
146-
raise ResourceNotFoundError(resource_type="Immunization", resource_id=identifier)
152+
raise ResourceNotFoundError(resource_type="Immunization", resource_id=identifier_pk)
147153
old_id, version = self._get_id_version(query_response)
148154
deleted_at_required, update_reinstated, is_reinstate = self._get_record_status(query_response)
149155

@@ -168,10 +174,10 @@ def delete_immunization(
168174
table: any,
169175
is_present: bool,
170176
) -> dict:
171-
identifier = self._identifier_response(immunization)
172-
query_response = _query_identifier(table, "IdentifierGSI", "IdentifierPK", identifier, is_present)
177+
identifier_pk = self._get_identifier_pk_from_immunization(immunization)
178+
query_response = _query_identifier(table, "IdentifierGSI", "IdentifierPK", identifier_pk, is_present)
173179
if query_response is None:
174-
raise ResourceNotFoundError(resource_type="Immunization", resource_id=identifier)
180+
raise ResourceNotFoundError(resource_type="Immunization", resource_id=identifier_pk)
175181
try:
176182
now_timestamp = int(time.time())
177183
imms_id = self._get_pk(query_response)
@@ -207,9 +213,19 @@ def _handle_dynamo_response(response, imms_id):
207213
raise UnhandledResponseError(message="Non-200 response from dynamodb", response=response)
208214

209215
@staticmethod
210-
def _identifier_response(immunization: any):
216+
def _get_identifier_pk_from_immunization(immunization: any):
211217
system_id = immunization["identifier"][0]["system"]
212218
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+
213229
return f"{system_id}#{system_value}"
214230

215231
@staticmethod

lambdas/recordforwarder/src/service/fhir_batch_service.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,6 @@
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-
317

328
class ImmunizationBatchService:
339
def __init__(
@@ -52,9 +28,6 @@ def create_immunization(
5228
the record in the database.
5329
"""
5430

55-
# TODO: Remove after DPS data migration to new identifiers
56-
uplift_legacy_identifier(immunization)
57-
5831
try:
5932
self.validator.validate(immunization)
6033
except (ValueError, MandatoryError) as error:
@@ -76,9 +49,6 @@ def update_immunization(
7649
the record in the database.
7750
"""
7851

79-
# TODO: Remove after DPS data migration to new identifiers
80-
uplift_legacy_identifier(immunization)
81-
8252
try:
8353
self.validator.validate(immunization)
8454
except (ValueError, MandatoryError) as error:
@@ -100,7 +70,4 @@ def delete_immunization(
10070
the record in the database.
10171
"""
10272

103-
# TODO: Remove after DPS data migration to new identifiers
104-
uplift_legacy_identifier(immunization)
105-
10673
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: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -97,38 +97,6 @@ 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-
132100

133101
class TestUpdateImmunizationBatchService(TestFhirBatchServiceBase):
134102
def setUp(self):
@@ -201,38 +169,6 @@ def test_update_immunization_post_validation_error(self):
201169
self.assertTrue(expected_msg in error.exception.message)
202170
self.mock_repo.update_immunization.assert_not_called()
203171

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-
236172

237173
class TestDeleteImmunizationBatchService(unittest.TestCase):
238174
def setUp(self):
@@ -258,38 +194,6 @@ def test_delete_immunization_valid(self):
258194
)
259195
self.assertEqual(result, imms_id)
260196

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-
293197

294198
if __name__ == "__main__":
295199
unittest.main()

0 commit comments

Comments
 (0)