Skip to content

Serialize CompletedWork flags in CompletedWorkSerializer #553

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Charl1996
Copy link

@Charl1996 Charl1996 commented Apr 10, 2025

Product Description

No user-facing changes, only API data update.

Technical Summary

Ticket

This PR makes sure to serialize the CompletedWork records' flags as part of the CompletedWorkSerializer in a dictionary style with slug as key and description as value. For example, the jsonified response would look like this:

{
  ...
  "deliveries": [
    {
      ...
      "flags": {
          "duration": "The form was completed too quickly."
        }
    }
  ]
}

Additional (unexpected) update

The test_delivery_progress_endpoint test asserts that the serialized delivery keys be similar to the UserVisitSerializer, but that's not the correct serializer class that's being used in the get_deliveries method of the DeliveryProgressSerializer.

I have updated the test to test against the CompletedWorkSerializer, but was just wondering if this was a previous oversight?

Safety Assurance

Safety story

Updated test_delivery_progress_endpoint test to reflect new changes.

Automated test coverage

No new tests, only updated an existing test

QA Plan

QA not planned

Labels & Review

  • The set of people pinged as reviewers is appropriate for the level of risk of the change

@Charl1996
Copy link
Author

I think I'm going to try Copilot for reviewing this quickly.

@Charl1996 Charl1996 requested a review from Copilot April 10, 2025 08:46
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

commcare_connect/opportunity/api/serializers.py:271

  • There is an assumption that 'visit' is a dictionary containing a 'flags' key. Consider adding a type check or exception handling to ensure that if 'visit' is not a dict, the code fails gracefully.
for slug, reason in visit.get("flags", []):

Copy link
Collaborator

@calellowitz calellowitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great. left a comment on the test change, also good to add a test that explicitly checks the correctness of the new field (that the flags get added in the right format)

@@ -235,7 +236,7 @@ def test_delivery_progress_endpoint(
assert response.data.keys() == DeliveryProgressSerializer().get_fields().keys()
assert len(response.data["deliveries"]) == 1
assert len(response.data["payments"]) == 0
assert response.data["deliveries"][0].keys() == UserVisitSerializer().get_fields().keys()
assert response.data["deliveries"][0].keys() == CompletedWorkSerializer().get_fields().keys()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good catch. It was orginally like that because we switched serializers and wanted to ensure compatibility, but I agree that now that we are making additions, we should use the correct one. That said, I am not sure this assertion is buying us much, and would prefer the test to check expected values of keys we care about in the fields rather than just the presence of the keys.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True true. I'll update!

@@ -237,6 +242,10 @@ def test_delivery_progress_endpoint(
assert len(response.data["deliveries"]) == 1
assert len(response.data["payments"]) == 0
assert response.data["deliveries"][0].keys() == CompletedWorkSerializer().get_fields().keys()
assert response.data["deliveries"][0]["flags"] == [
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I think about it, is this the expected format? Would it not make more sense to have it like this:

[
    {
        "duration": "The form was completed too quickly.",
        "attachment_missing": "Form was submitted without attachments.",
    },
]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@calellowitz Actually, I'm going to update it as above. Feel free to let me know if that's not in fact what was envisioned. So now a single visit's flags will be consolidated into this one dictionary structure.

See c384c9e

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops yes. This is definitely what was intended. Thanks for the fix (and one more benefit of tests, making this stuff explicit).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, shouldn't this not be a list at all. If this is always a list with a single dict, I think it can just be a dict.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Charl1996 Charl1996 requested a review from calellowitz April 15, 2025 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants