You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
3.5 KiB
139 lines
3.5 KiB
2 years ago
|
class LettingsLogsController < ApplicationController
|
||
3 years ago
|
include Pagy::Backend
|
||
2 years ago
|
include Modules::LettingsLogsFilter
|
||
3 years ago
|
include Modules::SearchFilter
|
||
3 years ago
|
|
||
3 years ago
|
skip_before_action :verify_authenticity_token, if: :json_api_request?
|
||
|
before_action :authenticate, if: :json_api_request?
|
||
3 years ago
|
before_action :authenticate_user!, unless: :json_api_request?
|
||
3 years ago
|
before_action :find_resource, except: %i[create index edit]
|
||
3 years ago
|
|
||
3 years ago
|
def index
|
||
3 years ago
|
set_session_filters
|
||
3 years ago
|
|
||
2 years ago
|
all_logs = current_user.lettings_logs
|
||
|
unpaginated_filtered_logs = filtered_lettings_logs(filtered_collection(all_logs, search_term))
|
||
3 years ago
|
|
||
3 years ago
|
respond_to do |format|
|
||
3 years ago
|
format.html do
|
||
2 years ago
|
@pagy, @lettings_logs = pagy(unpaginated_filtered_logs)
|
||
3 years ago
|
@searched = search_term.presence
|
||
|
@total_count = all_logs.size
|
||
|
end
|
||
|
|
||
3 years ago
|
format.csv do
|
||
2 years ago
|
send_data byte_order_mark + unpaginated_filtered_logs.to_csv(current_user), filename: "logs-#{Time.zone.now}.csv"
|
||
3 years ago
|
end
|
||
3 years ago
|
end
|
||
3 years ago
|
end
|
||
3 years ago
|
|
||
3 years ago
|
def create
|
||
2 years ago
|
lettings_log = LettingsLog.new(lettings_log_params)
|
||
3 years ago
|
respond_to do |format|
|
||
3 years ago
|
format.html do
|
||
2 years ago
|
lettings_log.save!
|
||
|
redirect_to lettings_log_url(lettings_log)
|
||
3 years ago
|
end
|
||
3 years ago
|
format.json do
|
||
2 years ago
|
if lettings_log.save
|
||
|
render json: lettings_log, status: :created
|
||
3 years ago
|
else
|
||
2 years ago
|
render json: { errors: lettings_log.errors.messages }, status: :unprocessable_entity
|
||
3 years ago
|
end
|
||
|
end
|
||
3 years ago
|
end
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def update
|
||
2 years ago
|
if @lettings_log
|
||
|
if @lettings_log.update(api_lettings_log_params)
|
||
|
render json: @lettings_log, status: :ok
|
||
3 years ago
|
else
|
||
2 years ago
|
render json: { errors: @lettings_log.errors.messages }, status: :unprocessable_entity
|
||
3 years ago
|
end
|
||
|
else
|
||
3 years ago
|
render_not_found_json("Log", params[:id])
|
||
3 years ago
|
end
|
||
|
end
|
||
|
|
||
3 years ago
|
def show
|
||
3 years ago
|
respond_to do |format|
|
||
3 years ago
|
# We don't have a dedicated non-editable show view
|
||
3 years ago
|
format.html { edit }
|
||
|
format.json do
|
||
2 years ago
|
if @lettings_log
|
||
|
render json: @lettings_log, status: :ok
|
||
3 years ago
|
else
|
||
3 years ago
|
render_not_found_json("Log", params[:id])
|
||
3 years ago
|
end
|
||
|
end
|
||
|
end
|
||
3 years ago
|
end
|
||
3 years ago
|
|
||
|
def edit
|
||
2 years ago
|
@lettings_log = current_user.lettings_logs.find_by(id: params[:id])
|
||
|
if @lettings_log
|
||
3 years ago
|
render :edit, locals: { current_user: }
|
||
3 years ago
|
else
|
||
3 years ago
|
render_not_found
|
||
3 years ago
|
end
|
||
3 years ago
|
end
|
||
|
|
||
3 years ago
|
def destroy
|
||
2 years ago
|
if @lettings_log
|
||
|
if @lettings_log.delete
|
||
3 years ago
|
head :no_content
|
||
|
else
|
||
2 years ago
|
render json: { errors: @lettings_log.errors.messages }, status: :unprocessable_entity
|
||
3 years ago
|
end
|
||
|
else
|
||
3 years ago
|
render_not_found_json("Log", params[:id])
|
||
3 years ago
|
end
|
||
|
end
|
||
|
|
||
3 years ago
|
private
|
||
3 years ago
|
|
||
3 years ago
|
API_ACTIONS = %w[create show update destroy].freeze
|
||
3 years ago
|
|
||
3 years ago
|
def search_term
|
||
|
params["search"]
|
||
|
end
|
||
|
|
||
3 years ago
|
def json_api_request?
|
||
|
API_ACTIONS.include?(request["action"]) && request.format.json?
|
||
3 years ago
|
end
|
||
|
|
||
|
def authenticate
|
||
|
http_basic_authenticate_or_request_with name: ENV["API_USER"], password: ENV["API_KEY"]
|
||
|
end
|
||
|
|
||
2 years ago
|
def lettings_log_params
|
||
3 years ago
|
if current_user && !current_user.support?
|
||
2 years ago
|
org_params.merge(api_lettings_log_params)
|
||
3 years ago
|
else
|
||
2 years ago
|
api_lettings_log_params
|
||
3 years ago
|
end
|
||
|
end
|
||
|
|
||
|
def org_params
|
||
|
{
|
||
|
"owning_organisation_id" => current_user.organisation.id,
|
||
3 years ago
|
"managing_organisation_id" => current_user.organisation.id,
|
||
3 years ago
|
"created_by_id" => current_user.id,
|
||
3 years ago
|
}
|
||
|
end
|
||
|
|
||
2 years ago
|
def api_lettings_log_params
|
||
|
return {} unless params[:lettings_log]
|
||
3 years ago
|
|
||
2 years ago
|
permitted = params.require(:lettings_log).permit(LettingsLog.editable_fields)
|
||
3 years ago
|
owning_id = permitted["owning_organisation_id"]
|
||
|
permitted["owning_organisation"] = Organisation.find(owning_id) if owning_id
|
||
3 years ago
|
permitted
|
||
3 years ago
|
end
|
||
3 years ago
|
|
||
|
def find_resource
|
||
2 years ago
|
@lettings_log = LettingsLog.find_by(id: params[:id])
|
||
3 years ago
|
end
|
||
3 years ago
|
end
|