From 2e8558a93d8dc531f35051c755bb7afe092da6e2 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Mon, 19 Apr 2021 13:47:16 +0300 Subject: [PATCH 1/4] implement get all list notifications repp endpoint --- .../v1/registrar/notifications_controller.rb | 26 +++++++++++++++++++ config/routes.rb | 6 ++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/controllers/repp/v1/registrar/notifications_controller.rb b/app/controllers/repp/v1/registrar/notifications_controller.rb index 4e00a93217..1108d434e6 100644 --- a/app/controllers/repp/v1/registrar/notifications_controller.rb +++ b/app/controllers/repp/v1/registrar/notifications_controller.rb @@ -18,6 +18,20 @@ def index render_success(data: data) end + api :GET, '/repp/v1/registrar/notifications/all_notifications' + desc 'Get the all unread poll messages' + def all_notifications + records = current_user.unread_notifications.order('created_at DESC').all + @notification = records.limit(limit).offset(offset) + # rubocop:disable Style/AndOr + render_success(data: nil) and return unless @notification + # rubocop:enable Style/AndOr + + data = @notification.as_json(only: %i[id text attached_obj_id attached_obj_type]) + + render_success(data: data) + end + api :GET, '/repp/v1/registrar/notifications/:notification_id' desc 'Get a specific poll message' def show @@ -45,6 +59,18 @@ def update def set_notification @notification = current_user.unread_notifications.find(params[:id]) end + + def limit + index_params[:limit] || 200 + end + + def offset + index_params[:offset] || 0 + end + + def index_params + params.permit(:limit, :offset) + end end end end diff --git a/config/routes.rb b/config/routes.rb index 4e78b7c0f7..48186070b0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -54,7 +54,11 @@ resources :auctions, only: %i[index] resources :retained_domains, only: %i[index] namespace :registrar do - resources :notifications, only: [:index, :show, :update] + resources :notifications, only: [:index, :show, :update] do + collection do + get '/all_notifications', to: 'notifications#all_notifications' + end + end resources :nameservers do collection do put '/', to: 'nameservers#update' From d5af1a7fb651e5fed6e9b1ab4fd81dbd5c01dce5 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Mon, 19 Apr 2021 14:10:00 +0300 Subject: [PATCH 2/4] added test --- .../repp/v1/registrar/notifications_controller.rb | 2 +- .../repp/v1/registrar/notifications_test.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/controllers/repp/v1/registrar/notifications_controller.rb b/app/controllers/repp/v1/registrar/notifications_controller.rb index 1108d434e6..23be6d7b79 100644 --- a/app/controllers/repp/v1/registrar/notifications_controller.rb +++ b/app/controllers/repp/v1/registrar/notifications_controller.rb @@ -63,7 +63,7 @@ def set_notification def limit index_params[:limit] || 200 end - + def offset index_params[:offset] || 0 end diff --git a/test/integration/repp/v1/registrar/notifications_test.rb b/test/integration/repp/v1/registrar/notifications_test.rb index 9fafca4436..2677d393b5 100644 --- a/test/integration/repp/v1/registrar/notifications_test.rb +++ b/test/integration/repp/v1/registrar/notifications_test.rb @@ -9,6 +9,17 @@ def setup @auth_headers = { 'Authorization' => token } end + def test_all_unreaded_poll_messages + notification = @user.registrar.notifications.where(read: false).order(created_at: :desc).all + get "/repp/v1/registrar/notifications/all_notifications", headers: @auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert_equal notification.count, json[:data].count + assert_equal json[:data].first[:text], notification.first.text + assert_equal json[:data].last[:text], notification.last.text + end + def test_gets_latest_unread_poll_message notification = @user.registrar.notifications.where(read: false).order(created_at: :desc).first get "/repp/v1/registrar/notifications", headers: @auth_headers From 759254d95482fa29515a7d17e62470654c852348 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Tue, 4 May 2021 14:37:50 +0300 Subject: [PATCH 3/4] change response message --- .../repp/v1/registrar/notifications_controller.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/repp/v1/registrar/notifications_controller.rb b/app/controllers/repp/v1/registrar/notifications_controller.rb index 23be6d7b79..b2406a52d2 100644 --- a/app/controllers/repp/v1/registrar/notifications_controller.rb +++ b/app/controllers/repp/v1/registrar/notifications_controller.rb @@ -22,6 +22,7 @@ def index desc 'Get the all unread poll messages' def all_notifications records = current_user.unread_notifications.order('created_at DESC').all + @notification = records.limit(limit).offset(offset) # rubocop:disable Style/AndOr render_success(data: nil) and return unless @notification @@ -29,7 +30,10 @@ def all_notifications data = @notification.as_json(only: %i[id text attached_obj_id attached_obj_type]) - render_success(data: data) + default_count = 200 + + message = "Command completed successfully. The total notifications are #{records.count}. Returns only #{@notification.count}. Limit by default is #{limit}. To change the amount of data returned, use the parameters limit and offset in url." + render_success(data: data, message: message) end api :GET, '/repp/v1/registrar/notifications/:notification_id' From 62177078c26e4117568b853cc3a5d25484d7eda0 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Tue, 4 May 2021 14:42:09 +0300 Subject: [PATCH 4/4] refactoring --- .../repp/v1/registrar/notifications_controller.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/repp/v1/registrar/notifications_controller.rb b/app/controllers/repp/v1/registrar/notifications_controller.rb index b2406a52d2..1185a1a737 100644 --- a/app/controllers/repp/v1/registrar/notifications_controller.rb +++ b/app/controllers/repp/v1/registrar/notifications_controller.rb @@ -30,9 +30,9 @@ def all_notifications data = @notification.as_json(only: %i[id text attached_obj_id attached_obj_type]) - default_count = 200 - - message = "Command completed successfully. The total notifications are #{records.count}. Returns only #{@notification.count}. Limit by default is #{limit}. To change the amount of data returned, use the parameters limit and offset in url." + message = 'Command completed successfully.'\ + " Returning #{@notification.count} out of #{records.count}."\ + ' Use URL parameters :limit and :offset to list other messages if needed.' render_success(data: data, message: message) end