diff --git a/app/models/concerns/requestable.rb b/app/models/concerns/requestable.rb index c3e485a853..75e7bc139a 100644 --- a/app/models/concerns/requestable.rb +++ b/app/models/concerns/requestable.rb @@ -39,15 +39,15 @@ def received? end def reviewed? - review_passed != nil + try(:review_passed) != nil end def review_passed? - review_passed == true + try(:review_passed) == true end def review_failed? - review_passed == false + try(:review_passed) == false end def verified? diff --git a/app/models/consent_request.rb b/app/models/consent_request.rb new file mode 100644 index 0000000000..32ff570021 --- /dev/null +++ b/app/models/consent_request.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class ConsentRequest < ApplicationRecord + ATTACHABLE_DOCUMENT_TYPES = %w[signed_consent unsigned_consent].freeze + + include Documentable + include Requestable + + belongs_to :qualification + + scope :order_by_role, + -> { joins(:qualification).order("qualifications.start_date": :desc) } + scope :order_by_user, + -> { joins(:qualification).order("qualifications.created_at": :asc) } + + def expires_after + 6.weeks + end +end diff --git a/config/analytics.yml b/config/analytics.yml index a5b1a7ba2e..b61e96c43e 100644 --- a/config/analytics.yml +++ b/config/analytics.yml @@ -86,6 +86,19 @@ - working_days_started_to_recommendation - working_days_submission_to_recommendation - working_days_submission_to_started + :consent_requests: + - assessment_id + - created_at + - expired_at + - id + - qualification_id + - received_at + - requested_at + - unsigned_document_downloaded + - updated_at + - verified_at + - verify_note + - verify_passed :countries: - code - created_at diff --git a/db/migrate/20240223065851_add_consent_requests.rb b/db/migrate/20240223065851_add_consent_requests.rb new file mode 100644 index 0000000000..3bd240f984 --- /dev/null +++ b/db/migrate/20240223065851_add_consent_requests.rb @@ -0,0 +1,16 @@ +class AddConsentRequests < ActiveRecord::Migration[7.1] + def change + create_table :consent_requests do |t| + t.datetime :received_at + t.datetime :requested_at + t.datetime :expired_at + t.boolean :unsigned_document_downloaded, default: false, null: false + t.datetime :verified_at + t.text :verify_note, default: "", null: false + t.boolean :verify_passed + t.references :assessment, null: false, foreign_key: true + t.references :qualification, null: false, foreign_key: true + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 80dd710571..7c8538ae8c 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[7.1].define(version: 2024_02_16_114800) do +ActiveRecord::Schema[7.1].define(version: 2024_02_23_065851) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -146,6 +146,22 @@ t.index ["application_form_id"], name: "index_assessments_on_application_form_id" end + create_table "consent_requests", force: :cascade do |t| + t.datetime "received_at" + t.datetime "requested_at" + t.datetime "expired_at" + t.boolean "unsigned_document_downloaded", default: false, null: false + t.datetime "verified_at" + t.text "verify_note", default: "", null: false + t.boolean "verify_passed" + t.bigint "assessment_id", null: false + t.bigint "qualification_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["assessment_id"], name: "index_consent_requests_on_assessment_id" + t.index ["qualification_id"], name: "index_consent_requests_on_qualification_id" + end + create_table "countries", force: :cascade do |t| t.string "code", null: false t.datetime "created_at", null: false @@ -551,6 +567,8 @@ add_foreign_key "application_forms", "teachers" add_foreign_key "assessment_sections", "assessments" add_foreign_key "assessments", "application_forms" + add_foreign_key "consent_requests", "assessments" + add_foreign_key "consent_requests", "qualifications" add_foreign_key "dqt_trn_requests", "application_forms" add_foreign_key "eligibility_checks", "regions" add_foreign_key "further_information_request_items", "work_histories" diff --git a/spec/factories/consent_requests.rb b/spec/factories/consent_requests.rb new file mode 100644 index 0000000000..c41b6017fe --- /dev/null +++ b/spec/factories/consent_requests.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :consent_request do + association :assessment + association :qualification, :completed + + trait :with_unsigned_upload do + after(:create) do |consent_request, _evaluator| + create(:upload, document: consent_request.unsigned_consent_document) + end + end + + trait :with_signed_upload do + after(:create) do |consent_request, _evaluator| + create(:upload, document: consent_request.signed_consent_document) + end + end + + trait :requested do + with_unsigned_upload + requested_at { Faker::Time.between(from: 1.month.ago, to: Time.zone.now) } + end + + trait :received do + requested + with_signed_upload + received_at { Faker::Time.between(from: 1.month.ago, to: Time.zone.now) } + end + + trait :expired do + requested + expired_at { Faker::Time.between(from: 1.month.ago, to: Time.zone.now) } + end + end +end diff --git a/spec/models/consent_request_spec.rb b/spec/models/consent_request_spec.rb new file mode 100644 index 0000000000..ee3a0abdb5 --- /dev/null +++ b/spec/models/consent_request_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe ConsentRequest, type: :model do + subject(:consent_request) { create(:consent_request) } + + it_behaves_like "a documentable" + it_behaves_like "a requestable" + + describe "#expires_after" do + subject(:expires_after) { consent_request.expires_after } + it { is_expected.to eq(6.weeks) } + end +end diff --git a/spec/support/shared_examples/requestable.rb b/spec/support/shared_examples/requestable.rb index 954386ab23..3495f835ba 100644 --- a/spec/support/shared_examples/requestable.rb +++ b/spec/support/shared_examples/requestable.rb @@ -9,7 +9,6 @@ it { is_expected.to_not validate_presence_of(:requested_at) } it { is_expected.to_not validate_presence_of(:received_at) } it { is_expected.to_not validate_presence_of(:expired_at) } - it { is_expected.to_not validate_presence_of(:reviewed_at) } end describe "#requested!" do @@ -49,16 +48,6 @@ end describe "#status" do - it "is completed when review passed is true" do - subject.review_passed = true - expect(subject.status).to eq("accepted") - end - - it "is rejected when review passed is false" do - subject.review_passed = false - expect(subject.status).to eq("rejected") - end - it "is received when received at is set" do subject.received_at = Time.zone.now expect(subject.status).to eq("received") @@ -81,13 +70,17 @@ describe "#review_status" do it "is accepted when passed is true" do - subject.review_passed = true - expect(subject.review_status).to eq("accepted") + if subject.respond_to?(:review_passed) + subject.review_passed = true + expect(subject.review_status).to eq("accepted") + end end it "is rejected when passed is false" do - subject.review_passed = false - expect(subject.review_status).to eq("rejected") + if subject.respond_to?(:review_passed) + subject.review_passed = false + expect(subject.review_status).to eq("rejected") + end end it "is not started if not reviewed" do @@ -96,6 +89,20 @@ end describe "#verify_status" do + it "is accepted when passed is true" do + if subject.respond_to?(:verify_passed) + subject.verify_passed = true + expect(subject.verify_status).to eq("accepted") + end + end + + it "is rejected when passed is false" do + if subject.respond_to?(:verify_passed) + subject.verify_passed = false + expect(subject.verify_status).to eq("rejected") + end + end + it "is not started if not verified" do expect(subject.verify_status).to eq("not_started") end