|
21 | 21 | from time_tracker_api.time_entries.custom_modules.utils import (
|
22 | 22 | add_project_name_to_time_entries,
|
23 | 23 | )
|
| 24 | +from time_tracker_api.projects.projects_model import ProjectCosmosDBModel, create_dao as project_create_dao |
24 | 25 | from time_tracker_api.projects import projects_model
|
25 | 26 | from time_tracker_api.database import CRUDDao, APICosmosDBDao
|
26 | 27 | from time_tracker_api.security import current_user_id
|
@@ -74,6 +75,14 @@ def __init__(self, data): # pragma: no cover
|
74 | 75 | def running(self):
|
75 | 76 | return self.end_date is None
|
76 | 77 |
|
| 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 | + |
77 | 86 | def __repr__(self):
|
78 | 87 | return '<Time Entry %r>' % self.start_date # pragma: no cover
|
79 | 88 |
|
@@ -116,21 +125,31 @@ def find_all(
|
116 | 125 | conditions: dict = {},
|
117 | 126 | date_range: dict = {},
|
118 | 127 | ):
|
119 |
| - custom_sql_conditions = [] |
120 |
| - custom_sql_conditions.append( |
121 |
| - self.create_sql_date_range_filter(date_range) |
122 |
| - ) |
| 128 | + custom_sql_conditions = [self.create_sql_date_range_filter(date_range)] |
123 | 129 |
|
124 |
| - custom_params = self.generate_params(date_range) |
| 130 | + if event_context.is_admin: |
| 131 | + conditions.pop("owner_id") |
| 132 | + # TODO should be removed when implementing a role-based permission module ↑ |
125 | 133 |
|
126 |
| - return CosmosDBRepository.find_all( |
| 134 | + custom_params = self.generate_params(date_range) |
| 135 | + time_entries = CosmosDBRepository.find_all( |
127 | 136 | self,
|
128 | 137 | event_context=event_context,
|
129 | 138 | conditions=conditions,
|
130 | 139 | custom_sql_conditions=custom_sql_conditions,
|
131 | 140 | custom_params=custom_params,
|
132 | 141 | )
|
133 | 142 |
|
| 143 | + if time_entries: |
| 144 | + projects_id = [project.project_id for project in time_entries] |
| 145 | + p_ids = str(tuple(projects_id)).replace(",", "") if len(projects_id) == 1 else str(tuple(projects_id)) |
| 146 | + custom_conditions = "c.id IN {}".format(p_ids) |
| 147 | + # TODO this must be refactored to be used from the utils module ↑ |
| 148 | + project_dao = projects_model.create_dao() |
| 149 | + projects = project_dao.get_all(custom_sql_conditions=[custom_conditions]) |
| 150 | + add_project_name_to_time_entries(time_entries, projects) |
| 151 | + return time_entries |
| 152 | + |
134 | 153 | def on_create(self, new_item_data: dict, event_context: EventContext):
|
135 | 154 | CosmosDBRepository.on_create(self, new_item_data, event_context)
|
136 | 155 |
|
@@ -282,19 +301,12 @@ def checks_owner_and_is_not_started(cls, data: dict):
|
282 | 301 | "The specified time entry is already running",
|
283 | 302 | )
|
284 | 303 |
|
285 |
| - def get_all(self, conditions: dict = {}) -> list: |
| 304 | + def get_all(self, conditions: dict = None, **kwargs) -> list: |
286 | 305 | event_ctx = self.create_event_context("read-many")
|
287 | 306 | conditions.update({"owner_id": event_ctx.user_id})
|
288 | 307 |
|
289 | 308 | date_range = self.handle_date_filter_args(args=conditions)
|
290 |
| - time_entries = self.repository.find_all( |
291 |
| - event_ctx, conditions=conditions, date_range=date_range |
292 |
| - ) |
293 |
| - |
294 |
| - project_dao = projects_model.create_dao() |
295 |
| - projects = project_dao.get_all() |
296 |
| - add_project_name_to_time_entries(time_entries, projects) |
297 |
| - return time_entries |
| 309 | + return self.repository.find_all(event_ctx, conditions=conditions, date_range=date_range) |
298 | 310 |
|
299 | 311 | def get(self, id):
|
300 | 312 | event_ctx = self.create_event_context("read")
|
@@ -366,19 +378,26 @@ def get_worked_time(self, conditions: dict = {}):
|
366 | 378 |
|
367 | 379 | @staticmethod
|
368 | 380 | def handle_date_filter_args(args: dict) -> dict:
|
| 381 | + date_range = None |
369 | 382 | if 'month' and 'year' in args:
|
370 | 383 | month = int(args.get("month"))
|
371 | 384 | year = int(args.get("year"))
|
372 | 385 | args.pop('month')
|
373 | 386 | 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") |
374 | 393 | elif 'month' in args:
|
375 | 394 | month = int(args.get("month"))
|
376 | 395 | year = get_current_year()
|
377 | 396 | args.pop('month')
|
378 | 397 | else:
|
379 | 398 | month = get_current_month()
|
380 | 399 | year = get_current_year()
|
381 |
| - return get_date_range_of_month(year, month) |
| 400 | + return date_range if date_range else get_date_range_of_month(year, month) |
382 | 401 |
|
383 | 402 |
|
384 | 403 | def create_dao() -> TimeEntriesDao:
|
|
0 commit comments