Skip to content

Commit 9c1e1f2

Browse files
authored
VED-348-names (#577)
Fix name for delta conversion
1 parent 483c5dd commit 9c1e1f2

File tree

4 files changed

+129
-24
lines changed

4 files changed

+129
-24
lines changed

delta_backend/src/extractor.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,29 @@ def _get_patient(self):
2828
contained = self.fhir_json_data.get("contained", [])
2929
return next((c for c in contained if isinstance(c, dict) and c.get("resourceType") == "Patient"), "")
3030

31-
def _get_valid_names(self, names, occurrence_time, resource_type="Patient"):
32-
33-
def has_required_fields(name):
34-
return "given" in name and "family" in name if resource_type == "Patient" else True
31+
def _get_valid_names(self, names, occurrence_time):
3532

3633
official_names = [n for n in names if n.get("use") == "official" and self._is_current_period(n, occurrence_time)]
3734
if official_names:
38-
filtered_official_names = [n for n in official_names if has_required_fields(n)]
39-
return filtered_official_names[0]
35+
return official_names[0]
4036

4137
valid_names = [n for n in names if self._is_current_period(n, occurrence_time) and n.get("use") != "old"]
42-
filtered_valid_names = [n for n in valid_names if has_required_fields(n)]
38+
if valid_names:
39+
return valid_names[0]
4340

44-
return filtered_valid_names[0] if filtered_valid_names else names[0]
41+
return names[0]
4542

4643

4744

4845
def _get_person_names(self):
4946
occurrence_time = self._get_occurance_date_time()
5047
patient = self._get_patient()
5148
names = patient.get("name", [])
52-
53-
if not isinstance(names, list) or not names:
49+
names = [n for n in names if "given" in n and "family" in n]
50+
if not names:
5451
return "", ""
5552

56-
selected_name = self._get_valid_names(names, occurrence_time, resource_type="Patient")
53+
selected_name = self._get_valid_names(names, occurrence_time)
5754
person_forename = " ".join(selected_name.get("given", []))
5855
person_surname = selected_name.get("family", "")
5956

@@ -74,7 +71,7 @@ def _get_practitioner_names(self):
7471
if not valid_practitioner_names:
7572
return "", ""
7673

77-
selected_practitioner_name = self._get_valid_names(valid_practitioner_names, occurrence_time, resource_type="Practitioner")
74+
selected_practitioner_name = self._get_valid_names(valid_practitioner_names, occurrence_time)
7875
performing_professional_forename = " ".join(selected_practitioner_name.get("given", []))
7976
performing_professional_surname = selected_practitioner_name.get("family", "")
8077

delta_backend/tests/sample_data/fhir_sample.json

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,39 @@
66
"resourceType": "Practitioner",
77
"id": "Pract1",
88
"name": [
9-
{
10-
"family": "Nightingale",
11-
"given": ["Florence"]
12-
}
13-
]
9+
{
10+
"family": "Furlong old 1",
11+
"given": [
12+
"Darren 2", "old use value"
13+
],
14+
"period": {
15+
"start": "2000-01-01",
16+
"end": "2030-01-01"
17+
}
18+
},
19+
{
20+
"family": "Furlong old",
21+
"given": [
22+
"Darren", "old use value"
23+
],
24+
"use": "old",
25+
"period": {
26+
"start": "2000-01-01",
27+
"end": "2030-01-01"
28+
}
29+
},
30+
{
31+
"family": "Furlong official",
32+
"given": [
33+
"Darren", "Official"
34+
],
35+
"use": "official",
36+
"period": {
37+
"start": "2000-01-01",
38+
"end": "2030-01-01"
39+
}
40+
}
41+
]
1442
},
1543
{
1644
"resourceType": "Patient",
@@ -22,11 +50,43 @@
2250
}
2351
],
2452
"name": [
25-
{
26-
"family": "Taylor",
27-
"given": ["Sarah"]
28-
}
29-
],
53+
{
54+
"use": "home",
55+
"text": "hello pat1",
56+
"family": "test10 ",
57+
"given": [
58+
"test11", "test12", "test13"
59+
],
60+
"period": {
61+
"start" : "2000-01-01",
62+
"end" : "2026-01-01"
63+
}
64+
},
65+
{
66+
"use": "official",
67+
"text": "hello pat2",
68+
"family": "test12",
69+
"given": [
70+
"test13", "test14", "test15"
71+
],
72+
"period": {
73+
"start" : "2000-01-01",
74+
"end" : "2026-01-01"
75+
}
76+
},
77+
{
78+
"use": "old",
79+
"text": "hello pat3",
80+
"family": "test14",
81+
"given": [
82+
"test15", "test16", "test17"
83+
],
84+
"period": {
85+
"start" : "2000-01-01",
86+
"end" : "2026-01-01"
87+
}
88+
}
89+
],
3090
"gender": "other",
3191
"birthDate": "2026-03-10",
3292
"address": [
@@ -115,7 +175,7 @@
115175
"patient": {
116176
"reference": "#Pat1"
117177
},
118-
"occurrenceDateTime": "2025-04-06T13:28:17.271+02:00",
178+
"occurrenceDateTime": "2025-03-06T13:28:17.2+01:00",
119179
"recorded": "2025-01-01T00:00:00.000000",
120180
"primarySource": "True",
121181
"manufacturer": {
@@ -129,7 +189,7 @@
129189
}
130190
},
131191
"lotNumber": "4120Z001",
132-
"expirationDate": "2024-10-05",
192+
"expirationDate": "2025-07-02",
133193

