Skip to content

Commit

Permalink
feat: add topics to wizard controller
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticspoon committed Feb 19, 2024
1 parent fbbbb5a commit 291614c
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 7 deletions.
4 changes: 4 additions & 0 deletions app/controllers/case_contacts/form_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def create_additional_case_contacts(case_contact)
other_expenses_describe: ae.other_expenses_describe
)
end
case_contact.contact_topic_answers.each do |cta|
new_case_contact.contact_topic_answers << cta.dup
end

new_case_contact.save!
end
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/case_contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def new
end

@case_contact = CaseContact.create(creator: current_user, draft_case_ids: draft_case_ids)
@case_contact.create_contact_topic_answers!(current_organization) # NOTE: Should this be a callback?
redirect_to case_contact_form_path(CaseContact::FORM_STEPS.first, case_contact_id: @case_contact.id)
end

Expand Down
3 changes: 2 additions & 1 deletion app/values/case_contact_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def initialize(params)
:volunteer_address,
draft_case_ids: [],
case_contact_contact_type_attributes: [:contact_type_id],
additional_expenses_attributes: [:id, :other_expense_amount, :other_expenses_describe]
additional_expenses_attributes: %i[id other_expense_amount other_expenses_describe],
contact_topic_answers_attributes: %i[id value selected]
)
if params.dig(:case_contact, :duration_minutes)
new_params[:duration_minutes] = convert_duration_minutes(params)
Expand Down
98 changes: 93 additions & 5 deletions spec/requests/case_contacts/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@
expect(page).not_to include(*contact_types_b.pluck(:name))
end
end

# TODO: Test that contact topics are shown
end
end

describe "PATCH /update" do
let!(:casa_case) { create(:casa_case, casa_org: organization) }
let!(:case_contact) { create(:case_contact, :details_status, casa_case:) }
let(:advance_form) { true }
let(:params) { {case_contact: attributes} }

Expand All @@ -72,7 +75,8 @@
end

context "submitting details step" do
let!(:case_contact) { create(:case_contact, :started_status, creator: creator) }
let!(:case_contact) { create(:case_contact, :started_status, creator: creator, contact_topic_answers: topic_answers) }
let(:topic_answers) { build_list(:contact_topic_answer, 3) }
let(:step) { :details }
let!(:contact_type_group_b) { create(:contact_type_group, casa_org: organization, name: "B") }
let!(:contact_types_b) do
Expand All @@ -98,7 +102,8 @@
duration_minutes: 50,
contact_made: true,
medium_type: CaseContact::CONTACT_MEDIUMS.second,
case_contact_contact_type_attributes: contact_type_attributes
case_contact_contact_type_attributes: contact_type_attributes,
contact_topic_answers_attributes: topic_answers_attributes
}
end
let(:contact_type_attributes) do
Expand All @@ -108,6 +113,14 @@
}
end

let(:topic_answers_attributes) do
{
"0" => {id: topic_answers.first.id, value: "test", selected: true},
"1" => {id: topic_answers.second.id, value: "test", selected: true},
"2" => {id: topic_answers.third.id, value: "test", selected: true}
}
end

it "with valid attributes updates the requested case_contact" do
request
case_contact.reload
Expand All @@ -117,6 +130,14 @@
expect(case_contact.medium_type).to eq(CaseContact::CONTACT_MEDIUMS.second)
end

it "updates only answer field for contact topics" do
request
case_contact.reload

expect(case_contact.contact_topic_answers.pluck(:value)).to be_all "test"
expect(case_contact.contact_topic_answers.pluck(:selected)).to be_all true
end

context "contact types" do
it "attaches contact types" do
request
Expand Down Expand Up @@ -163,6 +184,52 @@
end
end

context "submitting notes step: contact topics" do
let!(:case_contact) { create(:case_contact, :details_status, creator: creator, contact_topic_answers: topic_answers) }
let(:topic_answers) { build_list(:contact_topic_answer, 3) }
let(:topic_answers_attributes) do
{
"0" => {id: topic_answers.first.id, value: "test", selected: true},
"1" => {id: topic_answers.second.id, value: "test", selected: true},
"2" => {id: topic_answers.third.id, value: "test", selected: true}
}
end
let(:step) { :notes }
let(:attributes) do
{contact_topic_answers_attributes: topic_answers_attributes}
end

context "with valid contact topic answers" do
context "when submitting via button" do
it "updates the requested case_contact" do
request
case_contact.reload

expect(case_contact.contact_topic_answers.pluck(:value)).to be_all "test"
expect(case_contact.contact_topic_answers.pluck(:selected)).to be_all true
end
end

context "when autosaving" do
subject(:request) do
patch "/case_contacts/#{case_contact.id}/form/#{step}", params:, as: :json

response
end

it "updates the requested case_contact" do
request
case_contact.reload

expect(case_contact.contact_topic_answers.pluck(:value)).to be_all "test"
expect(case_contact.contact_topic_answers.pluck(:selected)).to be_all true
end

