Skip to content

Commit

Permalink
Merge pull request #2943 from alphagov/email-alert-config
Browse files Browse the repository at this point in the history
Massively simplify email-signup-related finder schema config
  • Loading branch information
ChrisBAshton authored Jan 9, 2025
2 parents 25eb1f6 + 4f52d23 commit 0fa2d50
Show file tree
Hide file tree
Showing 28 changed files with 451 additions and 2,418 deletions.
10 changes: 2 additions & 8 deletions app/models/finder_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class FinderSchema

define_model_callbacks :update

after_update :override_signup_copy, :remove_empty_related_links, :remove_empty_organisations
after_update :remove_empty_related_links, :remove_empty_organisations

# Pluralized names of all document types
def self.schema_names
Expand All @@ -30,6 +30,7 @@ def self.load_from_schema(type)
attribute :editing_organisations, default: []
attribute :email_filter_by
attribute :email_filter_facets, default: []
attribute :email_filter_options
attribute :facets, default: []
attribute :filter
attribute :format_name
Expand All @@ -42,7 +43,6 @@ def self.load_from_schema(type)
attribute :related
attribute :show_summaries, :boolean, default: false
attribute :signup_content_id
attribute :signup_copy
attribute :signup_link
attribute :subscription_list_title_prefix
attribute :summary, default: ""
Expand All @@ -64,12 +64,6 @@ def format
filter["format"]
end

def override_signup_copy
if signup_copy.present?
_assign_attribute(:signup_copy, "You'll get an email each time a #{document_noun} is updated or a new #{document_noun} is published.")
end
end

def remove_empty_organisations
organisations.reject!(&:blank?)
end
Expand Down
68 changes: 59 additions & 9 deletions app/presenters/finder_signup_content_item_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class FinderSignupContentItemPresenter
attr_reader :schema, :timestamp

def initialize(schema, timestamp)
def initialize(schema, timestamp = Time.zone.now)
@schema = schema
@timestamp = timestamp
end
Expand All @@ -12,7 +12,6 @@ def to_json(*_args)
title:,
schema_name:,
document_type:,
description:,
public_updated_at:,
update_type:,
publishing_app:,
Expand All @@ -27,8 +26,64 @@ def content_id
schema.fetch("signup_content_id")
end

def email_filter_facets
return [] if email_filter_by.nil?

map_facets_to_email_facets(select_relevant_facets)
end

private

def email_filter_by
email_filter_options.fetch("email_filter_by", nil)
end

def email_filter_options
schema.fetch("email_filter_options", {})
end

def select_relevant_facets
facets = schema.fetch("facets", [])
if email_filter_by == "all_selected_facets"
facets.select do |facet|
facet["filterable"] &&
facet["allowed_values"] &&
!(email_filter_options["all_selected_facets_except_for"] || []).include?(facet["key"])
end
else
facets.select { |facet| facet.fetch("key") == email_filter_by }
end
end

def map_facets_to_email_facets(facets)
facets.map do |facet|
overridden_topic_names = email_filter_options["email_alert_topic_name_overrides"]&.fetch(facet["key"], nil) || []

{
facet_id: facet["key"],
facet_name: facet["name"],
required: email_filter_by == "all_selected_facets" ? nil : true,
facet_choices: facet["allowed_values"].map do |allowed_value|
overridden_topic_name = overridden_topic_names.find { |override| override["facet_option_key"] == allowed_value["value"] }&.fetch("topic_name_override")
topic_name = if overridden_topic_name
overridden_topic_name
elsif email_filter_options["downcase_email_alert_topic_names"]
allowed_value["label"].downcase
else
allowed_value["label"]
end

{
key: allowed_value["value"],
radio_button_name: allowed_value["label"],
topic_name:,
prechecked: (email_filter_options["pre_checked_email_alert_checkboxes"] || []).include?(allowed_value["value"]),
}
end,
}.compact
end
end

def locale
"en"
end
Expand All @@ -41,10 +96,6 @@ def base_path
"#{schema.fetch('base_path')}/email-signup"
end

def description
schema.fetch("signup_copy", nil)
end

