fix: Update associations when a label is updated (#3046)

This commit is contained in:
Pranav Raj S
2021-09-21 10:16:32 +05:30
committed by GitHub
parent aaadd61e09
commit b59e73b10b
7 changed files with 124 additions and 5 deletions

View File

@@ -24,12 +24,12 @@ export default {
computed: {
savedLabels() {
const result = this.$store.getters['contactLabels/getContactLabels'](
this.contactId
const availableContactLabels = this.$store.getters[
'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({

View 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

View File

@@ -25,6 +25,8 @@ class Label < ApplicationRecord
format: { with: UNICODE_CHARACTER_NUMBER_HYPHEN_UNDERSCORE },
uniqueness: { scope: :account_id }
after_update_commit :update_associated_models
before_validation do
self.title = title.downcase if attribute_present?('title')
end
@@ -40,4 +42,12 @@ class Label < ApplicationRecord
def events
account.events.where(conversation_id: conversations.pluck(:id))
end
private
def update_associated_models
return unless title_previously_changed?
Labels::UpdateJob.perform_later(title, title_previously_was, account_id)
end
end

View 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