Skip to content

Commit

Permalink
Fix application round handled email send condition
Browse files Browse the repository at this point in the history
  • Loading branch information
matti-lamppu committed Nov 14, 2024
1 parent 651f00f commit ec8a6ab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
23 changes: 17 additions & 6 deletions tests/test_integrations/test_email/test_email_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.test import override_settings
from freezegun import freeze_time

from tests.factories import ApplicationFactory, ReservationFactory, UnitFactory, UserFactory
from tests.factories import ApplicationFactory, ApplicationRoundFactory, ReservationFactory, UnitFactory, UserFactory
from tests.helpers import TranslationsFromPOFiles, patch_method
from tilavarauspalvelu.enums import Language, ReservationNotification, ReservationStateChoice, ReservationTypeChoice
from tilavarauspalvelu.integrations.email.main import EmailService
Expand Down Expand Up @@ -104,7 +104,8 @@ def test_email_service__send_application_in_allocation_emails__no_recipients(out

@override_settings(SEND_EMAILS=True)
def test_email_service__send_application_handled_emails(outbox):
application = ApplicationFactory.create_in_status_handled()
application_round = ApplicationRoundFactory.create_in_status_results_sent()
application = ApplicationFactory.create_in_status_handled(application_round=application_round)

EmailService.send_application_handled_emails()

Expand All @@ -116,8 +117,15 @@ def test_email_service__send_application_handled_emails(outbox):

@override_settings(SEND_EMAILS=True)
def test_email_service__send_application_handled_emails__multiple_languages(outbox):
application_1 = ApplicationFactory.create_in_status_handled(user__preferred_language="fi")
application_2 = ApplicationFactory.create_in_status_handled(user__preferred_language="en")
application_round = ApplicationRoundFactory.create_in_status_results_sent()
application_1 = ApplicationFactory.create_in_status_handled(
application_round=application_round,
user__preferred_language="fi",
)
application_2 = ApplicationFactory.create_in_status_handled(
application_round=application_round,
user__preferred_language="en",
)

with TranslationsFromPOFiles():
EmailService.send_application_handled_emails()
Expand All @@ -134,7 +142,7 @@ def test_email_service__send_application_handled_emails__multiple_languages(outb
@override_settings(SEND_EMAILS=True)
@patch_method(SentryLogger.log_message)
def test_email_service__send_application_handled_emails__wrong_status(outbox):
ApplicationFactory.create_in_status_in_allocation()
ApplicationFactory.create_in_status_handled()

EmailService.send_application_handled_emails()

Expand All @@ -149,7 +157,10 @@ def test_email_service__send_application_handled_emails__wrong_status(outbox):
@override_settings(SEND_EMAILS=True)
@patch_method(SentryLogger.log_message)
def test_email_service__send_application_handled_emails__no_recipients(outbox):
ApplicationFactory.create_in_status_handled(user__email="", contact_person__email="")
application_round = ApplicationRoundFactory.create_in_status_results_sent()
ApplicationFactory.create_in_status_handled(
application_round=application_round, user__email="", contact_person__email=""
)

EmailService.send_application_handled_emails()

Expand Down
9 changes: 4 additions & 5 deletions tilavarauspalvelu/models/application/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
from helsinki_gdpr.models import SerializableMixin
from lookup_property import L

from tilavarauspalvelu.enums import ApplicationRoundStatusChoice, ApplicationSectionStatusChoice
from tilavarauspalvelu.enums import ApplicationRoundStatusChoice, ApplicationStatusChoice

if TYPE_CHECKING:
from tilavarauspalvelu.enums import ApplicationStatusChoice
from tilavarauspalvelu.models import Application


Expand Down Expand Up @@ -110,16 +109,16 @@ def should_send_in_allocation_email(self) -> Self:
"""Get all applications that need the "application in allocation" notification to be sent."""
return self.filter(
L(application_round__status=ApplicationRoundStatusChoice.IN_ALLOCATION.value),
L(status=ApplicationSectionStatusChoice.IN_ALLOCATION.value),
L(status=ApplicationStatusChoice.IN_ALLOCATION.value),
in_allocation_notification_sent_date__isnull=True,
application_sections__isnull=False,
)

def should_send_handled_email(self) -> Self:
"""Get all applications that need the "application handled" notification to be sent."""
return self.filter(
L(application_round__status=ApplicationRoundStatusChoice.HANDLED.value),
L(status=ApplicationSectionStatusChoice.HANDLED.value),
L(application_round__status=ApplicationRoundStatusChoice.RESULTS_SENT.value),
L(status=ApplicationStatusChoice.RESULTS_SENT.value),
results_ready_notification_sent_date__isnull=True,
application_sections__isnull=False,
)
Expand Down

0 comments on commit ec8a6ab

Please sign in to comment.