diff --git a/app/models/event_procedure.rb b/app/models/event_procedure.rb index c2a56bb..8697d9b 100644 --- a/app/models/event_procedure.rb +++ b/app/models/event_procedure.rb @@ -32,10 +32,18 @@ class EventProcedure < ApplicationRecord validates :urgency, inclusion: [true, false], if: -> { health_insurance? } validates :payment, presence: true + validate :match_user_with_patient_user validate :custom_and_urgency_cannot_be_true private + def match_user_with_patient_user + return if patient.nil? || user.nil? + return if patient.user_id == user_id + + errors.add(:base, "The patient must be associated with the same procedure user") + end + def custom_and_urgency_cannot_be_true return unless procedure&.custom && urgency diff --git a/spec/factories/event_procedures.rb b/spec/factories/event_procedures.rb index 95556f9..e6b3943 100644 --- a/spec/factories/event_procedures.rb +++ b/spec/factories/event_procedures.rb @@ -5,9 +5,8 @@ cbhpm health_insurance hospital - patient - procedure user + procedure patient_service_number { "202312150001" } date { "2023-12-15 16:02:00" } @@ -22,24 +21,20 @@ transient do health_insurance_attributes { nil } - end - - transient do patient_attributes { nil } - end - - transient do procedure_attributes { nil } end after(:build) do |event_procedure, evaluator| - event_procedure.patient = build(:patient, evaluator.patient_attributes) if evaluator.patient_attributes + event_procedure.user ||= build(:user) + event_procedure.patient ||= build( + :patient, + (evaluator.patient_attributes || {}).merge(user: event_procedure.user) + ) event_procedure.procedure = build(:procedure, evaluator.procedure_attributes) if evaluator.procedure_attributes + if evaluator.health_insurance_attributes - event_procedure.health_insurance = build( - :health_insurance, - evaluator.health_insurance_attributes - ) + event_procedure.health_insurance = build(:health_insurance, evaluator.health_insurance_attributes) end end end diff --git a/spec/models/event_procedure_spec.rb b/spec/models/event_procedure_spec.rb index 4a62056..7e54948 100644 --- a/spec/models/event_procedure_spec.rb +++ b/spec/models/event_procedure_spec.rb @@ -4,9 +4,11 @@ RSpec.describe EventProcedure do describe "acts_as_paranoid" do - it "soft deletes the record" do - event_procedure = create(:event_procedure) + let(:user) { create(:user) } + let(:patient) { create(:patient, user: user) } + let(:event_procedure) { create(:event_procedure, user: user, patient: patient) } + it "soft deletes the record" do event_procedure.destroy expect(event_procedure.deleted_at).to be_present @@ -14,24 +16,18 @@ end it "does not include the record in the default scope" do - event_procedure = create(:event_procedure) - event_procedure.destroy expect(described_class.all).not_to include(event_procedure) end it "includes the record in the default scope when with_deleted is called" do - event_procedure = create(:event_procedure) - event_procedure.destroy expect(described_class.with_deleted).to include(event_procedure) end it "restores a soft deleted record" do - event_procedure = create(:event_procedure) - event_procedure.destroy event_procedure.recover! @@ -75,6 +71,29 @@ expect(event).to be_valid end end + + context 'when patient belongs to a different user' do + it 'is invalid when patient and event_procedure have different users' do + user = create(:user) + other_user = create(:user) + patient = create(:patient, user: other_user) + + event_procedure = build(:event_procedure, user: user, patient: patient) + + expect(event_procedure).not_to be_valid + expect(event_procedure.errors[:base]).to include("The patient must be associated with the same procedure user") + end + end + + context 'when patient belongs to the same user' do + it 'is valid when patient and event_procedure have the same user' do + user = create(:user) + patient = create(:patient, user: user) + event_procedure = build(:event_procedure, user: user, patient: patient) + + expect(event_procedure).to be_valid + end + end end describe ".enumerations" do @@ -96,9 +115,16 @@ end describe "nested attributes for patient" do + let(:user) { create(:user) } + context "when patient_attributes are provided" do it "creates patient" do - event_procedure = build(:event_procedure, patient_attributes: { id: nil, name: "John Doe" }) + event_procedure = build( + :event_procedure, + user: user, + patient: nil, + patient_attributes: { name: "John Doe", user_id: user.id } + ) expect { event_procedure.save }.to change(Patient, :count).by(1) expect(event_procedure.patient).to be_persisted @@ -108,7 +134,11 @@ context "when patient_attributes are not provided" do it "does not create patient" do - event_procedure = build(:event_procedure, patient_attributes: { id: nil, name: nil }) + event_procedure = build( + :event_procedure, + patient: nil, + patient_attributes: { name: nil, user_id: nil } + ) expect(event_procedure.save).to be_falsey expect(event_procedure.errors[:"patient.name"]).to include("can't be blank") @@ -120,6 +150,7 @@ context "when procedure_attributes are provided" do it "creates procedure" do user = create(:user) + patient = create(:patient, user: user) procedure_attributes = { id: nil, name: "procedure name", @@ -129,7 +160,10 @@ custom: true, user_id: user.id } - event_procedure = build(:event_procedure, procedure_attributes: procedure_attributes) + event_procedure = build( + :event_procedure, user: user, patient: patient, + procedure_attributes: procedure_attributes + ) expect { event_procedure.save }.to change(Procedure, :count).by(1) expect(event_procedure.procedure).to be_persisted @@ -163,13 +197,17 @@ context "when health_insurance_attributes are provided" do it "creates health_insurance" do user = create(:user) + patient = create(:patient, user: user) health_insurance_attributes = { id: nil, name: "health_insurance name", custom: true, user_id: user.id } - event_procedure = build(:event_procedure, health_insurance_attributes: health_insurance_attributes) + event_procedure = build( + :event_procedure, user: user, patient: patient, + health_insurance_attributes: health_insurance_attributes + ) expect { event_procedure.save }.to change(HealthInsurance, :count).by(1) expect(event_procedure.health_insurance).to be_persisted diff --git a/spec/models/patient_spec.rb b/spec/models/patient_spec.rb index 8dbbb5b..86b4db9 100644 --- a/spec/models/patient_spec.rb +++ b/spec/models/patient_spec.rb @@ -15,16 +15,17 @@ describe "deletable?" do subject { patient.deletable? } - let(:patient) { create(:patient, event_procedures:) } + let(:user) { create(:user) } + let(:patient) { create(:patient, user: user) } context "when patient has no event_procedures" do - let(:event_procedures) { [] } - it { is_expected.to be_truthy } end context "when patient has event_procedures" do - let(:event_procedures) { create_list(:event_procedure, 1) } + before do + create(:event_procedure, patient: patient, user: user) + end it { is_expected.to be_falsy } end diff --git a/spec/operations/event_procedures/build_total_amount_cents_spec.rb b/spec/operations/event_procedures/build_total_amount_cents_spec.rb index 1956d10..b6a457e 100644 --- a/spec/operations/event_procedures/build_total_amount_cents_spec.rb +++ b/spec/operations/event_procedures/build_total_amount_cents_spec.rb @@ -5,12 +5,13 @@ RSpec.describe EventProcedures::BuildTotalAmountCents, type: :operation do describe ".result" do it "returns a success" do + user = create(:user) cbhpm = create(:cbhpm) procedure = create(:procedure) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A") create(:port_value, cbhpm: cbhpm, anesthetic_port: "1A", amount_cents: 1000) - event_procedure = create(:event_procedure, procedure: procedure, cbhpm: cbhpm) - + patient = create(:patient, user: user) + event_procedure = create(:event_procedure, user: user, procedure: procedure, cbhpm: cbhpm, patient: patient) expect(described_class.result(event_procedure: event_procedure)).to be_success end diff --git a/spec/operations/event_procedures/create_spec.rb b/spec/operations/event_procedures/create_spec.rb index 573bf96..7f494b5 100644 --- a/spec/operations/event_procedures/create_spec.rb +++ b/spec/operations/event_procedures/create_spec.rb @@ -3,10 +3,12 @@ require "rails_helper" RSpec.describe EventProcedures::Create, type: :operation do + let(:user) { create(:user) } + let(:patient) { create(:patient, user: user) } + describe ".result" do context "when params are valid" do it "is successful" do - user = create(:user) cbhpm = create(:cbhpm) procedure = create(:procedure) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A") @@ -19,7 +21,7 @@ urgency: false, room_type: EventProcedures::RoomTypes::WARD, payment: EventProcedures::Payments::HEALTH_INSURANCE, - patient_attributes: { id: create(:patient).id }, + patient_attributes: { id: patient.id, user_id: user.id }, procedure_attributes: { id: procedure.id }, health_insurance_attributes: { id: create(:health_insurance).id } } @@ -30,7 +32,6 @@ end it "creates a new event_procedure" do - user = create(:user) cbhpm = create(:cbhpm) procedure = create(:procedure) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A") @@ -43,7 +44,7 @@ urgency: true, room_type: EventProcedures::RoomTypes::WARD, payment: EventProcedures::Payments::HEALTH_INSURANCE, - patient_attributes: { id: create(:patient).id }, + patient_attributes: { id: patient.id, user_id: user.id }, procedure_attributes: { id: procedure.id }, health_insurance_attributes: { id: create(:health_insurance).id } } @@ -68,7 +69,6 @@ context "when create a new patient" do it "creates and does not duplicate the creation" do - user = create(:user) cbhpm = create(:cbhpm) procedure = create(:procedure) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A") @@ -94,7 +94,6 @@ context "when create a new procedure" do context "when procedure attributes are valid" do it "does not duplicate the creation" do - user = create(:user) cbhpm = create(:cbhpm) procedure = create(:procedure) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A") @@ -117,7 +116,7 @@ urgency: false, room_type: EventProcedures::RoomTypes::WARD, payment: EventProcedures::Payments::HEALTH_INSURANCE, - patient_attributes: { id: create(:patient).id }, + patient_attributes: { id: patient.id, user_id: user.id }, procedure_attributes: procedure_attributes, health_insurance_attributes: { id: create(:health_insurance).id } } @@ -163,7 +162,6 @@ context "when create a new health_insurance" do context "when health_insurance attributes are valid" do it "does not duplicate the creation" do - user = create(:user) cbhpm = create(:cbhpm) procedure = create(:procedure) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A") @@ -182,7 +180,7 @@ urgency: nil, room_type: nil, payment: EventProcedures::Payments::OTHERS, - patient_attributes: { id: create(:patient).id }, + patient_attributes: { id: patient.id, user_id: user.id }, procedure_attributes: { id: procedure.id }, health_insurance_attributes: health_insurance_attributes } @@ -249,9 +247,7 @@ end it "returns errors" do - user = create(:user) procedure = create(:procedure) - patient = create(:patient) health_insurance = create(:health_insurance) attributes = { patient_attributes: { id: patient.id }, diff --git a/spec/operations/event_procedures/list_spec.rb b/spec/operations/event_procedures/list_spec.rb index bf6e911..819c21b 100644 --- a/spec/operations/event_procedures/list_spec.rb +++ b/spec/operations/event_procedures/list_spec.rb @@ -24,7 +24,7 @@ it "includes the associations" do # rubocop:disable RSpec/MultipleExpectations user = create(:user) procedure = create(:procedure, name: "Tireoidectomia") - patient = create(:patient, name: "John Doe") + patient = create(:patient, name: "John Doe", user: user) hospital = create(:hospital, name: "General Hospital") health_insurance = create(:health_insurance, name: "Insurance Corp") create_list( diff --git a/spec/operations/event_procedures/total_amount_cents_spec.rb b/spec/operations/event_procedures/total_amount_cents_spec.rb index 2125cf6..d5ba170 100644 --- a/spec/operations/event_procedures/total_amount_cents_spec.rb +++ b/spec/operations/event_procedures/total_amount_cents_spec.rb @@ -6,7 +6,8 @@ describe ".result" do it "is successful" do user = create(:user) - event_procedure = create(:event_procedure, user_id: user.id) + patient = create(:patient, user: user) + event_procedure = create(:event_procedure, user_id: user.id, patient: patient) expect(described_class.result(event_procedures: [event_procedure])).to be_success end diff --git a/spec/operations/event_procedures/update_spec.rb b/spec/operations/event_procedures/update_spec.rb index 343da3a..4f7f0b1 100644 --- a/spec/operations/event_procedures/update_spec.rb +++ b/spec/operations/event_procedures/update_spec.rb @@ -22,17 +22,21 @@ context "when patient_attributes are provided" do it "update event_procedure association patient" do - old_patient = create(:patient, name: "Old Patient") - new_patient = create(:patient, name: "New Patient name") + user = create(:user) + old_patient = create(:patient, name: "Old Patient", user: user) + new_patient = create(:patient, name: "New Patient name", user: user) cbhpm = create(:cbhpm) procedure = create(:procedure) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A") create(:port_value, cbhpm: cbhpm, anesthetic_port: "1A", amount_cents: 1000) - event_procedure = create(:event_procedure, procedure: procedure, cbhpm: cbhpm, patient: old_patient) + event_procedure = create( + :event_procedure, procedure: procedure, cbhpm: cbhpm, patient: old_patient, + user: user + ) attributes = { urgency: true, room_type: EventProcedures::RoomTypes::WARD, - patient_attributes: { id: new_patient.id, name: nil } + patient_attributes: { id: new_patient.id, name: nil, user: user } } result = described_class.result(id: event_procedure.id.to_s, attributes: attributes) @@ -41,13 +45,13 @@ end it "creates a new patient and does not duplicate the creation" do - patient = create(:patient, name: "Old Patient") user = create(:user) + patient = create(:patient, name: "Old Patient", user: user) cbhpm = create(:cbhpm) procedure = create(:procedure) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A") create(:port_value, cbhpm: cbhpm, anesthetic_port: "1A", amount_cents: 1000) - event_procedure = create(:event_procedure, procedure: procedure, cbhpm: cbhpm, patient: patient) + event_procedure = create(:event_procedure, procedure: procedure, cbhpm: cbhpm, patient: patient, user: user) attributes = { urgency: true, room_type: EventProcedures::RoomTypes::WARD, diff --git a/spec/pdfs/event_procedures_report_pdf_spec.rb b/spec/pdfs/event_procedures_report_pdf_spec.rb index 70ec872..b5e2273 100644 --- a/spec/pdfs/event_procedures_report_pdf_spec.rb +++ b/spec/pdfs/event_procedures_report_pdf_spec.rb @@ -5,8 +5,9 @@ RSpec.describe EventProceduresReportPdf, type: :pdf do it "generates a report with the correct content" do user = create(:user) + patient = create(:patient, user: user) pdf = Prawn::Document.new - event_procedures = create_list(:event_procedure, 3, user_id: user.id) + event_procedures = create_list(:event_procedure, 3, user_id: user.id, patient: patient) amount = EventProcedures::TotalAmountCents.call(event_procedures: event_procedures) described_class.new( diff --git a/spec/pdfs/footer_pdf_spec.rb b/spec/pdfs/footer_pdf_spec.rb index 4cf844a..7656db5 100644 --- a/spec/pdfs/footer_pdf_spec.rb +++ b/spec/pdfs/footer_pdf_spec.rb @@ -5,7 +5,8 @@ RSpec.describe FooterPdf, type: :pdf do it "generates a footer with the correct content" do user = create(:user) - event_procedures = create_list(:event_procedure, 3, user_id: user.id) + patient = create(:patient, user: user) + event_procedures = create_list(:event_procedure, 3, user_id: user.id, patient: patient) total_amount_cents = EventProcedures::TotalAmountCents.call( event_procedures: event_procedures ) diff --git a/spec/requests/api/v1/event_procedures_request_spec.rb b/spec/requests/api/v1/event_procedures_request_spec.rb index 713d286..4907ce9 100644 --- a/spec/requests/api/v1/event_procedures_request_spec.rb +++ b/spec/requests/api/v1/event_procedures_request_spec.rb @@ -133,8 +133,9 @@ procedure = create(:procedure) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A") create(:port_value, cbhpm: cbhpm, anesthetic_port: "1A", amount_cents: 1000) - patient = create(:patient) + patient = create(:patient, user: user) params = { + user_id: user.id, hospital_id: create(:hospital).id, cbhpm_id: cbhpm.id, patient_service_number: "1234567890", @@ -153,7 +154,7 @@ end it "returns event_procedure" do - patient = create(:patient) + patient = create(:patient, user: user) procedure = create(:procedure, amount_cents: 20_000, description: "nice description") cbhpm = create(:cbhpm) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A") @@ -258,7 +259,7 @@ context "when procedure does not exist" do it "returns created" do - patient = create(:patient) + patient = create(:patient, user: user) health_insurance = create(:health_insurance) cbhpm = create(:cbhpm) create(:port_value, cbhpm: cbhpm, anesthetic_port: "1A", amount_cents: 1000) @@ -285,7 +286,7 @@ context "when health_insurance does not exist" do it "returns created" do - patient = create(:patient) + patient = create(:patient, user: user) procedure = create(:procedure) cbhpm = create(:cbhpm) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A") @@ -326,7 +327,7 @@ end it "returns error message" do - patient = create(:patient) + patient = create(:patient, user: user) procedure = create(:procedure) health_insurance = create(:health_insurance) params = { @@ -388,7 +389,7 @@ cbhpm = create(:cbhpm) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A") create(:port_value, cbhpm: cbhpm, anesthetic_port: "1A", amount_cents: 1000) - patient = create(:patient) + patient = create(:patient, user: user) event_procedure = create( :event_procedure, health_insurance_id: health_insurance.id, @@ -420,7 +421,7 @@ it "updates event_procedure" do health_insurance = create(:health_insurance) procedure = create(:procedure, name: "Angioplastia transluminal") - patient = create(:patient) + patient = create(:patient, user: user) cbhpm = create(:cbhpm) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, port: "12C", anesthetic_port: "6") create(:port_value, cbhpm: cbhpm, port: "12C", anesthetic_port: "6", amount_cents: 60_500) @@ -448,14 +449,15 @@ context "with valid attributes and the record not belongs to the user" do it "returns unauthorized" do + other_user = create(:user) + patient = create(:patient, user: other_user) health_insurance = create(:health_insurance) procedure = create(:procedure) cbhpm = create(:cbhpm) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A") create(:port_value, cbhpm: cbhpm, anesthetic_port: "1A", amount_cents: 1000) - patient = create(:patient) event_procedure = create( - :event_procedure, + :event_procedure, user: other_user, health_insurance_id: health_insurance.id, procedure_id: procedure.id, patient_id: patient.id, @@ -489,7 +491,7 @@ cbhpm = create(:cbhpm) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A") create(:port_value, cbhpm: cbhpm, anesthetic_port: "1A", amount_cents: 1000) - patient = create(:patient) + patient = create(:patient, user: user) event_procedure = create( :event_procedure, health_insurance_id: health_insurance.id, @@ -510,7 +512,7 @@ cbhpm = create(:cbhpm) create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A") create(:port_value, cbhpm: cbhpm, anesthetic_port: "1A", amount_cents: 1000) - patient = create(:patient) + patient = create(:patient, user: user) event_procedure = create( :event_procedure, health_insurance_id: health_insurance.id, diff --git a/spec/requests/api/v1/patients_request_spec.rb b/spec/requests/api/v1/patients_request_spec.rb index 22ddd51..89f10c3 100644 --- a/spec/requests/api/v1/patients_request_spec.rb +++ b/spec/requests/api/v1/patients_request_spec.rb @@ -160,7 +160,7 @@ end describe "DELETE api/v1/patients/:id" do - let!(:patient) { create(:patient, user: user) } + let(:patient) { create(:patient, user: user) } let(:path) { "/api/v1/patients/#{patient.id}" } let(:http_method) { :delete } let(:params) { {} } @@ -175,7 +175,7 @@ context "when patient cannot be destroyed" do it "returns unprocessable_content" do - create(:event_procedure, patient:) + create(:event_procedure, patient: patient, user: user) delete path, headers: headers @@ -183,7 +183,7 @@ end it "returns errors" do - create(:event_procedure, patient:) + create(:event_procedure, patient: patient, user: user) delete path, headers: headers