1class Admin::SignaturesController < Admin::AdminController
 
2  include BulkVerification
 
3
 
4  before_action :fetch_petition, if: :petition_scope?
 
5  before_action :fetch_signature, except: [:index, :bulk_validate, :bulk_invalidate, :bulk_subscribe, :bulk_unsubscribe, :bulk_destroy]
 
6  before_action :fetch_signatures, only: [:index]
 
7
 
8  helper_method :search_params
 
 9
 
10  def index
 
11    respond_to do |format|
 
12      format.html
 
13    end
 
14  end
 
 
16  def bulk_validate
 
17    begin
 
18      scope.validate!(selected_ids, force: true)
 
19      redirect_to index_url(search_params), notice: :signatures_validated
 
20    rescue StandardError => e
 
21      Appsignal.send_exception e
 
22      redirect_to index_url(search_params), alert: :signatures_not_validated
 
23    end
 
24  end
 
 
26  def validate
 
27    begin
 
28      @signature.validate!(force: true)
 
29      redirect_to index_url(search_params), notice: :signature_validated
 
30    rescue StandardError => e
 
31      Appsignal.send_exception e
 
32      redirect_to index_url(search_params), alert: :signature_not_validated
 
33    end
 
34  end
 
 
36  def bulk_invalidate
 
37    begin
 
38      scope.invalidate!(selected_ids)
 
39      redirect_to index_url(search_params), notice: :signatures_invalidated
 
40    rescue StandardError => e
 
41      Appsignal.send_exception e
 
42      redirect_to index_url(search_params), alert: :signatures_not_invalidated
 
43    end
 
44  end
 
 
46  def invalidate
 
47    begin
 
48      @signature.invalidate!
 
49      redirect_to index_url(search_params), notice: :signature_invalidated
 
50    rescue StandardError => e
 
51      Appsignal.send_exception e
 
52      redirect_to index_url(q: @signature.email), alert: :signature_not_invalidated
 
53    end
 
54  end
 
 
56  def bulk_destroy
 
57    begin
 
58      scope.destroy!(selected_ids)
 
59      redirect_to index_url(search_params), notice: :signatures_deleted
 
60    rescue StandardError => e
 
61      Appsignal.send_exception e
 
62      redirect_to index_url(search_params), alert: :signatures_not_deleted
 
63    end
 
64  end
 
 
66  def destroy
 
67    if @signature.destroy
 
68      redirect_to index_url(search_params), notice: :signature_deleted
 
69    else
 
70      redirect_to index_url(search_params), alert: :signature_not_deleted
 
71    end
 
72  end
 
 
74  def bulk_subscribe
 
75    begin
 
76      scope.subscribe!(selected_ids)
 
77      redirect_to index_url(search_params), notice: :signatures_subscribed
 
78    rescue StandardError => e
 
79      Appsignal.send_exception e
 
80      redirect_to index_url(search_params), alert: :signatures_not_subscribed
 
81    end
 
82  end
 
 
84  def subscribe
 
85    if @signature.update(notify_by_email: true)
 
86      redirect_to admin_signatures_url(search_params), notice: :signature_subscribed
 
87    else
 
88      redirect_to admin_signatures_url(search_params), alert: :signature_not_subscribed
 
89    end
 
90  end
 
 
92  def bulk_unsubscribe
 
93    begin
 
94      scope.unsubscribe!(selected_ids)
 
95      redirect_to index_url(search_params), notice: :signatures_unsubscribed
 
96    rescue StandardError => e
 
97      Appsignal.send_exception e
 
98      redirect_to index_url(search_params), alert: :signatures_not_unsubscribed
 
 99    end
 
100  end
 
 
102  def unsubscribe
 
103    if @signature.update(notify_by_email: false)
 
104      redirect_to index_url(search_params), notice: :signature_unsubscribed
 
105    else
 
106      redirect_to index_url(search_params), alert: :signature_not_unsubscribed
 
107    end
 
108  end
 
 
110  private
 
 
112  def petition_scope?
 
113    params.key?(:petition_id)
 
114  end
 
 
116  def fetch_petition
 
117    @petition = Petition.find(params[:petition_id])
 
118  end
 
 
120  def scope
 
121    params.key?(:petition_id) ? @petition.signatures : Signature
 
122  end
 
 
124  def fetch_signatures
 
125    @signatures = scope.search(params[:q], search_params)
 
126  end
 
 
128  def fetch_signature
 
129    @signature = scope.find(params[:id])
 
130  end
 
 
132  def search_params
 
133    params.slice(:q, :page, :state, :window)
 
134  end
 
 
136  def index_url(*args)
 
137    if petition_scope?
 
138      admin_petition_signatures_url(*args)
 
139    else
 
140      admin_signatures_url(*args)
 
141    end
 
142  end
 
143end