fix: Update associations when a label is updated (#3046)
This commit is contained in:
parent
aaadd61e09
commit
b59e73b10b
7 changed files with 124 additions and 5 deletions
|
@ -24,12 +24,12 @@ export default {
|
||||||
|
|
||||||
computed: {
|
computed: {
|
||||||
savedLabels() {
|
savedLabels() {
|
||||||
const result = this.$store.getters['contactLabels/getContactLabels'](
|
const availableContactLabels = this.$store.getters[
|
||||||
this.contactId
|
'contactLabels/getContactLabels'
|
||||||
|
](this.contactId);
|
||||||
|
return this.allLabels.filter(({ title }) =>
|
||||||
|
availableContactLabels.includes(title)
|
||||||
);
|
);
|
||||||
return result.map(value => {
|
|
||||||
return this.allLabels.find(label => label.title === value);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
...mapGetters({
|
...mapGetters({
|
||||||
|
|
11
app/jobs/labels/update_job.rb
Normal file
11
app/jobs/labels/update_job.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
class Labels::UpdateJob < ApplicationJob
|
||||||
|
queue_as :default
|
||||||
|
|
||||||
|
def perform(new_label_title, old_label_title, account_id)
|
||||||
|
Labels::UpdateService.new(
|
||||||
|
new_label_title: new_label_title,
|
||||||
|
old_label_title: old_label_title,
|
||||||
|
account_id: account_id
|
||||||
|
).perform
|
||||||
|
end
|
||||||
|
end
|
|
@ -25,6 +25,8 @@ class Label < ApplicationRecord
|
||||||
format: { with: UNICODE_CHARACTER_NUMBER_HYPHEN_UNDERSCORE },
|
format: { with: UNICODE_CHARACTER_NUMBER_HYPHEN_UNDERSCORE },
|
||||||
uniqueness: { scope: :account_id }
|
uniqueness: { scope: :account_id }
|
||||||
|
|
||||||
|
after_update_commit :update_associated_models
|
||||||
|
|
||||||
before_validation do
|
before_validation do
|
||||||
self.title = title.downcase if attribute_present?('title')
|
self.title = title.downcase if attribute_present?('title')
|
||||||
end
|
end
|
||||||
|
@ -40,4 +42,12 @@ class Label < ApplicationRecord
|
||||||
def events
|
def events
|
||||||
account.events.where(conversation_id: conversations.pluck(:id))
|
account.events.where(conversation_id: conversations.pluck(:id))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def update_associated_models
|
||||||
|
return unless title_previously_changed?
|
||||||
|
|
||||||
|
Labels::UpdateJob.perform_later(title, title_previously_was, account_id)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
35
app/services/labels/update_service.rb
Normal file
35
app/services/labels/update_service.rb
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
class Labels::UpdateService
|
||||||
|
pattr_initialize [:new_label_title!, :old_label_title!, :account_id!]
|
||||||
|
|
||||||
|
def perform
|
||||||
|
tagged_conversations.find_in_batches do |conversation_batch|
|
||||||
|
conversation_batch.each do |conversation|
|
||||||
|
conversation.label_list.remove(old_label_title)
|
||||||
|
conversation.label_list.add(new_label_title)
|
||||||
|
conversation.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
tagged_contacts.find_in_batches do |contact_batch|
|
||||||
|
contact_batch.each do |contact|
|
||||||
|
contact.label_list.remove(old_label_title)
|
||||||
|
contact.label_list.add(new_label_title)
|
||||||
|
contact.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def tagged_conversations
|
||||||
|
account.conversations.tagged_with(old_label_title)
|
||||||
|
end
|
||||||
|
|
||||||
|
def tagged_contacts
|
||||||
|
account.contacts.tagged_with(old_label_title)
|
||||||
|
end
|
||||||
|
|
||||||
|
def account
|
||||||
|
@account ||= Account.find(account_id)
|
||||||
|
end
|
||||||
|
end
|
15
spec/jobs/labels/update_job_spec.rb
Normal file
15
spec/jobs/labels/update_job_spec.rb
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe Labels::UpdateJob, type: :job do
|
||||||
|
subject(:job) { described_class.perform_later(new_label_title, old_label_title, account_id) }
|
||||||
|
|
||||||
|
let(:new_label_title) { 'new-title' }
|
||||||
|
let(:old_label_title) { 'old-title' }
|
||||||
|
let(:account_id) { 1 }
|
||||||
|
|
||||||
|
it 'queues the job' do
|
||||||
|
expect { job }.to have_enqueued_job(described_class)
|
||||||
|
.with(new_label_title, old_label_title, account_id)
|
||||||
|
.on_queue('default')
|
||||||
|
end
|
||||||
|
end
|
|
@ -39,4 +39,20 @@ RSpec.describe Label, type: :model do
|
||||||
expect(duplicate_label.valid?).to eq false
|
expect(duplicate_label.valid?).to eq false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '.after_update_commit' do
|
||||||
|
let(:label) { create(:label) }
|
||||||
|
|
||||||
|
it 'calls update job' do
|
||||||
|
expect(Labels::UpdateJob).to receive(:perform_later).with('new-title', label.title, label.account_id)
|
||||||
|
|
||||||
|
label.update(title: 'new-title')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not call update job if title is not updated' do
|
||||||
|
expect(Labels::UpdateJob).not_to receive(:perform_later)
|
||||||
|
|
||||||
|
label.update(description: 'new-description')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
32
spec/services/labels/update_service_spec.rb
Normal file
32
spec/services/labels/update_service_spec.rb
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe Labels::UpdateService do
|
||||||
|
let(:account) { create(:account) }
|
||||||
|
let(:conversation) { create(:conversation, account: account) }
|
||||||
|
let(:label) { create(:label, account: account) }
|
||||||
|
let(:contact) { conversation.contact }
|
||||||
|
|
||||||
|
before do
|
||||||
|
conversation.label_list.add(label.title)
|
||||||
|
conversation.save!
|
||||||
|
|
||||||
|
contact.label_list.add(label.title)
|
||||||
|
contact.save!
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#perform' do
|
||||||
|
it 'updates associated conversations/contacts labels' do
|
||||||
|
expect(conversation.label_list).to eq([label.title])
|
||||||
|
expect(contact.label_list).to eq([label.title])
|
||||||
|
|
||||||
|
described_class.new(
|
||||||
|
new_label_title: 'updated-label-title',
|
||||||
|
old_label_title: label.title,
|
||||||
|
account_id: account.id
|
||||||
|
).perform
|
||||||
|
|
||||||
|
expect(conversation.reload.label_list).to eq(['updated-label-title'])
|
||||||
|
expect(contact.reload.label_list).to eq(['updated-label-title'])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue