refactor(captain): simplify audience param permitting and engagement check

Drops the unreachable permitted[:config] fallback in the assistants
controller and the redundant respond_to? guard in the hook execution
service's captain_should_engage?.
This commit is contained in:
Pranav
2026-07-14 17:34:26 -07:00
parent 71ba10d0fa
commit bc79897419
2 changed files with 5 additions and 10 deletions
@@ -112,19 +112,18 @@ class Api::V1::Accounts::Captain::AssistantsController < Api::V1::Accounts::Base
permitted[:guardrails] = params[:assistant][:guardrails] if params[:assistant].key?(:guardrails)
# The audience is a recursive condition tree that strong params can't whitelist by shape;
# route it through separately. Validity is enforced by Captain::AudienceValidator.
permit_audience_config(permitted)
permitted
end
# The audience is a recursive condition tree that strong params can't whitelist by shape;
# pass it through raw and let Captain::AudienceValidator enforce validity.
def permit_audience_config(permitted)
config = params[:assistant][:config]
return unless config.respond_to?(:key?) && config.key?(:audience)
return unless config.try(:key?, :audience)
audience = config[:audience]
permitted[:config] ||= ActionController::Parameters.new.permit!
permitted[:config][:audience] = audience.respond_to?(:permit!) ? audience.permit!.to_h : audience
end
@@ -79,12 +79,8 @@ module Enterprise::MessageTemplates::HookExecutionService
conversation.pending? && captain_should_engage?
end
# True only when the inbox has a Captain assistant that should engage this conversation now —
# i.e. the contact is within the audience AND the assistant is on-schedule. Otherwise the
# conversation falls back to normal human handling (greeting/OOO templates fire, Captain silent).
def captain_should_engage?
inbox.respond_to?(:captain_assistant) &&
inbox.captain_assistant.present? &&
inbox.captain_assistant.engages?(conversation.contact, conversation)
assistant = inbox.captain_assistant
assistant.present? && assistant.engages?(conversation.contact, conversation)
end
end