Some checks failed
Lock Threads / action (push) Has been cancelled
Mark stale issues and pull requests / stale (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot EE docker images / merge (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot CE docker images / merge (push) Has been cancelled
Run Chatwoot CE spec / lint-backend (push) Has been cancelled
Run Chatwoot CE spec / lint-frontend (push) Has been cancelled
Run Chatwoot CE spec / frontend-tests (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (0, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (1, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (10, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (11, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (12, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (13, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (14, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (15, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (2, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (3, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (4, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (5, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (6, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (7, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (8, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (9, 16) (push) Has been cancelled
Run Linux nightly installer / nightly (push) Has been cancelled
- Add Logistics component with progress tracking - Add OrderDetail component for order information - Support data-driven steps and actions - Add blue color scale to widget SCSS - Fix node overflow and progress bar rendering issues - Add English translations for dashboard components Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
111 lines
3.6 KiB
Ruby
111 lines
3.6 KiB
Ruby
# This Builder will create a contact and contact inbox with specified attributes.
|
|
# If an existing identified contact exisits, it will be returned.
|
|
# for contact inbox logic it uses the contact inbox builder
|
|
|
|
class ContactInboxWithContactBuilder
|
|
pattr_initialize [:inbox!, :contact_attributes!, :source_id, :hmac_verified]
|
|
|
|
def perform
|
|
find_or_create_contact_and_contact_inbox
|
|
# in case of race conditions where contact is created by another thread
|
|
# we will try to find the contact and create a contact inbox
|
|
rescue ActiveRecord::RecordNotUnique
|
|
find_or_create_contact_and_contact_inbox
|
|
end
|
|
|
|
def find_or_create_contact_and_contact_inbox
|
|
@contact_inbox = inbox.contact_inboxes.find_by(source_id: source_id) if source_id.present?
|
|
return @contact_inbox if @contact_inbox
|
|
|
|
ActiveRecord::Base.transaction(requires_new: true) do
|
|
build_contact_with_contact_inbox
|
|
end
|
|
update_contact_avatar(@contact) unless @contact.avatar.attached?
|
|
@contact_inbox
|
|
end
|
|
|
|
private
|
|
|
|
def build_contact_with_contact_inbox
|
|
@contact = find_contact || create_contact
|
|
@contact_inbox = create_contact_inbox
|
|
end
|
|
|
|
def account
|
|
@account ||= inbox.account
|
|
end
|
|
|
|
def create_contact_inbox
|
|
ContactInboxBuilder.new(
|
|
contact: @contact,
|
|
inbox: @inbox,
|
|
source_id: @source_id,
|
|
hmac_verified: hmac_verified
|
|
).perform
|
|
end
|
|
|
|
def update_contact_avatar(contact)
|
|
::Avatar::AvatarFromUrlJob.perform_later(contact, contact_attributes[:avatar_url]) if contact_attributes[:avatar_url]
|
|
end
|
|
|
|
def create_contact
|
|
account.contacts.create!(
|
|
name: contact_attributes[:name] || ::Haikunator.haikunate(1000),
|
|
phone_number: contact_attributes[:phone_number],
|
|
email: contact_attributes[:email],
|
|
identifier: contact_attributes[:identifier],
|
|
additional_attributes: contact_attributes[:additional_attributes],
|
|
custom_attributes: contact_attributes[:custom_attributes]
|
|
)
|
|
end
|
|
|
|
def find_contact
|
|
contact = find_contact_by_identifier(contact_attributes[:identifier])
|
|
contact ||= find_contact_by_email(contact_attributes[:email])
|
|
contact ||= find_contact_by_phone_number(contact_attributes[:phone_number])
|
|
contact ||= find_contact_by_instagram_source_id(source_id) if instagram_channel?
|
|
|
|
contact
|
|
end
|
|
|
|
def instagram_channel?
|
|
inbox.channel_type == 'Channel::Instagram'
|
|
end
|
|
|
|
# There might be existing contact_inboxes created through Channel::FacebookPage
|
|
# with the same Instagram source_id. New Instagram interactions should create fresh contact_inboxes
|
|
# while still reusing contacts if found in Facebook channels so that we can create
|
|
# new conversations with the same contact.
|
|
def find_contact_by_instagram_source_id(instagram_id)
|
|
return if instagram_id.blank?
|
|
|
|
existing_contact_inbox = ContactInbox.joins(:inbox)
|
|
.where(source_id: instagram_id)
|
|
.where(
|
|
'inboxes.channel_type = ? AND inboxes.account_id = ?',
|
|
'Channel::FacebookPage',
|
|
account.id
|
|
).first
|
|
|
|
existing_contact_inbox&.contact
|
|
end
|
|
|
|
def find_contact_by_identifier(identifier)
|
|
return if identifier.blank?
|
|
|
|
account.contacts.find_by(identifier: identifier)
|
|
end
|
|
|
|
def find_contact_by_email(email)
|
|
return if email.blank?
|
|
|
|
account.contacts.from_email(email)
|
|
end
|
|
|
|
def find_contact_by_phone_number(phone_number)
|
|
return if phone_number.blank?
|
|
|
|
account.contacts.find_by(phone_number: phone_number)
|
|
end
|
|
end
|