Skip to content

Commit

Permalink
[3216] make past court reports like present court reports (#5032)
Browse files Browse the repository at this point in the history
assumption: we want the dates to mostly match
punted on: most_recent_court_date and latest_hearing_date being retrofitted (seems worth revisiting that in a future PR)
punted on: a stronger refactor of the two controller methods, but can be convinced its time
rearranged context so court date fields could be added in as desired

---------

Co-authored-by: Ian Norris <iannorris@Ians-MacBook-Air.local>
  • Loading branch information
FeminismIsAwesome and Ian Norris authored Jul 29, 2023
1 parent 62fb6d5 commit c67f7cb
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 49 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ group :test do
gem "rake"
gem "selenium-webdriver"
gem "simplecov"
gem "docx"
end

# gem "pdf-reader", "~> 2.9"
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ GEM
devise (>= 4.6)
diff-lcs (1.5.0)
docile (1.4.0)
docx (0.8.0)
nokogiri (~> 1.13, >= 1.13.0)
rubyzip (~> 2.0)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.8.1)
Expand Down Expand Up @@ -501,6 +504,7 @@ DEPENDENCIES
delayed_job_active_record
devise
devise_invitable
docx
dotenv-rails
draper
erb_lint
Expand Down
20 changes: 19 additions & 1 deletion app/controllers/court_dates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def show
respond_to do |format|
format.html {}
format.docx do
send_data @court_date.generate_report,
send_data generate_report_to_string(@court_date, params[:time_zone]),
type: :docx,
filename: "#{@court_date.display_name}.docx",
disposition: "attachment",
Expand Down Expand Up @@ -94,4 +94,22 @@ def court_dates_params
{case_court_orders_attributes: %i[text _destroy implementation_status id casa_case_id]}
)
end

def generate_report_to_string(court_date, time_zone)
casa_case = court_date.casa_case
casa_case.casa_org.open_org_court_report_template do |template_docx_file|
args = {
volunteer_id: current_user.volunteer? ? current_user.id : casa_case.assigned_volunteers.first&.id,
case_id: casa_case.id,
path_to_template: template_docx_file.to_path,
time_zone: time_zone,
court_date: court_date,
case_court_orders: court_date.case_court_orders
}
context = CaseCourtReportContext.new(args).context
court_report = CaseCourtReport.new(path_to_template: template_docx_file.to_path, context: context)

court_report.generate_to_string
end
end
end
17 changes: 8 additions & 9 deletions app/models/case_court_report_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def initialize(args = {})
@volunteer = Volunteer.find(args[:volunteer_id]) if args[:volunteer_id]
@time_zone = args[:time_zone]
@path_to_template = args[:path_to_template]
@court_date = args[:court_date] || @casa_case.next_court_date
@case_court_orders = args[:case_court_orders] || @casa_case.case_court_orders
end

def context
Expand All @@ -29,7 +31,8 @@ def prepare_context(is_default_template)
case_mandates: prepare_case_orders, # backwards compatible with old Montgomery template - keep this! TODO test full generation
latest_hearing_date: latest_hearing_date.nil? ? "___<LATEST HEARING DATE>____" : I18n.l(latest_hearing_date.date, format: :full, default: nil),
org_address: org_address(is_default_template),
volunteer: volunteer_info
volunteer: volunteer_info,
hearing_type_name: @court_date&.hearing_type&.name || "None"
}
end

Expand All @@ -42,16 +45,12 @@ def prepare_case_contacts
end

def prepare_case_orders
case_order_data = []

@casa_case.case_court_orders.each do |case_order|
case_order_data << {
@case_court_orders.map do |case_order|
{
order: case_order.text,
status: case_order.implementation_status&.humanize
}
end

case_order_data
end

def filter_out_old_case_contacts(interviewees)
Expand All @@ -65,11 +64,11 @@ def filter_out_old_case_contacts(interviewees)

def prepare_case_details
{
court_date: I18n.l(@casa_case.next_court_date&.date, format: :full, default: nil),
court_date: I18n.l(@court_date&.date, format: :full, default: nil),
case_number: @casa_case.case_number,
dob: I18n.l(@casa_case.birth_month_year_youth, format: :youth_date_of_birth, default: nil),
is_transitioning: @casa_case.in_transition_age?,
judge_name: @casa_case.next_court_date&.judge&.name
judge_name: @court_date&.judge&.name
}
end

Expand Down
29 changes: 0 additions & 29 deletions app/models/court_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class CourtDate < ApplicationRecord

scope :ordered_ascending, -> { order("date asc") }

DOCX_TEMPLATE_PATH = Rails.root.join("app", "documents", "templates", "default_past_court_date_template.docx")

# get reports associated with the case this belongs to before this court date but after the court date before this one
def associated_reports
prev = casa_case.court_dates.where("date < ?", date).order(:date).last
Expand All @@ -34,36 +32,9 @@ def additional_info?
case_court_orders.any? || hearing_type || judge
end

def generate_report
template = Sablon.template(File.expand_path(DOCX_TEMPLATE_PATH))

template.render_to_string(context_hash)
end

def display_name
"#{casa_case.case_number} - Court Date - #{I18n.l(date.to_date)}"
end

private

def context_hash
{
court_date: date,
case_number: casa_case.case_number,
judge_name: judge&.name || "None",
hearing_type_name: hearing_type&.name || "None",
case_court_orders: case_court_orders_context_hash
}
end

def case_court_orders_context_hash
case_court_orders.map do |order|
{
text: order.text,
implementation_status: order.implementation_status&.humanize
}
end
end
end
# == Schema Information
#
Expand Down
6 changes: 5 additions & 1 deletion spec/factories/case_court_report_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

transient do
casa_case { nil }
court_date { nil }
volunteer { nil }
case_court_orders { nil }
path_to_report { Rails.root.join("tmp", "test_report.docx").to_s }
path_to_template { Rails.root.join("app", "documents", "templates", "default_report_template.docx").to_s }
end
Expand All @@ -21,7 +23,9 @@
case_id: casa_case_for_context.id,
volunteer_id: volunteer_for_context.id,
path_to_report: path_to_report,
path_to_template: path_to_template
path_to_template: path_to_template,
court_date: court_date,
case_court_orders: case_court_orders
)
}
end
Expand Down
Binary file not shown.
21 changes: 21 additions & 0 deletions spec/models/case_court_report_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@
end
end

