diff --git a/db/migrate/20260316122813_remove_default_value_for_mou_signature_agreement_type.rb b/db/migrate/20260316122813_remove_default_value_for_mou_signature_agreement_type.rb new file mode 100644 index 000000000..cc01b1158 --- /dev/null +++ b/db/migrate/20260316122813_remove_default_value_for_mou_signature_agreement_type.rb @@ -0,0 +1,9 @@ +class RemoveDefaultValueForMouSignatureAgreementType < ActiveRecord::Migration[8.1] + def up + change_column_default :mou_signatures, :agreement_type, nil + end + + def down + change_column_default :mou_signatures, :agreement_type, "crown" + end +end diff --git a/db/schema.rb b/db/schema.rb index 84c26ea06..86095cd96 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.1].define(version: 2026_03_04_145907) do +ActiveRecord::Schema[8.1].define(version: 2026_03_16_122813) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -185,7 +185,7 @@ end create_table "mou_signatures", comment: "User signatures of a memorandum of understanding (MOU) for an organisation", force: :cascade do |t| - t.string "agreement_type", default: "crown", null: false + t.string "agreement_type", null: false t.datetime "created_at", null: false t.bigint "organisation_id", comment: "Organisation which user signed MOU on behalf of, or null" t.datetime "updated_at", null: false diff --git a/db/seeds.rb b/db/seeds.rb index bdeb9437a..66271dbf3 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -28,7 +28,7 @@ research_contact_status: :consented, user_research_opted_in_at: Time.zone.now }) - MouSignature.create! user: default_user, organisation: gds + MouSignature.create! user: default_user, organisation: gds, agreement_type: "crown" # create extra organisations test_org = Organisation.create! slug: "test-org", name: "Test Org", abbreviation: "TO" diff --git a/lib/tasks/mou_signatures.rake b/lib/tasks/mou_signatures.rake index a5a94100c..2d515f20e 100644 --- a/lib/tasks/mou_signatures.rake +++ b/lib/tasks/mou_signatures.rake @@ -1,14 +1,15 @@ namespace :mou_signatures do desc "Add MOU signature that was done offline" - task :create, %i[user_email organisation_name agreed_at_date] => :environment do |_, args| - usage_message = "usage: rake mou_signatures:create[, , ]".freeze - abort usage_message if args[:user_email].blank? || args[:organisation_name].blank? || args[:agreed_at_date].blank? + task :create, %i[user_email organisation_name agreed_at_date agreement_type] => :environment do |_, args| + usage_message = "usage: rake mou_signatures:create[, , , ]".freeze + abort usage_message if args[:user_email].blank? || args[:organisation_name].blank? || args[:agreed_at_date].blank? || + args[:agreement_type].blank? user = User.find_by!(email: args[:user_email]) organisation = Organisation.find_by!(name: args[:organisation_name]) agreed_at_date = Date.iso8601(args[:agreed_at_date]) - mou_signature = MouSignature.create!(user:, organisation:, created_at: agreed_at_date) + mou_signature = MouSignature.create!(user:, organisation:, created_at: agreed_at_date, agreement_type: args[:agreement_type]) puts "Added MOU signature for User: #{mou_signature.user.name} and Organisation: #{mou_signature.organisation.name} signed at: #{mou_signature.created_at}" end diff --git a/spec/lib/tasks/mou_signatures.rake_spec.rb b/spec/lib/tasks/mou_signatures.rake_spec.rb index 19fce76f7..1a19c9002 100644 --- a/spec/lib/tasks/mou_signatures.rake_spec.rb +++ b/spec/lib/tasks/mou_signatures.rake_spec.rb @@ -19,27 +19,49 @@ let(:date) { Date.iso8601 "2020-01-01T00:00" } it "aborts when the user is not found" do - expect { task.invoke("John Doe", organisation.name, date.iso8601) } + expect { task.invoke("John Doe", organisation.name, date.iso8601, "crown") } .to raise_error(ActiveRecord::RecordNotFound, /Couldn't find User/) end it "aborts when the organisation is not found" do - expect { task.invoke(user.email, "GDS", date.iso8601) } + expect { task.invoke(user.email, "GDS", date.iso8601, "crown") } .to raise_error(ActiveRecord::RecordNotFound, /Couldn't find Organisation/) end it "aborts when the agreed at date is not a valid date" do - expect { task.invoke(user.email, organisation.name, "not a date") } + expect { task.invoke(user.email, organisation.name, "not a date", "crown") } .to raise_error(Date::Error, /invalid date/) end it "creates an MOU signature" do freeze_time do - expect { task.invoke(user.email, organisation.name, date.iso8601) } + expect { task.invoke(user.email, organisation.name, date.iso8601, "crown") } .to change(MouSignature, :count).by(1) .and output(/Added MOU signature for User: A Person and Organisation: Government Digital Service signed at: 2020-01-01 00:00/).to_stdout - expect(MouSignature.last).to have_attributes(user:, organisation:, created_at: date, updated_at: Time.zone.now) + expect(MouSignature.last).to have_attributes( + user:, + organisation:, + created_at: date, + updated_at: Time.zone.now, + agreement_type: "crown", + ) + end + end + + it "creates a non-crown MOU signature" do + freeze_time do + expect { task.invoke(user.email, organisation.name, date.iso8601, "non_crown") } + .to change(MouSignature, :count).by(1) + .and output(/Added MOU signature for User: A Person and Organisation: Government Digital Service signed at: 2020-01-01 00:00/).to_stdout + + expect(MouSignature.last).to have_attributes( + user:, + organisation:, + created_at: date, + updated_at: Time.zone.now, + agreement_type: "non_crown", + ) end end end