it { is_expected.to have_http_status(:success) }
end
end
end

context "submitting notes step" do
let!(:case_contact) { create(:case_contact, :details_status, creator: creator) }
let(:step) { :notes }
Expand Down Expand Up @@ -215,7 +282,16 @@
end

context "submitting expenses step" do
let!(:case_contact) { create(:case_contact, :notes_status, draft_case_ids: [casa_case.id], creator: creator) }
let!(:case_contact) { create(:case_contact, :notes_status, draft_case_ids: [casa_case.id], creator: creator, contact_topic_answers: topic_answers) }
let(:case_contact_topics) { build_list(:contact_topic_answer, 3) }
let(:topic_answers) { build_list(:contact_topic_answer, 3) }
let(:topic_answers_attributes) do
{
"0" => {id: topic_answers.first.id, value: "test", selected: true},
"1" => {id: topic_answers.second.id, value: "test", selected: true},
"2" => {id: topic_answers.third.id, value: "test", selected: true}
}
end
let(:additional_expenses) do
{
"0" => {other_expense_amount: 50, other_expenses_describe: "meal"},
Expand All @@ -230,7 +306,8 @@
want_driving_reimbursement: true,
miles_driven: 60,
volunteer_address: "123 str",
additional_expenses_attributes: additional_expenses
additional_expenses_attributes: additional_expenses,
contact_topic_answers_attributes: topic_answers_attributes
}
end

Expand All @@ -245,6 +322,8 @@
expect(case_contact.additional_expenses.first.other_expenses_describe).to eq "meal"
expect(case_contact.additional_expenses.last.other_expense_amount).to eq 100
expect(case_contact.additional_expenses.last.other_expenses_describe).to eq "hotel"
expect(case_contact.contact_topic_answers.pluck(:value)).to be_all "test"
expect(case_contact.contact_topic_answers.pluck(:selected)).to be_all true
end

it "sets the case_contact's status to active" do
Expand Down Expand Up @@ -288,7 +367,10 @@

context "with multiple cases selected" do
let!(:other_casa_case) { create(:casa_case, casa_org: organization) }
let!(:case_contact) { create(:case_contact, :notes_status, draft_case_ids: [casa_case.id, other_casa_case.id], creator: admin) }
let!(:case_contact) {
create(:case_contact, :notes_status, draft_case_ids: [casa_case.id, other_casa_case.id],
creator: admin, contact_topic_answers: topic_answers)
}

it "creates a copy of the draft for each case" do
expect {
Expand All @@ -299,6 +381,12 @@
expect(CaseContact.last.status).to eq "active"
end

it "sets contact_topics for all cases" do
expect { request }.to change(ContactTopicAnswer, :count).by(3)
expect(CaseContact.last.contact_topic_answers.pluck(:value)).to be_all("test")
expect(CaseContact.last.contact_topic_answers.pluck(:selected)).to be_all(true)
end

it "sets the draft_case_ids of the draft to only the first case" do
expect(case_contact.draft_case_ids.count).to eq 2
request
Expand Down
11 changes: 11 additions & 0 deletions spec/requests/case_contacts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@
request
}.to change(CaseContact, :count).by(1)
end

context "when current org has contact topics" do
let(:contact_topics) { build_list(:contact_topic, 3) }
let(:organization) { create(:casa_org, contact_topics:) }

it "should set empty contact topic answers for new case contact to org topics" do
expect { request }.to change(ContactTopicAnswer, :count).by(3)
expect(CaseContact.last.contact_topic_answers.count).to eq(3)
expect(CaseContact.last.contact_topic_answers.pluck(:value)).to be_all(nil)
end
end
end

describe "GET /edit" do
Expand Down
11 changes: 10 additions & 1 deletion spec/values/case_contact_parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

RSpec.describe CaseContactParameters do
subject { described_class.new(params) }

let(:params) {
ActionController::Parameters.new(
case_contact: ActionController::Parameters.new(
Expand All @@ -13,10 +14,15 @@
miles_driven: "123",
want_driving_reimbursement: "want_driving_reimbursement",
notes: "notes",
case_contact_contact_type_attributes: [:contact_type_id]
case_contact_contact_type_attributes: [:contact_type_id],
contact_topic_answers_attributes:
)
)
}
let(:contact_topic_answers_attributes) do
{"0" => {"id" => 1, "value" => "test",
"question" => "question", "selected" => true}}
end

it "returns data" do
expect(subject["duration_minutes"]).to eq(62)
Expand All @@ -27,5 +33,8 @@
expect(subject["want_driving_reimbursement"]).to eq("want_driving_reimbursement")
expect(subject["notes"]).to eq("notes")
expect(subject["case_contact_contact_type_attributes"]).to eq([])

expected_attrs = contact_topic_answers_attributes["0"].except("question")
expect(subject["contact_topic_answers_attributes"]["0"].to_h).to eq(expected_attrs)
end
end

0 comments on commit 291614c

Please sign in to comment.