Skip to content

Send alert emails to organisation admins when forms change state#2613

Merged
stephencdaly merged 10 commits intomainfrom
trigger-org-admin-alerts
Mar 12, 2026
Merged

Send alert emails to organisation admins when forms change state#2613
stephencdaly merged 10 commits intomainfrom
trigger-org-admin-alerts

Conversation

@stephencdaly
Copy link
Contributor

@stephencdaly stephencdaly commented Mar 10, 2026

What problem does this pull request solve?

Trello card: https://trello.com/c/vnpXef5W

Send emails to all organisation admins for the state changes we care about if the feature flag is enabled.

These are as follows:

  • A form is made live. We send different emails for:
    • A form is made live for the first time
    • A form that was copied from another form is made live for the first time
    • Changes to a live form are made live
    • Changes to an archived form are made live
    • An archived form is made live without any changes
  • A new draft form is created in an active group. We send different emails for:
    • When the form is not a copy
    • When the form is copied from another form
  • A draft is made for an existing form. We send different emails for:
    • When the form is live
    • When the form is archived

Things to consider when reviewing

  • Ensure that you consider the wider context.
  • Does it work when run on your machine?
  • Is it clear what the code is doing?
  • Do the commit messages explain why the changes were made?
  • Are there all the unit tests needed?
  • Do the end to end tests need updating before these changes will pass?
  • Has all relevant documentation been updated?

@stephencdaly stephencdaly force-pushed the trigger-org-admin-alerts branch 5 times, most recently from ff85fdd to d162b11 Compare March 10, 2026 17:24
@SamJamCul SamJamCul force-pushed the add-mailers-for-org-admin-alerts branch 2 times, most recently from c125ce4 to 979317a Compare March 11, 2026 10:21
Base automatically changed from add-mailers-for-org-admin-alerts to main March 11, 2026 10:41
@stephencdaly stephencdaly force-pushed the trigger-org-admin-alerts branch 3 times, most recently from 7d4893e to 6066b63 Compare March 11, 2026 17:15
@stephencdaly stephencdaly marked this pull request as ready for review March 11, 2026 17:23
Add a service with a method to send the appropriate email when a form
is made live. The email to send depends on the previous state of the
form, and whether the form is a copy if it was in the 'draft' state.
When a form is made live, call the OrgAdminAlertsService to send the
appropriate alerts to org admins. This is done in the controller as
we need to know the user making the change to include in the alert.
There is a separate controller for making an archived form live agin
without changes. When the form is unarchived, send the appropriate
alerts to org admins.
Add a method that calls the correct mailer to send an email to
organisation admins when a new draft form is created. The mailer to
user depends on whether the form is a copy of another form or not.
When a new form is successfully created, call the OrgAdminAlerService
to send an email to all organisation admins. The controller does not
need to check if the group is active, as the service does this.
When a form is successfully copied, call the OrgAdminAlerService
to send an email to all organisation admins. The controller does not
need to check if the group is active, as the service does this.
Add a method that sends an email to all organisation admins when a
draft of a live or archived form is created. We do not need to check
whether the group is active in this case, as the group would need to
be active for the form to have been made live.
Send an email to organisation admins when a draft is created for an
existing live or archived form. This is added as an after_action in
the FormsController base controller as any controller action that
updates a form will create a draft if the form is currently in a live
or archived state.

We need to store the initial state of the form in the controller to
compare to in the after_action to determine if a draft was created.
This is because the form could have been updated in a separate
instance of the Form model, which is the case when adding a new
question to the form - so we cannot use the `state_previously_was`
helper method provided by ActiveModel::Dirty.

As all controllers that inherit from FormsController use the
`current_form`, we can set the instance variable in a before_action
along with the @initial_form_state. This makes the controller tests
for FormsController redundant as they were just checking the that
form was only retrieved once.
The FeatureService stubbing didn't work with the new feature flag that
had been added, so set the feature flag using RSpec metadata instead.
@stephencdaly stephencdaly force-pushed the trigger-org-admin-alerts branch from 6066b63 to 3799172 Compare March 11, 2026 17:29
@sonarqubecloud
Copy link

@github-actions
Copy link

🎉 A review copy of this PR has been deployed! You can reach it at: https://pr-2613.admin.review.forms.service.gov.uk/

It may take 5 minutes or so for the application to be fully deployed and working. If it still isn't ready
after 5 minutes, there may be something wrong with the ECS task. You will need to go to the integration AWS account
to debug, or otherwise ask an infrastructure person.

For the sign in details and more information, see the review apps wiki page.

Copy link
Contributor

@SamJamCul SamJamCul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, I think this does everything we need

@stephencdaly stephencdaly added this pull request to the merge queue Mar 12, 2026
Merged via the queue into main with commit 5a6da4f Mar 12, 2026
6 checks passed
@stephencdaly stephencdaly deleted the trigger-org-admin-alerts branch March 12, 2026 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants