Skip to content

Commit a4d244f

Browse files
authored
Merge pull request #90 from itk-dev-rpa/release/2.8.0
Release/2.8.0
2 parents 06eb9ac + a257404 commit a4d244f

File tree

6 files changed

+97
-19
lines changed

6 files changed

+97
-19
lines changed

changelog.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [2.8.0] - 2024-10-02
11+
12+
### Added
13+
14+
- misc.cpr_util: Function to get birth date from cpr number.
15+
- kmd_nova.nova_cases: Function to get a case on its uuid.
16+
1017
## [2.7.1] - 2024-09-24
1118

1219
### Fixed
@@ -174,7 +181,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
174181

175182
- Initial release
176183

177-
[Unreleased]: https://github.com/itk-dev-rpa/ITK-dev-shared-components/compare/2.7.1...HEAD
184+
[Unreleased]: https://github.com/itk-dev-rpa/ITK-dev-shared-components/compare/2.8.0...HEAD
185+
[2.8.0]: https://github.com/itk-dev-rpa/ITK-dev-shared-components/releases/tag/2.8.0
178186
[2.7.1]: https://github.com/itk-dev-rpa/ITK-dev-shared-components/releases/tag/2.7.1
179187
[2.7.0]: https://github.com/itk-dev-rpa/ITK-dev-shared-components/releases/tag/2.7.0
180188
[2.6.1]: https://github.com/itk-dev-rpa/ITK-dev-shared-components/releases/tag/2.6.1

itk_dev_shared_components/kmd_nova/nova_cases.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@
1212
from itk_dev_shared_components.kmd_nova.util import datetime_from_iso_string
1313

1414

