Class: SpreeCmCommissioner::UserCountersService

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/user_counters_service.rb

Class Method Summary collapse

Class Method Details

.decrement_request_orders_count(user) ⇒ Object



31
32
33
34
35
36
37
38
# File 'app/services/spree_cm_commissioner/user_counters_service.rb', line 31

def self.decrement_request_orders_count(user)
  return if user.nil?

  user.class
      .where(id: user.id)
      .where('request_orders_count > 0')
      .update_all('request_orders_count = request_orders_count - 1') # rubocop:disable Rails/SkipsModelValidations
end

.decrement_unread_notifications_count(user) ⇒ Object



9
10
11
12
13
14
15
16
# File 'app/services/spree_cm_commissioner/user_counters_service.rb', line 9

def self.decrement_unread_notifications_count(user)
  return if user.nil?

  user.class
      .where(id: user.id)
      .where('unread_notifications_count > 0')
      .update_all('unread_notifications_count = unread_notifications_count - 1') # rubocop:disable Rails/SkipsModelValidations
end

.increment_request_orders_count(user) ⇒ Object



25
26
27
28
29
# File 'app/services/spree_cm_commissioner/user_counters_service.rb', line 25

def self.increment_request_orders_count(user)
  return if user.nil?

  user.increment!(:request_orders_count) # rubocop:disable Rails/SkipsModelValidations
end

.increment_unread_notifications_count(user) ⇒ Object



3
4
5
6
7
# File 'app/services/spree_cm_commissioner/user_counters_service.rb', line 3

def self.increment_unread_notifications_count(user)
  return if user.nil?

  user.increment!(:unread_notifications_count) # rubocop:disable Rails/SkipsModelValidations
end

.reset_unread_notifications_count(user) ⇒ Object



18
19
20
21
22
23
# File 'app/services/spree_cm_commissioner/user_counters_service.rb', line 18

def self.reset_unread_notifications_count(user)
  return if user.nil?

  user.unread_notifications_count = 0
  user.save!
end

.sync_counters(user) ⇒ Object

This Method is to Resolve the race condition of increment and decrement user counters We will create a Cron job to sync user counters from database Later



42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/services/spree_cm_commissioner/user_counters_service.rb', line 42

def self.sync_counters(user)
  return if user.nil?

  unread = user.notifications.unread_notifications.count
  requested = user.orders.where(request_state: 'requested').count

  user.update_columns( # rubocop:disable Rails/SkipsModelValidations
    unread_notifications_count: unread,
    request_orders_count: requested,
    updated_at: Time.current
  )
end