Class: Lab::NotificationService

Inherits:
Object
  • Object
show all
Defined in:
app/services/lab/notification_service.rb

Overview

notification service

Instance Method Summary collapse

Instance Method Details

#clear(alert_id) ⇒ Object



12
13
14
15
16
# File 'app/services/lab/notification_service.rb', line 12

def clear(alert_id)
  alert = NotificationAlert.find(alert_id)
  # update the notification alert recipient to cleared and read only for the current user
  alert.notification_alert_recipients.where(user_id: User.current.user_id).update_all(cleared: true, alert_read: true)
end

#create_notification(alert_type, alert_message) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/lab/notification_service.rb', line 30

def create_notification(alert_type, alert_message)
  return if alert_type != 'LIMS'

  lab = User.find_by(username: 'lab_daemon')
  ActiveRecord::Base.transaction do
    alert = NotificationAlert.create!(text: alert_message.to_json, date_to_expire: Time.now + not_period.days,
                                      creator: lab, changed_by: lab, date_created: Time.now)
    notify(alert, User.joins(:roles).uniq)
    # ActionCable.server.broadcast('nlims_channel', alert)
  end
end

#not_periodObject



42
43
44
45
46
47
# File 'app/services/lab/notification_service.rb', line 42

def not_period
  result = GlobalProperty.where(property: 'notification.period')&.first
  return result.property_value.to_i if result.present?

  7 # default to 7 days
end

#notify(notification_alert, recipients) ⇒ Object



49
50
51
52
53
54
55
# File 'app/services/lab/notification_service.rb', line 49

def notify(notification_alert, recipients)
  recipients.each do |recipient|
    recipient.notification_alert_recipients.create(
      alert_id: notification_alert.id
    )
  end
end

#notify_all(notification_alert, users) ⇒ Object



57
58
59
60
61
62
63
# File 'app/services/lab/notification_service.rb', line 57

def notify_all(notification_alert, users)
  users.each do |user|
    user.notification_alert_recipients.create(
      alert_id: notification_alert.id
    )
  end
end

#notify_all_users(notification_alert) ⇒ Object



65
66
67
68
69
70
71
# File 'app/services/lab/notification_service.rb', line 65

def notify_all_users(notification_alert)
  User.all.each do |user|
    user.notification_alert_recipients.create!(
      alert_id: notification_alert.id
    )
  end
end

#read(alerts) ⇒ Object

this updates the notification to read



19
20
21
22
23
24
25
26
27
28
# File 'app/services/lab/notification_service.rb', line 19

def read(alerts)
  alerts.each do |alert|
    notification = NotificationAlertRecipient.where(user_id: User.current.user_id, alert_id: alert,
                                                    alert_read: false).first
    next if notification.blank?

    notification.alert_read = true
    notification.save
  end
end

#unclearedObject

this gets all uncleared notifications of the user



6
7
8
9
10
# File 'app/services/lab/notification_service.rb', line 6

def uncleared
  NotificationAlert.joins(:notification_alert_recipients).where(
    'notification_alert_recipient.user_id = ?', User.current.user_id
  )
end