77 read_product_team ,
88)
99from domain .core .cpm_product import CpmProduct
10+ from domain .core .cpm_system_id import AsidId
1011from domain .core .device import (
1112 Device ,
1213 DeviceTagAddedEvent ,
1314 QuestionnaireResponseUpdatedEvent ,
1415)
16+ from domain .core .device_key .v1 import DeviceKeyType
1517from domain .core .device_reference_data import DeviceReferenceData
1618from domain .core .error import (
1719 AccreditedSystemFatalError ,
2022)
2123from domain .core .product_key import ProductKeyType
2224from domain .core .questionnaire import Questionnaire , QuestionnaireResponse
25+ from domain .repository .cpm_system_id_repository import CpmSystemIdRepository
2326from 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+
88107def 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-
107120def 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
@@ -115,7 +131,7 @@ def add_device_reference_data_id(data, cache) -> Device:
115131 device_reference_data : DeviceReferenceData = data [read_device_reference_data ]
116132 for drd in device_reference_data :
117133 as_device .add_device_reference_data_id (
118- device_reference_data_id = str (drd .id ), path_to_data = ["*. Interaction ID" ]
134+ device_reference_data_id = str (drd .id ), path_to_data = ["Interaction ID" ]
119135 )
120136 return as_device
121137
@@ -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+
142168def 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]
0 commit comments