diff --git a/src/registrar/management/commands/send_domain_invitations.py b/src/registrar/management/commands/send_domain_invitations.py index 68e805be9..e02c87bf8 100644 --- a/src/registrar/management/commands/send_domain_invitations.py +++ b/src/registrar/management/commands/send_domain_invitations.py @@ -65,8 +65,6 @@ class Command(BaseCommand): self.send_emails() logger.info("done sending emails") - self.update_domains_as_sent() - logger.info("done sending emails and updating transition_domains") else: logger.info("not sending emails") @@ -143,11 +141,13 @@ class Command(BaseCommand): # to True for domain in email_data["domains"]: self.domains_with_errors.append(domain) - - def update_domains_as_sent(self): - """set email_sent to True in all transition_domains which have - been processed successfully""" - for transition_domain in self.transition_domains: - if transition_domain.domain_name not in self.domains_with_errors: - transition_domain.email_sent = True - transition_domain.save() + else: + # email was sent no exception, mark all these transition domains + # as email_sent. + this_email = email_data["email"] + for domain_name in email_data["domains"]: + # self.transition_domains is a queryset so we can sub-select + # from it and use the objects to mark them as sent + this_transition_domain = self.transition_domains.get(username=this_email, domain_name=domain_name) + this_transition_domain.email_sent = True + this_transition_domain.save() diff --git a/src/registrar/tests/test_transition_domain_migrations.py b/src/registrar/tests/test_transition_domain_migrations.py index 5c93fd8f0..e048b2830 100644 --- a/src/registrar/tests/test_transition_domain_migrations.py +++ b/src/registrar/tests/test_transition_domain_migrations.py @@ -13,6 +13,8 @@ from registrar.models import ( UserDomainRole, ) +import boto3_mocking # type: ignore + from django.core.management import call_command from unittest.mock import patch @@ -413,3 +415,15 @@ class TestMigrations(TestCase): self.assertIn("Found 2 transition domains", output) self.assertTrue("would send email to testuser@gmail.com", output) self.assertTrue("would send email to agustina.wyman7@test.com", output) + + # this tries to actually send an email, so mock it out + @boto3_mocking.patching + def test_send_domain_invitations_email_sent(self): + """A transition domain is marked email_sent.""" + with less_console_noise(): + self.run_load_domains() + self.run_transfer_domains() + call_command("send_domain_invitations", "-s", "testuser@gmail.com") + + self.assertTrue(TransitionDomain.objects.get(username="testuser@gmail.com").email_sent) + self.assertFalse(TransitionDomain.objects.get(username="agustina.wyman7@test.com").email_sent)