2019-07-30 21:29:24 +00:00
# frozen_string_literal: true
2018-03-25 16:15:39 +00:00
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
2018-03-25 17:30:42 +00:00
module Nonprofits
2019-07-30 21:29:24 +00:00
class RecurringDonationsController < ApplicationController
include Controllers :: NonprofitHelper
2018-03-25 17:30:42 +00:00
2019-07-30 21:29:24 +00:00
before_action :authenticate_nonprofit_user! , except : [ :create ]
2018-03-25 17:30:42 +00:00
2019-07-30 21:29:24 +00:00
# get /nonprofits/:nonprofit_id/recurring_donations
def index
@nonprofit = current_nonprofit
@panels_layout = true
respond_to do | format |
format . html
format . json do
# set dashboard params include externally active and failed
# TODO move into javascript
params [ :active ] = true
2018-03-25 17:30:42 +00:00
2019-07-30 21:29:24 +00:00
render json : QueryRecurringDonations . full_list ( params [ :nonprofit_id ] , params )
end
2018-03-25 17:30:42 +00:00
end
2019-07-30 21:29:24 +00:00
end
2018-03-25 17:30:42 +00:00
2019-07-30 21:29:24 +00:00
def export
begin
@nonprofit = current_nonprofit
@user = current_user_id
# TODO: move into javascript
if params . key? ( :active_and_not_failed )
params . delete ( :active ) if params . key? ( :active )
params . delete ( :failed ) if params . key? ( :failed )
end
2018-03-25 17:30:42 +00:00
2019-07-30 21:29:24 +00:00
% i [ active_and_not_failed active failed ] . each do | k |
if params . key? ( k )
params [ k ] = ActiveRecord :: ConnectionAdapters :: Column . value_to_boolean ( params [ k ] )
end
end
2018-03-25 17:30:42 +00:00
2019-07-30 21:29:24 +00:00
params [ :root_url ] = root_url
2018-03-25 17:30:42 +00:00
2019-07-30 21:29:24 +00:00
ExportRecurringDonations . initiate_export ( @nonprofit . id , params , current_user . id )
rescue StandardError = > e
e
end
if e . nil?
flash [ :notice ] = " Your export was successfully initiated and you'll be emailed at #{ current_user . email } as soon as it's available. Feel free to use the site in the meantime. "
render json : { } , status : :ok
else
render json : e , status : :ok
end
end
2018-03-25 17:30:42 +00:00
2019-07-30 21:29:24 +00:00
def show
@recurring_donation = current_recurring_donation
respond_to { | format | format . json }
end
2018-03-25 17:30:42 +00:00
2019-07-30 21:29:24 +00:00
def destroy
UpdateRecurringDonations . cancel ( params [ :id ] , current_user . email )
json_saved current_recurring_donation
end
2018-03-25 17:30:42 +00:00
2019-07-30 21:29:24 +00:00
def update
json_saved UpdateRecurringDonations
2019-08-06 14:07:35 +00:00
. update ( current_recurring_donation , recurring_donation_params )
2018-03-25 17:30:42 +00:00
end
2019-07-30 21:29:24 +00:00
# post /nonprofits/:nonprofit_id/recurring_donations
def create
2019-08-06 14:07:35 +00:00
if recurring_donation_params [ :token ]
render_json { InsertRecurringDonation . with_stripe ( recurring_donation_params ) }
elsif recurring_donation_params [ :direct_debit_detail_id ]
render JsonResp . new ( recurring_donation_params ) do | _data |
2019-07-30 21:29:24 +00:00
requires ( :amount ) . as_int
requires ( :supporter_id , :nonprofit_id , :direct_debit_detail_id ) . as_int
optional ( :dedication , :designation ) . as_string
optional ( :campaign_id , :event_id ) . as_int
end . when_valid do | data |
InsertRecurringDonation . with_sepa ( data )
end
else
render json : { } , status : 422
end
end
2018-03-25 17:30:42 +00:00
2019-08-06 14:07:35 +00:00
private
2018-03-25 17:30:42 +00:00
2019-07-30 21:29:24 +00:00
def current_recurring_donation
@recurring_donation || = current_nonprofit . recurring_donations . find params [ :id ]
end
2019-08-06 14:07:35 +00:00
def recurring_donation_params
params . require ( :recurring_donation ) . permit ( :amount , :active , :paydate , :interval , :time_unit , :start_date , :end_date , :n_failures , :edit_token , :cancelled_by , :cancelled_at , :donation_id , :nonprofit_id , :supporter_id )
2019-07-30 21:29:24 +00:00
end
2019-08-06 14:07:35 +00:00
end
2018-03-25 17:30:42 +00:00
end