From d56c61bf8488225317e4bffb822f1e7f19af5fd6 Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Thu, 28 Oct 2021 09:49:15 +0300 Subject: [PATCH] resolve structure conflict --- .../accreditation_info_controller.rb | 10 ++- .../accreditation_results_controller.rb | 75 +++++++++++++++++++ config/routes.rb | 5 +- .../20210729131100_add_field_to_user.rb | 6 ++ .../20210729134625_add_column_to_user.rb | 5 ++ db/structure.sql | 11 ++- .../v1/registrar/accreditaion_info_test.rb | 4 +- 7 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 app/controllers/repp/v1/registrar/accreditation_results_controller.rb create mode 100644 db/migrate/20210729131100_add_field_to_user.rb create mode 100644 db/migrate/20210729134625_add_column_to_user.rb diff --git a/app/controllers/repp/v1/registrar/accreditation_info_controller.rb b/app/controllers/repp/v1/registrar/accreditation_info_controller.rb index 5859b8acf..3851ded65 100644 --- a/app/controllers/repp/v1/registrar/accreditation_info_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_info_controller.rb @@ -2,17 +2,23 @@ module Repp module V1 module Registrar class AccreditationInfoController < BaseController - api :GET, 'repp/v1/registrar/accreditation_info' + api :GET, 'repp/v1/registrar/accreditation/get_info' desc 'check login user and return data' def index @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 # rubocop:enable Style/AndOr - data = @login.as_json + 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 diff --git a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb new file mode 100644 index 000000000..a959159c8 --- /dev/null +++ b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb @@ -0,0 +1,75 @@ +module Repp + module V1 + module Registrar + class AccreditationResultsController < ActionController::API + before_action :authenticate_admin + + # api :POST, 'repp/v1/registrar/push_results' + api :GET, 'repp/v1/registrar/accreditation/push_results' + desc 'added datetime results' + + def index + @login = @current_user + + # rubocop:disable Style/AndOr + render_success(data: nil) and return unless @login + # rubocop:enable Style/AndOr + + data = @login + render_success(data: data) + 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) + + return if @current_user + # return if @current_user.roles.include? "admin" + + raise(ArgumentError) + rescue NoMethodError, ArgumentError + @response = { code: 2202, message: 'Invalid authorization information' } + render(json: @response, status: :unauthorized) + end + + def basic_token + pattern = /^Basic / + header = request.headers['Authorization'] + header = header.gsub(pattern, '') if header&.match(pattern) + header.strip + end + + 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) + end + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 21827911e..a1e3d58e8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -72,9 +72,10 @@ Rails.application.routes.draw do get '/all_notifications', to: 'notifications#all_notifications' end end - resource :accreditation_info, only: [:index] do + resource :accreditation, only: [:index] do collection do - get '/', to: 'accreditation_info#index' + get '/get_info', to: 'accreditation_info#index' + get '/push_results', to: 'accreditation_results#index' end end resources :nameservers do diff --git a/db/migrate/20210729131100_add_field_to_user.rb b/db/migrate/20210729131100_add_field_to_user.rb new file mode 100644 index 000000000..38efcea49 --- /dev/null +++ b/db/migrate/20210729131100_add_field_to_user.rb @@ -0,0 +1,6 @@ +class AddFieldToUser < ActiveRecord::Migration[6.1] + def change + add_column :users, :accreditation_date, :datetime + add_column :users, :accreditation_expire_date, :datetime + end +end diff --git a/db/migrate/20210729134625_add_column_to_user.rb b/db/migrate/20210729134625_add_column_to_user.rb new file mode 100644 index 000000000..c2131d3fb --- /dev/null +++ b/db/migrate/20210729134625_add_column_to_user.rb @@ -0,0 +1,5 @@ +class AddColumnToUser < ActiveRecord::Migration[6.1] + def change + add_column :users, :uuid, :uuid, default: 'gen_random_uuid()' + end +end diff --git a/db/structure.sql b/db/structure.sql index fdfacff95..d718d0d33 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2582,7 +2582,10 @@ CREATE TABLE public.users ( remember_created_at timestamp without time zone, failed_attempts integer DEFAULT 0 NOT NULL, locked_at timestamp without time zone, - legacy_id integer + legacy_id integer, + accreditation_date timestamp without time zone, + accreditation_expire_date timestamp without time zone, + uuid uuid DEFAULT public.gen_random_uuid() ); @@ -5230,6 +5233,6 @@ INSERT INTO "schema_migrations" (version) VALUES ('20210616112332'), ('20210629074044'), ('20210628090353'), -('20210708131814'); - - +('20210708131814'), +('20210729131100'), +('20210729134625'); diff --git a/test/integration/repp/v1/registrar/accreditaion_info_test.rb b/test/integration/repp/v1/registrar/accreditaion_info_test.rb index c2a23d2c0..237fa319a 100644 --- a/test/integration/repp/v1/registrar/accreditaion_info_test.rb +++ b/test/integration/repp/v1/registrar/accreditaion_info_test.rb @@ -10,7 +10,7 @@ class ReppV1AccreditationInfoTest < ActionDispatch::IntegrationTest end def test_valid_login - get '/repp/v1/registrar/accreditation_info', headers: @auth_headers + get '/repp/v1/registrar/accreditation/get_info', headers: @auth_headers json = JSON.parse(response.body, symbolize_names: true) assert_response :ok @@ -24,7 +24,7 @@ class ReppV1AccreditationInfoTest < ActionDispatch::IntegrationTest auth_headers = { 'Authorization' => token } - get '/repp/v1/registrar/accreditation_info', headers: auth_headers + get '/repp/v1/registrar/accreditation/get_info', headers: auth_headers json = JSON.parse(response.body, symbolize_names: true) assert_response :unauthorized