Browse Source

Merge branch 'main' into CLDC-2996-merged-scheme-activation-date-bug

# Conflicts:
#	db/schema.rb
pull/2081/head
natdeanlewissoftwire 2 years ago
parent
commit
766570687a
  1. 5
      app/models/organisation.rb
  2. 2
      app/models/sales_log.rb
  3. 8
      app/services/bulk_upload/lettings/year2023/row_parser.rb
  4. 7
      app/services/bulk_upload/sales/year2023/row_parser.rb
  5. 10
      app/views/layouts/_footer.html.erb
  6. 7
      db/migrate/20231121131725_add_sales_managing_organisation.rb
  7. 5
      db/schema.rb
  8. 4
      lib/tasks/set_sales_managing_organisation.rake
  9. 48
      spec/lib/tasks/set_sales_managing_organisation_spec.rb
  10. 22
      spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb
  11. 40
      spec/services/bulk_upload/sales/year2023/row_parser_spec.rb

5
app/models/organisation.rb

@ -25,6 +25,11 @@ class Organisation < ApplicationRecord
ids << id
end
absorbed_organisations.each do |organisation|
ids.concat(organisation.stock_owners.pluck(:id))
ids << organisation.id if organisation.holds_own_stock?
end
ids.concat(stock_owners.pluck(:id))
Organisation.where(id: ids)

2
app/models/sales_log.rb

@ -33,6 +33,8 @@ class SalesLog < Log
before_validation :set_derived_fields!
before_validation :process_uprn_change!, if: :should_process_uprn_change?
belongs_to :managing_organisation, class_name: "Organisation", optional: true
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 :filter_by_purchaser_code, ->(purchid) { where("purchid ILIKE ?", "%#{purchid}%") }
scope :search_by, lambda { |param|

8
app/services/bulk_upload/lettings/year2023/row_parser.rb

@ -540,11 +540,11 @@ private
def validate_created_by_related
return unless created_by
return if created_by.organisation == owning_organisation || created_by.organisation == managing_organisation
return if created_by.organisation == owning_organisation&.absorbing_organisation || created_by.organisation == managing_organisation&.absorbing_organisation
unless (created_by.organisation == owning_organisation) || (created_by.organisation == managing_organisation)
block_log_creation!
errors.add(:field_3, "User must be related to owning organisation or managing organisation")
end
block_log_creation!
errors.add(:field_3, "User must be related to owning organisation or managing organisation")
end
def created_by

7
app/services/bulk_upload/sales/year2023/row_parser.rb

@ -1195,11 +1195,10 @@ private
def validate_created_by_related
return unless created_by
return if created_by.organisation == owning_organisation || created_by.organisation == owning_organisation&.absorbing_organisation
unless created_by.organisation == owning_organisation
block_log_creation!
errors.add(:field_2, "User must be related to owning organisation", category: :setup)
end
block_log_creation!
errors.add(:field_2, "User must be related to owning organisation", category: :setup)
end
def setup_question?(question)

10
app/views/layouts/_footer.html.erb

@ -17,16 +17,6 @@
<li>Monday to Friday, 9am to 5:30pm<br>(except public holidays)</li>
</ul>
</div>
<div class="govuk-grid-column-one-third-from-desktop">
<h3 class="govuk-heading-s govuk-!-margin-bottom-1">Email</h3>
<ul class="govuk-list govuk-!-font-size-16 govuk-!-margin-bottom-0">
<li>
<%= govuk_mail_to("dluhc.digital-services@levellingup.gov.uk", subject: "CORE:", class: "govuk-footer__link") %>
</li>
<li class="govuk-!-margin-bottom-0">We aim to respond within 2 working&nbsp;days</li>
</ul>
</div>
</div>
<h2 class="govuk-visually-hidden">Helpful links</h2>

7
db/migrate/20231121131725_add_sales_managing_organisation.rb

@ -0,0 +1,7 @@
class AddSalesManagingOrganisation < ActiveRecord::Migration[7.0]
def change
change_table :sales_logs, bulk: true do |t|
t.references :managing_organisation, class_name: "Organisation"
end
end
end

5
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_12_04_101105) do
ActiveRecord::Schema[7.0].define(version: 2023_11_21_131725) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -624,8 +624,10 @@ ActiveRecord::Schema[7.0].define(version: 2023_12_04_101105) do
t.integer "creation_method", default: 1
t.integer "old_form_id"
t.datetime "values_updated_at"
t.bigint "managing_organisation_id"
t.index ["bulk_upload_id"], name: "index_sales_logs_on_bulk_upload_id"
t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id"
t.index ["managing_organisation_id"], name: "index_sales_logs_on_managing_organisation_id"
t.index ["old_id"], name: "index_sales_logs_on_old_id", unique: true
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"
@ -659,7 +661,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_12_04_101105) do
t.string "old_visible_id"
t.integer "total_units"
t.boolean "confirmed"
t.datetime "startdate"
t.index ["owning_organisation_id"], name: "index_schemes_on_owning_organisation_id"
end

