|
2 | 2 | from dataclasses import dataclass, field
|
3 | 3 | from typing import List, Callable
|
4 | 4 | from azure.cosmos import PartitionKey
|
| 5 | +from azure.cosmos.exceptions import CosmosResourceNotFoundError |
5 | 6 | from flask_restplus import abort
|
6 | 7 | from flask_restplus._http import HTTPStatus
|
7 | 8 |
|
|
11 | 12 | CustomError,
|
12 | 13 | CosmosDBModel,
|
13 | 14 | current_datetime_str,
|
| 15 | + datetime_str, |
14 | 16 | get_date_range_of_month,
|
15 | 17 | get_current_year,
|
16 | 18 | get_current_month,
|
| 19 | + get_current_day, |
17 | 20 | )
|
18 | 21 | from commons.data_access_layer.database import EventContext
|
| 22 | +from time_tracker_api.activities import activities_model |
19 | 23 |
|
20 |
| -from utils.extend_model import add_project_name_to_time_entries |
| 24 | +from utils.extend_model import ( |
| 25 | + add_project_name_to_time_entries, |
| 26 | + add_activity_name_to_time_entries, |
| 27 | +) |
21 | 28 | from utils import worked_time
|
| 29 | +from utils.worked_time import str_to_datetime |
22 | 30 | from utils.extend_model import (
|
23 | 31 | create_in_condition,
|
24 | 32 | create_custom_query_from_str,
|
25 | 33 | )
|
26 |
| - |
27 | 34 | from time_tracker_api.projects.projects_model import ProjectCosmosDBModel
|
28 | 35 | from time_tracker_api.projects import projects_model
|
29 | 36 | from time_tracker_api.database import CRUDDao, APICosmosDBDao
|
@@ -78,6 +85,22 @@ def __init__(self, data): # pragma: no cover
|
78 | 85 | def running(self):
|
79 | 86 | return self.end_date is None
|
80 | 87 |
|
| 88 | + @property |
| 89 | + def was_left_running(self) -> bool: |
| 90 | + start_date = str_to_datetime(self.start_date) |
| 91 | + return ( |
| 92 | + get_current_day() > start_date.day |
| 93 | + or get_current_month() > start_date.month |
| 94 | + or get_current_year() > start_date.year |
| 95 | + ) |
| 96 | + |
| 97 | + @property |
| 98 | + def start_date_at_midnight(self) -> str: |
| 99 | + start_date = str_to_datetime(self.start_date) |
| 100 | + return datetime_str( |
| 101 | + start_date.replace(hour=23, minute=59, second=59, microsecond=0) |
| 102 | + ) |
| 103 | + |
81 | 104 | def __add__(self, other):
|
82 | 105 | if type(other) is ProjectCosmosDBModel:
|
83 | 106 | time_entry = self.__class__
|
@@ -150,6 +173,10 @@ def find_all(
|
150 | 173 | custom_sql_conditions=[custom_conditions]
|
151 | 174 | )
|
152 | 175 | add_project_name_to_time_entries(time_entries, projects)
|
| 176 | + |
| 177 | + activity_dao = activities_model.create_dao() |
| 178 | + activities = activity_dao.get_all() |
| 179 | + add_activity_name_to_time_entries(time_entries, activities) |
153 | 180 | return time_entries
|
154 | 181 |
|
155 | 182 | def on_create(self, new_item_data: dict, event_context: EventContext):
|
@@ -296,6 +323,21 @@ def check_time_entry_is_not_started(self, data):
|
296 | 323 | "The specified time entry is already running",
|
297 | 324 | )
|
298 | 325 |
|
| 326 | + def stop_time_entry_if_was_left_running( |
| 327 | + self, time_entry: TimeEntryCosmosDBModel |
| 328 | + ): |
| 329 | + |
| 330 | + if time_entry.was_left_running: |
| 331 | + end_date = time_entry.start_date_at_midnight |
| 332 | + event_ctx = self.create_event_context( |
| 333 | + "update", "Stop time-entry that was left running" |
| 334 | + ) |
| 335 | + |
| 336 | + self.repository.partial_update( |
| 337 | + time_entry.id, {'end_date': end_date}, event_ctx |
| 338 | + ) |
| 339 | + raise CosmosResourceNotFoundError() |
| 340 | + |
299 | 341 | def get_all(self, conditions: dict = None, **kwargs) -> list:
|
300 | 342 | event_ctx = self.create_event_context("read-many")
|
301 | 343 | conditions.update({"owner_id": event_ctx.user_id})
|
@@ -381,9 +423,16 @@ def delete(self, id):
|
381 | 423 |
|
382 | 424 | def find_running(self):
|
383 | 425 | event_ctx = self.create_event_context("find_running")
|
384 |
| - return self.repository.find_running( |
| 426 | + time_entry = self.repository.find_running( |
385 | 427 | event_ctx.tenant_id, event_ctx.user_id
|
386 | 428 | )
|
| 429 | + # TODO: we need to make this work using the users time zone |
| 430 | + # This is disabled as part of https://github.com/ioet/time-tracker-backend/issues/160 |
| 431 | + # Remove all these comments after implementing |
| 432 | + # https://github.com/ioet/time-tracker-backend/issues/159 |
| 433 | + # https://github.com/ioet/time-tracker-backend/issues/162 |
| 434 | + # self.stop_time_entry_if_was_left_running(time_entry) |
| 435 | + return time_entry |
387 | 436 |
|
388 | 437 | def get_worked_time(self, conditions: dict = {}):
|
389 | 438 | event_ctx = self.create_event_context(
|
|
0 commit comments