Skip to content

Commit a2758de

Browse files
committed
fix(time-entries): 🥅 stop time-entry if was left running
1 parent 58dc55e commit a2758de

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

commons/data_access_layer/cosmos_db.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ def get_current_month() -> int:
408408
return datetime.now().month
409409

410410

411+
def get_current_day() -> int:
412+
return datetime.now().day
413+
414+
411415
def get_date_range_of_month(year: int, month: int) -> Dict[str, str]:
412416
first_day_of_month = 1
413417
start_date = datetime(

tests/time_tracker_api/time_entries/time_entries_namespace_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ def test_get_running_should_call_find_running(
482482
'find_running',
483483
return_value=fake_time_entry,
484484
)
485+
time_entries_dao.stop_time_entry_if_was_left_running = Mock()
485486

486487
response = client.get(
487488
"/time-entries/running", headers=valid_header, follow_redirects=True

time_tracker_api/time_entries/time_entries_model.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from typing import List, Callable
44

55
from azure.cosmos import PartitionKey
6+
from azure.cosmos.exceptions import CosmosResourceNotFoundError
7+
68
from flask_restplus._http import HTTPStatus
79

810
from commons.data_access_layer.cosmos_db import (
@@ -11,14 +13,17 @@
1113
CustomError,
1214
CosmosDBModel,
1315
current_datetime_str,
16+
datetime_str,
1417
get_date_range_of_month,
1518
get_current_year,
1619
get_current_month,
20+
get_current_day,
1721
)
1822
from commons.data_access_layer.database import EventContext
1923

2024
from utils.extend_model import add_project_name_to_time_entries
2125
from utils import worked_time
26+
from utils.worked_time import str_to_datetime
2227

2328
from time_tracker_api.projects.projects_model import ProjectCosmosDBModel
2429
from time_tracker_api.projects import projects_model
@@ -74,6 +79,22 @@ def __init__(self, data): # pragma: no cover
7479
def running(self):
7580
return self.end_date is None
7681

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+
7798
def __add__(self, other):
7899
if type(other) is ProjectCosmosDBModel:
79100
time_entry = self.__class__
@@ -299,6 +320,21 @@ def check_time_entry_is_not_started(self, data):
299320
"The specified time entry is already running",
300321
)
301322

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+
302338
def get_all(self, conditions: dict = None, **kwargs) -> list:
303339
event_ctx = self.create_event_context("read-many")
304340
conditions.update({"owner_id": event_ctx.user_id})
@@ -364,9 +400,11 @@ def delete(self, id):
364400

365401
def find_running(self):
366402
event_ctx = self.create_event_context("find_running")
367-
return self.repository.find_running(
403+
time_entry = self.repository.find_running(
368404
event_ctx.tenant_id, event_ctx.user_id
369405
)
406+
self.stop_time_entry_if_was_left_running(time_entry)
407+
return time_entry
370408

371409
def get_worked_time(self, conditions: dict = {}):
372410
event_ctx = self.create_event_context(

0 commit comments

Comments
 (0)