2121from time_tracker_api .time_entries .custom_modules .utils import (
2222 add_project_name_to_time_entries ,
2323)
24+ from time_tracker_api .projects .projects_model import ProjectCosmosDBModel , create_dao as project_create_dao
2425from time_tracker_api .projects import projects_model
2526from time_tracker_api .database import CRUDDao , APICosmosDBDao
2627from time_tracker_api .security import current_user_id
@@ -74,6 +75,14 @@ def __init__(self, data): # pragma: no cover
7475 def running (self ):
7576 return self .end_date is None
7677
78+ def __add__ (self , other ):
79+ if type (other ) is ProjectCosmosDBModel :
80+ time_entry = self .__class__
81+ time_entry .project_id = other .__dict__
82+ return time_entry
83+ else :
84+ raise NotImplementedError
85+
7786 def __repr__ (self ):
7887 return '<Time Entry %r>' % self .start_date # pragma: no cover
7988
@@ -102,6 +111,7 @@ def create_sql_ignore_id_condition(id: str):
102111
103112 @staticmethod
104113 def create_sql_date_range_filter (date_range : dict ) -> str :
114+ print ("data: {}" .format (date_range ))
105115 if 'start_date' and 'end_date' in date_range :
106116 return """
107117 ((c.start_date BETWEEN @start_date AND @end_date) OR
@@ -134,13 +144,12 @@ def find_all(
134144 conditions : dict = {},
135145 date_range : dict = {},
136146 ):
137- custom_sql_conditions = []
138- custom_sql_conditions .append (
139- self .create_sql_date_range_filter (date_range )
140- )
147+ custom_sql_conditions = [self .create_sql_date_range_filter (date_range )]
141148
142- custom_params = self .generate_params (date_range )
149+ if event_context .is_admin :
150+ conditions .pop ("owner_id" )
143151
152+ custom_params = self .generate_params (date_range )
144153 time_entries = CosmosDBRepository .find_all (
145154 self ,
146155 event_context = event_context ,
@@ -149,9 +158,14 @@ def find_all(
149158 custom_params = custom_params ,
150159 )
151160
152- project_dao = projects_model .create_dao ()
153- projects = project_dao .get_all ()
154- add_project_name_to_time_entries (time_entries , projects )
161+ if time_entries :
162+ projects_id = [project .project_id for project in time_entries ]
163+ p_ids = str (tuple (projects_id )).replace ("," , "" ) if len (projects_id ) == 1 else str (tuple (projects_id ))
164+ custom_conditions = "c.id IN {}" .format (p_ids )
165+
166+ project_dao = projects_model .create_dao ()
167+ projects = project_dao .get_all (custom_sql_conditions = [custom_conditions ])
168+ add_project_name_to_time_entries (time_entries , projects )
155169 return time_entries
156170
157171 def on_create (self , new_item_data : dict , event_context : EventContext ):
@@ -305,14 +319,18 @@ def checks_owner_and_is_not_started(cls, data: dict):
305319 "The specified time entry is already running" ,
306320 )
307321
308- def get_all (self , conditions : dict = {} ) -> list :
322+ def get_all (self , conditions : dict = None , ** kwargs ) -> list :
309323 event_ctx = self .create_event_context ("read-many" )
310324 conditions .update ({"owner_id" : event_ctx .user_id })
311325
312- date_range = self .handle_date_filter_args (args = conditions )
313- return self .repository .find_all (
314- event_ctx , conditions = conditions , date_range = date_range
315- )
326+ if "start_date" and "end_date" in conditions :
327+ date_range = conditions .copy ()
328+ date_range .pop ("owner_id" )
329+ conditions .pop ("start_date" )
330+ conditions .pop ("end_date" )
331+ else :
332+ date_range = self .handle_date_filter_args (args = conditions )
333+ return self .repository .find_all (event_ctx , conditions = conditions , date_range = date_range )
316334
317335 def get (self , id ):
318336 event_ctx = self .create_event_context ("read" )
0 commit comments