diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 49d9e32c..d1b752a9 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -1,19 +1,71 @@ class NotificationsController < ApplicationController + before_action :set_receipts, only: [:index, :show, :mark_read, :mark_unread] + before_action :set_notification, only: [:show, :mark_read, :mark_unread] + before_action :set_receipt, only: [:show, :mark_read, :mark_unread] + def index @notifications = current_user.mailbox.notifications respond_to do |format| format.html - format.json { render json: @notifications.to_json } + format.json do + render json: @notifications.map do |notification| + receipt = @receipts.find_by(notification_id: notification.id) + notification.as_json.merge(is_read: receipt.is_read) + end + end end end def show - @notification = current_user.mailbox.notifications.find_by(id: params[:id]) - + @receipt.update(is_read: true) respond_to do |format| format.html - format.json { render json: @notification.to_json } + format.json do + render json: @notification.as_json.merge( + is_read: @receipt.is_read + ) + end end end + + def mark_read + @receipt.update(is_read: true) + respond_to do |format| + format.js + format.json do + render json: @notification.as_json.merge( + is_read: @receipt.is_read + ) + end + end + end + + def mark_unread + @receipt.update(is_read: false) + respond_to do |format| + format.js + format.json do + render json: @notification.as_json.merge( + is_read: @receipt.is_read + ) + end + end + end + + private + + def set_receipts + @receipts = current_user.mailbox.receipts + .includes(:notification).where(mailbox_type: nil) + end + + def set_notification + @notification = current_user.mailbox.notifications.find_by(id: params[:id]) + end + + def set_receipt + @receipt = @receipts.find_by(notification_id: params[:id]) + end + end diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb index 9efae3d6..a1f5f832 100644 --- a/app/views/notifications/index.html.erb +++ b/app/views/notifications/index.html.erb @@ -6,11 +6,19 @@