From c8cc046358a6b9cd976de76b3a718d747726d260 Mon Sep 17 00:00:00 2001 From: Pranav Date: Mon, 28 Oct 2024 23:35:16 -0700 Subject: [PATCH] Contact changes --- app/models/contact.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/models/contact.rb b/app/models/contact.rb index c79dd3e9e..3817ed815 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -63,9 +63,14 @@ class Contact < ApplicationRecord after_update_commit :dispatch_update_event after_destroy_commit :dispatch_destroy_event before_save :sync_contact_attributes + after_commit :schedule_counter_cache_update enum contact_type: { visitor: 0, lead: 1, customer: 2 } + scope :contactable, lambda { + where("email != '' OR phone_number != '' OR identifier != ''") + } + scope :order_on_last_activity_at, lambda { |direction| order( Arel::Nodes::SqlLiteral.new( @@ -223,4 +228,16 @@ class Contact < ApplicationRecord def dispatch_destroy_event Rails.configuration.dispatcher.dispatch(CONTACT_DELETED, Time.zone.now, contact: self) end + + def schedule_counter_cache_update + if saved_change_to_email? || + saved_change_to_phone_number? || + saved_change_to_identifier? || + destroyed? + + UpdateContactableContactsCountJob + .set(wait: 5.seconds) + .perform_later(account_id) + end + end end