feat: add NotificationFinder

This commit is contained in:
Muhsin Keloth
2023-12-14 12:53:01 +05:30
parent 34675da6fa
commit 1906c0ff63
3 changed files with 78 additions and 9 deletions
@@ -4,12 +4,11 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro
before_action :fetch_notification, only: [:update, :destroy, :snooze]
before_action :set_primary_actor, only: [:read_all]
before_action :set_current_page, only: [:index]
def index
@unread_count = current_user.notifications.where(account_id: current_account.id, read_at: nil).count
@count = notifications.count
@notifications = notifications.page(@current_page).per(RESULTS_PER_PAGE)
@notifications = notification_finder.perform
@count = @notifications.count
end
def read_all
@@ -57,11 +56,7 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro
@notification = current_user.notifications.find(params[:id])
end
def set_current_page
@current_page = params[:page] || 1
end
def notifications
@notifications ||= current_user.notifications.where(account_id: current_account.id)
def notification_finder
@notification_finder ||= NotificationFinder.new(Current.user, Current.account, params)
end
end
+40
View File
@@ -0,0 +1,40 @@
class NotificationFinder
attr_reader :current_user, :current_account, :params
RESULTS_PER_PAGE = 15
def initialize(current_user, current_account, params)
@current_user = current_user
@current_account = current_account
@params = params
end
def perform
set_up
notifications
end
private
def set_up
find_all_notifications
filter_by_status if params[:status] == 'snoozed'
end
def find_all_notifications
@notifications = current_user.notifications.where(account_id: @current_account.id)
end
def filter_by_status
@notifications = @notifications.where('snoozed_until > ?', DateTime.now.utc)
@notifications
end
def current_page
params[:page] || 1
end
def notifications
@notifications.page(current_page).per(RESULTS_PER_PAGE).order(updated_at: :desc)
end
end
+34
View File
@@ -0,0 +1,34 @@
require 'rails_helper'
describe NotificationFinder do
subject(:notification_finder) { described_class.new(user, account, params) }
let!(:account) { create(:account) }
let!(:user) { create(:user, account: account) }
before do
create(:notification, account: account, user: user)
create(:notification, account: account, user: user)
create(:notification, account: account, user: user, snoozed_until: DateTime.now.utc + 1.day)
end
describe '#perform' do
context 'with snoozed status' do
let(:params) { { status: 'snoozed' } }
it 'filter notifications by status' do
result = notification_finder.perform
expect(result.length).to be 1
end
end
context 'without snoozed status' do
let(:params) { { status: 'open' } }
it 'returns all notifications' do
result = notification_finder.perform
expect(result.length).to be 3
end
end
end
end