Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/controllers/api/v1/event_procedures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ def event_procedure_permitted_query_params
:year,
:paid,
hospital: [:name],
health_insurance: [:name]
health_insurance: [:name],
patient: [:name]
).to_h
end

Expand Down
1 change: 1 addition & 0 deletions app/models/event_procedure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class EventProcedure < ApplicationRecord
scope :date_between, EventProcedures::ByDateBetween
scope :by_hospital_name, EventProcedures::ByHospitalNameQuery
scope :by_health_insurance_name, EventProcedures::ByHealthInsuranceNameQuery
scope :by_patient, EventProcedures::ByPatientQuery

validates :date, presence: true
validates :patient_service_number, presence: true
Expand Down
5 changes: 5 additions & 0 deletions app/operations/event_procedures/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def apply_all_filters(query)
query = apply_year_filter(query)
query = apply_hospital_filter(query)
query = apply_health_insurance_filter(query)
query = apply_patient_filter(query)
query = apply_ids_filter(query)
apply_paid_filter(query)
end
Expand Down Expand Up @@ -60,6 +61,10 @@ def apply_health_insurance_filter(query)
end
end

def apply_patient_filter(query)
params.dig(:patient, :name).present? ? query.by_patient(patient_name: params[:patient][:name]) : query
end

def apply_ids_filter(query)
params[:ids].present? ? query.where(id: params[:ids]) : query
end
Expand Down
16 changes: 16 additions & 0 deletions app/queries/event_procedures/by_patient_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module EventProcedures
class ByPatientQuery < ApplicationQuery
attr_reader :patient_name, :relation

def initialize(patient_name:, relation: EventProcedure)
@patient_name = patient_name
@relation = relation
end

def call
relation.joins(:patient).where("patients.name ILIKE ?", "%#{patient_name}%")
end
end
end
14 changes: 14 additions & 0 deletions spec/operations/event_procedures/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@
it { expect(result.event_procedures).to eq [EventProcedure.last] }
end

context "when has filters by patient name" do
it "returns event_procedures for patients matching the name" do
user = create(:user)
joao = create(:patient, name: "João Silva", user: user)
maria = create(:patient, name: "Maria Souza", user: user)
ep_joao = create(:event_procedure, patient: joao, user: user)
_ep_maria = create(:event_procedure, patient: maria, user: user)

result = described_class.result(scope: EventProcedure.all, params: { patient: { name: "João" } })

expect(result.event_procedures).to contain_exactly(ep_joao)
end
end

describe "event_procedures_unpaginated" do
context "when there are event procedures outside of pagination" do
let!(:event_procedures) { create_list(:event_procedure, 11) }
Expand Down
49 changes: 49 additions & 0 deletions spec/queries/event_procedures/by_patient_query_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe EventProcedures::ByPatientQuery do
describe "#call" do
context "when a matching patient name is provided" do
it "returns event_procedures for patients matching the name" do
user = create(:user)
joao = create(:patient, name: "João Silva", user: user)
maria = create(:patient, name: "Maria Souza", user: user)
ep_joao = create(:event_procedure, user: user, patient: joao)
_ep_maria = create(:event_procedure, user: user, patient: maria)

query = described_class.call(patient_name: "João")

expect(query).to contain_exactly(ep_joao)
end

it "performs case-insensitive partial matching" do
user = create(:user)
patient = create(:patient, name: "Carlos Alberto", user: user)
ep = create(:event_procedure, user: user, patient: patient)

query = described_class.call(patient_name: "carlos")

expect(query).to contain_exactly(ep)
end
end

context "when patient name does not match any patient" do
it "returns an empty collection" do
user = create(:user)
patient = create(:patient, name: "Pedro Lima", user: user)
create(:event_procedure, user: user, patient: patient)

query = described_class.call(patient_name: "Zélia")

expect(query).to be_empty
end
end

context "when keyword arguments are missing" do
it "raises an ArgumentError" do
expect { described_class.call }.to raise_error(ArgumentError, "missing keyword: :patient_name")
end
end
end
end
20 changes: 20 additions & 0 deletions spec/requests/api/v1/event_procedures_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@
end
end

context "when has filters by patient name" do
it "returns event_procedures for patients matching the name" do
joao = create(:patient, name: "João Silva", user: user)
maria = create(:patient, name: "Maria Souza", user: user)
create(:event_procedure, patient: joao, user_id: user.id)
create(:event_procedure, patient: maria, user_id: user.id)

get path, params: { patient: { name: "João" } }, headers: headers

expect(response.parsed_body["event_procedures"].length).to eq(1)
expect(response.parsed_body["event_procedures"].first["patient"]).to eq("João Silva")
end

it "returns empty when no patient matches the name" do
get path, params: { patient: { name: "Zélia" } }, headers: headers

expect(response.parsed_body["event_procedures"]).to be_empty
end
end

context "when has pagination via page and per_page" do
before do
procedure = create(:procedure, custom: true, user: user, amount_cents: 5000)
Expand Down