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