Skip to content

Commit 5a6da4f

Browse files
authored
Merge pull request #2613 from alphagov/trigger-org-admin-alerts
Send alert emails to organisation admins when forms change state
2 parents 4c247e4 + 3799172 commit 5a6da4f

17 files changed

+632
-45
lines changed

app/controllers/forms/copy_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ def create
2525
if @copy_input.submit
2626
copied_form = FormCopyService.new(current_form, current_user).copy(tag: @copy_input.tag)
2727
copied_form.update!(name: @copy_input.name)
28+
29+
OrgAdminAlertsService.new(form: copied_form, current_user:).new_draft_form_created
30+
2831
redirect_to form_path(copied_form.id), success: t("banner.success.form.copied")
2932
else
3033
set_back_link(current_form)

app/controllers/forms/make_live_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def create
1717
@make_form_live_service = MakeFormLiveService.call(current_form:, current_user:)
1818
@make_form_live_service.make_live unless current_form.live?
1919

20+
if current_form.state_previously_changed?
21+
OrgAdminAlertsService.new(form: current_form, current_user:).form_made_live
22+
end
23+
2024
render "confirmation", locals: { current_form:, confirmation_page_title: @make_form_live_service.page_title, confirmation_page_body: @make_form_live_service.confirmation_page_body }
2125
end
2226

app/controllers/forms/unarchive_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def create
1717
@make_form_live_service = MakeFormLiveService.call(current_form:, current_user:)
1818
@make_form_live_service.make_live
1919

20+
if current_form.state_previously_changed?
21+
OrgAdminAlertsService.new(form: current_form, current_user:).form_made_live
22+
end
23+
2024
render "forms/make_live/confirmation", locals: { current_form:, confirmation_page_title: @make_form_live_service.page_title, confirmation_page_body: @make_form_live_service.confirmation_page_body }
2125
end
2226

app/controllers/forms_controller.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class FormsController < WebController
2+
before_action :load_form
23
after_action :verify_authorized
4+
after_action :alert_org_admins_if_draft_created
35

4-
def current_form
5-
@current_form ||= Form.find(params[:form_id])
6-
end
6+
attr_reader :current_form
77

88
def current_live_form
99
@current_live_form ||= FormDocument::Content.from_form_document(current_form.live_form_document)
@@ -20,4 +20,18 @@ def current_archived_form
2020
def current_archived_welsh_form
2121
@current_archived_welsh_form ||= FormDocument::Content.from_form_document(current_form.archived_welsh_form_document)
2222
end
23+
24+
private
25+
26+
def load_form
27+
@current_form = Form.find(params[:form_id])
28+
@initial_form_state = @current_form.state
29+
end
30+
31+
def alert_org_admins_if_draft_created
32+
return if current_form.destroyed?
33+
return unless @current_form.reload.draft_created?(@initial_form_state)
34+
35+
OrgAdminAlertsService.new(form: current_form, current_user:).draft_of_existing_form_created
36+
end
2337
end

app/controllers/group_forms_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def create
1919

2020
if @name_input.valid?
2121
@form = CreateFormService.new.create!(creator: @current_user, group: @group, name: @name_input.name)
22+
OrgAdminAlertsService.new(form: @form, current_user:).new_draft_form_created
2223

2324
redirect_to form_path(@form.id)
2425
else

app/models/form.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ def normalise_welsh!
211211
pages.each(&:normalise_welsh!)
212212
end
213213

214+
# Pass in the previous state rather than getting it from #state_previously_was as the Form may have been updated in a
215+
# separate instance
216+
def draft_created?(previous_state)
217+
return false if state.to_sym == previous_state.to_sym
218+
219+
(previous_state.to_sym == :live && live_with_draft?) ||
220+
(previous_state.to_sym == :archived && archived_with_draft?)
221+
end
222+
214223
private
215224

