-
Notifications
You must be signed in to change notification settings - Fork 11
/
remove_document_service.rb
85 lines (71 loc) · 1.95 KB
/
remove_document_service.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
84
85
class RemoveDocumentService
include Callable
def initialize(edition, removal, user: nil)
@edition = edition
@removal = removal
@user = user
end
def call
Document.transaction do
edition.document.lock!
check_removeable
set_removed_at
unpublish_edition
update_edition_status
create_timeline_entry
end
delete_assets
end
private
attr_reader :edition, :removal, :user
def unpublish_edition
GdsApi.publishing_api.unpublish(
edition.content_id,
type: removal.redirect? ? "redirect" : "gone",
explanation: removal.explanatory_note,
alternative_path: removal.alternative_url,
locale: edition.locale,
unpublished_at: removal.removed_at,
)
end
def update_edition_status
AssignEditionStatusService.call(edition,
state: :removed,
status_details: removal,
user: user)
edition.save!
end
def create_timeline_entry
TimelineEntry.create_for_status_change(
entry_type: :removed,
status: edition.status,
details: removal,
)
end
def set_removed_at
removal.removed_at = if edition.removed?
edition.status.details.removed_at
else
Time.zone.now
end
end
def check_removeable
unless edition.live?
raise "attempted to remove an edition other than the live edition"
end
unless edition.current?
raise "Publishing API does not support unpublishing while there is a draft"
end
end
def delete_assets
edition.assets.each do |asset|
next if asset.absent?
begin
GdsApi.asset_manager.delete_asset(asset.asset_manager_id)
rescue GdsApi::HTTPNotFound
Rails.logger.warn("No asset to delete for id #{asset.asset_manager_id}")
end
asset.absent!
end
end
end