Browse Source

feat: move shared managing org behaviour from general log to lettings log, update tests

pull/1169/head
natdeanlewissoftwire 3 years ago
parent
commit
66a8830435
  1. 2
      app/components/log_summary_component.html.erb
  2. 11
      app/controllers/lettings_logs_controller.rb
  3. 8
      app/controllers/logs_controller.rb
  4. 7
      app/controllers/sales_logs_controller.rb
  5. 14
      app/models/lettings_log.rb
  6. 14
      app/models/log.rb
  7. 9
      app/models/sales_log.rb
  8. 10
      app/services/filter_service.rb
  9. 13
      db/schema.rb
  10. 2
      spec/components/log_summary_component_spec.rb
  11. 1
      spec/factories/sales_log.rb
  12. 2
      spec/features/form/check_answers_page_lettings_logs_spec.rb
  13. 4
      spec/features/form/check_answers_page_sales_logs_spec.rb
  14. 2
      spec/features/organisation_spec.rb
  15. 2
      spec/models/organisation_spec.rb
  16. 16
      spec/models/sales_log_spec.rb
  17. 4
      spec/requests/organisations_controller_spec.rb
  18. 17
      spec/requests/sales_logs_controller_spec.rb

2
app/components/log_summary_component.html.erb

@ -43,7 +43,7 @@
<dt class="app-metadata__term">Owned by</dt>
<dd class="app-metadata__definition"><%= log.owning_organisation&.name %></dd>
</div>
<% if log.managing_organisation %>
<% if log.instance_of?(LettingsLog) && log.managing_organisation %>
<div class="app-metadata__item">
<dt class="app-metadata__term">Managed by</dt>
<dd class="app-metadata__definition"><%= log.managing_organisation&.name %></dd>

11
app/controllers/lettings_logs_controller.rb

@ -20,7 +20,9 @@ class LettingsLogsController < LogsController
end
def create
super { LettingsLog.new(log_params) }
super do
LettingsLog.new(log_params)
end
end
def update
@ -101,6 +103,13 @@ class LettingsLogsController < LogsController
end
end
def org_params
{
"owning_organisation_id" => current_user.organisation.id,
"managing_organisation_id" => current_user.organisation.id,
"created_by_id" => current_user.id,
}
end
private
def permitted_log_params

8
app/controllers/logs_controller.rb

@ -59,14 +59,6 @@ private
permitted
end
def org_params
{
"owning_organisation_id" => current_user.organisation.id,
"managing_organisation_id" => current_user.organisation.id,
"created_by_id" => current_user.id,
}
end
def search_term
params["search"]
end

7
app/controllers/sales_logs_controller.rb

@ -43,4 +43,11 @@ class SalesLogsController < LogsController
def permitted_log_params
params.require(:sales_log).permit(SalesLog.editable_fields)
end
def org_params
{
"owning_organisation_id" => current_user.organisation.id,
"created_by_id" => current_user.id,
}
end
end

14
app/models/lettings_log.rb

@ -34,6 +34,7 @@ class LettingsLog < Log
belongs_to :scheme, optional: true
belongs_to :location, optional: true
belongs_to :managing_organisation, class_name: "Organisation", optional: true
scope :filter_by_year, ->(year) { where(startdate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) }
scope :filter_by_tenant_code, ->(tenant_code) { where("tenancycode ILIKE ?", "%#{tenant_code}%") }
@ -49,6 +50,7 @@ class LettingsLog < Log
}
scope :filter_by_before_startdate, ->(date) { where("lettings_logs.startdate >= ?", date) }
scope :unresolved, -> { where(unresolved: true) }
scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) }
AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze
OPTIONAL_FIELDS = %w[first_time_property_let_as_social_housing tenancycode propcode].freeze
@ -519,6 +521,18 @@ class LettingsLog < Log
update(unresolved: false)
end
def managing_organisation_provider_type
managing_organisation&.provider_type
end
def reset_created_by!
return unless updated_by&.support?
return if owning_organisation.blank? || managing_organisation.blank? || created_by.blank?
return if created_by&.organisation == managing_organisation || created_by&.organisation == owning_organisation
update!(created_by: nil)
end
private
def reset_derived_questions

14
app/models/log.rb

