Skip to content

Commit 7818478

Browse files
authored
Merge pull request #3749 from alphagov/pnp-9732-redirect-orphaned-html-publications-to-their-paren
Add rake task to find semi-orphaned html_publications and redirect to their parents
2 parents 7a4b469 + 9fc0861 commit 7818478

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
desc "Finds HTML Publications that were deleted from their publication without being redirected to it, and fixes that"
2+
task redirect_semi_orphaned_html_publications: :environment do
3+
successes = 0
4+
editions = Edition.where(phase: "live", rendering_app: "government-frontend", schema_name: "html_publication", state: "published")
5+
found = editions.count
6+
7+
editions.each do |orphan_edition|
8+
successes += 1 if redirect_orphaned_document(orphan_edition.document)
9+
end
10+
11+
Rails.logger.info("Of #{found} orphans, #{successes} were redirected to their parent")
12+
end
13+
14+
def redirect_orphaned_document(document)
15+
parent_link = document.live.links.find_by(link_type: "parent")
16+
if parent_link.nil?
17+
puts("ERROR: Couldn't find parent link for #{document.content_id}")
18+
return false
19+
end
20+
21+
parent = parent_link.target_documents.find_by(locale: [document.locale, "en"])
22+
if parent.nil?
23+
puts("ERROR: Couldn't find parent for #{document.content_id}")
24+
return false
25+
end
26+
27+
if document.draft.present?
28+
Commands::V2::DiscardDraft.call(
29+
{
30+
content_id: document.content_id,
31+
locale: document.locale,
32+
},
33+
)
34+
end
35+
36+
Commands::V2::Unpublish.call(
37+
{
38+
content_id: document.content_id,
39+
locale: document.locale,
40+
type: "redirect",
41+
alternative_path: parent.live.base_path,
42+
},
43+
)
44+
45+
true
46+
end

0 commit comments

Comments
 (0)