|
6 | 6 | describe "validations" do
|
7 | 7 | it { should validate_length_of(:pre_screening_notes).is_at_most(1000) }
|
8 | 8 | end
|
| 9 | + |
| 10 | + describe "patient attendance validation on confirmation" do |
| 11 | + let(:organisation) { create(:organisation) } |
| 12 | + let(:programme) do |
| 13 | + Programme.find_by(type: "hpv") || create(:programme, :hpv) |
| 14 | + end |
| 15 | + let(:session) do |
| 16 | + create(:session, :today, organisation:, programmes: [programme]) |
| 17 | + end |
| 18 | + let(:user) { create(:user, organisation:) } |
| 19 | + let(:vaccine) { programme.vaccines.first } |
| 20 | + let(:batch) { create(:batch, organisation:, vaccine:) } |
| 21 | + let(:request_session) { {} } |
| 22 | + |
| 23 | + let(:draft_vaccination_record_attributes) do |
| 24 | + { |
| 25 | + outcome: "administered", |
| 26 | + notes: "Test vaccination notes", |
| 27 | + performed_at: Time.current, |
| 28 | + batch_id: batch.id, |
| 29 | + delivery_method: "intramuscular", |
| 30 | + delivery_site: "left_arm_upper_position", |
| 31 | + dose_sequence: 1, |
| 32 | + full_dose: true, |
| 33 | + patient_id: patient_session.patient.id, |
| 34 | + programme_id: programme.id, |
| 35 | + session_id: session.id |
| 36 | + } |
| 37 | + end |
| 38 | + |
| 39 | + let(:draft_vaccination_record) do |
| 40 | + DraftVaccinationRecord.new( |
| 41 | + request_session:, |
| 42 | + current_user: user, |
| 43 | + **draft_vaccination_record_attributes |
| 44 | + ) |
| 45 | + end |
| 46 | + |
| 47 | + context "when patient is not attending the session" do |
| 48 | + let(:patient_session) { create(:patient_session, session:) } |
| 49 | + |
| 50 | + before do |
| 51 | + patient_session.registration_status || |
| 52 | + create( |
| 53 | + :patient_session_registration_status, |
| 54 | + :unknown, |
| 55 | + patient_session: |
| 56 | + ) |
| 57 | + |
| 58 | + draft_vaccination_record.wizard_step = :confirm |
| 59 | + end |
| 60 | + |
| 61 | + it "cannot register a vaccination for a patient not attending the session" do |
| 62 | + expect(draft_vaccination_record).not_to be_valid(:update) |
| 63 | + expect(draft_vaccination_record.errors[:session_id]).to include( |
| 64 | + match(/not registered as attending/) |
| 65 | + ) |
| 66 | + end |
| 67 | + |
| 68 | + it "includes the patient name in the error message" do |
| 69 | + draft_vaccination_record.valid?(:update) |
| 70 | + |
| 71 | + expect(draft_vaccination_record.errors[:session_id]).to include( |
| 72 | + match(/#{patient_session.patient.full_name}/) |
| 73 | + ) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + context "when patient is attending the session" do |
| 78 | + let(:patient_session) do |
| 79 | + create(:patient_session, :in_attendance, session:) |
| 80 | + end |
| 81 | + |
| 82 | + before { draft_vaccination_record.wizard_step = :confirm } |
| 83 | + |
| 84 | + it "can register a vaccination for a patient attending the session" do |
| 85 | + expect(draft_vaccination_record).to be_valid(:update) |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + context "when patient has completed the session" do |
| 90 | + let(:patient_session) { create(:patient_session, session:) } |
| 91 | + |
| 92 | + before do |
| 93 | + create( |
| 94 | + :patient_session_registration_status, |
| 95 | + :completed, |
| 96 | + patient_session: |
| 97 | + ) |
| 98 | + draft_vaccination_record.wizard_step = :confirm |
| 99 | + end |
| 100 | + |
| 101 | + it "can register a vaccination for a patient who has completed the session" do |
| 102 | + expect(draft_vaccination_record).to be_valid(:update) |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + context "when patient is explicitly not attending" do |
| 107 | + let(:patient_session) { create(:patient_session, session:) } |
| 108 | + |
| 109 | + before do |
| 110 | + create( |
| 111 | + :patient_session_registration_status, |
| 112 | + :not_attending, |
| 113 | + patient_session: |
| 114 | + ) |
| 115 | + draft_vaccination_record.wizard_step = :confirm |
| 116 | + end |
| 117 | + |
| 118 | + it "cannot register a vaccination for a patient explicitly not attending" do |
| 119 | + expect(draft_vaccination_record).not_to be_valid(:update) |
| 120 | + expect(draft_vaccination_record.errors[:session_id]).to include( |
| 121 | + match(/not registered as attending/) |
| 122 | + ) |
| 123 | + end |
| 124 | + end |
| 125 | + end |
9 | 126 | end
|
0 commit comments