@ -2,7 +2,6 @@ class Log < ApplicationRecord
self.abstract_class = true
belongs_to :owning_organisation, class_name: "Organisation", optional: true
belongs_to :managing_organisation, class_name: "Organisation", optional: true
belongs_to :created_by, class_name: "User", optional: true
belongs_to :updated_by, class_name: "User", optional: true
before_save :update_status!
@ -10,7 +9,6 @@ class Log < ApplicationRecord
STATUS = { "not_started" => 0, "in_progress" => 1, "completed" => 2 }.freeze
enum status: STATUS
scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) }
scope :filter_by_status, ->(status, _user = nil) { where status: }
scope :filter_by_years, lambda { |years, _user = nil|
first_year = years.shift
@ -42,10 +40,6 @@ class Log < ApplicationRecord
ethnic_group == 17
end
def managing_organisation_provider_type
managing_organisation&.provider_type
end
def collection_period_open?
form.end_date > Time.zone.today
end
@ -81,14 +75,6 @@ private
reset_created_by!
end
def reset_created_by!
return unless updated_by&.support?
return if owning_organisation.blank? || managing_organisation.blank? || created_by.blank?
return if created_by&.organisation == managing_organisation || created_by&.organisation == owning_organisation
update!(created_by: nil)
end
PIO = PostcodeService.new
def process_previous_postcode_changes!

9
app/models/sales_log.rb

@ -26,6 +26,7 @@ class SalesLog < Log
scope :filter_by_year, ->(year) { where(saledate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) }
scope :search_by, ->(param) { filter_by_id(param) }
scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org) }
OPTIONAL_FIELDS = %w[purchid].freeze
@ -151,4 +152,12 @@ class SalesLog < Log
def mortgage_not_used?
mortgageused == 2
end
def reset_created_by!
return unless updated_by&.support?
return if owning_organisation.blank? || created_by.blank?
return if created_by&.organisation == owning_organisation
update!(created_by: nil)
end
end

10
app/services/filter_service.rb

@ -17,6 +17,14 @@ class FilterService
logs = logs.public_send("filter_by_#{category}", values, user)
end
logs = logs.order(created_at: :desc)
user.support? ? logs.all.includes(:owning_organisation, :managing_organisation) : logs
if user.support?
if logs.first.instance_of?(LettingsLog)
logs.all.includes(:owning_organisation, :managing_organisation)
else
logs.all.includes(:owning_organisation)
end
else
logs
end
end
end

13
db/schema.rb

@ -420,6 +420,9 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_10_094518) do
t.string "relat5"
t.string "relat6"
t.integer "hb"
t.string "sex4"
t.string "sex5"
t.string "sex6"
t.integer "savings_value_check"
t.integer "deposit_value_check"
t.integer "frombeds"
@ -456,21 +459,17 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_10_094518) do
t.integer "hhregres"
t.integer "hhregresstill"
t.integer "proplen"
t.integer "mscharge_known"
t.decimal "mscharge", precision: 10, scale: 2
t.integer "prevten"
t.integer "mortgageused"
t.integer "wchair"
t.integer "armedforcesspouse"
t.integer "mscharge_known"
t.decimal "mscharge", precision: 10, scale: 2
t.string "sex4"
t.string "sex5"
t.string "sex6"
t.integer "mortlen"
t.datetime "hodate", precision: nil
t.integer "hoday"
t.integer "homonth"
t.integer "hoyear"
t.integer "extrabor"
t.integer "mortlen"
t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id"
t.index ["owning_organisation_id"], name: "index_sales_logs_on_owning_organisation_id"
t.index ["updated_by_id"], name: "index_sales_logs_on_updated_by_id"

2
spec/components/log_summary_component_spec.rb

@ -19,7 +19,7 @@ RSpec.describe LogSummaryComponent, type: :component do
expect(result).to have_text("Created 8 February 2022")
expect(result).to have_text("by Danny Rojas")
expect(result).to have_content("Owned by\n DLUHC")
expect(result).to have_content("Managed by\n DLUHC")
expect(result).not_to have_content("Managed by\n DLUHC")
end
end

1
spec/factories/sales_log.rb

@ -2,7 +2,6 @@ FactoryBot.define do
factory :sales_log do
created_by { FactoryBot.create(:user) }
owning_organisation { created_by.organisation }
managing_organisation { created_by.organisation }
created_at { Time.utc(2022, 2, 8, 16, 52, 15) }
updated_at { Time.utc(2022, 2, 8, 16, 52, 15) }
trait :in_progress do

2
spec/features/form/check_answers_page_lettings_logs_spec.rb

