chore: Add unread notification count API (#1646)

Co-authored-by: Sojan <sojan@pepalo.com>
This commit is contained in:
Muhsin Keloth 2021-01-17 09:15:31 -08:00 committed by GitHub
parent b6e8173b24
commit dd90e24d02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 0 deletions

View file

@ -27,6 +27,11 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro
render json: @notification
end
def unread_count
@unread_count = current_user.notifications.where(account_id: current_account.id, read_at: nil).count
render json: @unread_count
end
private
def set_primary_actor

View file

@ -92,6 +92,7 @@ Rails.application.routes.draw do
resources :notifications, only: [:index, :update] do
collection do
post :read_all
get :unread_count
end
end
resource :notification_settings, only: [:show, :update]

View file

@ -102,4 +102,29 @@ RSpec.describe 'Notifications API', type: :request do
end
end
end
describe 'GET /api/v1/accounts/{account.id}/notifications/unread_count' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/notifications/unread_count"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
let(:admin) { create(:user, account: account, role: :administrator) }
it 'returns notifications unread count' do
2.times.each { create(:notification, account: account, user: admin) }
get "/api/v1/accounts/#{account.id}/notifications/unread_count",
headers: admin.create_new_auth_token,
as: :json
response_json = JSON.parse(response.body)
expect(response).to have_http_status(:success)
expect(response_json).to eq 2
end
end
end
end