mirror of
https://github.com/internetee/registry.git
synced 2025-06-10 14:44:47 +02:00
Merge remote-tracking branch 'origin/master' into log-bounced-emails
This commit is contained in:
commit
fc34105c40
70 changed files with 791 additions and 4967 deletions
52
app/controllers/api/v1/registrant/companies_controller.rb
Normal file
52
app/controllers/api/v1/registrant/companies_controller.rb
Normal 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
|
||||
[:ok, current_registrant_user.companies]
|
||||
rescue CompanyRegister::NotAvailableError
|
||||
[:service_unavailable, []]
|
||||
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)
|
||||
status, all_companies = current_user_companies
|
||||
@companies = all_companies.drop(offset).first(limit)
|
||||
|
||||
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
|
|
@ -91,7 +91,7 @@ module Api
|
|||
private
|
||||
|
||||
def current_user_contacts
|
||||
current_registrant_user.contacts
|
||||
current_registrant_user.contacts(representable: false)
|
||||
rescue CompanyRegister::NotAvailableError
|
||||
current_registrant_user.direct_contacts
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue