-
Notifications
You must be signed in to change notification settings - Fork 11
/
withdraw_document_service.rb
68 lines (57 loc) · 1.75 KB
/
withdraw_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
class WithdrawDocumentService
include Callable
def initialize(edition, user, public_explanation:)
@edition = edition
@public_explanation = public_explanation
@user = user
end
def call
edition.document.lock!
check_withdrawable
withdrawal = build_withdrawal
update_edition(withdrawal)
unpublish_edition
end
private
attr_reader :edition, :public_explanation, :user
def unpublish_edition
GdsApi.publishing_api.unpublish(
edition.content_id,
type: "withdrawal",
explanation: format_govspeak(public_explanation, edition),
locale: edition.locale,
unpublished_at: edition.status.details.withdrawn_at,
)
end
def update_edition(withdrawal)
AssignEditionStatusService.call(edition,
user: user,
state: :withdrawn,
status_details: withdrawal)
edition.save!
end
def check_withdrawable
document = edition.document
if edition != document.live_edition
raise "attempted to withdraw an edition other than the live edition"
end
if document.current_edition != document.live_edition
raise "Publishing API does not support unpublishing while there is a draft"
end
end
def build_withdrawal
if edition.withdrawn?
withdrawal = edition.status.details.dup
withdrawal.tap do |w|
w.assign_attributes(public_explanation: public_explanation)
end
else
Withdrawal.new(public_explanation: public_explanation,
published_status: edition.status,
withdrawn_at: Time.zone.now)
end
end
def format_govspeak(text, edition)
GovspeakDocument.new(text, edition).payload_html
end
end