|
7 | 7 | read_product_team, |
8 | 8 | ) |
9 | 9 | from domain.core.cpm_product import CpmProduct |
| 10 | +from domain.core.cpm_system_id import AsidId |
10 | 11 | from domain.core.device import ( |
11 | 12 | Device, |
12 | 13 | DeviceTagAddedEvent, |
13 | 14 | QuestionnaireResponseUpdatedEvent, |
14 | 15 | ) |
| 16 | +from domain.core.device_key.v1 import DeviceKeyType |
15 | 17 | from domain.core.device_reference_data import DeviceReferenceData |
16 | 18 | from domain.core.error import ( |
17 | 19 | AccreditedSystemFatalError, |
|
20 | 22 | ) |
21 | 23 | from domain.core.product_key import ProductKeyType |
22 | 24 | from domain.core.questionnaire import Questionnaire, QuestionnaireResponse |
| 25 | +from domain.repository.cpm_system_id_repository import CpmSystemIdRepository |
23 | 26 | from domain.repository.device_reference_data_repository import ( |
24 | 27 | DeviceReferenceDataRepository, |
25 | 28 | ) |
@@ -85,28 +88,41 @@ def validate_spine_as_questionnaire_response(data, cache) -> QuestionnaireRespon |
85 | 88 | ) |
86 | 89 |
|
87 | 90 |
|
| 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 | + |
88 | 107 | def create_as_device(data, cache) -> Device: |
89 | 108 | product: CpmProduct = data[read_product] |
| 109 | + asid: AsidId = data[create_asid] |
90 | 110 | payload: CreateAsDeviceIncomingParams = data[parse_as_device_payload] |
91 | 111 | party_key: str = data[get_party_key] |
92 | 112 |
|
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. |
95 | 113 | device_payload = payload.dict(exclude={"questionnaire_responses"}) |
96 | 114 | 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__), |
98 | 116 | **device_payload |
99 | 117 | ) |
100 | 118 |
|
101 | 119 |
|
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 | | - |
107 | 120 | def create_device_keys(data, cache) -> Device: |
108 | | - # We will need to add some keys in the future, ASID? |
109 | 121 | 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 | + ) |
110 | 126 | return as_device |
111 | 127 |
|
112 | 128 |
|
@@ -139,6 +155,16 @@ def write_device(data: dict[str, Device], cache) -> Device: |
139 | 155 | return repo.write(as_device) |
140 | 156 |
|
141 | 157 |
|
| 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 | + |
142 | 168 | def set_http_status(data, cache) -> tuple[HTTPStatus, dict]: |
143 | 169 | as_device: Device = data[create_as_device] |
144 | 170 | return HTTPStatus.CREATED, as_device.state_exclude_tags() |
@@ -170,11 +196,13 @@ def get_party_key(data, cache) -> str: |
170 | 196 | read_device_reference_data, |
171 | 197 | read_spine_as_questionnaire, |
172 | 198 | validate_spine_as_questionnaire_response, |
| 199 | + create_asid, |
173 | 200 | create_as_device, |
174 | 201 | create_party_key_tag, |
175 | 202 | create_device_keys, |
176 | 203 | add_device_reference_data_id, |
177 | 204 | add_spine_as_questionnaire_response, |
178 | 205 | write_device, |
| 206 | + write_asid, |
179 | 207 | set_http_status, |
180 | 208 | ] |
0 commit comments