implement api endpoint for record results

This commit is contained in:
Oleg Hasjanov 2021-08-06 12:26:41 +03:00 committed by olegphenomenon
parent d56c61bf84
commit 8a1967bdb9
5 changed files with 104 additions and 57 deletions

View file

@ -6,22 +6,32 @@ module Repp
desc 'check login user and return data'
def index
@login = current_user
login = current_user
registrar = current_user.registrar
# name = registrar.name
# reg_no = registrar.reg_no
# rubocop:disable Style/AndOr
render_success(data: nil) and return unless @login
render_success(data: nil) and return unless login
# rubocop:enable Style/AndOr
data = @login.as_json(only: %i[id username name reg_no uuid roles accreditation_date accreditation_expire_date])
data[:registrar_name] = registrar.name
data[:registrar_reg_no] = registrar.reg_no
data = set_values_to_data(login: login, registrar: registrar)
render_success(data: data)
end
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
end
end
end
end

View file

@ -2,65 +2,51 @@ module Repp
module V1
module Registrar
class AccreditationResultsController < ActionController::API
before_action :authenticate_admin
before_action :authenticate_shared_key
# api :POST, 'repp/v1/registrar/push_results'
api :GET, 'repp/v1/registrar/accreditation/push_results'
TEMPARY_SECRET_KEY = 'tempary-secret-key'.freeze
api :POST, 'repp/v1/registrar/accreditation/push_results'
desc 'added datetime results'
def index
@login = @current_user
def create
username = params[:accreditation_result][:username]
result = params[:accreditation_result][:result]
# rubocop:disable Style/AndOr
render_success(data: nil) and return unless @login
# rubocop:enable Style/AndOr
data = @login
render_success(data: data)
record_accreditation_result(username, result) if result
rescue ActiveRecord::RecordNotFound
record_not_found(username)
end
# def create
# @login = current_user
# registrar = current_user.registrar
# rubocop:disable Style/AndOr
# render_success(data: nil) and return unless @login
# rubocop:enable Style/AndOr
# user = ApiUser.find(params[:user_id])
# user.accreditation_date = Date.now
# user.save
# data = @login.as_json(only: %i[id username name reg_no uuid roles accreditation_date accreditation_expire_date])
# data[:registrar_name] = registrar.name
# data[:registrar_reg_no] = registrar.reg_no
# render_success(data: data)
# end
private
def authenticate_admin
# TODO: ADD MORE CONDITIONS FOR ACCR ADMIN REQUESTS
username, password = Base64.urlsafe_decode64(basic_token).split(':')
@current_user ||= User.find_by(username: username, plain_text_password: password)
def record_accreditation_result(username, result)
user = ApiUser.find_by(username: username)
return if @current_user
# return if @current_user.roles.include? "admin"
raise ActiveRecord::RecordNotFound if user.nil?
raise(ArgumentError)
rescue NoMethodError, ArgumentError
@response = { code: 2202, message: 'Invalid authorization information' }
render(json: @response, status: :unauthorized)
user.accreditation_date = DateTime.current
return unless user.save
render_success(data: { user: user,
result: result,
message: 'Accreditation info successfully added' })
end
def basic_token
pattern = /^Basic /
header = request.headers['Authorization']
header = header.gsub(pattern, '') if header&.match(pattern)
header.strip
def authenticate_shared_key
api_key = "Basic #{TEMPARY_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 render_failed
@response = { code: 2202, message: 'Invalid authorization information' }
render(json: @response, status: :unauthorized)
end
def render_success(code: nil, message: nil, data: nil)