Skip to content

Commit 1002e6e

Browse files
committed
Merge branch 'feature/PI-666-add_asid_to_as_device_key' into release/2024-12-11
2 parents bded662 + bbf0716 commit 1002e6e

File tree

4 files changed

+244
-146
lines changed

4 files changed

+244
-146
lines changed

src/api/createDeviceAccreditedSystem/src/v1/steps.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
read_product_team,
88
)
99
from domain.core.cpm_product import CpmProduct
10+
from domain.core.cpm_system_id import AsidId
1011
from domain.core.device import (
1112
Device,
1213
DeviceTagAddedEvent,
1314
QuestionnaireResponseUpdatedEvent,
1415
)
16+
from domain.core.device_key.v1 import DeviceKeyType
1517
from domain.core.device_reference_data import DeviceReferenceData
1618
from domain.core.error import (
1719
AccreditedSystemFatalError,
@@ -20,6 +22,7 @@
2022
)
2123
from domain.core.product_key import ProductKeyType
2224
from domain.core.questionnaire import Questionnaire, QuestionnaireResponse
25+
from domain.repository.cpm_system_id_repository import CpmSystemIdRepository
2326
from domain.repository.device_reference_data_repository import (
2427
DeviceReferenceDataRepository,
2528
)
@@ -85,28 +88,41 @@ def validate_spine_as_questionnaire_response(data, cache) -> QuestionnaireRespon
8588
)
8689

8790

91+
def create_party_key_tag(data, cache) -> DeviceTagAddedEvent:
92+
as_device: Device = data[create_as_device]
93+
return as_device.add_tag(party_key=data[get_party_key])
94+
95+
96+
def create_asid(data, cache) -> AsidId:
97+
repository = CpmSystemIdRepository[AsidId](
98+
table_name=cache["DYNAMODB_TABLE"],
99+
dynamodb_client=cache["DYNAMODB_CLIENT"],
100+
model=AsidId,
101+
)
102+
asid = repository.read()
103+
new_asid = AsidId.create(current_number=asid.latest_number)
104+
return new_asid
105+
106+
88107
def create_as_device(data, cache) -> Device:
89108
product: CpmProduct = data[read_product]
109+
asid: AsidId = data[create_asid]
90110
payload: CreateAsDeviceIncomingParams = data[parse_as_device_payload]
91111
party_key: str = data[get_party_key]
92112

93-
# Create a new Device dictionary excluding 'questionnaire_responses'
94-
# Ticket PI-666 adds ASID generation. This will need to be sent across in the arguments instead of an empty string.
95113
device_payload = payload.dict(exclude={"questionnaire_responses"})
96114
return product.create_device(
97-
name=EprNameTemplate.AS_DEVICE.format(party_key=party_key, asid=""),
115+
name=EprNameTemplate.AS_DEVICE.format(party_key=party_key, asid=asid.__root__),
98116
**device_payload
99117
)
100118

101119

102-
def create_party_key_tag(data, cache) -> DeviceTagAddedEvent:
103-
as_device: Device = data[create_as_device]
104-
return as_device.add_tag(party_key=data[get_party_key])
105-
106-
107120
def create_device_keys(data, cache) -> Device:
108-
# We will need to add some keys in the future, ASID?
109121
as_device: Device = data[create_as_device]
122+
asid: AsidId = data[create_asid]
123+
as_device.add_key(
124+
key_type=DeviceKeyType.ACCREDITED_SYSTEM_ID, key_value=asid.__root__
125+
)
110126
return as_device
111127

112128

@@ -139,6 +155,16 @@ def write_device(data: dict[str, Device], cache) -> Device:
139155
return repo.write(as_device)
140156

141157

158+
def write_asid(data: dict[str, AsidId], cache) -> str:
159+
repository = CpmSystemIdRepository[AsidId](
160+
table_name=cache["DYNAMODB_TABLE"],
161+
dynamodb_client=cache["DYNAMODB_CLIENT"],
162+
model=AsidId,
163+
)
164+
asid: AsidId = data[create_asid]
165+
return repository.create_or_update(asid)
166+
167+
142168
def set_http_status(data, cache) -> tuple[HTTPStatus, dict]:
143169
as_device: Device = data[create_as_device]
144170
return HTTPStatus.CREATED, as_device.state_exclude_tags()
@@ -170,11 +196,13 @@ def get_party_key(data, cache) -> str:
170196
read_device_reference_data,
171197
read_spine_as_questionnaire,
172198
validate_spine_as_questionnaire_response,
199+
create_asid,
173200
create_as_device,
174201
create_party_key_tag,
175202
create_device_keys,
176203
add_device_reference_data_id,
177204
add_spine_as_questionnaire_response,
178205
write_device,
206+
write_asid,
179207
set_http_status,
180208
]

src/api/createDeviceAccreditedSystem/tests/test_index.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from domain.core.cpm_product import CpmProduct
1111
from domain.core.cpm_system_id import ProductId
1212
from domain.core.device import Device
13+
from domain.core.device_key.v1 import DeviceKey, DeviceKeyType
1314
from domain.core.product_key import ProductKeyType
1415
from domain.core.root import Root
1516
from domain.repository.cpm_product_repository import CpmProductRepository
@@ -425,7 +426,7 @@ def test_index() -> None:
425426
device = Device(**_device)
426427
assert device.product_team_id == product.product_team_id
427428
assert device.product_id == product.id
428-
assert device.name == "ABC1234-987654/ - Accredited System"
429+
assert device.name == "ABC1234-987654/200000100000 - Accredited System"
429430
assert device.ods_code == ODS_CODE
430431
assert device.created_on.date() == datetime.today().date()
431432
assert device.updated_on.date() == datetime.today().date()
@@ -451,6 +452,13 @@ def test_index() -> None:
451452
expected_party_key = (str(ProductKeyType.PARTY_KEY), "abc1234-987654")
452453
assert any(expected_party_key in tag.__root__ for tag in created_device.tags)
453454

455+
# Check an ASID is generated and added to the keys.
456+
assert isinstance(created_device.keys[0], DeviceKey)
457+
assert created_device.keys[0].__dict__ == {
458+
"key_type": DeviceKeyType.ACCREDITED_SYSTEM_ID,
459+
"key_value": "200000100000",
460+
}
461+
454462

455463
@pytest.mark.parametrize(
456464
["body", "path_parameters", "error_code", "status_code"],

0 commit comments

Comments
 (0)