Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(time-entries): 🥅 stop time-entry if was left running
  • Loading branch information
Angeluz-07 committed May 27, 2020
commit 50f6d3ac3d0edeacdccb5ab61c25d2344ac91c13
4 changes: 4 additions & 0 deletions commons/data_access_layer/cosmos_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ def get_current_month() -> int:
return datetime.now().month


def get_current_day() -> int:
return datetime.now().day


def get_date_range_of_month(year: int, month: int) -> Dict[str, str]:
first_day_of_month = 1
start_date = datetime(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ def test_get_running_should_call_find_running(
'find_running',
return_value=fake_time_entry,
)
time_entries_dao.stop_time_entry_if_was_left_running = Mock()

response = client.get(
"/time-entries/running", headers=valid_header, follow_redirects=True
Expand Down
40 changes: 39 additions & 1 deletion time_tracker_api/time_entries/time_entries_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from typing import List, Callable

from azure.cosmos import PartitionKey
from azure.cosmos.exceptions import CosmosResourceNotFoundError

from flask_restplus._http import HTTPStatus

from commons.data_access_layer.cosmos_db import (
Expand All @@ -11,14 +13,17 @@
CustomError,
CosmosDBModel,
current_datetime_str,
datetime_str,
get_date_range_of_month,
get_current_year,
get_current_month,
get_current_day,
)
from commons.data_access_layer.database import EventContext

from utils.extend_model import add_project_name_to_time_entries
from utils import worked_time
from utils.worked_time import str_to_datetime

from time_tracker_api.projects.projects_model import ProjectCosmosDBModel
from time_tracker_api.projects import projects_model
Expand Down Expand Up @@ -74,6 +79,22 @@ def __init__(self, data): # pragma: no cover
def running(self):
return self.end_date is None

@property
def was_left_running(self) -> bool:
start_date = str_to_datetime(self.start_date)
return (
get_current_day() > start_date.day
or get_current_month() > start_date.month
or get_current_year() > start_date.year
)

@property
def start_date_at_midnight(self) -> str:
start_date = str_to_datetime(self.start_date)
return datetime_str(
start_date.replace(hour=23, minute=59, second=59, microsecond=0)
)

def __add__(self, other):
if type(other) is ProjectCosmosDBModel:
time_entry = self.__class__
Expand Down Expand Up @@ -299,6 +320,21 @@ def check_time_entry_is_not_started(self, data):
"The specified time entry is already running",
)

def stop_time_entry_if_was_left_running(
self, time_entry: TimeEntryCosmosDBModel
):

if time_entry.was_left_running:
end_date = time_entry.start_date_at_midnight
event_ctx = self.create_event_context(
"update", "Stop time-entry that was left running"
)

self.repository.partial_update(
time_entry.id, {'end_date': end_date}, event_ctx
)
raise CosmosResourceNotFoundError()

def get_all(self, conditions: dict = None, **kwargs) -> list:
event_ctx = self.create_event_context("read-many")
conditions.update({"owner_id": event_ctx.user_id})
Expand Down Expand Up @@ -364,9 +400,11 @@ def delete(self, id):

def find_running(self):
event_ctx = self.create_event_context("find_running")
return self.repository.find_running(
time_entry = self.repository.find_running(
event_ctx.tenant_id, event_ctx.user_id
)
self.stop_time_entry_if_was_left_running(time_entry)
return time_entry

def get_worked_time(self, conditions: dict = {}):
event_ctx = self.create_event_context(
Expand Down