From e010f0c6f033eb9153b07e01ce5cd5c5dd484aba Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Mon, 18 Apr 2022 14:06:27 +0530 Subject: [PATCH] chore: Sync pre-chat fields after custom attribute destroy (#4456) --- .../sync_widget_pre_chat_custom_fields_job.rb | 11 ++++++++ app/models/custom_attribute_definition.rb | 7 +++++ ..._widget_pre_chat_custom_fields_job_spec.rb | 26 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 app/jobs/inboxes/sync_widget_pre_chat_custom_fields_job.rb create mode 100644 spec/jobs/inboxes/sync_widget_pre_chat_custom_fields_job_spec.rb diff --git a/app/jobs/inboxes/sync_widget_pre_chat_custom_fields_job.rb b/app/jobs/inboxes/sync_widget_pre_chat_custom_fields_job.rb new file mode 100644 index 000000000..d4192a665 --- /dev/null +++ b/app/jobs/inboxes/sync_widget_pre_chat_custom_fields_job.rb @@ -0,0 +1,11 @@ +class Inboxes::SyncWidgetPreChatCustomFieldsJob < ApplicationJob + queue_as :default + + def perform(account, field_name) + account.web_widgets.all.find_each do |web_widget| + pre_chat_fields = web_widget.pre_chat_form_options['pre_chat_fields'] + web_widget.pre_chat_form_options['pre_chat_fields'] = pre_chat_fields.reject { |field| field['name'] == field_name } + web_widget.save! + end + end +end diff --git a/app/models/custom_attribute_definition.rb b/app/models/custom_attribute_definition.rb index 72b67ce06..9feca3994 100644 --- a/app/models/custom_attribute_definition.rb +++ b/app/models/custom_attribute_definition.rb @@ -34,4 +34,11 @@ 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_destroy :sync_widget_pre_chat_custom_fields + + private + + def sync_widget_pre_chat_custom_fields + ::Inboxes::SyncWidgetPreChatCustomFieldsJob.perform_later(attribute_key) + 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 new file mode 100644 index 000000000..e6b23dccc --- /dev/null +++ b/spec/jobs/inboxes/sync_widget_pre_chat_custom_fields_job_spec.rb @@ -0,0 +1,26 @@ +require 'rails_helper' + +RSpec.describe Inboxes::SyncWidgetPreChatCustomFieldsJob, 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.' + 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 'reopens snoozed conversations whose snooze until has passed' do + described_class.perform_now(account, 'developer_id') + expect(web_widget.reload.pre_chat_form_options['pre_chat_fields']).to eq [{ + 'label' => 'Full Name', + 'name' => 'full_name' + }] + end + end +end