Skip to content
Merged
9 changes: 9 additions & 0 deletions app/models/event_procedure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,19 @@ class EventProcedure < ApplicationRecord
validates :urgency, inclusion: [true, false], if: -> { health_insurance? }
validates :payment, presence: true

validate :match_patient_event_procedure_user
validate :custom_and_urgency_cannot_be_true

private

def match_patient_event_procedure_user
return if patient.nil? || user.nil?

return unless patient.user_id != user_id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using unless together with negation

Suggested change
return unless patient.user_id != user_id
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

Expand Down
21 changes: 8 additions & 13 deletions spec/factories/event_procedures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
cbhpm
health_insurance
hospital
patient
procedure
user
procedure

patient_service_number { "202312150001" }
date { "2023-12-15 16:02:00" }
Expand All @@ -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
Expand Down
60 changes: 48 additions & 12 deletions spec/models/event_procedure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,30 @@

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
expect(described_class.with_deleted).to include(event_procedure)
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!

Expand Down Expand Up @@ -75,6 +71,27 @@
expect(event).to be_valid
end
end

context "when patient belongs to a different user" do
it "is invalid when has 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

it "is valid when has equal 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
Expand All @@ -96,9 +113,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
Expand All @@ -108,7 +132,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")
Expand All @@ -120,6 +148,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",
Expand All @@ -129,7 +158,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
Expand Down Expand Up @@ -163,13 +195,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
Expand Down
9 changes: 5 additions & 4 deletions spec/models/patient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 7 additions & 11 deletions spec/operations/event_procedures/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 }
}
Expand All @@ -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")
Expand All @@ -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 }
}
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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 }
}
Expand Down Expand Up @@ -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")
Expand All @@ -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
}
Expand Down Expand Up @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion spec/operations/event_procedures/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion spec/operations/event_procedures/total_amount_cents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 10 additions & 6 deletions spec/operations/event_procedures/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand Down
Loading