4
lib/tasks/set_sales_managing_organisation.rake

@ -0,0 +1,4 @@
desc "Set sales managing organisation id to owning organisation id"
task set_sales_managing_organisation: :environment do
SalesLog.where.not(owning_organisation_id: nil).update_all("managing_organisation_id = owning_organisation_id")
end

48
spec/lib/tasks/set_sales_managing_organisation_spec.rb

@ -0,0 +1,48 @@
require "rails_helper"
require "rake"
RSpec.describe "set_sales_managing_organisation" do
describe ":set_sales_managing_organisation", type: :task do
subject(:task) { Rake::Task["set_sales_managing_organisation"] }
before do
Rake.application.rake_require("tasks/set_sales_managing_organisation")
Rake::Task.define_task(:environment)
task.reenable
end
context "when the rake task is run" do
let!(:sales_log) { create(:sales_log, :completed, managing_organisation_id: nil) }
it "updates sales log managing_organisation_id with owning_organisation_id" do
expect(sales_log.managing_organisation_id).to eq(nil)
expect(sales_log.status).to eq("completed")
task.invoke
sales_log.reload
expect(sales_log.managing_organisation_id).to eq(sales_log.owning_organisation_id)
expect(sales_log.status).to eq("completed")
end
it "does not update sales log managing_organisation_id if owning_organisation_id is nil" do
sales_log.update!(owning_organisation_id: nil)
expect(sales_log.status).to eq("in_progress")
expect(sales_log.managing_organisation_id).to eq(nil)
task.invoke
sales_log.reload
expect(sales_log.managing_organisation_id).to eq(nil)
expect(sales_log.status).to eq("in_progress")
end
it "skips validations" do
sales_log.saledate = Time.zone.local(2021, 3, 3)
sales_log.save!(validate: false)
expect(sales_log.managing_organisation_id).to eq(nil)
expect(sales_log.status).to eq("in_progress")
task.invoke
sales_log.reload
expect(sales_log.managing_organisation_id).to eq(sales_log.owning_organisation_id)
expect(sales_log.status).to eq("in_progress")
end
end
end
end

22
spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb

@ -1384,6 +1384,28 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do
expect(parser).to be_block_log_creation
end
end
context "when user's org has absorbed owning organisation" do
let(:merged_org) { create(:organisation, :with_old_visible_id, holds_own_stock: true) }
let(:merged_org_stock_owner) { create(:organisation, :with_old_visible_id, holds_own_stock: true) }
let(:attributes) { { bulk_upload:, field_1: merged_org_stock_owner.old_visible_id, field_2: merged_org.old_visible_id, field_3: user.email } }
before do
create(:organisation_relationship, parent_organisation: merged_org_stock_owner, child_organisation: merged_org)
merged_org.update!(absorbing_organisation: user.organisation, merge_date: Time.zone.today)
merged_org.reload
user.organisation.reload
end
it "is permitted" do
parser = described_class.new(attributes)
parser.valid?
expect(parser.errors.where(:field_1)).not_to be_present
expect(parser.errors.where(:field_3)).not_to be_present
end
end
end
describe "#field_2" do # managing org

40
spec/services/bulk_upload/sales/year2023/row_parser_spec.rb

@ -417,6 +417,46 @@ RSpec.describe BulkUpload::Sales::Year2023::RowParser do
expect(parser).to be_block_log_creation
end
end
context "when user's org has absorbed owning organisation with stock owners" do
let(:merged_org) { create(:organisation, :with_old_visible_id, holds_own_stock: true) }
let(:merged_org_stock_owner) { create(:organisation, :with_old_visible_id, holds_own_stock: true) }
let(:attributes) { { bulk_upload:, field_1: merged_org_stock_owner.old_visible_id } }
before do
create(:organisation_relationship, parent_organisation: merged_org_stock_owner, child_organisation: merged_org)
merged_org.update!(absorbing_organisation: user.organisation, merge_date: Time.zone.today)
merged_org.reload
user.organisation.reload
end
it "is permitted" do
parser.valid?
expect(parser.errors.where(:field_1)).not_to be_present
end
end
context "when user's org has absorbed owning organisation" do
let(:merged_org) { create(:organisation, :with_old_visible_id, holds_own_stock: true) }
let(:attributes) { { bulk_upload:, field_1: merged_org.old_visible_id, field_2: user.email } }
before do
merged_org.update!(absorbing_organisation: user.organisation, merge_date: Time.zone.today)
merged_org.reload
user.organisation.reload
user.reload
end
it "is permitted" do
parser = described_class.new(attributes)
parser.valid?
expect(parser.errors.where(:field_1)).not_to be_present
expect(parser.errors.where(:field_2)).not_to be_present
end
end
end
describe "#field_2" do # username for created_by

Loading…
Cancel
Save