@ -143,7 +143,7 @@ RSpec.describe "Lettings Log Check Answers Page" do
end
context "when the user is checking their answers for the household characteristics subsection" do
it "they see a seperate summary card for each member of the household" do
it "they see a separate summary card for each member of the household" do
visit("/lettings-logs/#{completed_lettings_log.id}/#{subsection}/check-answers")
assert_selector ".x-govuk-summary-card__title", text: "Lead tenant", count: 1
assert_selector ".x-govuk-summary-card__title", text: "Person 2", count: 1

4
spec/features/form/check_answers_page_sales_logs_spec.rb

@ -39,7 +39,7 @@ RSpec.describe "Sales Log Check Answers Page" do
context "when the user is checking their answers for the household characteristics subsection" do
context "and the log is for a joint purchase" do
it "they see a seperate summary card for each member of the household" do
it "they see a separate summary card for each member of the household" do
visit("/sales-logs/#{completed_sales_log_joint_purchase.id}/#{subsection}/check-answers")
assert_selector ".x-govuk-summary-card__title", text: "Buyer 1", count: 1
assert_selector ".x-govuk-summary-card__title", text: "Buyer 2", count: 1
@ -49,7 +49,7 @@ RSpec.describe "Sales Log Check Answers Page" do
end
context "and the log is for a non-joint purchase" do
it "they see a seperate summary card for each member of the household" do
it "they see a separate summary card for each member of the household" do
visit("/sales-logs/#{completed_sales_log_non_joint_purchase.id}/#{subsection}/check-answers")
assert_selector ".x-govuk-summary-card__title", text: "Buyer 1", count: 1
assert_selector ".x-govuk-summary-card__title", text: "Buyer 2", count: 0

2
spec/features/organisation_spec.rb

@ -215,7 +215,7 @@ RSpec.describe "User Features" do
let(:number_of_sales_logs) { SalesLog.count }
before do
FactoryBot.create_list(:sales_log, 4, owning_organisation_id: organisation.id, managing_organisation_id: organisation.id)
FactoryBot.create_list(:sales_log, 4, owning_organisation_id: organisation.id)
visit("/organisations/#{org_id}/sales-logs")
end

2
spec/models/organisation_spec.rb

@ -200,7 +200,7 @@ RSpec.describe Organisation, type: :model do
let!(:user) { FactoryBot.create(:user, :support, last_sign_in_at: Time.zone.now, organisation:) }
let!(:scheme_to_delete) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:log_to_delete) { FactoryBot.create(:lettings_log, owning_organisation: user.organisation) }
let!(:sales_log_to_delete) { FactoryBot.create(:sales_log, owning_organisation: user.organisation, managing_organisation: user.organisation) }
let!(:sales_log_to_delete) { FactoryBot.create(:sales_log, owning_organisation: user.organisation) }
context "when organisation is deleted" do
it "child relationships ie logs, schemes and users are deleted too - application" do

16
spec/models/sales_log_spec.rb

