Simplify backend logic

This commit is contained in:
zandercymatics 2024-09-30 15:05:09 -06:00
parent 7cc5231cc0
commit 6bb907c6bc
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 32 additions and 44 deletions

View file

@ -651,9 +651,9 @@ class customActionNeededEmail extends CustomizableEmailBase {
apiUrl: document.getElementById("get-action-needed-email-for-user-json")?.value || null, apiUrl: document.getElementById("get-action-needed-email-for-user-json")?.value || null,
textAreaFormGroup: document.querySelector('.field-action_needed_reason'), textAreaFormGroup: document.querySelector('.field-action_needed_reason'),
dropdownFormGroup: document.querySelector('.field-action_needed_reason_email'), dropdownFormGroup: document.querySelector('.field-action_needed_reason_email'),
statusToCheck: "rejected", statusToCheck: "action needed",
sessionVariableName: "showRejectionReason", sessionVariableName: "showActionNeededReason",
apiErrorMessage: "Error when attempting to grab rejected email: " apiErrorMessage: "Error when attempting to grab action needed email: "
} }
super(emailConfig); super(emailConfig);
} }

View file

@ -665,12 +665,12 @@ class DomainRequest(TimeStampedModel):
# An email is sent out when a, for example, action_needed_reason is changed or added. # An email is sent out when a, for example, action_needed_reason is changed or added.
statuses_that_send_custom_emails = [self.DomainRequestStatus.ACTION_NEEDED, self.DomainRequestStatus.REJECTED] statuses_that_send_custom_emails = [self.DomainRequestStatus.ACTION_NEEDED, self.DomainRequestStatus.REJECTED]
if self.status in statuses_that_send_custom_emails: if self.status in statuses_that_send_custom_emails:
self.send_another_status_reason_email(self.status) self.send_custom_status_update_email(self.status)
# Update the cached values after saving # Update the cached values after saving
self._cache_status_and_status_reasons() self._cache_status_and_status_reasons()
def send_another_status_reason_email(self, status): def send_custom_status_update_email(self, status):
"""Helper function to send out a second status email when the status remains the same, """Helper function to send out a second status email when the status remains the same,
but the reason has changed.""" but the reason has changed."""
@ -691,24 +691,29 @@ class DomainRequest(TimeStampedModel):
"excluded_reasons": [DomainRequest.RejectionReasons.OTHER], "excluded_reasons": [DomainRequest.RejectionReasons.OTHER],
} }
} }
status_info = status_information.get(status)
current_status = status_information.get(status) # Don't send an email if there is nothing to send.
old_reason = status_information.get("cached_reason") if status_info.get("email") is None:
new_reason = status_information.get("reason") logger.warning("send_custom_status_update_email() => Tried sending an empty email.")
email_to_send = status_information.get("email")
# If the status itself changed, then we already sent out an email
if self._cached_status != status or old_reason is None:
return return
# We should never send an email if no reason was specified # We should never send an email if no reason was specified.
# Additionally, Don't send out emails for reasons that shouldn't send them # Additionally, Don't send out emails for reasons that shouldn't send them.
if new_reason is None or new_reason in current_status.get("excluded_reasons"): if status_info.get("reason") is None or status_info.get("reason") in status_info.get("excluded_reasons"):
return return
# Only send out an email if the underlying email itself changed # Only send out an email if the underlying reason itself changed or if no email was sent previously.
if old_reason != new_reason: if status_info.get("cached_reason") != status_info.get("reason") or status_info.get("cached_reason") is None:
self._send_custom_status_update_email(email_content=email_to_send) bcc_address = settings.DEFAULT_FROM_EMAIL if settings.IS_PRODUCTION else ""
self._send_status_update_email(
new_status="action needed",
email_template=f"emails/includes/custom_email.txt",
email_template_subject=f"emails/status_change_subject.txt",
bcc_address=bcc_address,
custom_email_content=status_info.get("email"),
wrap_email=True,
)
def sync_yes_no_form_fields(self): def sync_yes_no_form_fields(self):
"""Some yes/no forms use a db field to track whether it was checked or not. """Some yes/no forms use a db field to track whether it was checked or not.
@ -835,19 +840,6 @@ class DomainRequest(TimeStampedModel):
except EmailSendingError: except EmailSendingError:
logger.warning("Failed to send confirmation email", exc_info=True) logger.warning("Failed to send confirmation email", exc_info=True)
def _send_custom_status_update_email(self, email_content):
"""Wrapper for `_send_status_update_email` that bcc's help@get.gov
and sends an email equivalent to the 'email_content' variable."""
bcc_address = settings.DEFAULT_FROM_EMAIL if settings.IS_PRODUCTION else ""
self._send_status_update_email(
new_status="action needed",
email_template=f"emails/includes/custom_email.txt",
email_template_subject=f"emails/status_change_subject.txt",
bcc_address=bcc_address,
custom_email_content=email_content,
wrap_email=True,
)
def investigator_exists_and_is_staff(self): def investigator_exists_and_is_staff(self):
"""Checks if the current investigator is in a valid state for a state transition""" """Checks if the current investigator is in a valid state for a state transition"""
is_valid = True is_valid = True
@ -959,23 +951,23 @@ class DomainRequest(TimeStampedModel):
This action cleans up the rejection status if moving away from rejected. This action cleans up the rejection status if moving away from rejected.
As side effects this will delete the domain and domain_information As side effects this will delete the domain and domain_information
(will cascade) when they exist.""" (will cascade) when they exist.
Afterwards, we send out an email for action_needed in def save().
See the function send_custom_status_update_email.
"""
if self.status == self.DomainRequestStatus.APPROVED: if self.status == self.DomainRequestStatus.APPROVED:
self.delete_and_clean_up_domain("reject_with_prejudice") self.delete_and_clean_up_domain("reject_with_prejudice")
elif self.status == self.DomainRequestStatus.REJECTED: elif self.status == self.DomainRequestStatus.REJECTED:
self.rejection_reason = None self.rejection_reason = None
# Check if the tuple is setup correctly, then grab its value.
literal = DomainRequest.DomainRequestStatus.ACTION_NEEDED literal = DomainRequest.DomainRequestStatus.ACTION_NEEDED
# Check if the tuple is setup correctly, then grab its value
action_needed = literal if literal is not None else "Action Needed" action_needed = literal if literal is not None else "Action Needed"
logger.info(f"A status change occurred. {self} was changed to '{action_needed}'") logger.info(f"A status change occurred. {self} was changed to '{action_needed}'")
# Send out an email if an action needed reason exists
if self.action_needed_reason and self.action_needed_reason != self.ActionNeededReasons.OTHER:
email_content = self.action_needed_reason_email
self._send_custom_status_update_email(email_content)
@transition( @transition(
field="status", field="status",
source=[ source=[
@ -1070,16 +1062,12 @@ class DomainRequest(TimeStampedModel):
"""Reject an domain request that has been submitted. """Reject an domain request that has been submitted.
As side effects this will delete the domain and domain_information As side effects this will delete the domain and domain_information
(will cascade), and send an email notification.""" (will cascade), and send an email notification using send_custom_status_update_email.
"""
if self.status == self.DomainRequestStatus.APPROVED: if self.status == self.DomainRequestStatus.APPROVED:
self.delete_and_clean_up_domain("reject") self.delete_and_clean_up_domain("reject")
# Send out an email if a rejection reason exists
if self.rejection_reason:
email_content = self.rejection_reason_email
self._send_custom_status_update_email(email_content)
@transition( @transition(
field="status", field="status",
source=[ source=[