|
1 | 1 | """Functions for filtering a FHIR Immunization Resource""" |
2 | 2 |
|
3 | | -from common.models.constants import Urls |
| 3 | +from common.models.constants import Constants, Urls |
4 | 4 | from common.models.utils.generic_utils import ( |
5 | 5 | get_contained_patient, |
6 | 6 | get_contained_practitioner, |
@@ -28,23 +28,34 @@ def create_reference_to_patient_resource(patient_full_url: str, patient: dict) - |
28 | 28 | """ |
29 | 29 | Returns a reference to the given patient which includes the patient nhs number identifier (system and value fields |
30 | 30 | only) and a reference to patient full url. "Type" field is set to "Patient". |
| 31 | +
|
| 32 | + Due to validation business rules, it is possible (VED-1073) for a patient to be created without a value for NHS |
| 33 | + Number or indeed an entry for their identifier. |
31 | 34 | """ |
32 | | - patient_nhs_number_identifier = [x for x in patient["identifier"] if x.get("system") == Urls.NHS_NUMBER][0] |
| 35 | + patient_nhs_number_identifier: dict | None = None |
| 36 | + |
| 37 | + for identifier in patient.get("identifier", []): |
| 38 | + if identifier.get("system", "") == Urls.NHS_NUMBER: |
| 39 | + patient_nhs_number_identifier = identifier |
| 40 | + break |
| 41 | + |
| 42 | + if patient_nhs_number_identifier is None: |
| 43 | + return { |
| 44 | + "reference": patient_full_url, |
| 45 | + "type": Constants.PATIENT_RESOURCE_TYPE, |
| 46 | + } |
33 | 47 |
|
34 | 48 | return { |
35 | 49 | "reference": patient_full_url, |
36 | | - "type": "Patient", |
37 | | - "identifier": { |
38 | | - "system": patient_nhs_number_identifier["system"], |
39 | | - "value": patient_nhs_number_identifier["value"], |
40 | | - }, |
| 50 | + "type": Constants.PATIENT_RESOURCE_TYPE, |
| 51 | + "identifier": patient_nhs_number_identifier, |
41 | 52 | } |
42 | 53 |
|
43 | 54 |
|
44 | 55 | def replace_address_postal_codes(imms: dict) -> dict: |
45 | 56 | """Replace any postal codes found in contained patient address with 'ZZ99 3CZ'""" |
46 | 57 | for resource in imms.get("contained", [{}]): |
47 | | - if resource.get("resourceType") == "Patient": |
| 58 | + if resource.get("resourceType") == Constants.PATIENT_RESOURCE_TYPE: |
48 | 59 | for address in resource.get("address", [{}]): |
49 | 60 | if address.get("postalCode") is not None: |
50 | 61 | address["postalCode"] = "ZZ99 3CZ" |
|
0 commit comments