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
29 changes: 26 additions & 3 deletions app/operations/event_procedures/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def call
create_event_procedure
assign_total_amount_cents(event_procedure)
end
log_success
end

private
Expand All @@ -25,7 +26,10 @@ def assign_total_amount_cents(event_procedure)
def create_event_procedure
self.event_procedure = EventProcedure.new(event_procedure_attributes)

fail!(error: event_procedure.errors) unless event_procedure.save
unless event_procedure.save
log_error("EventProcedure", event_procedure.errors.full_messages)
fail!(error: event_procedure.errors)
end

event_procedure
end
Expand All @@ -42,11 +46,17 @@ def event_procedure_attributes
end

def validate_procedure(procedure)
fail!(error: procedure.errors) if procedure.errors.any?
return unless procedure.errors.any?

log_error("Procedure", procedure.errors.full_messages)
fail!(error: procedure.errors)
end

def validate_health_insurance(health_insurance)
fail!(error: health_insurance.errors) if health_insurance.errors.any?
return unless health_insurance.errors.any?

log_error("HealthInsurance", health_insurance.errors.full_messages)
fail!(error: health_insurance.errors)
end

def merge_attributes(patient, procedure, health_insurance)
Expand Down Expand Up @@ -74,5 +84,18 @@ def find_or_create_patient
def find_or_create_procedure
Procedures::FindOrCreate.result(params: attributes[:procedure_attributes]).procedure
end

def log_success
Rails.logger.info(
">>> EventProcedure created successfully. ID: #{event_procedure.id}, User ID: #{user_id}"
)
end

def log_error(model_name, errors)
Rails.logger.error(
">>> Failed to create EventProcedure. User ID: #{user_id}, " \
"#{model_name} Errors: #{errors.join(', ')}"
)
end
end
end
41 changes: 41 additions & 0 deletions spec/operations/event_procedures/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@
expect(result.event_procedure.total_amount_cents).to eq(1300)
end

it "logs the success message" do
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)
params = {
hospital_id: create(:hospital).id,
cbhpm_id: cbhpm.id,
patient_service_number: "1234567890",
date: Time.zone.now,
urgency: false,
room_type: EventProcedures::RoomTypes::WARD,
payment: EventProcedures::Payments::HEALTH_INSURANCE,
patient_attributes: { id: patient.id, user_id: user.id },
procedure_attributes: { id: procedure.id },
health_insurance_attributes: { id: create(:health_insurance).id }
}

expect(Rails.logger).to receive(:info).with(
/>>> EventProcedure created successfully. ID: \d+, User ID: #{user.id}/
)

described_class.result(attributes: params, user_id: user.id)
end

context "when create a new patient" do
it "creates and does not duplicate the creation" do
cbhpm = create(:cbhpm)
Expand Down Expand Up @@ -268,6 +293,22 @@
)
end

it "logs the error message" do
procedure = create(:procedure)
health_insurance = create(:health_insurance)
attributes = {
patient_attributes: { id: patient.id },
procedure_attributes: { id: procedure.id },
health_insurance_attributes: { id: health_insurance.id }
}

expect(Rails.logger).to receive(:error).with(
/>>> Failed to create EventProcedure. User ID: #{user.id}, EventProcedure Errors:/
)

described_class.result(attributes: attributes, user_id: user.id)
end

context "when patient attributes are invalid" do
it "returns errors" do
user = create(:user)
Expand Down