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, uniq_checkers: { test_type_id: nil, order_id: nil, specimen_id: nil }) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/services/lab/notification_service.rb', line 30

def create_notification(alert_type, alert_message,
                        uniq_checkers: { test_type_id: nil, order_id: nil, specimen_id: nil })
  return if alert_type != 'LIMS'

  test_type_id = uniq_checkers[:test_type_id]
  order_id = uniq_checkers[:order_id]
  specimen_id = uniq_checkers[:specimen_id]

  return unless test_type_id.present? && order_id.present? && specimen_id.present?

  lab = Lab::Lims::Utils.lab_user
  unless lab
    Rails.logger.warn('NotificationService: lab_daemon user not found, skipping notification creation')
    return
  end

  ActiveRecord::Base.transaction do
    # Atomic find or create - checks and creates in one operation
    alert = NotificationAlert.find_or_create_by!(
      test_type_id: test_type_id,
      order_id: order_id,
      specimen_id: specimen_id
    ) do |new_alert|
      # Only set these attributes on creation
      new_alert.text = alert_message.to_json
      new_alert.date_to_expire = Time.now + not_period.days
      new_alert.creator = lab
      new_alert.changed_by = lab
      new_alert.date_created = Time.now
    end

    # Only notify if this is a new record (not a duplicate)
    notify(alert, User.joins(:roles).uniq) if alert.previously_new_record?
  rescue ActiveRecord::RecordNotUnique
    # Handle race condition if unique constraint exists
    Rails.logger.info("Duplicate notification prevented for test_type: #{test_type_id}, order: #{order_id}, specimen: #{specimen_id}")
  rescue ActiveRecord::InvalidForeignKey => e
    Rails.logger.error("Invalid foreign key: #{e.message}")
  rescue StandardError => e
    Rails.logger.error("Unexpected error: #{e.message}")
  end
end

#not_periodObject



73
74
75
76
77
78
# File 'app/services/lab/notification_service.rb', line 73

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



80
81
82
83
84
85
86
# File 'app/services/lab/notification_service.rb', line 80

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



88
89
90
91
92
93
94
# File 'app/services/lab/notification_service.rb', line 88

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



96
97
98
99
100
101
102
# File 'app/services/lab/notification_service.rb', line 96

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