1010from flask import Flask
1111from werkzeug .exceptions import HTTPException
1212
13- from commons .data_access_layer .database import CRUDDao
14- from time_tracker_api .security import current_user_tenant_id
13+ from commons .data_access_layer .database import CRUDDao , EventContext
1514
1615
1716class CosmosDBFacade :
@@ -140,23 +139,35 @@ def replace_empty_value_per_none(item_data: dict) -> dict:
140139 if isinstance (v , str ) and len (v ) == 0 :
141140 item_data [k ] = None
142141
143- def create (self , data : dict , mapper : Callable = None ):
144- self .on_create (data )
142+ def attach_context (data : dict , event_context : EventContext ):
143+ data ["_last_event_ctx" ] = {
144+ "user_id" : event_context .user_id ,
145+ "tenant_id" : event_context .tenant_id ,
146+ "action" : event_context .action ,
147+ "description" : event_context .description ,
148+ "container_id" : event_context .container_id ,
149+ "session_id" : event_context .session_id ,
150+ }
151+
152+ def create (self , data : dict , event_context : EventContext , mapper : Callable = None ):
153+ self .on_create (data , event_context )
145154 function_mapper = self .get_mapper_or_dict (mapper )
155+ self .attach_context (data , event_context )
146156 return function_mapper (self .container .create_item (body = data ))
147157
148- def find (self , id : str , partition_key_value , peeker : 'function' = None ,
158+ def find (self , id : str , event_context : EventContext , peeker : 'function' = None ,
149159 visible_only = True , mapper : Callable = None ):
160+ partition_key_value = self .find_partition_key_value (event_context )
150161 found_item = self .container .read_item (id , partition_key_value )
151162 if peeker :
152163 peeker (found_item )
153164
154165 function_mapper = self .get_mapper_or_dict (mapper )
155166 return function_mapper (self .check_visibility (found_item , visible_only ))
156167
157- def find_all (self , partition_key_value : str , conditions : dict = {}, max_count = None , offset = 0 ,
158- visible_only = True , mapper : Callable = None ):
159- # TODO Use the tenant_id param and change container alias
168+ def find_all (self , event_context : EventContext , conditions : dict = {}, max_count = None ,
169+ offset = 0 , visible_only = True , mapper : Callable = None ):
170+ partition_key_value = self . find_partition_key_value ( event_context )
160171 max_count = self .get_page_size_or (max_count )
161172 params = [
162173 {"name" : "@partition_key_value" , "value" : partition_key_value },
@@ -179,27 +190,32 @@ def find_all(self, partition_key_value: str, conditions: dict = {}, max_count=No
179190 function_mapper = self .get_mapper_or_dict (mapper )
180191 return list (map (function_mapper , result ))
181192
182- def partial_update (self , id : str , changes : dict , partition_key_value : str ,
193+ def partial_update (self , id : str , changes : dict , event_context : EventContext ,
183194 peeker : 'function' = None , visible_only = True , mapper : Callable = None ):
184- item_data = self .find (id , partition_key_value , peeker = peeker ,
185- visible_only = visible_only , mapper = dict )
195+ item_data = self .find (id , event_context , peeker = peeker , visible_only = visible_only , mapper = dict )
186196 item_data .update (changes )
187- return self .update (id , item_data , mapper = mapper )
197+ return self .update (id , item_data , event_context = event_context , mapper = mapper )
188198
189- def update (self , id : str , item_data : dict , mapper : Callable = None ):
199+ def update (self , id : str , item_data : dict , event_context : EventContext ,
200+ mapper : Callable = None ):
190201 self .on_update (item_data )
191202 function_mapper = self .get_mapper_or_dict (mapper )
203+ self .attach_context (item_data , event_context )
192204 return function_mapper (self .container .replace_item (id , body = item_data ))
193205
194- def delete (self , id : str , partition_key_value : str ,
206+ def delete (self , id : str , event_context : EventContext ,
195207 peeker : 'function' = None , mapper : Callable = None ):
208+ partition_key_value = self .find_partition_key_value (event_context )
196209 return self .partial_update (id , {
197210 'deleted' : str (uuid .uuid4 ())
198211 }, partition_key_value , peeker = peeker , visible_only = True , mapper = mapper )
199212
200213 def delete_permanently (self , id : str , partition_key_value : str ) -> None :
201214 self .container .delete_item (id , partition_key_value )
202215
216+ def find_partition_key_value (self , event_context : EventContext ):
217+ return getattr (event_context , self .partition_key_attribute )
218+
203219 def get_mapper_or_dict (self , alternative_mapper : Callable ) -> Callable :
204220 return alternative_mapper or self .mapper or dict
205221
@@ -208,10 +224,12 @@ def get_page_size_or(self, custom_page_size: int) -> int:
208224 # or any other repository for the settings
209225 return custom_page_size or 100
210226
211- def on_create (self , new_item_data : dict ):
227+ def on_create (self , new_item_data : dict , event_context : EventContext ):
212228 if new_item_data .get ('id' ) is None :
213229 new_item_data ['id' ] = generate_uuid4 ()
214230
231+ new_item_data [self .partition_key_attribute ] = self .find_partition_key_value (event_context )
232+
215233 self .replace_empty_value_per_none (new_item_data )
216234
217235 def on_update (self , update_item_data : dict ):
@@ -228,27 +246,35 @@ def __init__(self, repository: CosmosDBRepository):
228246 self .repository = repository
229247
230248 def get_all (self , conditions : dict = {}) -> list :
231- return self .repository .find_all (partition_key_value = self .partition_key_value ,
249+ event_ctx = self .create_event_context ("read-many" )
250+ return self .repository .find_all (event_context = event_ctx ,
232251 conditions = conditions )
233252
234253 def get (self , id ):
235- return self .repository .find (id , partition_key_value = self .partition_key_value )
254+ event_ctx = self .create_event_context ("read" )
255+ return self .repository .find (id , event_context = event_ctx )
236256
237257 def create (self , data : dict ):
238- data [ self . repository . partition_key_attribute ] = self .partition_key_value
239- return self .repository .create (data )
258+ event_ctx = self .create_event_context ( "create" )
259+ return self .repository .create (data , event_context = event_ctx )
240260
241261 def update (self , id , data : dict ):
242- return self .repository .partial_update (id ,
243- changes = data ,
244- partition_key_value = self .partition_key_value )
262+ event_ctx = self .create_event_context ("update" )
263+ return self .repository .partial_update (id , changes = data , event_context = event_ctx )
245264
246265 def delete (self , id ):
247- self .repository .delete (id , partition_key_value = self .partition_key_value )
266+ event_ctx = self .create_event_context ("update" )
267+ self .repository .delete (id , event_context = event_ctx )
248268
249269 @property
250- def partition_key_value (self ):
251- return current_user_tenant_id ()
270+ def find_partition_key_value (self , event_context : EventContext ):
271+ return event_context .tenant_id
272+
273+ # Replace by decorator and put it in the repository
274+ def create_event_context (self , action : str = None , description : str = None ):
275+ return EventContext (self .repository .container .id ,
276+ action ,
277+ description = description )
252278
253279
254280class CustomError (HTTPException ):
0 commit comments