diff --git a/app/decorators/contact_type_decorator.rb b/app/decorators/contact_type_decorator.rb index 808a90d5d0..d5ad16232a 100644 --- a/app/decorators/contact_type_decorator.rb +++ b/app/decorators/contact_type_decorator.rb @@ -15,4 +15,9 @@ def last_time_used_with_cases(casa_case_ids) last_contact.nil? ? "never" : "#{time_ago_in_words(last_contact.occurred_at)} ago" end + + def last_contact_timestamp(casa_case_ids) + last_contact = CaseContact.joins(:contact_types).where(casa_case_id: casa_case_ids, contact_types: {id: object.id}).order(occurred_at: :desc).first + last_contact&.occurred_at || Time.at(0) + end end diff --git a/app/models/case_contact.rb b/app/models/case_contact.rb index 213c55d1be..2bc85d759d 100644 --- a/app/models/case_contact.rb +++ b/app/models/case_contact.rb @@ -18,6 +18,7 @@ class CaseContact < ApplicationRecord validate :reimbursement_only_when_miles_driven, if: :active_or_expenses? validate :volunteer_address_when_reimbursement_wanted, if: :active_or_expenses? validate :volunteer_address_is_valid, if: :active_or_expenses? + validates :contact_type_ids, presence: true, if: :active_or_details? belongs_to :creator, class_name: "User" has_one :supervisor_volunteer, -> { diff --git a/app/views/case_contacts/form/_contact_types.html.erb b/app/views/case_contacts/form/_contact_types.html.erb index 1f91e5f1d1..8365bbf83f 100644 --- a/app/views/case_contacts/form/_contact_types.html.erb +++ b/app/views/case_contacts/form/_contact_types.html.erb @@ -3,7 +3,7 @@ <%= render(Form::MultipleSelectComponent.new( form: form, name: :contact_type_ids, - options: options.decorate.map { |ct| ct.hash_for_multi_select_with_cases(casa_cases&.pluck(:id)) }, + options: options.decorate.sort_by { |ct| ct.last_contact_timestamp(casa_cases.pluck(:id)) }.reverse.map { |ct| ct.hash_for_multi_select_with_cases(casa_cases.pluck(:id)) }, selected_items: selected_items, render_option_subtext: true )) %> diff --git a/app/views/case_contacts/form/details.html.erb b/app/views/case_contacts/form/details.html.erb index 049fbd6d53..aeee3956fb 100644 --- a/app/views/case_contacts/form/details.html.erb +++ b/app/views/case_contacts/form/details.html.erb @@ -19,9 +19,9 @@