context "when they specify a specific court date they are interested in looking at" do
it "contains the selected court date in a human readable format" do
court_date_1 = create(:court_date, date: 2.months.since)
court_date_2 = create(:court_date, date: 5.months.since)

casa_case.court_dates << court_date_1
casa_case.court_dates << court_date_2

court_report_context = build(:case_court_report_context, casa_case: casa_case, court_date: court_date_2)
expect(court_report_context.context[:casa_case][:court_date]).to eq("June 1, 2021")
end
end

context "when there are no future court dates" do
let(:past_court_date) { create(:court_date, date: 2.months.ago) }

Expand Down Expand Up @@ -247,6 +260,14 @@
casa_case.case_court_orders << court_order_unimplemented
end

context "when using specified orders to a specific casa date" do
it "does not lean on orders of the casa case if specified directly" do
court_order = build(:case_court_order, text: "Some Court Text", implementation_status: :implemented)
court_report_context = build(:case_court_report_context, casa_case: casa_case, case_court_orders: [court_order]).context
expect(court_report_context[:case_court_orders]).to eq([{order: "Some Court Text", status: "Implemented"}])
end
end

it "has a list of court orders the same length as all the court orders in the case" do
expect(court_report_context[:case_court_orders].length).to eq(casa_case.case_court_orders.length)
end
Expand Down
7 changes: 0 additions & 7 deletions spec/models/court_date_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,6 @@
end
end

describe "#generate_report" do
subject { court_date.generate_report }

# TODO write a better test for this
it { is_expected.not_to be_nil }
end

describe "#display_name" do
subject { court_date.display_name }
it "contains case number and date" do
Expand Down
9 changes: 7 additions & 2 deletions spec/requests/court_dates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@
describe "GET /show" do
subject(:show) { get casa_case_court_date_path(casa_case, court_date) }

before { show }
before do
casa_org = court_date.casa_case.casa_org
casa_org.court_report_template.attach(io: File.new(Rails.root.join("spec", "fixtures", "files", "default_past_court_date_template.docx")), filename: "test_past_date_template.docx")
casa_org.court_report_template.save!
show
end

context "when the request is authenticated" do
it { expect(response).to have_http_status(:success) }
Expand All @@ -63,7 +68,7 @@

document_inspector = DocxInspector.new(docx_contents: response.body)

expect(document_inspector.word_list_document_contains?(court_date.date.to_s)).to eq(true)
expect(document_inspector.word_list_document_contains?("December 25, 2020")).to eq(true)
end

context "when a judge is attached" do
Expand Down
5 changes: 5 additions & 0 deletions spec/support/download_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def download_content
File.read(download)
end

def download_docx
wait_for_download
Docx::Document.open(download)
end

def download_file_name
File.basename(download)
end
Expand Down
38 changes: 38 additions & 0 deletions spec/system/court_dates/view_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require "rails_helper"

RSpec.describe "court_dates/edit", type: :system do
context "with date"
let(:now) { Date.new(2021, 1, 1) }
let(:organization) { create(:casa_org) }
let(:admin) { create(:casa_admin, casa_org: organization) }
let(:volunteer) { create(:volunteer) }
let(:supervisor) { create(:casa_admin, casa_org: organization) }
let!(:casa_case) { create(:casa_case, casa_org: organization) }
let!(:court_date) { create(:court_date, :with_court_details, casa_case: casa_case, date: now - 1.week) }
let!(:future_court_date) { create(:court_date, :with_court_details, casa_case: casa_case, date: now + 1.week) }

before do
travel_to now
end

context "as a volunteer" do
it "can download a report which focuses on the court date", js: true do
volunteer.casa_cases = [casa_case]
sign_in volunteer

visit root_path
click_on "Cases"
click_on casa_case.case_number

expect(CourtDate.count).to eq 2
click_on "January 8, 2021"
expect(page).to have_content "Court Date"

click_on "Download Report"

wait_for_download

expect(download_docx.paragraphs.map(&:to_s)).to include("Hearing Date: January 8, 2021")
end
end
end

0 comments on commit c67f7cb

Please sign in to comment.