-
Notifications
You must be signed in to change notification settings - Fork 11
/
discard_draft_edition_service.rb
49 lines (39 loc) · 1.38 KB
/
discard_draft_edition_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
class DiscardDraftEditionService
include Callable
def initialize(edition, user, **)
@edition = edition
@user = user
end
def call
raise "Only current editions can be deleted" unless edition.current?
raise "Trying to delete a live edition" if edition.live?
DeleteDraftAssetsService.call(edition)
discard_draft(edition)
reset_live_edition if document.live_edition
DiscardPathReservationsService.call(edition) if edition.first?
document.reload_current_edition
end
private
attr_reader :edition, :user
delegate :document, to: :edition
def reset_live_edition
document.live_edition.update!(current: true)
document.reload_live_edition
end
def discard_draft(edition)
begin
GdsApi.publishing_api.discard_draft(edition.content_id)
rescue GdsApi::HTTPNotFound
Rails.logger.warn("No draft to discard for content id #{edition.content_id}")
rescue GdsApi::HTTPUnprocessableEntity => e
no_draft_message = "There is not a draft edition of this document to discard"
if e.error_details.respond_to?(:dig) && e.error_details.dig("error", "message") == no_draft_message
Rails.logger.warn("No draft to discard for content id #{edition.content_id}")
else
raise
end
end
AssignEditionStatusService.call(edition, user: user, state: :discarded)
edition.update!(current: false)
end
end