8
8
read_product_team ,
9
9
)
10
10
from domain .core .cpm_product import CpmProduct
11
- from domain .core .device import Device
12
- from domain .core .error import InvalidSpineMhsResponse
11
+ from domain .core .device import (
12
+ MHS_DEVICE_NAME ,
13
+ Device ,
14
+ DeviceKeyAddedEvent ,
15
+ DeviceReferenceDataIdAddedEvent ,
16
+ DeviceTagAddedEvent ,
17
+ QuestionnaireResponseUpdatedEvent ,
18
+ )
19
+ from domain .core .device_key import DeviceKeyType
20
+ from domain .core .device_reference_data import DeviceReferenceData
21
+ from domain .core .error import ConfigurationError
22
+ from domain .core .product_team import ProductTeam
13
23
from domain .core .questionnaire import Questionnaire , QuestionnaireResponse
24
+ from domain .repository .device_reference_data_repository import (
25
+ DeviceReferenceDataRepository ,
26
+ )
14
27
from domain .repository .device_repository import DeviceRepository
15
28
from domain .repository .questionnaire_repository import (
16
29
QuestionnaireInstance ,
17
30
QuestionnaireRepository ,
18
31
)
19
- from domain .request_models import CreateMhsDeviceIncomingParams
32
+ from domain .request_models import CpmProductPathParams , CreateMhsDeviceIncomingParams
20
33
from domain .response .validation_errors import mark_validation_errors_as_inbound
21
34
22
35
23
36
@mark_validation_errors_as_inbound
24
- def parse_mhs_device_payload (data , cache ) -> Device :
37
+ def parse_mhs_device_payload (data , cache ) -> CreateMhsDeviceIncomingParams :
25
38
payload : dict = data [parse_event_body ]
26
39
return CreateMhsDeviceIncomingParams (** payload )
27
40
28
41
42
+ def check_for_existing_mhs (data , cache ):
43
+ product_team : ProductTeam = data [read_product_team ]
44
+ product : CpmProduct = data [read_product ]
45
+
46
+ device_repo = DeviceRepository (
47
+ table_name = cache ["DYNAMODB_TABLE" ], dynamodb_client = cache ["DYNAMODB_CLIENT" ]
48
+ )
49
+
50
+ devices = device_repo .search (product_team_id = product_team .id , product_id = product .id )
51
+ if any (device .name == MHS_DEVICE_NAME for device in devices ):
52
+ raise ConfigurationError (
53
+ "There is already an existing MHS Device for this Product"
54
+ )
55
+
56
+
57
+ def read_device_reference_data (data , cache ) -> DeviceReferenceData :
58
+ path_params : CpmProductPathParams = data [parse_path_params ]
59
+ drd_repo = DeviceReferenceDataRepository (
60
+ table_name = cache ["DYNAMODB_TABLE" ], dynamodb_client = cache ["DYNAMODB_CLIENT" ]
61
+ )
62
+ device_reference_datas = drd_repo .search (
63
+ product_id = path_params .product_id ,
64
+ product_team_id = path_params .product_team_id ,
65
+ )
66
+
67
+ party_key : str = data [get_party_key ]
68
+ # use {QuestionnaireInstance.SPINE_MHS_MESSAGE_SETS}
69
+ mhs_message_set_drd_name = f"{ party_key } - MHS Message Set"
70
+
71
+ try :
72
+ (device_reference_data ,) = filter (
73
+ lambda drd : drd .name == mhs_message_set_drd_name , device_reference_datas
74
+ )
75
+ except ValueError :
76
+ raise ConfigurationError (
77
+ "You must configure exactly one MessageSet Device Reference Data before creating an MHS Device"
78
+ )
79
+ return device_reference_data
80
+
81
+
29
82
def read_spine_mhs_questionnaire (data , cache ) -> Questionnaire :
30
83
return QuestionnaireRepository ().read (QuestionnaireInstance .SPINE_MHS )
31
84
32
85
33
86
def validate_spine_mhs_questionnaire_response (data , cache ) -> QuestionnaireResponse :
34
87
spine_mhs_questionnaire : Questionnaire = data [read_spine_mhs_questionnaire ]
35
88
payload : CreateMhsDeviceIncomingParams = data [parse_mhs_device_payload ]
36
- questionnaire_responses = payload .questionnaire_responses
37
89
38
- # Ensure there's a questionnaire named 'spine_mhs' in the responses
39
- if QuestionnaireInstance .SPINE_MHS not in questionnaire_responses :
40
- raise InvalidSpineMhsResponse (
41
- "Require a 'spine_mhs' questionnaire response to create a MHS Device"
42
- )
43
-
44
- raw_spine_mhs_questionnaire_response = payload .questionnaire_responses [
90
+ spine_mhs_questionnaire_response = payload .questionnaire_responses [
45
91
QuestionnaireInstance .SPINE_MHS
46
92
]
47
- # Ensure there's only one response to 'spine_mhs'
48
- if len (raw_spine_mhs_questionnaire_response ) != 1 :
49
- raise InvalidSpineMhsResponse (
50
- "Expected only one response for the 'spine_mhs' questionnaire"
51
- )
52
93
53
94
return spine_mhs_questionnaire .validate (
54
- data = raw_spine_mhs_questionnaire_response [0 ]
95
+ data = spine_mhs_questionnaire_response . __root__ [0 ]
55
96
)
56
97
57
98
@@ -64,32 +105,63 @@ def create_mhs_device(data, cache) -> Device:
64
105
return product .create_device (** device_payload )
65
106
66
107
67
- def create_party_key_tag (data , cache ):
108
+ def create_party_key_tag (data , cache ) -> DeviceTagAddedEvent :
109
+ mhs_device : Device = data [create_mhs_device ]
110
+ return mhs_device .add_tag (party_key = data [get_party_key ])
111
+
112
+
113
+ def create_cpa_id_keys (data , cache ) -> DeviceKeyAddedEvent :
68
114
mhs_device : Device = data [create_mhs_device ]
69
- mhs_device .add_tag (party_key = data [get_party_key ])
115
+ party_key = data [get_party_key ]
116
+ drd : DeviceReferenceData = data [read_device_reference_data ]
117
+ interaction_ids = []
118
+
119
+ # Extract Interaction IDs from questionnaire responses
120
+ questionnaire_responses = drd .questionnaire_responses .get (
121
+ f"{ QuestionnaireInstance .SPINE_MHS_MESSAGE_SETS } /1" , []
122
+ )
123
+ for response in questionnaire_responses :
124
+ interaction_ids .append (response .data .get ("Interaction ID" ))
125
+
126
+ # Use cpa_id in furture
127
+ for id in interaction_ids :
128
+ mhs_device .add_key (
129
+ key_type = DeviceKeyType .INTERACTION_ID , key_value = f"{ party_key } :{ id } "
130
+ )
131
+
70
132
return mhs_device
71
133
72
134
73
- def add_spine_mhs_questionnaire_response (data , cache ) -> list [QuestionnaireResponse ]:
135
+ def add_device_reference_data_id (data , cache ) -> DeviceReferenceDataIdAddedEvent :
136
+ mhs_device : Device = data [create_mhs_device ]
137
+ drd : DeviceReferenceData = data [read_device_reference_data ]
138
+ return mhs_device .add_device_reference_data_id (
139
+ device_reference_data_id = str (drd .id ), path_to_data = ["*" ]
140
+ )
141
+
142
+
143
+ def add_spine_mhs_questionnaire_response (
144
+ data , cache
145
+ ) -> QuestionnaireResponseUpdatedEvent :
74
146
spine_mhs_questionnaire_response : QuestionnaireResponse = data [
75
147
validate_spine_mhs_questionnaire_response
76
148
]
77
- mhs_device : Device = data [create_party_key_tag ]
78
- mhs_device . add_questionnaire_response ( spine_mhs_questionnaire_response )
79
- return mhs_device
149
+ mhs_device : Device = data [create_mhs_device ]
150
+
151
+ return mhs_device . add_questionnaire_response ( spine_mhs_questionnaire_response )
80
152
81
153
82
- def write_device (data : dict [str , CpmProduct ], cache ) -> CpmProduct :
83
- mhs_device : Device = data [add_spine_mhs_questionnaire_response ]
154
+ def write_device (data : dict [str , Device ], cache ) -> Device :
155
+ mhs_device : Device = data [create_mhs_device ]
84
156
repo = DeviceRepository (
85
157
table_name = cache ["DYNAMODB_TABLE" ], dynamodb_client = cache ["DYNAMODB_CLIENT" ]
86
158
)
87
159
return repo .write (mhs_device )
88
160
89
161
90
- def set_http_status (data , cache ) -> tuple [HTTPStatus , str ]:
91
- device : Device = data [create_mhs_device ]
92
- return HTTPStatus .CREATED , device .state_exclude_tags ()
162
+ def set_http_status (data , cache ) -> tuple [HTTPStatus , dict ]:
163
+ mhs_device : Device = data [create_mhs_device ]
164
+ return HTTPStatus .CREATED , mhs_device .state_exclude_tags ()
93
165
94
166
95
167
steps = [
@@ -99,10 +171,14 @@ def set_http_status(data, cache) -> tuple[HTTPStatus, str]:
99
171
read_product_team ,
100
172
read_product ,
101
173
get_party_key ,
174
+ check_for_existing_mhs ,
175
+ read_device_reference_data ,
102
176
read_spine_mhs_questionnaire ,
103
177
validate_spine_mhs_questionnaire_response ,
104
178
create_mhs_device ,
105
179
create_party_key_tag ,
180
+ create_cpa_id_keys ,
181
+ add_device_reference_data_id ,
106
182
add_spine_mhs_questionnaire_response ,
107
183
write_device ,
108
184
set_http_status ,
0 commit comments