Repository: adfinis/timed-backend

Issue:
bug: remaining effort on initial report creation is buggy
## Scenario
Create a report and add an estimated remaining effort to it.

## Expected behavior
It updates the remaining effort of the certain task in the statistics view.

## Actual behavior
After submitting the report the remaining effort does **not** update in stats view. But after editing the rem. effort and saving the report again in the timesheet, it will update the values in the stats view.

The attributes are fine in both requests to the backend. I suspect that a the `POST` and `PATCH` requests have different handling of the remaining effort attribute. Maybe the `POST` does not properly trigger the update?
![image](https://user-images.githubusercontent.com/10029904/219358043-268b4856-1b2e-430a-b4f0-e9891d1f799e.png)



Discussion:
Yes, you're totally right. The signal to update `remaining_effort` isn't properly triggered when creating a report.
Fix is on the way.

Resolving patch:
diff --git a/timed/tracking/signals.py b/timed/tracking/signals.py
--- a/timed/tracking/signals.py
+++ b/timed/tracking/signals.py
@@ -26,21 +26,30 @@ def update_most_recent_remaining_effort(sender, instance, **kwargs):
     if kwargs.get("raw", False):  # pragma: no cover
         return
 
-    if not instance.pk:
-        return
     if instance.task.project.remaining_effort_tracking is not True:
         return
 
+    # update most_recent_remaining_effort and total_remaining_effort on report creation
+    if not instance.pk:
+        update_remaining_effort(instance)
+        return
+
+    # check if remaining effort has changed on report update
     if instance.remaining_effort != Report.objects.get(id=instance.id).remaining_effort:
-        task = instance.task
-        task.most_recent_remaining_effort = instance.remaining_effort
-        task.save()
-
-        project = task.project
-        total_remaining_effort = (
-            project.tasks.all()
-            .aggregate(sum_remaining=Sum("most_recent_remaining_effort"))
-            .get("sum_remaining")
-        )
-        project.total_remaining_effort = total_remaining_effort
-        project.save()
+        update_remaining_effort(instance)
+
+
+def update_remaining_effort(report):
+    task = report.task
+    project = task.project
+
+    task.most_recent_remaining_effort = report.remaining_effort
+    task.save()
+
+    total_remaining_effort = (
+        task.project.tasks.all()
+        .aggregate(sum_remaining=Sum("most_recent_remaining_effort"))
+        .get("sum_remaining")
+    )
+    project.total_remaining_effort = total_remaining_effort
+    project.save()


Test patch:
diff --git a/timed/tracking/tests/test_report.py b/timed/tracking/tests/test_report.py
--- a/timed/tracking/tests/test_report.py
+++ b/timed/tracking/tests/test_report.py
@@ -1905,6 +1905,10 @@ def test_report_create_remaining_effort(
         assert json["data"]["relationships"]["user"]["data"]["id"] == str(user.id)
         assert json["data"]["relationships"]["task"]["data"]["id"] == str(task.id)
 
+        task.refresh_from_db()
+        assert task.most_recent_remaining_effort == timedelta(hours=1)
+        assert task.project.total_remaining_effort == timedelta(hours=1)
+
 
 def test_report_remaining_effort_total(
     internal_employee_client,