216225
def set_external_id
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
class OrgAdminAlertsService
2+
def initialize(form:, current_user:)
3+
@form = form
4+
@current_user = current_user
5+
@org_admins = @form.group.organisation.admin_users.where.not(id: @current_user.id)
6+
end
7+
8+
def form_made_live
9+
return unless feature_enabled?
10+
11+
@org_admins.each do |org_admin_user|
12+
form_made_live_email(to_email: org_admin_user.email).deliver_now
13+
end
14+
end
15+
16+
def new_draft_form_created
17+
return unless feature_enabled?
18+
return unless @form.group.active?
19+
20+
@org_admins.each do |org_admin_user|
21+
new_draft_form_created_email(to_email: org_admin_user.email).deliver_now
22+
end
23+
end
24+
25+
def draft_of_existing_form_created
26+
return unless feature_enabled?
27+
28+
@org_admins.each do |org_admin_user|
29+
draft_of_existing_form_created_email(to_email: org_admin_user.email).deliver_now
30+
end
31+
end
32+
33+
private
34+
35+
def form_made_live_email(to_email:)
36+
previous_state = @form.state_previously_was.to_sym
37+
38+
case previous_state
39+
when :draft
40+
new_draft_made_live_email(to_email:)
41+
when :live_with_draft
42+
OrgAdminAlerts::MadeLiveMailer.live_form_changes_made_live(form: @form, user: @current_user, to_email:)
43+
when :archived
44+
OrgAdminAlerts::MadeLiveMailer.archived_form_made_live(form: @form, user: @current_user, to_email:)
45+
when :archived_with_draft
46+
OrgAdminAlerts::MadeLiveMailer.archived_form_changes_made_live(form: @form, user: @current_user, to_email:)
47+
else
48+
raise StandardError, "Unexpected previous state: #{previous_state}"
49+
end
50+
end
51+
52+
def new_draft_made_live_email(to_email:)
53+
if copied_from_form
54+
OrgAdminAlerts::MadeLiveMailer.copied_form_made_live(form: @form, copied_from_form:, user: @current_user, to_email:)
55+
else
56+
OrgAdminAlerts::MadeLiveMailer.new_draft_form_made_live(form: @form, user: @current_user, to_email:)
57+
end
58+
end
59+
60+
def new_draft_form_created_email(to_email:)
61+
if copied_from_form
62+
OrgAdminAlerts::DraftCreatedMailer.copied_draft_form_created(form: @form, copied_from_form:, user: @current_user, to_email:)
63+
else
64+
OrgAdminAlerts::DraftCreatedMailer.new_draft_form_created(form: @form, user: @current_user, to_email:)
65+
end
66+
end
67+
68+
def draft_of_existing_form_created_email(to_email:)
69+
case @form.state.to_sym
70+
when :live_with_draft
71+
OrgAdminAlerts::DraftCreatedMailer.new_live_form_draft_created(form: @form, user: @current_user, to_email:)
72+
when :archived_with_draft
73+
OrgAdminAlerts::DraftCreatedMailer.new_archived_form_draft_created(form: @form, user: @current_user, to_email:)
74+
else
75+
raise StandardError, "Unexpected form state: #{@form.state}"
76+
end
77+
end
78+
79+
def copied_from_form
80+
Form.find_by(id: @form.copied_from_id)
81+
end
82+
83+
def feature_enabled?
84+
FeatureService.enabled?(:org_admin_alerts_enabled)
85+
end
86+
end

spec/controllers/forms_controller_spec.rb

Lines changed: 0 additions & 26 deletions
This file was deleted.

spec/models/form_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,45 @@
13061306
}
13071307
end
13081308

1309+
describe "#draft_created?" do
1310+
subject(:draft_created?) { form.draft_created?(initial_state) }
1311+
1312+
context "when a draft has been created for a live form" do
1313+
let(:form) { create(:form, :live_with_draft) }
1314+
let(:initial_state) { "live" }
1315+
1316+
it { is_expected.to be true }
1317+
end
1318+
1319+
context "when a draft has been created for an archived form" do
1320+
let(:form) { create(:form, :archived_with_draft) }
1321+
let(:initial_state) { "archived" }
1322+
1323+
it { is_expected.to be true }
1324+
end
1325+
1326+
context "when the state has not changed" do
1327+
let(:form) { create(:form, :live_with_draft) }
1328+
let(:initial_state) { "live_with_draft" }
1329+
1330+
it { is_expected.to be false }
1331+
end
1332+
1333+
context "when the state has been updated to live" do
1334+
let(:form) { create(:form, :live) }
1335+
let(:initial_state) { "live_with_draft" }
1336+
1337+
it { is_expected.to be false }
1338+
end
1339+
1340+
context "when the state has been updated from live with draft to archived with draft" do
1341+
let(:form) { create(:form, :archived_with_draft) }
1342+
let(:initial_state) { "live_with_draft" }
1343+
1344+
it { is_expected.to be false }
1345+
end
1346+
end
1347+
13091348
context "when Welsh is not an available language" do
13101349
let(:languages) { %w[en] }
13111350

spec/requests/forms/copy_controller_spec.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
require "rails_helper"
22

3-
RSpec.describe Forms::CopyController, type: :request do
3+
RSpec.describe Forms::CopyController, :feature_org_admin_alerts_enabled, type: :request do
44
let(:id) { form.id }
55
let(:form) { create(:form) }
66

7-
let(:group) { create(:group, organisation: standard_user.organisation) }
7+
let(:group) { create(:group, organisation: standard_user.organisation, status: :active) }
88

99
before do
1010
Membership.create!(group_id: group.id, user: standard_user, added_by: standard_user)
@@ -54,13 +54,25 @@
5454
end
5555

5656
describe "#create" do
57+
before do
58+
create(:organisation_admin_user, organisation: standard_user.organisation)
59+
end
60+
5761
context "when the copy is successful" do
5862
it "redirects to the form page" do
5963
post create_copy_form_path(id), params: { forms_copy_input: { name: "Copied Form", tag: "draft" } }
6064

6165
expect(response).to have_http_status(:found)
6266
expect(response).to redirect_to(form_path(Form.last.id))
6367
end
68+
69+
it "sends an email to the organisation admins" do
70+
post create_copy_form_path(id), params: { forms_copy_input: { name: "Copied Form", tag: "draft" } }
71+
expect(ActionMailer::Base.deliveries.count).to eq(1)
72+
73+
template_id = Settings.govuk_notify.org_admin_alerts.copied_draft_form_created_template_id
74+
expect(ActionMailer::Base.deliveries.last.govuk_notify_template).to eq(template_id)
75+
end
6476
end
6577

6678
context "when the copy fails" do
@@ -70,6 +82,11 @@
7082
expect(response).to have_http_status(:ok)
7183
expect(response).to render_template(:confirm)
7284
end
85+
86+
it "does not send an email to the organisation admins" do
87+
post create_copy_form_path(id), params: { forms_copy_input: { name: "", tag: "draft" } }
88+
expect(ActionMailer::Base.deliveries.count).to eq(0)
89+
end
7390
end
7491
end
7592
end

0 commit comments

Comments
 (0)