-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathemail_alert.rb
83 lines (68 loc) · 2.53 KB
/
email_alert.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require "gds_api/email_alert_api"
class EmailAlert
HIGH_PRIORITY_DOCUMENT_TYPES = %w[travel_advice medical_safety_alert].freeze
BLANK_DESCRIPTION_DOCUMENT_TYPES = %w[travel_advice].freeze
def initialize(document, logger)
@document = document
@logger = logger
end
def trigger
logger.info "Received major change notification for #{document['title']}, with details #{document['details']}"
lock_handler.with_lock_unless_done do
Services.email_api_client.create_content_change(format_for_email_api, govuk_request_id: document["govuk_request_id"])
rescue GdsApi::HTTPConflict
logger.info "email-alert-api returned conflict for #{document['content_id']}, #{document['base_path']}, #{document['public_updated_at']}"
rescue GdsApi::HTTPUnprocessableEntity
logger.info "email-alert-api returned unprocessable entity for #{document['content_id']}, #{document['base_path']}, #{document['public_updated_at']}"
end
end
def format_for_email_api
{
"title" => document["title"],
"description" => description,
"change_note" => change_note,
"subject" => document["title"],
"tags" => strip_empty_arrays(document.fetch("details", {}).fetch("tags", {})),
"links" => document_links,
"document_type" => document_type,
"email_document_supertype" => document["email_document_supertype"],
"government_document_supertype" => document["government_document_supertype"],
"content_id" => document["content_id"],
"public_updated_at" => document["public_updated_at"],
"publishing_app" => document["publishing_app"],
"base_path" => document["base_path"],
"priority" => priority,
}
end
private
attr_reader :document, :logger
def change_note
ChangeHistory.new(
history: document["details"]["change_history"],
).latest_change_note
end
def lock_handler
LockHandler.new(document.fetch("title"), document.fetch("public_updated_at"))
end
def strip_empty_arrays(tag_hash)
tag_hash.reject { |_, tags| tags.empty? }
end
def document_links
strip_empty_arrays(
document.fetch("links", {}).merge("taxon_tree" => taxon_tree),
)
end
def taxon_tree
TaxonTree.ancestors(document.dig("expanded_links", "taxons").to_a)
end
def document_type
document.fetch("document_type")
end
def priority
HIGH_PRIORITY_DOCUMENT_TYPES.include?(document_type) ? "high" : "normal"
end
def description
return "" if BLANK_DESCRIPTION_DOCUMENT_TYPES.include?(document_type)
document["description"]
end
end