1class Admin::Archived::DebateOutcomesController < Admin::AdminController
 
2  before_action :fetch_petition
 
3  before_action :fetch_debate_outcome
 
4
 
5  rescue_from ActiveRecord::RecordNotUnique do
 
6    @debate_outcome = @petition.debate_outcome(true) and update
 
7  end
 
8
 
 9  def show
 
10    render 'admin/archived/petitions/show'
 
11  end
 
 
13  def update
 
14    if @debate_outcome.update(debate_outcome_params)
 
15      if send_email_to_petitioners?
 
16        ::Archived::EmailDebateOutcomesJob.run_later_tonight(petition: @petition)
 
17        message = :email_sent_overnight
 
18      else
 
19        message = :debate_outcome_updated
 
20      end
 
 
22      redirect_to admin_archived_petition_url(@petition), notice: message
 
23    else
 
24      render 'admin/archived/petitions/show'
 
25    end
 
26  end
 
 
28  private
 
 
30  def fetch_petition
 
31    @petition = ::Archived::Petition.debateable.find(params[:petition_id])
 
32  end
 
 
34  def fetch_debate_outcome
 
35    @debate_outcome = @petition.debate_outcome || @petition.build_debate_outcome
 
36  end
 
 
38  def debate_outcome_params
 
39    params.require(:archived_debate_outcome).permit(*debate_outcome_attributes)
 
40  end
 
 
42  def debate_outcome_attributes
 
43    %i[debated_on overview transcript_url video_url debate_pack_url debated commons_image]
 
44  end
 
 
46  def send_email_to_petitioners?
 
47    params.key?(:save_and_email)
 
48  end
 
49end