Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(backend/activities): end activities when they get transferred #311

Merged
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
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
30 changes: 30 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,33 @@ 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
winged marked this conversation as resolved.
Show resolved Hide resolved
assert activity.transferred
Loading