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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@
# Ignore all environment files (except example).
/.env*
!.env.example

lib/data/procedures/
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
## Getting Started

- create a .env file based on .env.example and copy the content of .env.example to .env (`$ cp .env.example .env`)
- docker compose build
- docker compose run web bundle install
- docker compose run web bin/rails db:setup
- bin/dev
- run `docker compose build`
- run `docker compose up -d`
- run `docker compose exec web bin/setup`
- run `bin/dev`
- visit http://localhost:3000/

## Run tests
Expand Down
12 changes: 8 additions & 4 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
# Not proud of, but we are using this as a place to run scripts,
# since we can't run rake tasks or access console on render free version

# Rails.logger.debug "Seeding default data..."
# Dir[Rails.root.join("db/seeds/*.rb")].each do |seed|
# load seed
# end
if Rails.env.development?
Rails.logger.debug "Seeding data..."
Dir[Rails.root.join("db/seeds/*.rb")].each do |seed|
load seed
end
else
Rails.logger.debug { "Skipping seed data loading in #{Rails.env} environment" }
end

# Rails.logger.debug { "Seeding #{Rails.env} data..." }
# Dir[Rails.root.join("db/seeds", Rails.env, "*.rb")].each do |seed|
Expand Down
11 changes: 11 additions & 0 deletions db/seeds/01_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

Rails.logger.debug "1. Creating User..."

user = User.create!(
email: "user@email.com",
password: "qwe123",
password_confirmation: "qwe123"
)
Rails.logger.debug { "Created User with email: #{user.email}" }
Rails.logger.debug { "User's password: #{user.password}" }
26 changes: 26 additions & 0 deletions db/seeds/02_procedures_default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

Rake::Task.clear
Rails.application.load_tasks

Rails.logger.debug "2. Creating default Procedures..."

Rake::Task["port_values_2008:import"].invoke
Rake::Task["procedures:create_json_file"].invoke("lib/data/procedures.csv", 100)

batch_files = Rails.root.glob("lib/data/procedures/batch_*.json")

if batch_files.empty?
Rails.logger.debug "No JSON files found."
else
batch_files.each do |batch_file|
next unless File.exist?(batch_file)

Rails.logger.debug { "Importing #{batch_file}" }

Rake::Task["procedures:persist_in_database"].invoke(batch_file)
Rake::Task["procedures:persist_in_database"].reenable
end
end

Rails.logger.debug { "Created #{batch_files.count} default Procedures!" }
14 changes: 14 additions & 0 deletions db/seeds/03_procedures_custom.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

Rails.logger.debug "8. Creating custom Procedure..."

user = User.last
procedure = Procedure.create!(
name: "Procedure A CUSTOM",
code: "123",
amount_cents: 12_345,
description: "Desc A",
custom: true,
user_id: user.id
)
Rails.logger.debug { "Created custom Procedure: #{procedure.name} (#{procedure.code})" }
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

Rails.logger.debug "4. Creating default Health Insurances..."

