Merge pull request #1709 from internetee/1708-business-registry-endpoint

Add registrant api companies endpoint
This commit is contained in:
Timo Võhmar 2020-10-14 17:05:25 +03:00 committed by GitHub
commit c8552d9f43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 158 additions and 5 deletions

View file

@ -68,7 +68,8 @@ 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: 'master'
gem 'e_invoice', github: 'internetee/e_invoice', branch: :master
gem 'lhv', github: 'internetee/lhv', branch: 'master'
gem 'domain_name'

View file

@ -1,6 +1,6 @@
GIT
remote: https://github.com/internetee/company_register.git
revision: 86d691997aa7def9f86d88f6c92cabb86cd65487
revision: 4181b2fff9080e2d87d15612561131f9e3fdfb4c
branch: master
specs:
company_register (0.1.0)

View file

@ -0,0 +1,52 @@
require 'serializers/registrant_api/company'
module Api
module V1
module Registrant
class CompaniesController < ::Api::V1::Registrant::BaseController
MAX_LIMIT = 200
MIN_OFFSET = 0
def index
result = error_result('limit') if limit > MAX_LIMIT || limit < 1
result = error_result('offset') if offset < MIN_OFFSET
result ||= companies_result(limit, offset)
render result
end
def current_user_companies
current_registrant_user.companies
rescue CompanyRegister::NotAvailableError
[]
end
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
serializer = ::Serializers::RegistrantApi::Company.new(company: item,
country_code: country_code)
serializer.to_json
end
{ json: { companies: serialized_companies }, status: status }
end
end
end
end
end

View file

@ -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

View file

@ -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"
}
]
}
```

View file

@ -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

View file

@ -0,0 +1,43 @@
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_accepts_limit_and_offset_parameters
contacts(:william).update!(ident: '1234', ident_type: 'priv', ident_country_code: 'US')
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.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
token_creator = AuthTokenCreator.create_with_defaults(@user)
hash = token_creator.token_in_hash
"Bearer #{hash[:access_token]}"
end
end

View file

@ -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

View file

@ -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