added endpoints to demo registry for accr results

This commit is contained in:
olegphenomenon 2021-12-10 16:53:25 +02:00
parent 487cbb1774
commit c07abaea59
20 changed files with 401 additions and 78 deletions

View file

@ -1,36 +1,38 @@
module Repp
module V1
module Registrar
class AccreditationInfoController < BaseController
api :GET, 'repp/v1/registrar/accreditation/get_info'
desc 'check login user and return data'
if Rails.env.development? || Rails.env.staging?
class AccreditationInfoController < BaseController
api :GET, 'repp/v1/registrar/accreditation/get_info'
desc 'check login user and return data'
def index
login = current_user
registrar = current_user.registrar
def index
login = current_user
registrar = current_user.registrar
# rubocop:disable Style/AndOr
render_success(data: nil) and return unless login
# rubocop:enable Style/AndOr
# rubocop:disable Style/AndOr
render_success(data: nil) and return unless login
# rubocop:enable Style/AndOr
data = set_values_to_data(login: login, registrar: registrar)
data = set_values_to_data(login: login, registrar: registrar)
render_success(data: data)
end
render_success(data: data)
end
private
private
def set_values_to_data(login:, registrar:)
data = login.as_json(only: %i[id
username
name
uuid
roles
accreditation_date
accreditation_expire_date])
data[:registrar_name] = registrar.name
data[:registrar_reg_no] = registrar.reg_no
data
def set_values_to_data(login:, registrar:)
data = login.as_json(only: %i[id
username
name
uuid
roles
accreditation_date
accreditation_expire_date])
data[:registrar_name] = registrar.name
data[:registrar_reg_no] = registrar.reg_no
data
end
end
end
end

View file

@ -1,30 +1,31 @@
module Repp
module V1
module Registrar
class AccreditationResultsController < ActionController::API
before_action :authenticate_shared_key
if Rails.env.development? || Rails.env.staging?
class AccreditationResultsController < ActionController::API
before_action :authenticate_shared_key
TEMPORARY_SECRET_KEY = ENV['accreditation_secret'].freeze
EXPIRE_DEADLINE = 15.minutes.freeze
api :POST, 'repp/v1/registrar/accreditation/push_results'
desc 'added datetime results'
api :POST, 'repp/v1/registrar/accreditation/push_results'
desc 'added datetime results'
def create
username = params[:accreditation_result][:username]
result = params[:accreditation_result][:result]
def create
username = params[:accreditation_result][:username]
result = params[:accreditation_result][:result]
record_accreditation_result(username, result) if result
rescue ActiveRecord::RecordNotFound
record_not_found(username)
end
record_accreditation_result(username, result) if result
rescue ActiveRecord::RecordNotFound
record_not_found(username)
end
private
private
def record_accreditation_result(username, result)
user = ApiUser.find_by(username: username)
def record_accreditation_result(username, result)
user = ApiUser.find_by(username: username)
raise ActiveRecord::RecordNotFound if user.nil?
raise ActiveRecord::RecordNotFound if user.nil?
user.accreditation_date = DateTime.current
user.accreditation_expire_date = user.accreditation_date + EXPIRE_DEADLINE
@ -55,26 +56,27 @@ module Repp
end
end
def authenticate_shared_key
api_key = "Basic #{TEMPORARY_SECRET_KEY}"
render_failed unless api_key == request.authorization
end
def authenticate_shared_key
api_key = "Basic #{TEMPORARY_SECRET_KEY}"
render_failed unless api_key == request.authorization
end
def record_not_found(username)
@response = { code: 2303, message: "Object '#{username}' does not exist" }
render(json: @response)
end
def record_not_found(username)
@response = { code: 2303, message: "Object '#{username}' does not exist" }
render(json: @response)
end
def render_failed
@response = { code: 2202, message: 'Invalid authorization information' }
render(json: @response, status: :unauthorized)
end
def render_failed
@response = { code: 2202, message: 'Invalid authorization information' }
render(json: @response, status: :unauthorized)
end
def render_success(code: nil, message: nil, data: nil)
@response = { code: code || 1000, message: message || 'Command completed successfully',
data: data || {} }
def render_success(code: nil, message: nil, data: nil)
@response = { code: code || 1000, message: message || 'Command completed successfully',
data: data || {} }
render(json: @response, status: :ok)
render(json: @response, status: :ok)
end
end
end
end