2019-01-25 14:53:06 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class UserSystemNotification < ApplicationRecord
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :system_notification
|
2019-02-13 13:06:14 +01:00
|
|
|
|
2019-04-25 08:54:50 +02:00
|
|
|
validates :system_notification, uniqueness: { scope: :user }
|
2019-02-19 11:08:59 +01:00
|
|
|
|
2019-02-14 14:16:49 +01:00
|
|
|
scope :unseen, -> { where(seen_at: nil) }
|
2019-02-14 12:51:30 +01:00
|
|
|
|
2019-02-21 16:15:13 +01:00
|
|
|
def self.mark_as_seen
|
|
|
|
unseen.update_all(seen_at: Time.now)
|
2019-02-13 13:06:14 +01:00
|
|
|
end
|
2019-02-15 13:07:29 +01:00
|
|
|
|
|
|
|
def self.mark_as_read(notification_id)
|
|
|
|
notification = find_by_system_notification_id(notification_id)
|
2019-04-25 08:54:50 +02:00
|
|
|
notification.update(read_at: Time.now) if notification && notification.read_at.nil?
|
2019-02-15 13:07:29 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.show_on_login(update_read_time = false)
|
|
|
|
# for notification check leave update_read_time empty
|
|
|
|
notification = joins(:system_notification)
|
|
|
|
.where('system_notifications.show_on_login = true')
|
2019-04-24 08:04:47 +02:00
|
|
|
.order('system_notifications.created_at DESC')
|
2019-02-15 13:07:29 +01:00
|
|
|
.select(
|
|
|
|
:modal_title,
|
|
|
|
:modal_body,
|
|
|
|
'user_system_notifications.id',
|
|
|
|
:read_at,
|
|
|
|
:user_id,
|
2019-04-24 08:04:47 +02:00
|
|
|
:system_notification_id,
|
|
|
|
:created_at
|
2019-02-15 13:07:29 +01:00
|
|
|
)
|
|
|
|
.first
|
|
|
|
if notification && notification.read_at.nil?
|
|
|
|
if update_read_time
|
|
|
|
notification.update(
|
|
|
|
read_at: Time.now,
|
|
|
|
seen_at: Time.now
|
|
|
|
)
|
|
|
|
end
|
|
|
|
notification
|
|
|
|
end
|
|
|
|
end
|
2019-01-25 14:53:06 +01:00
|
|
|
end
|