## Linear ticket - https://linear.app/chatwoot/issue/CW-7374/agent-declined-voice-calls-miscounted-as-failed ## Description Agent rejections were stored with status: failed, so call reports lumped deliberate declines together with real technical failure. Fix: give declines their own terminal rejected status (Twilio + WhatsApp paths), keeping end_reason: agent_rejected. Genuine provider/network failures stay failed. Frontend renders declines exactly as before; existing rows backfilled via migration. ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) ## How Has This Been Tested? - Tested on UI ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Sony Mathew <sony@chatwoot.com>
83 lines
2.7 KiB
Ruby
83 lines
2.7 KiB
Ruby
class Api::V1::Accounts::ConferenceController < Api::V1::Accounts::BaseController
|
|
before_action :set_voice_inbox_for_conference
|
|
rescue_from CustomExceptions::CallAlreadyAccepted, with: :render_call_already_accepted
|
|
|
|
def token
|
|
render json: Voice::Provider::Twilio::TokenService.new(
|
|
inbox: @voice_inbox,
|
|
user: Current.user,
|
|
account: Current.account
|
|
).generate
|
|
end
|
|
|
|
def create
|
|
call = resolve_call!
|
|
|
|
conference_service = Voice::Provider::Twilio::ConferenceService.new(call: call)
|
|
conference_sid = conference_service.ensure_conference_sid
|
|
conference_service.mark_agent_joined(user: current_user)
|
|
|
|
render json: {
|
|
status: 'success',
|
|
id: call.conversation.display_id,
|
|
conference_sid: conference_sid,
|
|
using_webrtc: true
|
|
}
|
|
end
|
|
|
|
def destroy
|
|
call = resolve_call!
|
|
rejecting = agent_rejecting_before_pickup?(call)
|
|
# Tear down provider side first so a teardown failure leaves the call repairable.
|
|
Voice::Provider::Twilio::ConferenceService.new(call: call).end_conference
|
|
finalize_as_agent_reject!(call) if rejecting
|
|
render json: { status: 'success', id: call.conversation.display_id }
|
|
end
|
|
|
|
private
|
|
|
|
def resolve_call!
|
|
sid = params[:call_sid].presence
|
|
raise ActionController::ParameterMissing, :call_sid if sid.blank?
|
|
|
|
conversation = fetch_conversation_by_display_id
|
|
Call.where(inbox_id: @voice_inbox.id, provider: :twilio, conversation_id: conversation.id)
|
|
.find_by!(provider_call_id: sid)
|
|
end
|
|
|
|
def set_voice_inbox_for_conference
|
|
@voice_inbox = Current.account.inboxes.find(params[:inbox_id])
|
|
authorize @voice_inbox, :show?
|
|
end
|
|
|
|
def fetch_conversation_by_display_id
|
|
cid = params[:conversation_id]
|
|
raise ActiveRecord::RecordNotFound, 'conversation_id required' if cid.blank?
|
|
|
|
conversation = @voice_inbox.conversations.find_by!(display_id: cid)
|
|
authorize conversation, :show?
|
|
conversation
|
|
end
|
|
|
|
def render_call_already_accepted(error)
|
|
render json: { error: error.message }, status: :conflict
|
|
end
|
|
|
|
# A hangup before pickup is treated as an agent rejection, matching WhatsApp.
|
|
def agent_rejecting_before_pickup?(call)
|
|
call.ringing? && call.accepted_by_agent_id.nil?
|
|
end
|
|
|
|
def finalize_as_agent_reject!(call)
|
|
# Re-check under a row lock: a webhook may have accepted/completed the call
|
|
# while end_conference was in flight, so don't force agent_rejected on stale state.
|
|
rejected = call.with_lock do
|
|
next false unless agent_rejecting_before_pickup?(call)
|
|
|
|
call.update!(status: 'rejected', end_reason: 'agent_rejected', accepted_by_agent_id: Current.user.id)
|
|
true
|
|
end
|
|
Voice::CallMessageBuilder.new(call).update_status!(status: 'rejected', agent: Current.user) if rejected
|
|
end
|
|
end
|