diff --git a/app/components/data_sharing_agreement_banner_component.html.erb b/app/components/data_sharing_agreement_banner_component.html.erb index 40128d590..849b04843 100644 --- a/app/components/data_sharing_agreement_banner_component.html.erb +++ b/app/components/data_sharing_agreement_banner_component.html.erb @@ -1,7 +1,7 @@ <% if display_banner? %> <%= govuk_notification_banner(title_text: "Important") do %>

- Your organisation must accept the Data Sharing Agreement before you can create any logs. + <%= I18n.t("validations.organisation.data_sharing_agreement_not_signed") %>

<%= govuk_link_to( "Read the Data Sharing Agreement", diff --git a/app/models/log.rb b/app/models/log.rb index 09c760934..3e37489b3 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -9,6 +9,7 @@ class Log < ApplicationRecord belongs_to :bulk_upload, optional: true before_save :update_status! + before_validation :verify_dsa_signed, on: :create STATUS = { "not_started" => 0, @@ -177,6 +178,13 @@ class Log < ApplicationRecord private + def verify_dsa_signed + return unless owning_organisation + return if owning_organisation.data_sharing_agreement.present? + + errors.add :owning_organisation, I18n.t("validations.organisation.data_sharing_agreement_not_signed") + end + # Handle logs that are older than previous collection start date def older_than_previous_collection_year? return false unless startdate diff --git a/config/locales/en.yml b/config/locales/en.yml index 6cba97954..f5d8a56b7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -105,7 +105,6 @@ en: confirm_soft_errors: blank: You must select if there are errors in these fields - activerecord: attributes: user: @@ -178,6 +177,7 @@ en: validations: organisation: + data_sharing_agreement_not_signed: Your organisation must accept the Data Sharing Agreement before you can create any logs. name_missing: "Enter the name of the organisation" provider_type_missing: "Select the organisation type" stock_owner: diff --git a/spec/shared/shared_log_examples.rb b/spec/shared/shared_log_examples.rb index f9cc59633..ed5810b34 100644 --- a/spec/shared/shared_log_examples.rb +++ b/spec/shared/shared_log_examples.rb @@ -104,5 +104,26 @@ RSpec.shared_examples "shared log examples" do |log_type| end end end + + describe "#verify_dsa_signed" do + it "is valid if the DSA is signed" do + log = build(log_type, :in_progress, owning_organisation: create(:organisation)) + + expect(log).to be_valid + end + + it "is not valid if the DSA is not signed" do + log = build(log_type, owning_organisation: create(:organisation, :without_dsa)) + + expect(log).not_to be_valid + expect(log.errors[:owning_organisation]).to eq(["Your organisation must accept the Data Sharing Agreement before you can create any logs."]) + end + + it "is valid when owning_organisation nil" do + log = build(log_type, owning_organisation: nil) + + expect(log).to be_valid + end + end end # rubocop:enable RSpec/AnyInstance