mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-12-27 18:52:07 +08:00
7dcc774414
* Remove redundant use of respond_to in controlller actions with just one responder format [SCI-8811] * Remove redundant begin block and replace 303 status code with :see_other [SCI-8811]
70 lines
2.4 KiB
Ruby
70 lines
2.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class UserNotificationsController < ApplicationController
|
|
def index
|
|
page = (params[:page] || 1).to_i
|
|
notifications = load_notifications.page(page).per(Constants::INFINITE_SCROLL_LIMIT).without_count
|
|
|
|
render json: {
|
|
notifications: notification_serializer(notifications),
|
|
next_page: notifications.next_page
|
|
}
|
|
|
|
UserNotification.where(
|
|
notification_id: notifications.except(:select).where.not(type_of: 2).select(:id)
|
|
).seen_by_user(current_user)
|
|
|
|
current_user.user_system_notifications.where(
|
|
system_notification_id: notifications.except(:select).where(type_of: 2).select(:id)
|
|
).mark_as_seen
|
|
end
|
|
|
|
def unseen_counter
|
|
render json: {
|
|
unseen: load_notifications.where(checked: false).size
|
|
}
|
|
end
|
|
|
|
private
|
|
|
|
def load_notifications
|
|
user_notifications = current_user.notifications
|
|
.select(:id, :type_of, :title, :message, :created_at, 'user_notifications.checked')
|
|
system_notifications = current_user.system_notifications
|
|
.select(
|
|
:id,
|
|
'2 AS type_of',
|
|
:title,
|
|
'description AS message',
|
|
:created_at,
|
|
'CASE WHEN seen_at IS NULL THEN false ELSE true END AS checked'
|
|
)
|
|
notifications =
|
|
case params[:type]
|
|
when 'message'
|
|
user_notifications
|
|
when 'system'
|
|
Notification.from("(#{system_notifications.to_sql}) AS notifications")
|
|
else
|
|
Notification.from(
|
|
"((#{user_notifications.to_sql}) UNION ALL (#{system_notifications.to_sql})) AS notifications"
|
|
)
|
|
end
|
|
notifications.order(created_at: :desc)
|
|
end
|
|
|
|
def notification_serializer(notifications)
|
|
notifications.map do |notification|
|
|
{
|
|
id: notification.id,
|
|
type_of: notification.type_of,
|
|
title: notification.title,
|
|
message: notification.message,
|
|
created_at: I18n.l(notification.created_at, format: :full),
|
|
today: notification.created_at.today?,
|
|
checked: notification.checked,
|
|
action_url: (system_notification_path(notification.id) if notification.type_of == 'system_message')
|
|
}
|
|
end
|
|
end
|
|
end
|