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 SupportersController < ApplicationController
include Controllers :: NonprofitHelper
before_action :authenticate_nonprofit_user! , except : % i [ new create ]
# get /nonprofit/:nonprofit_id/supporters
def index
@panels_layout = true
2018-05-07 21:05:27 +00:00
@nonprofit = current_nonprofit
2019-07-30 21:29:24 +00:00
respond_to do | format |
format . html
format . json do
render json : QuerySupporters . full_search ( params [ :nonprofit_id ] , params )
end
format . csv do
file_date = Date . today . strftime ( '%m-%d-%Y' )
supporters = QuerySupporters . for_export ( params [ :nonprofit_id ] , params )
send_data ( Format :: Csv . from_vectors ( supporters ) , filename : " supporters- #{ file_date } .csv " )
end
end
2018-05-07 21:05:27 +00:00
end
2019-07-30 21:29:24 +00:00
def export
begin
@nonprofit = current_nonprofit
@user = current_user_id
ExportSupporters . initiate_export ( @nonprofit . id , params , @user )
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
2018-05-07 21:05:27 +00:00
end
2018-03-25 17:30:42 +00:00
2019-07-30 21:29:24 +00:00
def index_metrics
render_json do
QuerySupporters . full_search_metrics ( params [ :nonprofit_id ] , params )
end
2018-03-25 17:30:42 +00:00
end
2019-07-30 21:29:24 +00:00
def show
render json : { data : QuerySupporters . for_crm_profile ( params [ :nonprofit_id ] , [ params [ :id ] ] ) . first }
2018-03-25 17:30:42 +00:00
end
2019-07-30 21:29:24 +00:00
def email_address
render json : Supporter . find ( params [ :supporter_id ] ) . email
2018-03-25 17:30:42 +00:00
end
2019-07-30 21:29:24 +00:00
def full_contact
fc = FullContactInfo . where ( " supporter_id= #{ params [ :supporter_id ] } " ) . first
if fc
render json : { full_contact : QueryFullContactInfos . fetch_associated_tables ( fc . id ) }
else
render json : { full_contact : nil }
end
end
def info_card
render json : QuerySupporters . for_info_card ( params [ :supporter_id ] )
end
# post /nonprofits/:nonprofit_id/supporters
def create
2019-11-05 18:44:54 +00:00
render_json { InsertSupporter . create_or_update ( create_supporter_params [ :nonprofit_id ] , params [ :supporter ] ) }
2019-07-30 21:29:24 +00:00
end
# put /nonprofits/:nonprofit_id/supporters/:id
def update
@supporter = current_nonprofit . supporters . find ( params [ :id ] )
2019-11-05 18:44:54 +00:00
json_saved UpdateSupporter . from_info ( @supporter , update_supporter_params [ :supporter ] )
2019-07-30 21:29:24 +00:00
end
def bulk_delete
if params [ :selecting_all ]
supporter_ids = QuerySupporters . full_filter_expr ( current_nonprofit . id , params [ :query ] ) . select ( 'supporters.id' ) . execute . map { | h | h [ 'id' ] }
else
supporter_ids = params [ :supporter_ids ] . map ( & :to_i )
end
render_json { UpdateSupporter . bulk_delete ( current_nonprofit . id , supporter_ids ) }
end
# get /nonprofits/:nonprofit_id/supporters/merge_data
# returns the info required to merge two supporters
def merge_data
render json : QuerySupporters . merge_data ( params [ :ids ] )
end
# post /nonprofits/:nonprofit_id/supporters/merge
def merge
render JsonResp . new ( params ) do | _params |
requires ( :supporter )
requires ( :nonprofit_id ) . as_int
requires ( :supporter_ids ) . as_array
end . when_valid do | params |
params [ :supporter ] [ :nonprofit_id ] = params [ :nonprofit_id ]
2019-11-05 18:44:54 +00:00
MergeSupporters . selected ( update_supporter_params [ :supporter ] , params [ :supporter_ids ] , params [ :nonprofit_id ] , current_user . id )
2019-07-30 21:29:24 +00:00
end
end
2019-11-05 18:44:54 +00:00
private
def create_supporter_params
params . require ( :supporter ) . permit ( :name , :address , :city , :state_code , :country , :address_line2 , :first_name , :last_name , :custom_fields )
end
def update_supporter_params
params . require ( :supporter ) . permit ( :name , :address , :city , :state_code , :country , :address_line2 )
2019-07-30 21:29:24 +00:00
end
2019-11-07 18:11:39 +00:00
end
2018-03-25 17:30:42 +00:00
end