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: query for the undownloaded attachments #2983

Merged
merged 1 commit into from
May 14, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions backend/benefit/applications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dateutil.relativedelta import relativedelta
from django.conf import settings
from django.db import connection, models
from django.db.models import F, JSONField, OuterRef, Prefetch, Subquery
from django.db.models import Exists, F, JSONField, OuterRef, Prefetch, Subquery
from django.db.models.constraints import UniqueConstraint
from django.utils.translation import gettext_lazy as _
from encrypted_fields.fields import EncryptedCharField, SearchField
Expand Down Expand Up @@ -129,7 +129,7 @@ def with_non_downloaded_attachments(self):
which means that a case has been opened for them in AHJO.
"""

qs = self.get_queryset().filter(ahjo_case_id__isnull=False)
# Define a queryset for attachments where downloaded_by_ahjo is NULL
attachments_queryset = Attachment.objects.filter(
downloaded_by_ahjo__isnull=True,
attachment_type__in=[
Expand All @@ -142,7 +142,26 @@ def with_non_downloaded_attachments(self):
AttachmentType.OTHER_ATTACHMENT,
],
)

# Create an Exists subquery for at least one undownloaded attachment
attachment_exists = Exists(
attachments_queryset.filter(application_id=OuterRef("pk"))
)

# Annotate applications with a boolean indicating the existence of undownloaded attachments
qs = (
self.get_queryset()
.annotate(has_undownloaded_attachments=attachment_exists)
.filter(
ahjo_case_id__isnull=False,
has_undownloaded_attachments=True, # Filter using the annotated field
)
)

# Use Prefetch to specify the filtered queryset for prefetching attachments
attachments_prefetch = Prefetch("attachments", queryset=attachments_queryset)

# Return the filtered applications with the specified prefetched related attachments
return qs.prefetch_related(attachments_prefetch)

def get_by_statuses(
Expand Down
7 changes: 7 additions & 0 deletions backend/benefit/applications/tests/test_ahjo_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,13 @@ def test_with_non_downloaded_attachments(decided_application):
attachments = applications[0].attachments.all()
assert attachments.count() == 6

for a in attachments:
a.downloaded_by_ahjo = timezone.now()
a.save()

applications = Application.objects.with_non_downloaded_attachments()
assert applications.count() == 0


dummy_case_id = "HEL 1999-123"

Expand Down