def document_type
"finder_email_signup"
end
Expand All @@ -71,9 +122,8 @@ def routes
def details
{
"beta" => schema.fetch("signup_beta", false),
"email_filter_facets" => schema.fetch("email_filter_facets", []),
# TODO: Remove email_filter_by once finder-frontend doesn't use it.
"email_filter_by" => schema.fetch("email_filter_by", nil),
"email_filter_facets" => email_filter_facets,
"email_filter_by" => email_filter_by,
"filter" => schema.fetch("filter", nil),
"subscription_list_title_prefix" => schema.fetch("subscription_list_title_prefix", {}),
}
Expand Down
83 changes: 4 additions & 79 deletions lib/documents/schemas/algorithmic_transparency_records.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,88 +21,13 @@
],
"show_summaries": true,
"signup_content_id": "99f5bacb-d1ef-4b93-87ef-bae129fed396",
"signup_copy": "You'll get an email each time a record is updated or a new record is published.",
"subscription_list_title_prefix": {
"singular": "Algorithmic transparency records with the following function: ",
"plural": "Algorithmic transparency records with the following functions: ",
"many": "Algorithmic transparency records: "
"plural": "Algorithmic transparency records with the following functions: "
},
"email_filter_options": {
"email_filter_by": "algorithmic_transparency_record_function"
},
"email_filter_by": "algorithmic_transparency_record_function",
"email_filter_facets": [
{
"facet_id": "algorithmic_transparency_record_function",
"facet_name": "Function",
"required": true,
"facet_choices": [
{
"key": "general-public-services",
"radio_button_name": "General public services",
"topic_name": "General public services",
"prechecked": false
},
{
"key": "defence",
"radio_button_name": "Defence",
"topic_name": "Defence",
"prechecked": false
},
{
"key": "public-order-and-safety",
"radio_button_name": "Public order and safety",
"topic_name": "Public order and safety",
"prechecked": false
},
{
"key": "economic-affairs",
"radio_button_name": "Economic affairs",
"topic_name": "Economic affairs",
"prechecked": false
},
{
"key": "environmental-protection",
"radio_button_name": "Environmental protection",
"topic_name": "Environmental protection",
"prechecked": false
},
{
"key": "housing-and-community-amenities",
"radio_button_name": "Housing and community amenities",
"topic_name": "Housing and community amenities",
"prechecked": false
},
{
"key": "health",
"radio_button_name": "Health",
"topic_name": "Health",
"prechecked": false
},
{
"key": "recreation-culture-and-religion",
"radio_button_name": "Recreation culture and religion",
"topic_name": "Recreation culture and religion",
"prechecked": false
},
{
"key": "education",
"radio_button_name": "Education",
"topic_name": "Education",
"prechecked": false
},
{
"key": "social-protection",
"radio_button_name": "Social protection",
"topic_name": "Social protection",
"prechecked": false
},
{
"key": "regulation",
"radio_button_name": "Regulation",
"topic_name": "Regulation",
"prechecked": false
}
]
}
],
"summary": "<p>Find algorithmic transparency records from UK public sector organisations with information on algorithmic tools used in decision making. These are completed in accordance with the Algorithmic Transparency Recording Standard.</p><p>Let us know what you think of this service: [<a href=\"https://forms.office.com/e/XNn3GBqzhY\">feedback form</a>]</p>",
"document_noun": "record",
"document_title": "Algorithmic transparency record",
Expand Down
1 change: 0 additions & 1 deletion lib/documents/schemas/animal_disease_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"5fa8f693-7631-11e4-a3cb-005056011aef"
],
"signup_content_id": "e39c8c6e-b4c2-4dec-84fd-7f3d77f96a7d",
"signup_copy": "You'll get an email each time a case is updated or a new case is published.",
"subscription_list_title_prefix": "Notifiable animal disease cases and control zones",
"summary": "<p>Find notifiable exotic animal disease cases and control zone declarations for England.</p><p>Read our <a href=\"https://www.gov.uk/government/collections/notifiable-diseases-in-animals\">guidance on notifiable diseases in animals</a>.</p>",
"document_noun": "case",
Expand Down
1 change: 0 additions & 1 deletion lib/documents/schemas/asylum_support_decisions.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"e1d2032c-6a59-4a1a-919c-dc149847dffb"
],
"signup_content_id": "307ea825-594c-4d88-85c9-0809fdc9c79e",
"signup_copy": "You'll get an email each time a decision is updated or a new decision is published.",
"subscription_list_title_prefix": "First-tier Tribunal (Asylum Support) decisions",
"summary": "<p>Find decisions on appeals against the Home Office heard by the First-tier Tribunal (Asylum Support).</p>",
"document_noun": "decision",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
},
"show_summaries": true,
"signup_content_id": "32e87f32-8e37-4186-bc0c-fbed4c0f0239",
"signup_copy": "You'll get an email each time a scheme is updated or a new scheme is published.",
"organisations": [
"aa750cdf-7925-429d-a2b3-0d9fa47d2c48"
],
Expand Down
92 changes: 10 additions & 82 deletions lib/documents/schemas/cma_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"46708567-70a6-4f20-8257-6c222b3d8cb3"
],
"signup_content_id": "43dd2b13-93ec-4ca6-a7a4-e2eb5f5d485a",
"signup_copy": "You'll get an email each time a case is updated or a new case is published.",
"organisations": [
"957eb4ec-089b-4f71-ba2a-dc69ac8919ea"
],
Expand All @@ -29,91 +28,20 @@
],
"subscription_list_title_prefix": {
"singular": "CMA cases with the following case type: ",
"plural": "CMA cases with the following case types: ",
"many": "Competition and Markets Authority (CMA) cases: "
"plural": "CMA cases with the following case types: "
},
"email_filter_by": "case_type",
"email_filter_facets": [
{
"facet_id": "case_type",
"facet_name": "Case type",
"required": true,
"facet_choices": [
{
"key": "ca98-and-civil-cartels",
"radio_button_name": "CA98 and civil cartels",
"topic_name": "CA98 and civil cartels",
"prechecked": false
},
{
"key": "competition-disqualification",
"radio_button_name": "Competition disqualification",
"topic_name": "competition disqualification",
"prechecked": false
},
{
"key": "criminal-cartels",
"radio_button_name": "Criminal cartels",
"topic_name": "criminal cartels",
"prechecked": false
},
{
"key": "digital-markets-unit",
"radio_button_name": "Digital Markets Unit",
"topic_name": "digital markets unit",
"prechecked": false
},
{
"key": "information-and-advice-to-government",
"radio_button_name": "Information and advice to government",
"topic_name": "information and advice to government",
"prechecked": false
},
{
"key": "markets",
"radio_button_name": "Markets",
"topic_name": "markets",
"prechecked": false
},
{
"key": "mergers",
"radio_button_name": "Mergers",
"topic_name": "mergers",
"prechecked": false
},
{
"key": "oim-project",
"radio_button_name": "OIM Project",
"topic_name": "oim project",
"prechecked": false
},
{
"key": "consumer-enforcement",
"radio_button_name": "Consumer enforcement",
"topic_name": "consumer enforcement",
"prechecked": false
},
{
"key": "regulatory-references-and-appeals",
"radio_button_name": "Regulatory references and appeals",
"topic_name": "regulatory references and appeals",
"prechecked": false
},
{
"key": "review-of-orders-and-undertakings",
"radio_button_name": "Reviews of orders and undertakings",
"topic_name": "reviews of orders and undertakings",
"prechecked": false
},
{
"key": "sau-referral",
"radio_button_name": "SAU referral",
"topic_name": "sau referral",
"prechecked": false
"email_filter_options": {
"email_filter_by": "case_type",
"downcase_email_alert_topic_names": true,
"email_alert_topic_name_overrides": {
"case_type": [
{
"facet_option_key": "ca98-and-civil-cartels",
"topic_name_override": "CA98 and civil cartels"
}
]
}
],
},
"document_noun": "case",
"document_title": "CMA Case",
"facets": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"357110bb-cbc5-4708-9711-1b26e6c63e86"
],
"signup_content_id": "1f5911f4-417a-4380-a5a0-674ebff332df",
"signup_copy": "You'll get an email each time a decision is updated or a new decision is published.",
"subscription_list_title_prefix": "Employment Appeal Tribunal decisions",
"summary": "<p>Find decisions on appeals against employment tribunals heard by the Employment Appeal Tribunal.</p><p>Includes decisions after December 2015. Find details of <a rel=\"external\" href=\"http://www.employmentappeals.gov.uk/Public/Search.aspx\">older cases.</a></p>",
"document_noun": "decision",
Expand Down
Loading

0 comments on commit 0fa2d50

Please sign in to comment.