Class: SpreeCmCommissioner::CustomerNotificationSender

Inherits:
BaseInteractor
  • Object
show all
Defined in:
app/interactors/spree_cm_commissioner/customer_notification_sender.rb

Instance Method Summary collapse

Instance Method Details

#callObject



5
6
7
8
# File 'app/interactors/spree_cm_commissioner/customer_notification_sender.rb', line 5

def call
  send_notification
  update_sent_at
end

#deliver_notification_to_user(user) ⇒ Object



38
39
40
41
42
43
# File 'app/interactors/spree_cm_commissioner/customer_notification_sender.rb', line 38

def deliver_notification_to_user(user)
  SpreeCmCommissioner::CustomerContentNotificationCreator.call(
    user_id: user.id,
    customer_notification_id: customer_notification.id
  )
end

#end_usersObject



57
58
59
60
61
62
63
64
65
66
67
# File 'app/interactors/spree_cm_commissioner/customer_notification_sender.rb', line 57

def end_users
  if customer_notification.notification_taxons.exists?
    taxon_ids = customer_notification.notification_taxons.pluck(:taxon_id)
    SpreeCmCommissioner::UsersByEventFetcherQuery
      .new(taxon_ids)
      .call
      .end_users_push_notificable
  else
    Spree.user_class.end_users_push_notificable
  end
end

#send_notificationObject



17
18
19
20
21
22
23
# File 'app/interactors/spree_cm_commissioner/customer_notification_sender.rb', line 17

def send_notification
  if user_ids.present?
    send_to_specific_users(user_ids)
  else
    send_to_target_users
  end
end

#send_to_specific_users(user_ids) ⇒ Object



25
26
27
28
29
30
# File 'app/interactors/spree_cm_commissioner/customer_notification_sender.rb', line 25

def send_to_specific_users(user_ids)
  users = Spree::User.push_notificable.where(id: user_ids)
  users.find_each do |user|
    deliver_notification_to_user(user)
  end
end

#send_to_target_usersObject



32
33
34
35
36
# File 'app/interactors/spree_cm_commissioner/customer_notification_sender.rb', line 32

def send_to_target_users
  users_not_received_notifications.find_each do |user|
    deliver_notification_to_user(user)
  end
end

#update_sent_atObject

even sent_at is present, it still can be sent but will only to users that not yet received the notification



12
13
14
15
# File 'app/interactors/spree_cm_commissioner/customer_notification_sender.rb', line 12

def update_sent_at
  customer_notification.sent_at = Time.current
  customer_notification.save
end

#users_not_received_notificationsObject



45
46
47
48
49
50
51
52
53
54
55
# File 'app/interactors/spree_cm_commissioner/customer_notification_sender.rb', line 45

def users_not_received_notifications
  end_users
    .joins(
      "LEFT JOIN cm_notifications ON (
        cm_notifications.recipient_id = spree_users.id
        AND cm_notifications.notificable_type = 'SpreeCmCommissioner::CustomerNotification'
        AND cm_notifications.notificable_id = #{customer_notification.id}
      )"
    )
    .where(cm_notifications: { id: nil })
end