|
3 | 3 | from typing import List, Callable
|
4 | 4 |
|
5 | 5 | from azure.cosmos import PartitionKey
|
| 6 | +from azure.cosmos.exceptions import CosmosResourceNotFoundError |
| 7 | + |
6 | 8 | from flask_restplus._http import HTTPStatus
|
7 | 9 |
|
8 | 10 | from commons.data_access_layer.cosmos_db import (
|
|
11 | 13 | CustomError,
|
12 | 14 | CosmosDBModel,
|
13 | 15 | current_datetime_str,
|
| 16 | + datetime_str, |
14 | 17 | get_date_range_of_month,
|
15 | 18 | get_current_year,
|
16 | 19 | get_current_month,
|
| 20 | + get_current_day, |
17 | 21 | )
|
18 | 22 | from commons.data_access_layer.database import EventContext
|
19 | 23 |
|
20 | 24 | from utils.extend_model import add_project_name_to_time_entries
|
21 | 25 | from utils import worked_time
|
| 26 | +from utils.worked_time import str_to_datetime |
22 | 27 |
|
23 | 28 | from time_tracker_api.projects.projects_model import ProjectCosmosDBModel
|
24 | 29 | from time_tracker_api.projects import projects_model
|
@@ -74,6 +79,22 @@ def __init__(self, data): # pragma: no cover
|
74 | 79 | def running(self):
|
75 | 80 | return self.end_date is None
|
76 | 81 |
|
| 82 | + @property |
| 83 | + def was_left_running(self) -> bool: |
| 84 | + start_date = str_to_datetime(self.start_date) |
| 85 | + return ( |
| 86 | + get_current_day() > start_date.day |
| 87 | + or get_current_month() > start_date.month |
| 88 | + or get_current_year() > start_date.year |
| 89 | + ) |
| 90 | + |
| 91 | + @property |
| 92 | + def start_date_at_midnight(self) -> str: |
| 93 | + start_date = str_to_datetime(self.start_date) |
| 94 | + return datetime_str( |
| 95 | + start_date.replace(hour=23, minute=59, second=59, microsecond=0) |
| 96 | + ) |
| 97 | + |
77 | 98 | def __add__(self, other):
|
78 | 99 | if type(other) is ProjectCosmosDBModel:
|
79 | 100 | time_entry = self.__class__
|
@@ -299,6 +320,21 @@ def check_time_entry_is_not_started(self, data):
|
299 | 320 | "The specified time entry is already running",
|
300 | 321 | )
|
301 | 322 |
|
| 323 | + def stop_time_entry_if_was_left_running( |
| 324 | + self, time_entry: TimeEntryCosmosDBModel |
| 325 | + ): |
| 326 | + |
| 327 | + if time_entry.was_left_running: |
| 328 | + end_date = time_entry.start_date_at_midnight |
| 329 | + event_ctx = self.create_event_context( |
| 330 | + "update", "Stop time-entry that was left running" |
| 331 | + ) |
| 332 | + |
| 333 | + self.repository.partial_update( |
| 334 | + time_entry.id, {'end_date': end_date}, event_ctx |
| 335 | + ) |
| 336 | + raise CosmosResourceNotFoundError() |
| 337 | + |
302 | 338 | def get_all(self, conditions: dict = None, **kwargs) -> list:
|
303 | 339 | event_ctx = self.create_event_context("read-many")
|
304 | 340 | conditions.update({"owner_id": event_ctx.user_id})
|
@@ -364,9 +400,11 @@ def delete(self, id):
|
364 | 400 |
|
365 | 401 | def find_running(self):
|
366 | 402 | event_ctx = self.create_event_context("find_running")
|
367 |
| - return self.repository.find_running( |
| 403 | + time_entry = self.repository.find_running( |
368 | 404 | event_ctx.tenant_id, event_ctx.user_id
|
369 | 405 | )
|
| 406 | + self.stop_time_entry_if_was_left_running(time_entry) |
| 407 | + return time_entry |
370 | 408 |
|
371 | 409 | def get_worked_time(self, conditions: dict = {}):
|
372 | 410 | event_ctx = self.create_event_context(
|
|
0 commit comments