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

#call ⇒ Object



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

def call
  send_notification
  update_sent_at unless user_ids
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_users ⇒ Object

Resolves the base user scope for the notification: by its taxons, by its vendor's own taxons (when the vendor has no tenant), or by tenant otherwise



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/interactors/spree_cm_commissioner/customer_notification_sender.rb', line 59

def end_users
  if customer_notification.notification_taxons.exists?
    taxon_ids = customer_notification.notification_taxons.pluck(:taxon_id)
    users_by_taxon_ids(taxon_ids)
  elsif notification_tenant_id.nil? && customer_notification.vendor.present?
    taxon_ids = customer_notification.vendor.taxons.ids
    users_by_taxon_ids(taxon_ids)
  else
    Spree.user_class
         .where(tenant_id: notification_tenant_id)
         .end_users_push_notificable
  end
end

#notification_tenant_id ⇒ Object



84
85
86
# File 'app/interactors/spree_cm_commissioner/customer_notification_sender.rb', line 84

def notification_tenant_id
  @notification_tenant_id ||= customer_notification.vendor&.tenant_id
end

#send_notification ⇒ Object



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_users ⇒ Object



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_at ⇒ Object

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_by_taxon_ids(taxon_ids) ⇒ Object

Fetches push-notificable users who purchased under the given taxon(s), scoped to the notification's tenant when it has one



75
76
77
78
79
80
81
82
# File 'app/interactors/spree_cm_commissioner/customer_notification_sender.rb', line 75

def users_by_taxon_ids(taxon_ids)
  users_scope = SpreeCmCommissioner::UsersByEventFetcherQuery
                .new(event_id: taxon_ids)
                .call
                .end_users_push_notificable
  users_scope = users_scope.where(tenant_id: notification_tenant_id) if notification_tenant_id.present?
  users_scope
end

#users_not_received_notifications ⇒ Object



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