From a8c6cd729b51dfee644da9a8bc1516c53d6f9ea1 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Mon, 20 Jun 2022 14:16:49 +0530 Subject: [PATCH] chore: Sync pre-chat fields after custom attribute update (#4692) --- ...pdate_widget_pre_chat_custom_fields_job.rb | 21 +++++++++++++ app/models/custom_attribute_definition.rb | 5 ++++ ..._widget_pre_chat_custom_fields_job_spec.rb | 2 +- ..._widget_pre_chat_custom_fields_job_spec.rb | 30 +++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 app/jobs/inboxes/update_widget_pre_chat_custom_fields_job.rb create mode 100644 spec/jobs/inboxes/update_widget_pre_chat_custom_fields_job_spec.rb diff --git a/app/jobs/inboxes/update_widget_pre_chat_custom_fields_job.rb b/app/jobs/inboxes/update_widget_pre_chat_custom_fields_job.rb new file mode 100644 index 000000000..b17ecdf55 --- /dev/null +++ b/app/jobs/inboxes/update_widget_pre_chat_custom_fields_job.rb @@ -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 diff --git a/app/models/custom_attribute_definition.rb b/app/models/custom_attribute_definition.rb index fa0370375..59a314da6 100644 --- a/app/models/custom_attribute_definition.rb +++ b/app/models/custom_attribute_definition.rb @@ -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 diff --git a/spec/jobs/inboxes/sync_widget_pre_chat_custom_fields_job_spec.rb b/spec/jobs/inboxes/sync_widget_pre_chat_custom_fields_job_spec.rb index e6b23dccc..9348754d1 100644 --- a/spec/jobs/inboxes/sync_widget_pre_chat_custom_fields_job_spec.rb +++ b/spec/jobs/inboxes/sync_widget_pre_chat_custom_fields_job_spec.rb @@ -15,7 +15,7 @@ RSpec.describe Inboxes::SyncWidgetPreChatCustomFieldsJob, type: :job do end context 'when called' do - it 'reopens snoozed conversations whose snooze until has passed' do + it 'sync pre chat fields if custom attribute deleted' do described_class.perform_now(account, 'developer_id') expect(web_widget.reload.pre_chat_form_options['pre_chat_fields']).to eq [{ 'label' => 'Full Name', diff --git a/spec/jobs/inboxes/update_widget_pre_chat_custom_fields_job_spec.rb b/spec/jobs/inboxes/update_widget_pre_chat_custom_fields_job_spec.rb new file mode 100644 index 000000000..80a32be34 --- /dev/null +++ b/spec/jobs/inboxes/update_widget_pre_chat_custom_fields_job_spec.rb @@ -0,0 +1,30 @@ +require 'rails_helper' + +RSpec.describe Inboxes::UpdateWidgetPreChatCustomFieldsJob, type: :job do + pre_chat_fields = [{ + 'label' => 'Developer Id', + 'name' => 'developer_id' + }, { + 'label' => 'Full Name', + 'name' => 'full_name' + }] + pre_chat_message = 'Share your queries here.' + custom_attribute = { + 'attribute_key' => 'developer_id', + 'attribute_display_name' => 'Developer Number' + } + let!(:account) { create(:account) } + let!(:web_widget) do + create(:channel_widget, account: account, pre_chat_form_options: { pre_chat_message: pre_chat_message, pre_chat_fields: pre_chat_fields }) + end + + context 'when called' do + it 'sync pre chat fields if custom attribute updated' do + described_class.perform_now(account, custom_attribute) + expect(web_widget.reload.pre_chat_form_options['pre_chat_fields']).to eq [ + { 'label' => 'Developer Number', 'name' => 'developer_id', 'placeholder' => 'Developer Number', + 'values' => nil }, { 'label' => 'Full Name', 'name' => 'full_name' } + ] + end + end +end