@ -80,21 +80,20 @@ RSpec.describe SalesLog, type: :model do
let(:organisation_3) { create(:organisation) }
before do
create(:sales_log, :in_progress, owning_organisation: organisation_1, managing_organisation: organisation_1)
create(:sales_log, :completed, owning_organisation: organisation_1, managing_organisation: organisation_2)
create(:sales_log, :completed, owning_organisation: organisation_2, managing_organisation: organisation_1)
create(:sales_log, :completed, owning_organisation: organisation_2, managing_organisation: organisation_2)
create(:sales_log, :in_progress, owning_organisation: organisation_1)
create(:sales_log, :completed, owning_organisation: organisation_1)
create(:sales_log, :completed, owning_organisation: organisation_2)
end
it "filters by given organisation id" do
expect(described_class.filter_by_organisation([organisation_1.id]).count).to eq(3)
expect(described_class.filter_by_organisation([organisation_1.id, organisation_2.id]).count).to eq(4)
expect(described_class.filter_by_organisation([organisation_1.id]).count).to eq(2)
expect(described_class.filter_by_organisation([organisation_1.id, organisation_2.id]).count).to eq(3)
expect(described_class.filter_by_organisation([organisation_3.id]).count).to eq(0)
end
it "filters by given organisation" do
expect(described_class.filter_by_organisation([organisation_1]).count).to eq(3)
expect(described_class.filter_by_organisation([organisation_1, organisation_2]).count).to eq(4)
expect(described_class.filter_by_organisation([organisation_1]).count).to eq(2)
expect(described_class.filter_by_organisation([organisation_1, organisation_2]).count).to eq(3)
expect(described_class.filter_by_organisation([organisation_3]).count).to eq(0)
end
end
@ -143,7 +142,6 @@ RSpec.describe SalesLog, type: :model do
let!(:address_sales_log) do
described_class.create({
managing_organisation: owning_organisation,
owning_organisation:,
created_by: created_by_user,
ppcodenk: 1,

4
spec/requests/organisations_controller_spec.rb

@ -734,8 +734,8 @@ RSpec.describe OrganisationsController, type: :request do
let(:number_of_org2_sales_logs) { 4 }
before do
FactoryBot.create_list(:sales_log, number_of_org1_sales_logs, owning_organisation_id: organisation.id, managing_organisation_id: organisation.id)
FactoryBot.create_list(:sales_log, number_of_org2_sales_logs, owning_organisation_id: unauthorised_organisation.id, managing_organisation_id: unauthorised_organisation.id)
FactoryBot.create_list(:sales_log, number_of_org1_sales_logs, owning_organisation_id: organisation.id)
FactoryBot.create_list(:sales_log, number_of_org2_sales_logs, owning_organisation_id: unauthorised_organisation.id)
get "/organisations/#{organisation.id}/sales-logs", headers:, params: {}
end

17
spec/requests/sales_logs_controller_spec.rb

@ -3,7 +3,6 @@ require "rails_helper"
RSpec.describe SalesLogsController, type: :request do
let(:user) { FactoryBot.create(:user) }
let(:owning_organisation) { user.organisation }
let(:managing_organisation) { owning_organisation }
let(:api_username) { "test_user" }
let(:api_password) { "test_password" }
let(:basic_credentials) do
@ -14,7 +13,6 @@ RSpec.describe SalesLogsController, type: :request do
let(:params) do
{
"owning_organisation_id": owning_organisation.id,
"managing_organisation_id": managing_organisation.id,
"created_by_id": user.id,
}
end
@ -51,7 +49,6 @@ RSpec.describe SalesLogsController, type: :request do
it "creates a sales log with the values passed" do
json_response = JSON.parse(response.body)
expect(json_response["owning_organisation_id"]).to eq(owning_organisation.id)
expect(json_response["managing_organisation_id"]).to eq(managing_organisation.id)
expect(json_response["created_by_id"]).to eq(user.id)
end
@ -94,14 +91,12 @@ RSpec.describe SalesLogsController, type: :request do
FactoryBot.create(
:sales_log,
owning_organisation: organisation,
managing_organisation: organisation,
)
end
let!(:unauthorized_sales_log) do
FactoryBot.create(
:sales_log,
owning_organisation: other_organisation,
managing_organisation: other_organisation,
)
end
@ -146,13 +141,11 @@ RSpec.describe SalesLogsController, type: :request do
let!(:not_started_sales_log) do
FactoryBot.create(:sales_log,
owning_organisation: organisation,
managing_organisation: organisation,
created_by: user)
end
let!(:completed_sales_log) do
FactoryBot.create(:sales_log, :completed,
owning_organisation: organisation_2,
managing_organisation: organisation,
created_by: user_2)
end
@ -189,14 +182,12 @@ RSpec.describe SalesLogsController, type: :request do
let!(:sales_log_2021) do
FactoryBot.create(:sales_log, :in_progress,
owning_organisation: organisation,
saledate: Time.zone.local(2022, 3, 1),
managing_organisation: organisation)
saledate: Time.zone.local(2022, 3, 1))
end
let!(:sales_log_2022) do
sales_log = FactoryBot.build(:sales_log, :completed,
owning_organisation: organisation,
saledate: Time.zone.local(2022, 12, 1),
managing_organisation: organisation)
saledate: Time.zone.local(2022, 12, 1))
sales_log.save!(validate: false)
sales_log
end
@ -227,14 +218,12 @@ RSpec.describe SalesLogsController, type: :request do
FactoryBot.create(:sales_log, :completed,
owning_organisation: organisation,
saledate: Time.zone.local(2022, 3, 1),
managing_organisation: organisation,
created_by: user)
end
let!(:sales_log_2022) do
FactoryBot.create(:sales_log,
owning_organisation: organisation,
saledate: Time.zone.local(2022, 12, 1),
managing_organisation: organisation,
created_by: user)
end
@ -362,7 +351,7 @@ RSpec.describe SalesLogsController, type: :request do
context "when there are more than 20 logs" do
before do
FactoryBot.create_list(:sales_log, 25, owning_organisation: organisation, managing_organisation: organisation)
FactoryBot.create_list(:sales_log, 25, owning_organisation: organisation)
end
context "when on the first page" do

Loading…
Cancel
Save