mirror of
https://github.com/internetee/registry.git
synced 2025-06-06 04:37:30 +02:00
Add registrant api companies endpoint
This commit is contained in:
parent
497ba6514d
commit
c79042f789
8 changed files with 105 additions and 5 deletions
|
@ -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/"
|
||||
|
|
2
Gemfile
2
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'
|
||||
|
|
|
@ -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
|
||||
|
|
41
app/controllers/api/v1/registrant/companies_controller.rb
Normal file
41
app/controllers/api/v1/registrant/companies_controller.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
20
lib/serializers/registrant_api/company.rb
Normal file
20
lib/serializers/registrant_api/company.rb
Normal 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
|
|
@ -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
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue