Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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: 1 addition & 1 deletion app/controllers/contact_topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ def set_contact_topic

# Only allow a list of trusted parameters through.
def contact_topic_params
params.require(:contact_topic).permit(:casa_org_id, :question, :details, :active)
params.require(:contact_topic).permit(:casa_org_id, :question, :details, :active, :exclude_from_court_report)
end
end
1 change: 1 addition & 0 deletions app/models/case_court_report_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def org_address(path_to_template)
def court_topics
topics = ContactTopic
.joins(contact_topic_answers: {case_contact: [:casa_case, :contact_types]}).distinct
.where(exclude_from_court_report: false)
.where("casa_cases.id": @casa_case.id)
.where("case_contacts.occurred_at": @date_range)
.order(:occurred_at, :value)
Expand Down
17 changes: 9 additions & 8 deletions app/models/contact_topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ def default_contact_topics
#
# Table name: contact_topics
#
# id :bigint not null, primary key
# active :boolean default(TRUE), not null
# details :text
# question :string
# soft_delete :boolean default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
# casa_org_id :bigint not null
# id :bigint not null, primary key
# active :boolean default(TRUE), not null
# details :text
# exclude_from_court_report :boolean default(FALSE), not null
# question :string
# soft_delete :boolean default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
# casa_org_id :bigint not null
#
# Foreign Keys
#
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def no_attempt_for_two_weeks
if volunteer.supervisor.active? &&
volunteer.case_assignments.any? { |assignment| assignment.active? } &&
(volunteer.case_contacts.none? ||
volunteer.case_contacts.maximum(:created_at) < (14.days.ago))
volunteer.case_contacts.maximum(:created_at) < 14.days.ago)

no_attempt_count += 1
end
Expand Down
4 changes: 4 additions & 0 deletions app/views/contact_topics/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<%= form.check_box :active, class: 'form-check-input' %>
<%= form.label :active, "Active?", class: 'form-check-label' %>
</div>
<div class="form-check checkbox-style mb-20">
<%= form.check_box :exclude_from_court_report, class: 'form-check-input' %>
<%= form.label :exclude_from_court_report, "Exclude from Court Report?", class: 'form-check-label' %>
</div>
<div class="actions mb-10">
<%= button_tag(type: "submit", class: "btn-sm main-btn primary-btn btn-hover") do %>
<i class="lni lni-checkmark-circle mr-5"></i> Submit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddExcludeFromCourtReportToContactTopics < ActiveRecord::Migration[7.2]
def change
add_column :contact_topics, :exclude_from_court_report, :boolean, default: false, null: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2025_05_28_092341) do
ActiveRecord::Schema[7.2].define(version: 2025_07_02_142004) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -270,6 +270,7 @@
t.string "question"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "exclude_from_court_report", default: false, null: false
end

create_table "contact_type_groups", force: :cascade do |t|
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/no_contact_made_reminder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_contact_types_in_past_2_weeks(volunteer, contact_made)
def valid_past_reminders(volunteer)
reminder = UserReminderTime.find_by(user_id: volunteer.id)

if reminder&.case_contact_types && reminder.case_contact_types.today?
if reminder&.case_contact_types&.today?
return false
end

Expand Down
121 changes: 89 additions & 32 deletions spec/models/case_court_report_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@
let(:org) { create(:casa_org) }
let(:casa_case) { create(:casa_case, casa_org: org) }
let(:topics) { [1, 2, 3].map { |i| create(:contact_topic, casa_org: org, question: "Question #{i}", details: "Details #{i}") } }
let(:hidden_topic) do
create(:contact_topic, casa_org: org, question: "Dont Show Me", details: "I Shall Not Appear On Court Reports")
end
let(:contacts) do
[1, 2, 3, 4].map do |i|
create(:case_contact,
Expand All @@ -193,23 +196,9 @@
])
end
end
# let(:contacts) { create_list(:case_contact, 4, casa_case: casa_case, occurred_at: 1.month.ago) }

context "when given data" do
# Add some values that should get filtered out
before do
contact_one = create(:case_contact, casa_case: casa_case, medium_type: "in-person", occurred_at: 1.day.ago)
create_list(:contact_topic_answer, 2, case_contact: contact_one, contact_topic: topics[0], value: "Not included")

contact_two = create(:case_contact, casa_case: casa_case, medium_type: "in-person", occurred_at: 50.day.ago)
create_list(:contact_topic_answer, 2, case_contact: contact_two, contact_topic: topics[0], value: "Not included")

other_case = create(:casa_case, casa_org: org)
contact_three = create(:case_contact, casa_case: other_case, medium_type: "in-person", occurred_at: 50.day.ago)
create_list(:contact_topic_answer, 2, case_contact: contact_three, contact_topic: topics[0], value: "Not included")
end

it "generates correctly shaped data" do
# Contact 1 Answers
create(:contact_topic_answer, case_contact: contacts[0], contact_topic: topics[0], value: "Answer 1")
create(:contact_topic_answer, case_contact: contacts[0], contact_topic: topics[1], value: "Answer 2")
Expand All @@ -222,27 +211,95 @@
create(:contact_topic_answer, case_contact: contacts[2], contact_topic: topics[1], value: "Answer 5")
create(:contact_topic_answer, case_contact: contacts[2], contact_topic: topics[2], value: "")

# Contact 4 Answers
# No Answers

expected_topics = {
"Question 1" => {topic: "Question 1", details: "Details 1", answers: [
{date: "12/02/20", medium: "Type A1, Type B1", value: "Answer 1"},
{date: "12/03/20", medium: "Type A2, Type B2", value: "Answer 3"}
]},
"Question 2" => {topic: "Question 2", details: "Details 2", answers: [
{date: "12/02/20", medium: "Type A1, Type B1", value: "Answer 2"},
{date: "12/04/20", medium: "Type A3, Type B3", value: "Answer 5"}
]},
"Question 3" => {topic: "Question 3", details: "Details 3", answers: [
{date: "12/03/20", medium: "Type A2, Type B2", value: "No Answer Provided"},
{date: "12/04/20", medium: "Type A3, Type B3", value: "No Answer Provided"}
]}
# Contacts that will be filtered
one_day_ago_contact = create(:case_contact, casa_case: casa_case, medium_type: "in-person", occurred_at: 1.day.ago)
create_list(:contact_topic_answer, 2, case_contact: one_day_ago_contact, contact_topic: topics[0], value: "Answer From One Day Ago")

one_year_ago_contact = create(:case_contact, casa_case: casa_case, medium_type: "in-person", occurred_at: 1.year.ago)
create_list(:contact_topic_answer, 2, case_contact: one_year_ago_contact, contact_topic: topics[0], value: "Answer From One Year Ago")

other_case = create(:casa_case, casa_org: org)
other_case_contact = create(:case_contact, casa_case: other_case, medium_type: "in-person", occurred_at: 1.month.ago)
create_list(:contact_topic_answer, 2, case_contact: other_case_contact, contact_topic: topics[0], value: "Answer From Another Case")
end

let(:related_topics_hash) do
{
"Question 1" => {
answers: [
{date: "01/01/20", medium: "Type 2, Type 2", value: "Answer From One Year Ago"},
{date: "12/02/20", medium: "Type A1, Type B1", value: "Answer 1"},
{date: "12/03/20", medium: "Type A2, Type B2", value: "Answer 3"},
{date: "12/31/20", medium: "Type 1, Type 1", value: "Answer From One Day Ago"}
],
topic: "Question 1",
details: "Details 1"
},
"Question 2" => {
answers: [
{date: "12/02/20", medium: "Type A1, Type B1", value: "Answer 2"},
{date: "12/04/20", medium: "Type A3, Type B3", value: "Answer 5"}
],
topic: "Question 2",
details: "Details 2"
},
"Question 3" => {
answers: [
{date: "12/03/20", medium: "Type A2, Type B2", value: "No Answer Provided"},
{date: "12/04/20", medium: "Type A3, Type B3", value: "No Annilswer Provided"}
],
topic: "Question 3",
details: "Details 3"
}
}
end

court_report_context = build(:case_court_report_context, start_date: 45.day.ago.to_s, end_date: 5.day.ago.to_s, casa_case: casa_case)
it "returns a hash of topics with the correct shape" do
court_topics = build(:case_court_report_context, casa_case: casa_case).court_topics

expect(court_topics).to be_a(Hash)

expect(court_topics.keys).to all(a_kind_of(String))
expect(court_topics.values).to all(
a_hash_including(
topic: a_kind_of(String),
details: a_kind_of(String),
answers: all(
a_hash_including(
date: a_string_matching(/\d{2}\/\d{2}\/\d{2}/),
medium: a_kind_of(String),
value: a_kind_of(String)
)
)
)
)
end

it "returns topics related to the case" do
court_topics = build(:case_court_report_context, casa_case: casa_case).court_topics

expect(court_topics.keys).to match_array(["Question 1", "Question 2", "Question 3"])
expect(court_topics["Question 1"][:answers].map { |a| a[:value] }).to match_array(
["Answer From One Year Ago", "Answer 1", "Answer 3", "Answer From One Day Ago"]
)
expect(court_topics["Question 2"][:answers].map { |a| a[:value] }).to match_array(["Answer 2", "Answer 5"])
expect(court_topics["Question 3"][:answers].map { |a| a[:value] }).to match_array(["No Answer Provided", "No Answer Provided"])
end

it "filters by date range" do
court_topics = build(:case_court_report_context, start_date: 45.day.ago.to_s, end_date: 5.day.ago.to_s, casa_case: casa_case).court_topics

expect(court_topics.keys).to match_array(["Question 1", "Question 2", "Question 3"])
expect(court_topics["Question 1"][:answers].map { |a| a[:value] }).to match_array(["Answer 1", "Answer 3"])
end

it "filters answers from topics set be excluded from court report" do
Copy link
Collaborator

Choose a reason for hiding this comment

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

yay tests :)

