fix(time-entries): use server time for running timers

This commit is contained in:
2026-05-21 13:01:51 +03:30
parent 0fea265cfb
commit e234eac26d
4 changed files with 59 additions and 14 deletions

View File

@@ -22,24 +22,29 @@ def _verify_workspace_access(user, workspace_id):
raise PermissionDenied("You do not have access to this workspace.")
def create_time_entry(user, workspace_id, start_time, end_time=None, project=None, tags=None, description="", is_billable=False):
def create_time_entry(user, workspace_id, start_time=None, end_time=None, project=None, tags=None, description="", is_billable=False):
"""
Creates a new time entry. If end_time is None, it acts as a running timer.
"""
_verify_workspace_access(user, workspace_id)
if not end_time:
has_running_timer = TimeEntry.objects.filter(
workspace_id=workspace_id,
user=user,
end_time__isnull=True,
is_deleted=False
).exists()
if has_running_timer:
raise ValidationError({"non_field_errors": "You already have a running timer in this workspace."})
if start_time and end_time and start_time >= end_time:
raise ValidationError({"end_time": "End time must be strictly after start time."})
if not end_time:
has_running_timer = TimeEntry.objects.filter(
workspace_id=workspace_id,
user=user,
end_time__isnull=True,
is_deleted=False
).exists()
if has_running_timer:
raise ValidationError({"non_field_errors": "You already have a running timer in this workspace."})
if start_time is None:
if end_time is not None:
raise ValidationError({"start_time": "Start time is required when end time is provided."})
start_time = timezone.now()
if start_time and end_time and start_time >= end_time:
raise ValidationError({"end_time": "End time must be strictly after start time."})
if project and project.workspace_id != workspace_id:
raise ValidationError({"project": "Project must belong to the same workspace."})