Skip to content

Commit

Permalink
Allow users to set a contact as the local authority main contact
Browse files Browse the repository at this point in the history
Users have requested the ability to set a local authority contact as the main
local authority contact, should a project have multiple local authority
contacts.
  • Loading branch information
lozette committed Jul 4, 2024
1 parent fc01790 commit 7d6a280
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
information clearer.
- the external contacts are now shown as 'cards' to make them clearer.

### Added

- Users can now indicate a local authority contact is the local authority main
contact for a project

## [Release-77][release-77]

### Added
Expand Down
7 changes: 6 additions & 1 deletion app/forms/contact/create_project_contact_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Contact::CreateProjectContactForm
CATEGORIES_WITH_PRIMARY_CONTACT = [
"school_or_academy",
"incoming_trust",
"outgoing_trust"
"outgoing_trust",
"local_authority"
].freeze

attribute :category
Expand Down Expand Up @@ -79,6 +80,8 @@ def save
@contact.incoming_trust_main_contact
when "outgoing_trust"
@contact.outgoing_trust_main_contact
when "local_authority"
@contact.local_authority_main_contact
end
end

Expand All @@ -90,6 +93,8 @@ def save
@project.incoming_trust_main_contact_id = primary_contact_for_category ? @contact.id : nil
when "outgoing_trust"
@project.outgoing_trust_main_contact_id = primary_contact_for_category ? @contact.id : nil
when "local_authority"
@project.local_authority_main_contact_id = primary_contact_for_category ? @contact.id : nil
end
end
end
5 changes: 5 additions & 0 deletions app/models/contact/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def self.policy_class
has_one :main_contact_for_establishment, class_name: "::Project", inverse_of: :establishment_main_contact
has_one :main_contact_for_incoming_trust, class_name: "::Project", inverse_of: :incoming_trust_main_contact
has_one :main_contact_for_outgoing_trust, class_name: "::Project", inverse_of: :outgoing_trust_main_contact
has_one :main_contact_for_local_authority, class_name: "::Project", inverse_of: :local_authority_main_contact
has_one :contact_for_chair_of_governors, class_name: "Conversion::Project", inverse_of: :chair_of_governors_contact

def establishment_main_contact
Expand All @@ -21,4 +22,8 @@ def incoming_trust_main_contact
def outgoing_trust_main_contact
project.outgoing_trust_main_contact_id == id
end

def local_authority_main_contact
project.local_authority_main_contact_id == id
end
end
1 change: 1 addition & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Project < ApplicationRecord
belongs_to :establishment_main_contact, inverse_of: :main_contact_for_establishment, dependent: :destroy, class_name: "Contact::Project", optional: true
belongs_to :incoming_trust_main_contact, inverse_of: :main_contact_for_incoming_trust, dependent: :destroy, class_name: "Contact::Project", optional: true
belongs_to :outgoing_trust_main_contact, inverse_of: :main_contact_for_outgoing_trust, dependent: :destroy, class_name: "Contact::Project", optional: true
belongs_to :local_authority_main_contact, inverse_of: :main_contact_for_local_authority, dependent: :destroy, class_name: "Contact::Project", optional: true

validates :urn, presence: true
validates :urn, urn: true
Expand Down
6 changes: 6 additions & 0 deletions app/views/external_contacts/_contact_group.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@
row.with_value { t("yes") }
end
end
if contact.local_authority_main_contact
summary_list.with_row do |row|
row.with_key { t("contact.details.local_authority_main_contact") }
row.with_value { t("yes") }
end
end
end
end %>
</div>
Expand Down
1 change: 1 addition & 0 deletions config/locales/contact.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ en:
establishment_main_contact: Primary contact for school or academy?
incoming_trust_main_contact: Primary contact for incoming trust?
outgoing_trust_main_contact: Primary contact for outgoing trust?
local_authority_main_contact: Primary contact for local authority?
new:
title: Add contact
save_contact_button: Add contact
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240703142240_local_authority_main_contact_id.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class LocalAuthorityMainContactId < ActiveRecord::Migration[7.0]
def change
add_column :projects, :local_authority_main_contact_id, :uuid
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 31 additions & 2 deletions spec/forms/contact/contact_project_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,24 @@
end

context "and the category is local authority" do
it "is invalid" do
it "is valid" do
contact_form.primary_contact_for_category = true
contact_form.category = "local_authority"

expect(contact_form).to be_invalid
expect(contact_form).to be_valid
end

it "sets the contact as the local authority main contact" do
contact_form.primary_contact_for_category = true
contact_form.category = "local_authority"
contact_form.name = "New Contact"
contact_form.title = "Local councillor"
contact_form.organisation_name = "Council"

contact_form.save
contact = Contact::Project.last

expect(project.reload.local_authority_main_contact).to eq(contact)
end
end

Expand Down Expand Up @@ -255,6 +268,22 @@
expect(project.outgoing_trust_main_contact_id).to be_nil
end
end

context "and the contact is for the local authority category" do
let(:contact) { create(:project_contact, project: project, category: "local_authority") }

it "can be unset" do
project.update!(local_authority_main_contact_id: contact.id)
contact_form = Contact::CreateProjectContactForm.new(contact: contact, project: project)
contact_form.primary_contact_for_category = false

expect(project.local_authority_main_contact_id).to eql(contact.id)

contact_form.save

expect(project.local_authority_main_contact_id).to be_nil
end
end
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/models/contact/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,26 @@
expect(contact.outgoing_trust_main_contact).to be false
end
end

describe "#local_authority_main_contact" do
before do
mock_successful_api_responses(urn: any_args, ukprn: any_args)
end

it "returns true if the contact is the local_authority_main_contact for its project" do
project = create(:conversion_project)
contact = create(:project_contact, project: project)
project.local_authority_main_contact_id = contact.id
project.save

expect(contact.reload.local_authority_main_contact).to be true
end

it "returns false if the contact is NOT the local_authority_main_contact for its project" do
project = create(:conversion_project, local_authority_main_contact_id: SecureRandom.uuid)
contact = create(:project_contact, project: project)

expect(contact.local_authority_main_contact).to be false
end
end
end
19 changes: 19 additions & 0 deletions spec/models/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
it { is_expected.to belong_to(:establishment_main_contact).optional(true) }
it { is_expected.to belong_to(:incoming_trust_main_contact).optional(true) }
it { is_expected.to belong_to(:outgoing_trust_main_contact).optional(true) }
it { is_expected.to belong_to(:local_authority_main_contact).optional(true) }
it { is_expected.to have_one(:dao_revocation).dependent(:destroy) }

describe "delete related entities" do
Expand Down Expand Up @@ -128,6 +129,24 @@
expect(project.outgoing_trust_main_contact).to eql(outgoing_trust_main_contact)
end
end

describe "#local_authority_contact" do
it "returns nil when no association exists, but there are project contacts" do
project = create(:conversion_project)
create_list(:project_contact, 3, project: project)

expect(project.local_authority_main_contact).to be_nil
end

it "returns the contact when one is set" do
project = create(:conversion_project)
local_authority_contact = create(:project_contact)

project.update(local_authority_main_contact: local_authority_contact)

expect(project.local_authority_main_contact).to eql(local_authority_contact)
end
end
end

describe "Validations" do
Expand Down

0 comments on commit 7d6a280

Please sign in to comment.