Skip to content

Commit

Permalink
implement project preference read settings endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Tooyosi committed Feb 7, 2024
1 parent db1c680 commit 91d871e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
34 changes: 27 additions & 7 deletions app/controllers/api/v1/project_preferences_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ class Api::V1::ProjectPreferencesController < Api::ApiController
schema_type :json_schema
before_action :find_upp_for_update_settings, only: [:update_settings]

def read_settings
skip_policy_scope
read_and_update_settings_response
end

def update_settings
skip_policy_scope
@upp.settings.merge! params_for[:settings]
@upp.save!
update_settings_response
read_and_update_settings_response
end

private
Expand All @@ -29,15 +34,30 @@ def user_allowed?
@upp.project.owners_and_collaborators.include?(api_user.user) || api_user.is_admin?
end

def update_settings_response
def read_and_update_settings_response
set_last_modified_header if action_name == "update_settings"

render_json_response
end

private

def set_last_modified_header
response.headers['Last-Modified'] = @upp.updated_at.httpdate
end

def render_json_response
if action_name == "update_settings"
preferences = UserProjectPreference.where(id: @upp.id)
else
project = Project.find_by!(id: params[:project_id])
preferences = project.user_project_preference.where.not(email_communication: nil)
preferences = params[:user_id].present? ? preferences.where(user_id: params[:user_id]) : preferences
end

render(
status: :ok,
json_api: serializer.resource(
{},
UserProjectPreference.where(id: @upp.id),
context
)
json_api: serializer.resource({}, preferences, context)
)
end
end
1 change: 1 addition & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Project < ApplicationRecord
has_many :tutorials
has_many :field_guides, dependent: :destroy
belongs_to :organization
has_many :user_project_preference
# uses the activated_state enum on the workflow
has_many :workflows,
-> { where(serialize_with_project: true).active},
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
json_api_resources :project_preferences do
collection do
post :update_settings
get :read_settings
end
end

Expand Down
41 changes: 41 additions & 0 deletions spec/controllers/api/v1/project_preferences_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,45 @@
end
end
end

describe '#read_settings' do
let!(:project) { create(:project, owner: authorized_user) }
let!(:upp) { create(:user_project_preference, project: project) }
let(:run_generic_read) { get :read_settings, params: { project_id: project.id, format: :json } }
let(:unauthorised_user) { create(:user) }
let(:run_unauthorised_user_read) { get :read_settings, params: { project_id: project.id, user_id: unauthorised_user.id, format: :json } }

describe 'genetic preferences' do
before(:each) do
default_request user_id: authorized_user.id, scopes: scopes
run_generic_read
end

it 'responds with a 200' do
expect(response.status).to eq(200)
end

it 'returns the correct response data' do
json_response = JSON.parse(response.body)
expect(json_response["project_preferences"]).to be_a(Array)
expect(json_response["project_preferences"].count).to eq(1)
end
end

describe 'user specific preferences' do
before(:each) do
default_request user_id: unauthorised_user.id, scopes: scopes
run_unauthorised_user_read
end

it 'responds with a 200' do
expect(response.status).to eq(200)
end

it 'returns the correct response data' do
json_response = JSON.parse(response.body)
expect(json_response["project_preferences"].count).to eq(0)
end
end
end
end

0 comments on commit 91d871e

Please sign in to comment.