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 CustomFieldJoinsController < ApplicationController
|
|
|
|
include Controllers::NonprofitHelper
|
|
|
|
before_action :authenticate_nonprofit_user!
|
|
|
|
|
|
|
|
def index
|
|
|
|
@custom_field_joins = current_nonprofit
|
2019-08-06 12:49:08 +00:00
|
|
|
.supporters.find(custom_field_params[:supporter_id])
|
2019-07-30 21:29:24 +00:00
|
|
|
.custom_field_joins
|
|
|
|
.order('created_at DESC')
|
|
|
|
end
|
|
|
|
|
|
|
|
# used for modify a single supporter's custom fields or a group of
|
|
|
|
# selected supporters' CFs or all supporters' CFs
|
|
|
|
def modify
|
2019-08-06 12:49:08 +00:00
|
|
|
if custom_field_params[:custom_fields].blank? || custom_field_params[:custom_fields].empty?
|
2019-07-30 21:29:24 +00:00
|
|
|
render json: {}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-08-06 12:49:08 +00:00
|
|
|
if custom_field_params[:selecting_all]
|
|
|
|
supporter_ids = QuerySupporters.full_filter_expr(current_nonprofit.id, custom_field_params[:query]).select('supporters.id').execute.map { |h| h['id'] }
|
2019-07-30 21:29:24 +00:00
|
|
|
else
|
2019-08-06 12:49:08 +00:00
|
|
|
supporter_ids = custom_field_params[:supporter_ids]. map(&:to_i)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 12:49:08 +00:00
|
|
|
render InsertCustomFieldJoins.in_bulk(current_nonprofit.id, supporter_ids, custom_field_params[:custom_fields])
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2019-08-06 12:49:08 +00:00
|
|
|
supporter = current_nonprofit.supporters.find(custom_field_params[:supporter_id])
|
|
|
|
supporter.custom_field_joins.find(custom_field_params[:id]).destroy
|
2019-07-30 21:29:24 +00:00
|
|
|
render json: {}, status: :ok
|
|
|
|
end
|
2019-08-06 12:49:08 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def custom_field_params
|
|
|
|
params.permit(:selecting_all, :supporter_id, :supporter_ids, :custom_fields, :query, :id)
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|