From c79042f7890eccb9a60d761f132ac34c83c1fee4 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Mon, 12 Oct 2020 16:25:10 +0500 Subject: [PATCH 1/5] Add registrant api companies endpoint --- .codeclimate.yml | 1 + Gemfile | 2 +- Gemfile.lock | 4 +- .../api/v1/registrant/companies_controller.rb | 41 +++++++++++++++++++ config/routes.rb | 3 ++ lib/serializers/registrant_api/company.rb | 20 +++++++++ .../registrant_api_companies_test.rb | 35 ++++++++++++++++ test/test_helper.rb | 4 +- 8 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 app/controllers/api/v1/registrant/companies_controller.rb create mode 100644 lib/serializers/registrant_api/company.rb create mode 100644 test/integration/api/registrant/registrant_api_companies_test.rb diff --git a/.codeclimate.yml b/.codeclimate.yml index 2bc90b200..f7ff3bab1 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -32,6 +32,7 @@ exclude_patterns: - "bin/" - "config/" - "db/" + - "app/controllers/api/v1/registrant/companies_controller.rb" - "lib/core_monkey_patches/" - "lib/daemons/" - "lib/gem_monkey_patches/" diff --git a/Gemfile b/Gemfile index 6ba54e871..04ec5f13b 100644 --- a/Gemfile +++ b/Gemfile @@ -68,7 +68,7 @@ gem 'pdfkit' gem 'jquery-ui-rails', '5.0.5' gem 'airbrake' -gem 'company_register', github: 'internetee/company_register', branch: :master +gem 'company_register', github: 'internetee/company_register', branch: '1708-registrant-companies-endpoint' gem 'e_invoice', github: 'internetee/e_invoice', branch: :master gem 'lhv', github: 'internetee/lhv', branch: 'master' gem 'domain_name' diff --git a/Gemfile.lock b/Gemfile.lock index 4ebedc95c..c79ac1e93 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ GIT remote: https://github.com/internetee/company_register.git - revision: 86d691997aa7def9f86d88f6c92cabb86cd65487 - branch: master + revision: 13c5cfdf2dea8e13bf52628b566c0fecc2ad1c6a + branch: 1708-registrant-companies-endpoint specs: company_register (0.1.0) activesupport diff --git a/app/controllers/api/v1/registrant/companies_controller.rb b/app/controllers/api/v1/registrant/companies_controller.rb new file mode 100644 index 000000000..f8f2554c0 --- /dev/null +++ b/app/controllers/api/v1/registrant/companies_controller.rb @@ -0,0 +1,41 @@ +require 'serializers/registrant_api/company' + +module Api + module V1 + module Registrant + class CompaniesController < ::Api::V1::Registrant::BaseController + def index + limit = params[:limit] || 200 + offset = params[:offset] || 0 + + if limit.to_i > 200 || limit.to_i < 1 + render(json: { errors: [{ limit: ['parameter is out of range'] }] }, + status: :bad_request) && return + end + + if offset.to_i.negative? + render(json: { errors: [{ offset: ['parameter is out of range'] }] }, + status: :bad_request) && return + end + + @companies = current_user_companies.drop(offset.to_i).first(limit.to_i) + + serialized_companies = @companies.map do |item| + country_code = current_registrant_user.country.alpha3 + serializer = ::Serializers::RegistrantApi::Company.new(company: item, + country_code: country_code) + serializer.to_json + end + + render json: serialized_companies + end + + def current_user_companies + current_registrant_user.companies + rescue CompanyRegister::NotAvailableError + nil + end + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 41f857bc8..4a996bfbb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -61,9 +61,11 @@ Rails.application.routes.draw do resource :registry_lock, only: %i[create destroy] end resources :contacts, only: %i[index show update], param: :uuid + resources :companies, only: %i[index] end resources :auctions, only: %i[index show update], param: :uuid + end match '*all', controller: 'cors', action: 'cors_preflight_check', via: [:options], @@ -179,6 +181,7 @@ Rails.application.routes.draw do end resources :registrars, only: :show + # resources :companies, only: :index resources :domains, only: %i[index show] do resources :contacts, only: %i[show edit update] member do diff --git a/lib/serializers/registrant_api/company.rb b/lib/serializers/registrant_api/company.rb new file mode 100644 index 000000000..d0d914cd7 --- /dev/null +++ b/lib/serializers/registrant_api/company.rb @@ -0,0 +1,20 @@ +module Serializers + module RegistrantApi + class Company + attr_reader :company, :country_code + + def initialize(company:, country_code:) + @company = company + @country_code = country_code + end + + def to_json(*_args) + { + name: company.company_name, + registry_no: company.registration_number, + country_code: @country_code, + } + end + end + end +end diff --git a/test/integration/api/registrant/registrant_api_companies_test.rb b/test/integration/api/registrant/registrant_api_companies_test.rb new file mode 100644 index 000000000..20b9f3896 --- /dev/null +++ b/test/integration/api/registrant/registrant_api_companies_test.rb @@ -0,0 +1,35 @@ +require 'test_helper' +require 'auth_token/auth_token_creator' + +class RegistrantApiCompaniesTest < ApplicationIntegrationTest + def setup + super + + @contact = contacts(:john) + @user = users(:registrant) + @auth_headers = { 'HTTP_AUTHORIZATION' => auth_token } + end + + def test_root_accepts_limit_and_offset_parameters + contacts(:william).update!(ident: '1234', ident_type: 'priv', ident_country_code: 'US') + assert_equal 4, @user.contacts(representable: false).size + + get '/api/v1/registrant/companies', params: { 'limit' => 1, 'offset' => 0 }, + headers: @auth_headers + response_json = JSON.parse(response.body, symbolize_names: true) + assert_equal(200, response.status) + assert_equal(1, response_json.count) + + get '/api/v1/registrant/companies', headers: @auth_headers + response_json = JSON.parse(response.body, symbolize_names: true) + assert_equal(@user.companies.size, response_json.count) + end + + private + + def auth_token + token_creator = AuthTokenCreator.create_with_defaults(@user) + hash = token_creator.token_in_hash + "Bearer #{hash[:access_token]}" + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 6e1b10c88..459d4f8f5 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -27,10 +27,10 @@ Rake::Task.clear Rails.application.load_tasks class CompanyRegisterClientStub - Company = Struct.new(:registration_number) + Company = Struct.new(:registration_number, :company_name) def representation_rights(citizen_personal_code:, citizen_country_code:) - [Company.new('1234567')] + [Company.new('1234567', 'ACME Ltd')] end end From c43ae63521a6ff800a2894d51652c85e8cf42fa3 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Tue, 13 Oct 2020 12:52:40 +0500 Subject: [PATCH 2/5] Fix output format & format test --- .codeclimate.yml | 1 - Gemfile | 3 +- .../api/v1/registrant/companies_controller.rb | 49 ++++++++++++------- .../registrant_api_companies_test.rb | 14 ++++-- 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/.codeclimate.yml b/.codeclimate.yml index f7ff3bab1..2bc90b200 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -32,7 +32,6 @@ exclude_patterns: - "bin/" - "config/" - "db/" - - "app/controllers/api/v1/registrant/companies_controller.rb" - "lib/core_monkey_patches/" - "lib/daemons/" - "lib/gem_monkey_patches/" diff --git a/Gemfile b/Gemfile index 04ec5f13b..5b4091f4b 100644 --- a/Gemfile +++ b/Gemfile @@ -68,7 +68,8 @@ gem 'pdfkit' gem 'jquery-ui-rails', '5.0.5' gem 'airbrake' -gem 'company_register', github: 'internetee/company_register', branch: '1708-registrant-companies-endpoint' +gem 'company_register', github: 'internetee/company_register', + branch: '1708-registrant-companies-endpoint' gem 'e_invoice', github: 'internetee/e_invoice', branch: :master gem 'lhv', github: 'internetee/lhv', branch: 'master' gem 'domain_name' diff --git a/app/controllers/api/v1/registrant/companies_controller.rb b/app/controllers/api/v1/registrant/companies_controller.rb index f8f2554c0..b11c06909 100644 --- a/app/controllers/api/v1/registrant/companies_controller.rb +++ b/app/controllers/api/v1/registrant/companies_controller.rb @@ -4,21 +4,39 @@ module Api module V1 module Registrant class CompaniesController < ::Api::V1::Registrant::BaseController + MAX_LIMIT = 200 + MIN_OFFSET = 0 + def index - limit = params[:limit] || 200 - offset = params[:offset] || 0 + result = error_result('limit') if limit > MAX_LIMIT || limit < 1 + result = error_result('offset') if offset < MIN_OFFSET + result ||= companies_result(limit, offset) - if limit.to_i > 200 || limit.to_i < 1 - render(json: { errors: [{ limit: ['parameter is out of range'] }] }, - status: :bad_request) && return - end + render result + end - if offset.to_i.negative? - render(json: { errors: [{ offset: ['parameter is out of range'] }] }, - status: :bad_request) && return - end + def current_user_companies + current_registrant_user.companies + rescue CompanyRegister::NotAvailableError + [] + end - @companies = current_user_companies.drop(offset.to_i).first(limit.to_i) + def limit + (params[:limit] || MAX_LIMIT).to_i + end + + def offset + (params[:offset] || MIN_OFFSET).to_i + end + + def error_result(attr_name) + { json: { errors: [{ attr_name.to_sym => ['parameter is out of range'] }] }, + status: :bad_request } + end + + def companies_result(limit, offset) + @companies = current_user_companies.drop(offset).first(limit) + status = @companies.present? ? :ok : :not_found serialized_companies = @companies.map do |item| country_code = current_registrant_user.country.alpha3 @@ -26,14 +44,7 @@ module Api country_code: country_code) serializer.to_json end - - render json: serialized_companies - end - - def current_user_companies - current_registrant_user.companies - rescue CompanyRegister::NotAvailableError - nil + { json: { companies: serialized_companies }, status: status } end end end diff --git a/test/integration/api/registrant/registrant_api_companies_test.rb b/test/integration/api/registrant/registrant_api_companies_test.rb index 20b9f3896..b08383796 100644 --- a/test/integration/api/registrant/registrant_api_companies_test.rb +++ b/test/integration/api/registrant/registrant_api_companies_test.rb @@ -10,21 +10,29 @@ class RegistrantApiCompaniesTest < ApplicationIntegrationTest @auth_headers = { 'HTTP_AUTHORIZATION' => auth_token } end - def test_root_accepts_limit_and_offset_parameters + def test_accepts_limit_and_offset_parameters contacts(:william).update!(ident: '1234', ident_type: 'priv', ident_country_code: 'US') - assert_equal 4, @user.contacts(representable: false).size get '/api/v1/registrant/companies', params: { 'limit' => 1, 'offset' => 0 }, headers: @auth_headers response_json = JSON.parse(response.body, symbolize_names: true) assert_equal(200, response.status) - assert_equal(1, response_json.count) + assert_equal(1, response_json.values.flatten.count) get '/api/v1/registrant/companies', headers: @auth_headers response_json = JSON.parse(response.body, symbolize_names: true) assert_equal(@user.companies.size, response_json.count) end + def test_format + contacts(:william).update!(ident: '1234', ident_type: 'priv', ident_country_code: 'US') + get '/api/v1/registrant/companies', headers: @auth_headers + response_json = JSON.parse(response.body, symbolize_names: true) + assert_equal(1, response_json.count) + assert response_json.is_a?(Hash) + assert_equal(:companies, response_json.keys.first) + end + private def auth_token From c59211239d78a6589ec55e669f4cfae8bf7a8150 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Wed, 14 Oct 2020 11:31:17 +0500 Subject: [PATCH 3/5] Update API documentation --- doc/registrant-api/v1/companies.md | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 doc/registrant-api/v1/companies.md diff --git a/doc/registrant-api/v1/companies.md b/doc/registrant-api/v1/companies.md new file mode 100644 index 000000000..2804a63c8 --- /dev/null +++ b/doc/registrant-api/v1/companies.md @@ -0,0 +1,34 @@ +## GET /api/v1/registrant/companies +Returns list of companies registered in business register for the current registrant. + + +#### Parameters + +| Field name | Required | Type | Allowed values | Description | +| ---------- | -------- | ---- | -------------- | ----------- | +| limit | false | Integer | [1..200] | How many companies to show | +| offset | false | Integer | | Company number to start at | + +#### Request +``` +GET /api/v1/registrant/companies?limit=1 HTTP/1.1 +Accept: application/json +Authorization: Bearer Z2l0bGFiOmdoeXQ5ZTRmdQ== +Content-Type: application/json +``` + +#### Response +``` +HTTP/1.1 200 +Content-Type: application/json + +{ + "companies":[ + { + "name":"ACME Ltd", + "registry_no":"37605030299", + "country_code":"EE" + } + ] +} +``` From ecaf8fc43d232d5aaddadf678cb92af3dc20b0e4 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Wed, 14 Oct 2020 18:43:47 +0500 Subject: [PATCH 4/5] Update company_register gem version --- Gemfile | 2 +- Gemfile.lock | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 5b4091f4b..25c3eafff 100644 --- a/Gemfile +++ b/Gemfile @@ -69,7 +69,7 @@ gem 'jquery-ui-rails', '5.0.5' gem 'airbrake' gem 'company_register', github: 'internetee/company_register', - branch: '1708-registrant-companies-endpoint' + branch: 'master' gem 'e_invoice', github: 'internetee/e_invoice', branch: :master gem 'lhv', github: 'internetee/lhv', branch: 'master' gem 'domain_name' diff --git a/Gemfile.lock b/Gemfile.lock index c79ac1e93..6c94dc56f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ GIT remote: https://github.com/internetee/company_register.git - revision: 13c5cfdf2dea8e13bf52628b566c0fecc2ad1c6a - branch: 1708-registrant-companies-endpoint + revision: 4181b2fff9080e2d87d15612561131f9e3fdfb4c + branch: master specs: company_register (0.1.0) activesupport From ce93425ad8599b492fb757502709b3070d80a5f7 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Wed, 14 Oct 2020 18:58:59 +0500 Subject: [PATCH 5/5] Fix array comparison in tests --- test/mailers/domain_delete_mailer_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mailers/domain_delete_mailer_test.rb b/test/mailers/domain_delete_mailer_test.rb index ae060921f..b65ba5d2e 100644 --- a/test/mailers/domain_delete_mailer_test.rb +++ b/test/mailers/domain_delete_mailer_test.rb @@ -73,7 +73,7 @@ class DomainDeleteMailerTest < ActionMailer::TestCase assert_emails 1 assert_equal ['legal@registry.test'], email.from - assert_equal @domain.force_delete_contact_emails, email.to + assert @domain.force_delete_contact_emails.sort == email.to.sort assert_equal 'Domeen shop.test on kustutusmenetluses' \ ' / Domain shop.test is in deletion process' \ ' / Домен shop.test в процессе удаления', email.subject