health_insurance_data = [
"São Camilo",
"ISSEC",
Expand All @@ -15,3 +17,5 @@
health_insurance_data.each do |name|
HealthInsurance.create!(name: name)
end

Rails.logger.debug { "Created #{health_insurance_data.count} default Health Insurances..." }
11 changes: 11 additions & 0 deletions db/seeds/05_health_insurances_custom.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

Rails.logger.debug "7. Creating custom Health Insurance..."

user = User.last
health_insurance = HealthInsurance.create!(
name: "Plano de saude CUSTOM",
custom: true,
user_id: user.id
)
Rails.logger.debug { "Created custom Health Insurance: #{health_insurance.name}" }
9 changes: 9 additions & 0 deletions db/seeds/06_hospitals.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

Rails.logger.debug "5. Creating Hospital..."

hospital = Hospital.create!(
name: "Hospital A",
address: "A St."
)
Rails.logger.debug { "Created Hospital: #{hospital.name}" }
10 changes: 10 additions & 0 deletions db/seeds/07_patients.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

Rails.logger.debug "6. Creating Patient..."

user = User.last
patient = Patient.create!(
name: "Paciente Usuario 1",
user_id: user.id
)
Rails.logger.debug { "Created Patient: #{patient.name}" }
28 changes: 28 additions & 0 deletions db/seeds/08_medical_shifts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

Rails.logger.debug "9. Creating Medical Shifts..."

date = Date.yesterday
user = User.last
hospital = Hospital.last
MedicalShift.create!(
user_id: user.id,
workload: :twelve,
start_date: date,
start_hour: Time.zone.local(date.year, date.month, date.day, 8, 0, 0),
amount_cents: 1_511_80,
payd: false,
hospital_name: hospital.name
)

MedicalShift.create!(
user_id: user.id,
workload: :six,
start_date: date,
start_hour: Time.zone.local(date.year, date.month, date.day, 14, 0, 0),
amount_cents: 755_90,
payd: true,
hospital_name: hospital.name
)

Rails.logger.debug { "Created Medical Shifts for user on #{date}" }
90 changes: 90 additions & 0 deletions db/seeds/09_event_procedures.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# frozen_string_literal: true

Rails.logger.debug "Creating EventProcedures..."

user = User.last
patient = Patient.last
hospital = Hospital.last
procedure_default = Procedure.first
procedure_custom = Procedure.last
health_insurance_default = HealthInsurance.first
health_insurance_custom = HealthInsurance.last
cbhpm = Cbhpm.last

event_procedure_default = EventProcedure.create!(
procedure_id: procedure_default.id,
patient_id: patient.id,
hospital_id: hospital.id,
health_insurance_id: health_insurance_default.id,
patient_service_number: "PSN12345",
date: DateTime.current + 1.day,
urgency: true,
room_type: :apartment,
user_id: user.id,
payd: true,
payment: :health_insurance,
cbhpm_id: cbhpm.id
)

event_procedure_default_updated = EventProcedures::BuildTotalAmountCents.result(
event_procedure: event_procedure_default
)
event_procedure_default.update(total_amount_cents: event_procedure_default_updated.total_amount_cents)

custom_event_procedures_data_unpaid = [
{ psn: "PSN67890", date: DateTime.current - 2.days, amount: 15_00 },
{ psn: "PSN67891", date: DateTime.current - 3.days, amount: 20_00 },
{ psn: "PSN67892", date: DateTime.current - 4.days, amount: 80_00 },
{ psn: "PSN67893", date: DateTime.current - 5.days, amount: 25_00 },
{ psn: "PSN67894", date: DateTime.current - 6.days, amount: 20_00 },
{ psn: "PSN67895", date: DateTime.current - 7.days, amount: 30_00 },
{ psn: "PSN67896", date: DateTime.current - 8.days, amount: 40_00 }
]

custom_paid_event_procedures_data_paid = [
{ psn: "PSN67900", date: DateTime.current - 9.days, amount: 10_00 },
{ psn: "PSN67901", date: DateTime.current - 10.days, amount: 150_00 },
{ psn: "PSN67902", date: DateTime.current - 11.days, amount: 50_00 },
{ psn: "PSN67903", date: DateTime.current - 12.days, amount: 100_00 },
{ psn: "PSN67904", date: DateTime.current - 13.days, amount: 1_000_00 },
{ psn: "PSN67905", date: DateTime.current - 14.days, amount: 200_00 },
{ psn: "PSN67906", date: DateTime.current - 15.days, amount: 300_00 }
]

custom_event_procedures_data_unpaid.each do |data|
EventProcedure.create!(
procedure_id: procedure_custom.id,
patient_id: patient.id,
hospital_id: hospital.id,
health_insurance_id: health_insurance_custom.id,
patient_service_number: data[:psn],
date: data[:date],
urgency: false,
room_type: :ward,
total_amount_cents: data[:amount],
user_id: user.id,
payd: false,
payment: :others,
cbhpm_id: cbhpm.id
)
end

custom_paid_event_procedures_data_paid.each do |data|
EventProcedure.create!(
procedure_id: procedure_custom.id,
patient_id: patient.id,
hospital_id: hospital.id,
health_insurance_id: health_insurance_custom.id,
patient_service_number: data[:psn],
date: data[:date],
urgency: false,
room_type: :ward,
total_amount_cents: data[:amount],
user_id: user.id,
payd: true,
payment: :health_insurance,
cbhpm_id: cbhpm.id
)
end

Rails.logger.debug "Created EventProcedures for user"
45 changes: 0 additions & 45 deletions db/seeds/procedures.rb

This file was deleted.

28 changes: 28 additions & 0 deletions spec/db/seeds/event_procedures_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "Event Procedures seed" do
subject(:load_seed) { load seed_file }

let(:seed_file) { Rails.root.join("db/seeds/09_event_procedures.rb") }

before do
user = create(:user)
create(:patient, user: user)
create(:hospital)
create(:procedure, custom: false)
create(:procedure, custom: true, user: user)
create(:health_insurance, custom: false)
create(:health_insurance, custom: true, user: user)
create(:cbhpm)
end

it "runs without errors" do
expect { load_seed }.not_to raise_error
end

it "creates records in the database" do
expect { load_seed }.to change(EventProcedure, :count).from(0).to(15)
end
end
21 changes: 21 additions & 0 deletions spec/db/seeds/health_insurances_custom_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "Custom Health Insurances seed" do
subject(:load_seed) { load seed_file }

let(:seed_file) { Rails.root.join("db/seeds/05_health_insurances_custom.rb") }

before do
create(:user)
end

it "runs without errors" do
expect { load_seed }.not_to raise_error
end

it "creates records in the database" do
expect { load_seed }.to change(HealthInsurance, :count).from(0).to(1)
end
end
17 changes: 17 additions & 0 deletions spec/db/seeds/health_insurances_default_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "Default Health Insurances seed" do
subject(:load_seed) { load seed_file }

let(:seed_file) { Rails.root.join("db/seeds/04_health_insurances_default.rb") }

it "runs without errors" do
expect { load_seed }.not_to raise_error
end

it "creates records in the database" do
expect { load_seed }.to change(HealthInsurance, :count).from(0).to(9)
end
end
17 changes: 17 additions & 0 deletions spec/db/seeds/hospitals_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "Hospitals seed" do
subject(:load_seed) { load seed_file }

let(:seed_file) { Rails.root.join("db/seeds/06_hospitals.rb") }

it "runs without errors" do
expect { load_seed }.not_to raise_error
end

it "creates records in the database" do
expect { load_seed }.to change(Hospital, :count).from(0).to(1)
end
end
Loading
Loading