diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 160b906ab..811946549 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1485,6 +1485,7 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin): "status", "rejection_reason", "action_needed_reason", + "action_needed_reason_email", "investigator", "creator", "submitter", diff --git a/src/registrar/assets/js/get-gov-admin.js b/src/registrar/assets/js/get-gov-admin.js index 524cfe594..5824f22c2 100644 --- a/src/registrar/assets/js/get-gov-admin.js +++ b/src/registrar/assets/js/get-gov-admin.js @@ -57,6 +57,7 @@ function openInNewTab(el, removeAttribute = false){ createPhantomModalFormButtons(); })(); + /** An IIFE for DomainRequest to hook a modal to a dropdown option. * This intentionally does not interact with createPhantomModalFormButtons() */ @@ -518,3 +519,79 @@ function initializeWidgetOnList(list, parentId) { handleShowMoreButton(toggleButton, descriptionDiv) } })(); + + + +/** An IIFE that hooks up to the "show email" button + * which shows the auto generated email on action needed reason +*/ +(function () { + let statusDropdown = document.getElementById("id_status"); + + statusDropdown.addEventListener('change', function() { + // TODO we should also handle when action needed + if (statusDropdown.value != "action needed"){ + formRow.classList.add("display-none") + } + }); + + let actionNeededDropdownReason = document.getElementById("id_action_needed_reason"); + // Store the domain request id on this record for simplicity + let showEmailButton = document.getElementById("show_action_needed_email"); + let actionNeededEmail = document.getElementById("id_action_needed_reason_email") + let formRow = actionNeededEmail.closest('.form-row'); + if(actionNeededDropdownReason && showEmailButton && actionNeededEmail && formRow) { + actionNeededDropdownReason.addEventListener('change', function() { + // TODO on change if not actionneeded on status, hide show email button + const pk = showEmailButton.getAttribute("domain-request-id") + const reason = actionNeededDropdownReason.value + fetch(`/get-domain-requests-json/${pk}/action-needed-email/${reason}`) + .then(response => response.json()) + .then(data => { + if (data.error) { + console.log('Error in AJAX call: ' + data.error); + return; + } + + let noEmailMessage = document.getElementById("no-email-message"); + if(data && data.email_body_text) { + actionNeededEmail.value = data.email_body_text + + // Show the text field + if(actionNeededEmail.classList.contains("display-none")) { + actionNeededEmail.classList.remove("display-none") + } + + // Hide the message + if(noEmailMessage && !noEmailMessage.classList.contains("display-none")) { + noEmailMessage.classList.add("display-none") + } + + }else if (data && !data.email_body_text) { + if (!noEmailMessage) { + noEmailMessage = document.createElement("p"); + noEmailMessage.id = "no-email-message"; + noEmailMessage.textContent = "No email will be sent"; + actionNeededEmail.parentNode.appendChild(noEmailMessage); + } + + // Hide the text field + if(!actionNeededEmail.classList.contains("display-none")) { + actionNeededEmail.classList.add("display-none") + } + + // Show the message + if(noEmailMessage.classList.contains("display-none")) { + noEmailMessage.classList.remove("display-none") + } + } + console.log(data) + }); + }); + + showEmailButton.addEventListener('click', function() { + formRow.classList.remove("display-none") + }); + } + +})(); \ No newline at end of file diff --git a/src/registrar/migrations/0103_domainrequest_action_needed_reason_email.py b/src/registrar/migrations/0103_domainrequest_action_needed_reason_email.py new file mode 100644 index 000000000..8df3e47dc --- /dev/null +++ b/src/registrar/migrations/0103_domainrequest_action_needed_reason_email.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.10 on 2024-06-18 20:13 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("registrar", "0102_domain_dsdata_last_change"), + ] + + operations = [ + migrations.AddField( + model_name="domainrequest", + name="action_needed_reason_email", + field=models.TextField(blank=True, null=True), + ), + ] diff --git a/src/registrar/models/domain_request.py b/src/registrar/models/domain_request.py index 7a7f46f65..14b698da7 100644 --- a/src/registrar/models/domain_request.py +++ b/src/registrar/models/domain_request.py @@ -295,6 +295,11 @@ class DomainRequest(TimeStampedModel): blank=True, ) + action_needed_reason_email = models.TextField( + null=True, + blank=True, + ) + federal_agency = models.ForeignKey( "registrar.FederalAgency", on_delete=models.PROTECT, @@ -547,7 +552,10 @@ class DomainRequest(TimeStampedModel): """Returns the default email associated with the given action needed reason""" logger.info(f"reason? {action_needed_reason}") if action_needed_reason is None or action_needed_reason == self.ActionNeededReasons.OTHER: - return {} + return { + "subject_text": None, + "email_body_text": None, + } # Get the email body template_path = f"emails/action_needed_reasons/{action_needed_reason}.txt" @@ -606,6 +614,11 @@ class DomainRequest(TimeStampedModel): def save(self, *args, **kwargs): """Save override for custom properties""" + + if self.action_needed_reason and not self.action_needed_reason_email: + text = self.get_action_needed_reason_default_email_text(self.action_needed_reason) + self.action_needed_reason_email = text.get("email_body_text") + self.sync_organization_type() self.sync_yes_no_form_fields() @@ -851,6 +864,9 @@ class DomainRequest(TimeStampedModel): # Unknown and other are default cases - do nothing can_send_email = False + # TODO - replace this logic with self.action_needed_reason_email in #1901. + # The email content should be dependent on that field. + # Assumes that the template name matches the action needed reason if nothing is specified. # This is so you can override if you need, or have this taken care of for you. if not email_template_name and not email_template_subject_name: diff --git a/src/registrar/templates/django/admin/includes/detail_table_fieldset.html b/src/registrar/templates/django/admin/includes/detail_table_fieldset.html index 0f4274802..e4779c332 100644 --- a/src/registrar/templates/django/admin/includes/detail_table_fieldset.html +++ b/src/registrar/templates/django/admin/includes/detail_table_fieldset.html @@ -106,12 +106,39 @@ This is using a custom implementation fieldset.html (see admin/fieldset.html) + + + {% comment %} +
+ {% endcomment %} {% elif field.field.name == "creator" %} diff --git a/src/registrar/views/domain_requests_json.py b/src/registrar/views/domain_requests_json.py index 21096891b..afc457b00 100644 --- a/src/registrar/views/domain_requests_json.py +++ b/src/registrar/views/domain_requests_json.py @@ -112,9 +112,7 @@ def get_action_needed_email(request, pk, reason): if not has_access: raise PermissionDenied("You do not have permission to access this resource.") - logger.info(f"pk: {pk} reason: {reason}") domain_request = DomainRequest.objects.filter(id=pk).first() - reason_dict = domain_request.get_action_needed_reason_default_email_text(reason) return JsonResponse(