topics[0].update(exclude_from_court_report: true)

court_topics = build(:case_court_report_context, casa_case: casa_case).court_topics

expect(court_report_context.court_topics).to eq(expected_topics)
expect(court_topics.keys).not_to include("Question 1")
expect(court_topics.keys).to include("Question 2", "Question 3")
end
end

Expand Down
19 changes: 19 additions & 0 deletions spec/requests/contact_topics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@
post contact_topics_url, params: {contact_topic: attributes}
expect(response).to redirect_to(edit_casa_org_path(casa_org))
end

it "can set exclude_from_court_report attribute" do
Copy link
Collaborator

Choose a reason for hiding this comment

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

yay tests :)

attributes[:exclude_from_court_report] = true

expect do
post contact_topics_url, params: {contact_topic: attributes}
end.to change(ContactTopic, :count).by(1)

topic = ContactTopic.last
expect(topic.exclude_from_court_report).to be true
end
end

context "with invalid parameters" do
Expand Down Expand Up @@ -107,6 +118,14 @@
patch contact_topic_url(contact_topic), params: {contact_topic: new_attributes}
expect(response).to redirect_to(edit_casa_org_path(casa_org))
end

it "can change exclude_from_court_report" do
Copy link
Collaborator

Choose a reason for hiding this comment

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

yay tests :)

new_attributes = {exclude_from_court_report: true}

expect {
patch contact_topic_url(contact_topic), params: {contact_topic: new_attributes}
}.to change { contact_topic.reload.exclude_from_court_report }.from(false).to(true)
end
end

context "with invalid parameters" do
Expand Down