chore: Sync pre-chat fields after custom attribute update (#4692)

This commit is contained in:
Muhsin Keloth
2022-06-20 14:16:49 +05:30
committed by GitHub
parent 6c6df8661b
commit a8c6cd729b
4 changed files with 57 additions and 1 deletions

View File

@@ -0,0 +1,21 @@
class Inboxes::UpdateWidgetPreChatCustomFieldsJob < ApplicationJob
queue_as :default
def perform(account, custom_attribute)
attribute_key = custom_attribute['attribute_key']
account.web_widgets.all.find_each do |web_widget|
pre_chat_fields = web_widget.pre_chat_form_options['pre_chat_fields']
pre_chat_fields.each_with_index do |pre_chat_field, index|
next unless pre_chat_field['name'] == attribute_key
web_widget.pre_chat_form_options['pre_chat_fields'][index] =
pre_chat_field.deep_merge({
'label' => custom_attribute['attribute_display_name'],
'placeholder' => custom_attribute['attribute_display_name'],
'values' => custom_attribute['attribute_values']
})
end
web_widget.save!
end
end
end

View File

@@ -34,6 +34,7 @@ class CustomAttributeDefinition < ApplicationRecord
enum attribute_display_type: { text: 0, number: 1, currency: 2, percent: 3, link: 4, date: 5, list: 6, checkbox: 7 }
belongs_to :account
after_update :update_widget_pre_chat_custom_fields
after_destroy :sync_widget_pre_chat_custom_fields
private
@@ -41,4 +42,8 @@ class CustomAttributeDefinition < ApplicationRecord
def sync_widget_pre_chat_custom_fields
::Inboxes::SyncWidgetPreChatCustomFieldsJob.perform_now(account, attribute_key)
end
def update_widget_pre_chat_custom_fields
::Inboxes::UpdateWidgetPreChatCustomFieldsJob.perform_now(account, self)
end
end