From 972f98bac06b47cde33e5105b3a0278908fb82cb Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Thu, 8 Aug 2019 17:45:00 +0200 Subject: [PATCH] fix issn support --- app/controllers/clients_controller.rb | 2 +- app/controllers/repositories_controller.rb | 2 +- app/models/client.rb | 4 +++- app/serializers/client_serializer.rb | 2 +- app/serializers/repository_serializer.rb | 2 +- spec/requests/repositories_spec.rb | 21 +++++++++++++++++++++ 6 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index 8619310ac..a4b3b910e 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -213,7 +213,7 @@ def set_client def safe_params fail JSON::ParserError, "You need to provide a payload following the JSONAPI spec" unless params[:data].present? ActiveModelSerializers::Deserialization.jsonapi_parse!( - params, only: [:symbol, :name, "systemEmail", :domains, :provider, :url, "repositoryType", { "repositoryType" => [] }, :description, :language, { language: [] }, "alternateName", :software, "targetId", "isActive", "passwordInput", "clientType", :re3data, :opendoar, :issn, { issn: [:issl, :electronic, :print] }, :certificate, { certificate: [] }, "serviceContact", { "serviceContact": [:email, "givenName", "familyName"] }, "salesforceId"], + params, only: [:symbol, :name, "systemEmail", :domains, :provider, :url, "repositoryType", { "repositoryType" => [] }, :description, :language, { language: [] }, "alternateName", :software, "targetId", "isActive", "passwordInput", "clientType", :re3data, :opendoar, :issn, { issn: [:issnl, :electronic, :print] }, :certificate, { certificate: [] }, "serviceContact", { "serviceContact": [:email, "givenName", "familyName"] }, "salesforceId"], keys: { "systemEmail" => :system_email, "salesforceId" => :salesforce_id, "targetId" => :target_id, "isActive" => :is_active, "passwordInput" => :password_input, "clientType" => :client_type, "alternateName" => :alternate_name, "repositoryType" => :repository_type, "serviceContact" => :service_contact } ) end diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 88fa2395e..9640ad2a3 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -188,7 +188,7 @@ def set_repository def safe_params fail JSON::ParserError, "You need to provide a payload following the JSONAPI spec" unless params[:data].present? ActiveModelSerializers::Deserialization.jsonapi_parse!( - params, only: [:symbol, :name, "systemEmail", :domains, :provider, :url, "repositoryType", { "repositoryType" => [] }, :description, :language, { language: [] }, "alternateName", :software, "targetId", "isActive", "passwordInput", "clientType", :re3data, :opendoar, :issn, { issn: [:issl, :electronic, :print] }, :certificate, { certificate: [] }, "serviceContact", { "serviceContact": [:email, "givenName", "familyName"] }, "salesforceId"], + params, only: [:symbol, :name, "systemEmail", :domains, :provider, :url, "repositoryType", { "repositoryType" => [] }, :description, :language, { language: [] }, "alternateName", :software, "targetId", "isActive", "passwordInput", "clientType", :re3data, :opendoar, :issn, { issn: [:issnl, :electronic, :print] }, :certificate, { certificate: [] }, "serviceContact", { "serviceContact": [:email, "givenName", "familyName"] }, "salesforceId"], keys: { "systemEmail" => :system_email, "salesforceId" => :salesforce_id, "targetId" => :target_id, "isActive" => :is_active, "passwordInput" => :password_input, "clientType" => :client_type, "alternateName" => :alternate_name, "repositoryType" => :repository_type, "serviceContact" => :service_contact } ) end diff --git a/app/models/client.rb b/app/models/client.rb index d3424d397..f8c4576a2 100644 --- a/app/models/client.rb +++ b/app/models/client.rb @@ -369,7 +369,9 @@ def to_jsonapi def check_issn Array.wrap(issn).each do |i| - if i["issnl"].present? + if !(i.is_a?(Hash)) + errors.add(:issn, "ISSN should be an object and not a string.") + elsif i["issnl"].present? errors.add(:issn, "ISSN-L #{i["issnl"]} is in the wrong format.") unless /\A\d{4}(-)?\d{3}[0-9X]+\z/.match(i["issnl"]) end if i["electronic"].present? diff --git a/app/serializers/client_serializer.rb b/app/serializers/client_serializer.rb index ce0bc368d..09035d9ba 100644 --- a/app/serializers/client_serializer.rb +++ b/app/serializers/client_serializer.rb @@ -4,7 +4,7 @@ class ClientSerializer set_type :clients set_id :uid - attributes :name, :symbol, :year, :contact_email, :alternate_name, :description, :language, :client_type, :domains, :url, :salesforce_id, :created, :updated + attributes :name, :symbol, :year, :contact_email, :alternate_name, :description, :language, :client_type, :domains, :issn, :url, :salesforce_id, :created, :updated belongs_to :provider, record_type: :providers belongs_to :consortium, record_type: :providers, serializer: ProviderSerializer, if: Proc.new { |client| client.consortium_id } diff --git a/app/serializers/repository_serializer.rb b/app/serializers/repository_serializer.rb index 43616bbc4..e33c53c50 100644 --- a/app/serializers/repository_serializer.rb +++ b/app/serializers/repository_serializer.rb @@ -4,7 +4,7 @@ class RepositorySerializer set_type :repositories set_id :uid - attributes :name, :symbol, :re3data, :opendoar, :year, :system_email, :alternate_name, :description, :client_type, :repository_type, :language, :certificate, :domains, :url, :salesforce_id, :created, :updated + attributes :name, :symbol, :re3data, :opendoar, :year, :system_email, :alternate_name, :description, :client_type, :repository_type, :language, :certificate, :domains, :issn, :url, :salesforce_id, :created, :updated belongs_to :provider, record_type: :providers has_many :prefixes, record_type: :prefixes diff --git a/spec/requests/repositories_spec.rb b/spec/requests/repositories_spec.rb index 870db490a..0f02ee2e8 100644 --- a/spec/requests/repositories_spec.rb +++ b/spec/requests/repositories_spec.rb @@ -174,6 +174,27 @@ end end + context 'updating with ISSNs' do + let(:params) do + { "data" => { "type" => "repositories", + "attributes" => { + "name" => "Journal of Insignificant Results", + "clientType" => "periodical", + "issn" => { "electronic" => "1544-9173", + "print" => "1545-7885" } }} } + end + + it 'updates the record' do + put "/repositories/#{client.symbol}", params, headers + + expect(last_response.status).to eq(200) + expect(json.dig('data', 'attributes', 'name')).to eq("Journal of Insignificant Results") + expect(json.dig('data', 'attributes', 'name')).not_to eq(client.name) + expect(json.dig('data', 'attributes', 'clientType')).to eq("periodical") + expect(json.dig('data', 'attributes', 'issn')).to eq("electronic"=>"1544-9173", "print"=>"1545-7885") + end + end + context 'when the request is invalid' do let(:params) do { "data" => { "type" => "repositories",