Skip to content

Commit

Permalink
feat: Sort Notification API changes (#8865)
Browse files Browse the repository at this point in the history
* feat: Inbox sort API changes

* Update notification_finder.rb

* Update notification_finder.rb

* Update notification_finder.rb
  • Loading branch information
muhsin-k committed Feb 6, 2024
1 parent a4fc28a commit d67b91d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/finders/notification_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def count

def set_up
find_all_notifications
filter_by_read_status
filter_by_status
end

Expand All @@ -37,11 +38,15 @@ def filter_by_status
@notifications = @notifications.where('snoozed_until > ?', DateTime.now.utc) if params[:status] == 'snoozed'
end

def filter_by_read_status
@notifications = @notifications.where.not(read_at: nil) if params[:type] == 'read'
end

def current_page
params[:page] || 1
end

def notifications
@notifications.page(current_page).per(RESULTS_PER_PAGE).order(last_activity_at: :desc)
@notifications.page(current_page).per(RESULTS_PER_PAGE).order(last_activity_at: params[:sort_order] || :desc)
end
end
37 changes: 37 additions & 0 deletions spec/finders/notification_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,42 @@
expect(result).to be 1
end
end

context 'when type read param is passed' do
let(:params) { { type: 'read' } }

it 'returns only read notifications' do
result = notification_finder.perform
expect(result.length).to be 2
end

it 'returns count' do
result = notification_finder.count
expect(result).to be 2
end
end

context 'when type read and snoozed param is passed' do
let(:params) { { type: 'read', status: 'snoozed' } }

it 'returns only read notifications' do
result = notification_finder.perform
expect(result.length).to be 0
end

it 'returns count' do
result = notification_finder.count
expect(result).to be 0
end
end

context 'when sort order is passed' do
let(:params) { { sort_order: :asc } }

it 'returns notifications in ascending order' do
result = notification_finder.perform
expect(result.first.last_activity_at).to be < result.last.last_activity_at
end
end
end
end

0 comments on commit d67b91d

Please sign in to comment.