Submit social housing lettings and sales data (CORE)
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.

74 lines
1.8 KiB

class LogsController < ApplicationController
include Pagy::Backend
include Modules::LogsFilter
include Modules::SearchFilter
skip_before_action :verify_authenticity_token, if: :json_api_request?
before_action :authenticate, if: :json_api_request?
before_action :authenticate_user!, unless: :json_api_request?
private
def create
log = yield
raise "Caller must pass a block that implements model creation" if log.blank?
respond_to do |format|
format.html do
log.save!
redirect_to post_create_redirect_url(log)
end
format.json do
if log.save
render json: log, status: :created
else
render json: { errors: log.errors.messages }, status: :unprocessable_entity
end
end
end
end
def post_create_redirect_url
raise "implement in sub class"
end
API_ACTIONS = %w[create show update destroy].freeze
def json_api_request?
API_ACTIONS.include?(request["action"]) && request.format.json?
end
def authenticate
http_basic_authenticate_or_request_with name: ENV["API_USER"], password: ENV["API_KEY"]
end
def log_params
if current_user && !current_user.support?
org_params.merge(api_log_params)
else
api_log_params
end
end
def api_log_params
return {} unless params[:lettings_log] || params[:sales_log]
permitted = permitted_log_params
owning_id = permitted["owning_organisation_id"]
permitted["owning_organisation"] = Organisation.find(owning_id) if owning_id
permitted
end
def org_params
CLDC-1723 Overhaul letting log owning & managing org questions & tests (#1140) * test: check managing org not gone from answer opts when relationship deleted * feat: add current managing org to answer opts * feat: check if managing org exists before trying to show it * wip * test: improve managing orgs opts test when not support * test: improve managing orgs opts tests when support * test: make relationship deletion test consistent with other tests * test: add "(with hint)" to managing org opts test descriptions * test: refactor managing orgs opts tests for support user case * fix: don't call user in get_answer_label in CYA component * style: reorder instance vars and remove old comments in managing_organisation.rb * refactor: ensure label_from_value always accepts log & nil as args * lint * test: pass in log and user in housing provider opts test for support user * test: update housing provider opts tests for non-support user * feat: update housing provider answer opts to include current HP in db * style: add space after user definition * test: make context definition more human-readable * test: refactor housing providers opts tests (not support user) * test: check housing prov. still selectable after deleting relationship * fix: define log and current_user instance vars in label_from_value (housing prov.) * lint * test: update lettings log feature tests to differentiate between different numbers of stock owners when acting as a data coordinator * test: check owning & managing orgs set correctly when a log is created * test: add line breaks and start context descriptions with and (not if) * test: artificially reference org_rel2 to avoid lint offense * feat: don't set log owning org as user's org if that org doesn't hold stock * test: improve test context descriptions in lettings_log_spec * test: finish overhauling owning and managing org tests in lettings_log_spec * test: change let! to let where possible in spec/features/lettings_log_spec.rb * test: change let! to let where possible in spec/models/form/lettings/questions/managing_organisation_spec.rb * test: change let! to let where possible in spec/models/form/lettings/questions/stock_owner_spec.rb * test: remove if statement from "coordinator user's org doesn't hold stock" managing org test * test: remove if statement from "coordinator user's org does hold stock" no managing orgs managing org test * test: remove if statement from "coordinator user's org does hold stock" >=1 managing orgs managing org test plus refactor previous test * test: explicitly reference org rels in "coordinator user's org doesn't hold stock" managing org test * test: don't create vars inside other vars (for tests edited/created in this branch) * chore: save schema changes after migration Co-authored-by: Phil Lee <asmega@users.noreply.github.com>
2 years ago
owning_organisation_id = current_user.organisation.holds_own_stock? ? current_user.organisation.id : nil
{
CLDC-1723 Overhaul letting log owning & managing org questions & tests (#1140) * test: check managing org not gone from answer opts when relationship deleted * feat: add current managing org to answer opts * feat: check if managing org exists before trying to show it * wip * test: improve managing orgs opts test when not support * test: improve managing orgs opts tests when support * test: make relationship deletion test consistent with other tests * test: add "(with hint)" to managing org opts test descriptions * test: refactor managing orgs opts tests for support user case * fix: don't call user in get_answer_label in CYA component * style: reorder instance vars and remove old comments in managing_organisation.rb * refactor: ensure label_from_value always accepts log & nil as args * lint * test: pass in log and user in housing provider opts test for support user * test: update housing provider opts tests for non-support user * feat: update housing provider answer opts to include current HP in db * style: add space after user definition * test: make context definition more human-readable * test: refactor housing providers opts tests (not support user) * test: check housing prov. still selectable after deleting relationship * fix: define log and current_user instance vars in label_from_value (housing prov.) * lint * test: update lettings log feature tests to differentiate between different numbers of stock owners when acting as a data coordinator * test: check owning & managing orgs set correctly when a log is created * test: add line breaks and start context descriptions with and (not if) * test: artificially reference org_rel2 to avoid lint offense * feat: don't set log owning org as user's org if that org doesn't hold stock * test: improve test context descriptions in lettings_log_spec * test: finish overhauling owning and managing org tests in lettings_log_spec * test: change let! to let where possible in spec/features/lettings_log_spec.rb * test: change let! to let where possible in spec/models/form/lettings/questions/managing_organisation_spec.rb * test: change let! to let where possible in spec/models/form/lettings/questions/stock_owner_spec.rb * test: remove if statement from "coordinator user's org doesn't hold stock" managing org test * test: remove if statement from "coordinator user's org does hold stock" no managing orgs managing org test * test: remove if statement from "coordinator user's org does hold stock" >=1 managing orgs managing org test plus refactor previous test * test: explicitly reference org rels in "coordinator user's org doesn't hold stock" managing org test * test: don't create vars inside other vars (for tests edited/created in this branch) * chore: save schema changes after migration Co-authored-by: Phil Lee <asmega@users.noreply.github.com>
2 years ago
"owning_organisation_id" => owning_organisation_id,
"created_by_id" => current_user.id,
}
end
def search_term
params["search"]
end
end