15+
def get_case(case_uuid: str, nova_access: NovaAccess) -> NovaCase:
16+
"""Get a case from based on its uuid.
17+
18+
Args:
19+
case_uuid: The uuid of the case to get.
20+
nova_access: The NovaAccess object used to authenticate.
21+
22+
Raises:
23+
ValueError: If no case was found.
24+
25+
Returns:
26+
The case with the given uuid.
27+
"""
28+
payload = _create_payload(case_uuid=case_uuid)
29+
cases = _get_nova_cases(nova_access, payload)
30+
31+
if not cases:
32+
raise ValueError(f"No case found with the given uuid: {case_uuid}")
33+
34+
return cases[0]
35+
36+
1537
def get_cases(nova_access: NovaAccess, cpr: str = None, case_number: str = None, case_title: str = None, limit: int = 100) -> list[NovaCase]:
1638
"""Search for cases on different search terms.
1739
Currently supports search on cpr number, case number and case title. At least one search term must be given.
@@ -33,7 +55,7 @@ def get_cases(nova_access: NovaAccess, cpr: str = None, case_number: str = None,
3355
if not any((cpr, case_number, case_title)):
3456
raise ValueError("No search terms given.")
3557

36-
payload = _create_payload(cpr, "CprNummer", case_number, case_title, limit)
58+
payload = _create_payload(identification=cpr, identification_type="CprNummer", case_number=case_number, case_title=case_title, limit=limit)
3759
return _get_nova_cases(nova_access, payload)
3860

3961

@@ -58,7 +80,7 @@ def get_cvr_cases(nova_access: NovaAccess, cvr: str = None, case_number: str =
5880
if not any((cvr, case_number, case_title)):
5981
raise ValueError("No search terms given.")
6082

61-
payload = _create_payload(cvr, "CvrNummer", case_number, case_title, limit)
83+
payload = _create_payload(identification=cvr, identification_type="CvrNummer", case_number=case_number, case_title=case_title, limit=limit)
6284
return _get_nova_cases(nova_access, payload)
6385

6486

@@ -113,10 +135,11 @@ def _get_nova_cases(nova_access: NovaAccess, payload: dict) -> list[NovaCase]:
113135
return cases
114136

115137

116-
def _create_payload(identification: str = None, identification_type: str = "CprNummer", case_number: str = None, case_title: str = None, limit: int = 100) -> dict:
138+
def _create_payload(*, case_uuid: str = None, identification: str = None, identification_type: str = "CprNummer", case_number: str = None, case_title: str = None, limit: int = 100) -> dict:
117139
return {
118140
"common": {
119-
"transactionId": str(uuid.uuid4())
141+
"transactionId": str(uuid.uuid4()),
142+
"uuid": case_uuid
120143
},
121144
"paging": {
122145
"startRow": 1,

itk_dev_shared_components/misc/cpr_util.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
from datetime import date
44

55

6-
def get_age(cpr: str, current_date: date = date.today()) -> int:
7-
"""Get the age of a person based on their cpr number
6+
def get_birth_date(cpr: str) -> date:
7+
"""Get the birth date of a person based on their cpr number
88
using the 7th digit to infer the century.
99
1010
Args:
1111
cpr: The cpr number in the format 'ddmmyyxxxx'.
12-
current_date: The date where the age is calculated from. Defaults to the current date.
1312
1413
Returns:
15-
The age in integer years based on the cpr number.
14+
The birth date based on cpr number.
1615
1716
Raises:
1817
ValueError: If the given cpr is not in 'ddmmyyxxxx' format.
@@ -51,7 +50,24 @@ def get_age(cpr: str, current_date: date = date.today()) -> int:
5150
else:
5251
year += t[2]
5352

54-
birthdate = date(year, month, day)
53+
return date(year, month, day)
54+
55+
56+
def get_age(cpr: str, current_date: date = date.today()) -> int:
57+
"""Get the age of a person based on their cpr number
58+
using the 7th digit to infer the century.
59+
60+
Args:
61+
cpr: The cpr number in the format 'ddmmyyxxxx'.
62+
current_date: The date where the age is calculated from. Defaults to the current date.
63+
64+
Returns:
65+
The age in integer years based on the cpr number.
66+
67+
Raises:
68+
ValueError: If the given cpr is not in 'ddmmyyxxxx' format.
69+
"""
70+
birthdate = get_birth_date(cpr)
5571

5672
age = current_date.year - birthdate.year - ((current_date.month, current_date.day) < (birthdate.month, birthdate.day))
5773

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "itk_dev_shared_components"
7-
version = "2.7.1"
7+
version = "2.8.0"
88
authors = [
99
{ name="ITK Development", email="itk-rpa@mkb.aarhus.dk" },
1010
]

tests/test_misc/test_cpr_util.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,38 @@ def test_get_age_from_cpr(self):
4343
with self.assertRaises(ValueError):
4444
cpr_util.get_age("010190xxxx")
4545

46+
def test_get_birth_date_from_cpr(self):
47+
"""Test getting the age from a cpr number."""
48+
# A table of fictional cpr-numbers and the expected birth dates.
49+
test_data = (
50+
("0101920000", date(1992, 1, 1)),
51+
("0101921000", date(1992, 1, 1)),
52+
("0101922000", date(1992, 1, 1)),
53+
("0101923000", date(1992, 1, 1)),
54+
("0101154000", date(2015, 1, 1)),
55+
("0101924000", date(1992, 1, 1)),
56+
("0101155000", date(2015, 1, 1)),
57+
("0101925000", date(1892, 1, 1)),
58+
("0101156000", date(2015, 1, 1)),
59+
("0101926000", date(1892, 1, 1)),
60+
("0101157000", date(2015, 1, 1)),
61+
("0101927000", date(1892, 1, 1)),
62+
("0101158000", date(2015, 1, 1)),
63+
("0101928000", date(1892, 1, 1)),
64+
("0101159000", date(2015, 1, 1)),
65+
("0101929000", date(1992, 1, 1)),
66+
)
67+
68+
for cpr, birth_date in test_data:
69+
with self.subTest(cpr=cpr, birth_date=birth_date):
70+
self.assertEqual(cpr_util.get_birth_date(cpr), birth_date)
71+
72+
with self.assertRaises(ValueError):
73+
cpr_util.get_birth_date("010190-1234")
74+
75+
with self.assertRaises(ValueError):
76+
cpr_util.get_birth_date("010190xxxx")
77+
4678

4779
if __name__ == '__main__':
4880
unittest.main()

tests/test_nova_api/test_cases.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ def test_get_cvr_cases(self):
8787
nova_cases.get_cvr_cases(nova_access=self.nova_access)
8888

8989
def test_add_case(self):
90-
"""Test adding a case to Nova."""
90+
"""Test adding a case to Nova.
91+
Also tests getting a case on uuid.
92+
"""
9193
nova_party = os.getenv('NOVA_PARTY').split(',')
9294
party = CaseParty(
9395
role="Primær",
@@ -126,17 +128,14 @@ def test_add_case(self):
126128
nova_case = None
127129
for _ in range(10):
128130
time.sleep(1)
129-
cases = nova_cases.get_cases(self.nova_access, cpr=party.identification, case_title="Test")
130-
for c in cases:
131-
if c.title == case.title:
132-
nova_case = c
133-
break
134-
if nova_case:
131+
try:
132+
nova_case = nova_cases.get_case(case.uuid, self.nova_access)
135133
break
134+
except ValueError:
135+
pass
136136

137137
self.assertIsNotNone(nova_case)
138138

139-
nova_case = cases[0]
140139
self.assertEqual(nova_case.uuid, case.uuid)
141140
self.assertEqual(nova_case.title, case.title)
142141
self.assertEqual(nova_case.progress_state, case.progress_state)

0 commit comments

Comments
 (0)