Skip to content

Commit

Permalink
fix(backend/activities): end activities when they get transferred
Browse files Browse the repository at this point in the history
  • Loading branch information
c0rydoras committed Jul 1, 2024
1 parent 6f01325 commit e2affc3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
15 changes: 15 additions & 0 deletions backend/timed/tracking/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.contrib.auth import get_user_model
from django.db.models import BooleanField, Case, Q, When
from django.utils.duration import duration_string
from django.utils.timezone import datetime
from django.utils.translation import gettext_lazy as _
from rest_framework_json_api import relations, serializers
from rest_framework_json_api.relations import ResourceRelatedField
Expand Down Expand Up @@ -68,6 +69,20 @@ def validate_running_activity():

return data

def update(
self, instance: models.Activity, validated_data: dict
) -> models.Activity:
"""Update an activity.
Ensure that transferring an activity ends it.
"""
to_time = validated_data.get("to_time", instance.to_time)

if validated_data.get("transferred", instance.transferred) and not to_time:
validated_data["to_time"] = datetime.now().time()

return super().update(instance, validated_data)

class Meta:
"""Meta information for the activity serializer."""

Expand Down
29 changes: 29 additions & 0 deletions backend/timed/tracking/tests/test_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,32 @@ def test_activity_set_to_time_none(internal_employee_client, activity_factory):

res = internal_employee_client.patch(url, data)
assert res.status_code == status.HTTP_400_BAD_REQUEST


def test_activity_transfer_ends_it(internal_employee_client):
"""Test that transferring activities ends them."""

activity = ActivityFactory.create(
user=internal_employee_client.user,
transferred=False,
to_time=None,
)

data = {
"data": {
"type": "activities",
"id": activity.id,
"attributes": {"transferred": True},
}
}

url = reverse("activity-detail", args=[activity.id])
response = internal_employee_client.patch(url, data)
assert response.status_code == status.HTTP_200_OK

json = response.json()

assert json["data"]["attributes"]["to-time"]

activity.refresh_from_db()
assert activity.to_time

0 comments on commit e2affc3

Please sign in to comment.