134194
"site": {
135195
"coding": [

delta_backend/tests/test_convert_person_forename.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
from common.mappings import ConversionFieldName
77

88
class TestPersonForenmeToFlatJson(unittest.TestCase):
9+
""""
10+
Test cases for converting person forename to flat JSON format.
11+
## 1. If there is only one name use that one, else
12+
## 2. Select first name where Use=official with period covering vaccination date, else
13+
## 3. select instance where current name with use!=old at vaccination date
14+
## 4. Fallback to first available name instance
15+
"""
916

1017
def setUp(self):
1118
self.request_json_data = copy.deepcopy(ValuesForTests.json_data)
@@ -104,6 +111,23 @@ def test_person_forename_multiple_given_names_concatenation(self):
104111
expected_forename = "Alice Marie"
105112
self._run_test(expected_forename)
106113

114+
def test_person_forename_where_names_only_exist_when_use_isequal_to_old(self):
115+
"""Test case where only available name is selected name where use=old"""
116+
self.request_json_data["contained"][1]["name"] = [
117+
{
118+
"use": "official",
119+
"period": {"start": "2021-01-01", "end": "2023-01-01"},
120+
},
121+
{
122+
"family": "Doe",
123+
"given": ["Alice", "Marie"],
124+
"use": "old",
125+
"period": {"start": "2021-01-01", "end": "2022-12-31"},
126+
},
127+
]
128+
expected_forename = "Alice Marie"
129+
self._run_test(expected_forename)
130+
107131
def test_person_forename_exists_only(self):
108132
"""Test case where the selected name has multiple given names"""
109133
self.request_json_data["contained"][1]["name"] = [

delta_backend/tests/test_convert_person_surname.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
from common.mappings import ConversionFieldName
77

88
class TestPersonSurnameToFlatJson(unittest.TestCase):
9+
""""
10+
Test cases for converting person surname to flat JSON format.
11+
## 1. If there is only one name use that one, else
12+
## 2. Select first name where Use=official with period covering vaccination date, else
13+
## 3. select instance where current name with use!=old at vaccination date
14+
## 4. Fallback to first available name instance
15+
"""
916

1017
def setUp(self):
1118
self.request_json_data = copy.deepcopy(ValuesForTests.json_data)
@@ -50,6 +57,23 @@ def test_person_surname_multiple_names_current(self):
5057
expected_surname = "Manny"
5158
self._run_test_surname(expected_surname)
5259

60+
def test_person_surname_where_names_only_exist_when_use_isequal_to_old(self):
61+
"""Test case where only available name is selected name where use=old"""
62+
self.request_json_data["contained"][1]["name"] = [
63+
{
64+
"use": "official",
65+
"period": {"start": "2021-01-01", "end": "2023-01-01"},
66+
},
67+
{
68+
"family": "Manny",
69+
"given": ["Alice", "Marie"],
70+
"use": "old",
71+
"period": {"start": "2021-01-01", "end": "2022-12-31"},
72+
},
73+
]
74+
expected_surname = "Manny"
75+
self._run_test_surname(expected_surname)
76+
5377
def test_person_surname_single_name(self):
5478
"""Test case where only one name instance exists"""
5579
self.request_json_data["contained"][1]["name"] = [{"family": "Doe", "given": ["Alex"], "use": "nickname"}]

0 commit comments

Comments
 (0)