fix(voice): scope inbound Call lookup and handle create races

- Scope the existing-call pre-check to the inbound account + inbox.
- Rescue ActiveRecord::RecordNotUnique so a concurrent Twilio retry
  doesn't 500 when two webhooks race to create the same Call.
This commit is contained in:
Muhsin
2026-04-17 22:45:39 +04:00
parent 4282dec7d6
commit 91da28a329
@@ -13,7 +13,7 @@ class Voice::InboundCallBuilder
end
def perform!
existing = Call.find_by(provider: :twilio, provider_call_id: call_sid)
existing = find_existing_call
return existing if existing
ActiveRecord::Base.transaction do
@@ -25,10 +25,18 @@ class Voice::InboundCallBuilder
call.update!(message_id: message.id)
call
end
rescue ActiveRecord::RecordNotUnique
# A concurrent Twilio retry won the create race; return what now exists.
find_existing_call || raise
end
private
def find_existing_call
Call.where(account_id: account.id, inbox_id: inbox.id)
.find_by(provider: :twilio, provider_call_id: call_sid)
end
def ensure_contact!
account.contacts.find_or_create_by!(phone_number: from_number) do |record|
record.name = from_number if record.name.blank?