API: Fix fetching explicitly not only unread notifications
This commit is contained in:
parent
cd0995abf3
commit
8068d8747b
2 changed files with 24 additions and 8 deletions
|
|
@ -3,6 +3,8 @@
|
||||||
module Api
|
module Api
|
||||||
module V1
|
module V1
|
||||||
class NotificationsController < Api::V1::BaseController
|
class NotificationsController < Api::V1::BaseController
|
||||||
|
BOOLEAN_TYPE = ActiveModel::Type::Boolean.new
|
||||||
|
|
||||||
before_action do
|
before_action do
|
||||||
require_access_token %w[notifications]
|
require_access_token %w[notifications]
|
||||||
end
|
end
|
||||||
|
|
@ -24,25 +26,25 @@ module Api
|
||||||
def index
|
def index
|
||||||
after_date = Date.iso8601(params[:only_after]) if params.has_key?(:only_after)
|
after_date = Date.iso8601(params[:only_after]) if params.has_key?(:only_after)
|
||||||
|
|
||||||
notifications_query = service.index(params[:only_unread], after_date)
|
notifications_query = service.index(BOOLEAN_TYPE.cast(params[:only_unread]), after_date)
|
||||||
notifications_page = time_pager(notifications_query).response
|
notifications_page = time_pager(notifications_query).response
|
||||||
notifications_page[:data] = notifications_page[:data].map do |note|
|
notifications_page[:data] = notifications_page[:data].map do |note|
|
||||||
NotificationPresenter.new(note, default_serializer_options).as_api_json
|
NotificationPresenter.new(note, default_serializer_options).as_api_json
|
||||||
end
|
end
|
||||||
render_paged_api_response notifications_page
|
render_paged_api_response notifications_page
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
render_error 422, "Couldnt process the notifications requestt process the notifications request"
|
render_error 422, "Could not process the notifications request"
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
read = ActiveModel::Type::Boolean.new.cast(params.require(:read))
|
read = BOOLEAN_TYPE.cast(params.require(:read))
|
||||||
if service.update_status_by_guid(params[:id], read)
|
if service.update_status_by_guid(params[:id], read)
|
||||||
head :no_content
|
head :no_content
|
||||||
else
|
else
|
||||||
render_error 422, "Couldnt process the notifications requestt process the notifications request"
|
render_error 422, "Could not process the notifications request"
|
||||||
end
|
end
|
||||||
rescue ActionController::ParameterMissing
|
rescue ActionController::ParameterMissing
|
||||||
render_error 422, "Couldnt process the notifications requestt process the notifications request"
|
render_error 422, "Could not process the notifications request"
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,20 @@ describe Api::V1::NotificationsController do
|
||||||
expect(notification.length).to eq(1)
|
expect(notification.length).to eq(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "with proper credentials and unread only explicitly false" do
|
||||||
|
@notification.set_read_state(true)
|
||||||
|
get(
|
||||||
|
api_v1_notifications_path,
|
||||||
|
params: {only_unread: false, access_token: access_token}
|
||||||
|
)
|
||||||
|
expect(response.status).to eq(200)
|
||||||
|
notifications = response_body_data(response)
|
||||||
|
expect(notifications.length).to eq(2)
|
||||||
|
confirm_notification_format(notifications[1], @notification, "also_commented", nil)
|
||||||
|
|
||||||
|
expect(notifications.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/notifications")
|
||||||
|
end
|
||||||
|
|
||||||
it "with proper credentials and after certain date" do
|
it "with proper credentials and after certain date" do
|
||||||
get(
|
get(
|
||||||
api_v1_notifications_path,
|
api_v1_notifications_path,
|
||||||
|
|
@ -87,7 +101,7 @@ describe Api::V1::NotificationsController do
|
||||||
api_v1_notifications_path,
|
api_v1_notifications_path,
|
||||||
params: {only_after: "January 1, 2018", access_token: access_token}
|
params: {only_after: "January 1, 2018", access_token: access_token}
|
||||||
)
|
)
|
||||||
confirm_api_error(response, 422, "Couldnt process the notifications requestt process the notifications request")
|
confirm_api_error(response, 422, "Could not process the notifications request")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "with insufficient credentials" do
|
it "with insufficient credentials" do
|
||||||
|
|
@ -190,7 +204,7 @@ describe Api::V1::NotificationsController do
|
||||||
api_v1_notification_path("999_999_999"),
|
api_v1_notification_path("999_999_999"),
|
||||||
params: {access_token: access_token}
|
params: {access_token: access_token}
|
||||||
)
|
)
|
||||||
confirm_api_error(response, 422, "Couldnt process the notifications requestt process the notifications request")
|
confirm_api_error(response, 422, "Could not process the notifications request")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "with proper missing read field" do
|
it "with proper missing read field" do
|
||||||
|
|
@ -198,7 +212,7 @@ describe Api::V1::NotificationsController do
|
||||||
api_v1_notification_path(@notification.guid),
|
api_v1_notification_path(@notification.guid),
|
||||||
params: {access_token: access_token}
|
params: {access_token: access_token}
|
||||||
)
|
)
|
||||||
confirm_api_error(response, 422, "Couldnt process the notifications requestt process the notifications request")
|
confirm_api_error(response, 422, "Could not process the notifications request")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "with insufficient credentials" do
|
it "with insufficient credentials" do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue