Skip to content

Commit e877315

Browse files
committed
fix: Refactor code #122
1 parent a41aac4 commit e877315

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

time_tracker_api/time_entries/time_entries_model.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def find_all(
129129

130130
if event_context.is_admin:
131131
conditions.pop("owner_id")
132+
# TODO should be removed when implementing a role-based permission module ↑
132133

133134
custom_params = self.generate_params(date_range)
134135
time_entries = CosmosDBRepository.find_all(
@@ -143,7 +144,7 @@ def find_all(
143144
projects_id = [project.project_id for project in time_entries]
144145
p_ids = str(tuple(projects_id)).replace(",", "") if len(projects_id) == 1 else str(tuple(projects_id))
145146
custom_conditions = "c.id IN {}".format(p_ids)
146-
147+
# TODO this must be refactored to be used from the utils module ↑
147148
project_dao = projects_model.create_dao()
148149
projects = project_dao.get_all(custom_sql_conditions=[custom_conditions])
149150
add_project_name_to_time_entries(time_entries, projects)
@@ -304,13 +305,7 @@ def get_all(self, conditions: dict = None, **kwargs) -> list:
304305
event_ctx = self.create_event_context("read-many")
305306
conditions.update({"owner_id": event_ctx.user_id})
306307

307-
if "start_date" and "end_date" in conditions:
308-
date_range = conditions.copy()
309-
date_range.pop("owner_id")
310-
conditions.pop("start_date")
311-
conditions.pop("end_date")
312-
else:
313-
date_range = self.handle_date_filter_args(args=conditions)
308+
date_range = self.handle_date_filter_args(args=conditions)
314309
return self.repository.find_all(event_ctx, conditions=conditions, date_range=date_range)
315310

316311
def get(self, id):
@@ -383,19 +378,26 @@ def get_worked_time(self, conditions: dict = {}):
383378

384379
@staticmethod
385380
def handle_date_filter_args(args: dict) -> dict:
381+
date_range = None
386382
if 'month' and 'year' in args:
387383
month = int(args.get("month"))
388384
year = int(args.get("year"))
389385
args.pop('month')
390386
args.pop('year')
387+
elif "start_date" and "end_date" in args:
388+
date_range = args.copy()
389+
if "owner_id" in date_range:
390+
date_range.pop("owner_id")
391+
args.pop("start_date")
392+
args.pop("end_date")
391393
elif 'month' in args:
392394
month = int(args.get("month"))
393395
year = get_current_year()
394396
args.pop('month')
395397
else:
396398
month = get_current_month()
397399
year = get_current_year()
398-
return get_date_range_of_month(year, month)
400+
return date_range if date_range else get_date_range_of_month(year, month)
399401

400402

401403
def create_dao() -> TimeEntriesDao:

0 commit comments

Comments
 (0)