diff --git a/app/controllers/api/v1/business_registry/reserve_domains_controller.rb b/app/controllers/api/v1/business_registry/reserve_domains_controller.rb index 4c26dfb45..57d2b10db 100644 --- a/app/controllers/api/v1/business_registry/reserve_domains_controller.rb +++ b/app/controllers/api/v1/business_registry/reserve_domains_controller.rb @@ -2,7 +2,12 @@ module Api module V1 module BusinessRegistry class ReserveDomainsController < BaseController - before_action :validate_params + before_action :validate_params, only: :create + before_action :set_free_domain_reservation_holder, only: :show + + def show + render_success(@free_domain_reservation_holder.output_reserved_domains) + end def create domain_names = params[:domain_names] @@ -27,6 +32,11 @@ def create private + def set_free_domain_reservation_holder + @free_domain_reservation_holder = FreeDomainReservationHolder.find_by(user_unique_id: params[:user_unique_id]) + render_error("Reserved domains not found. Invalid user_unique_id", :not_found) unless @free_domain_reservation_holder + end + def validate_params domain_names = params[:domain_names] diff --git a/app/models/free_domain_reservation_holder.rb b/app/models/free_domain_reservation_holder.rb index 6d7c316bc..693b881e8 100644 --- a/app/models/free_domain_reservation_holder.rb +++ b/app/models/free_domain_reservation_holder.rb @@ -6,10 +6,19 @@ def reserved_domains ReservedDomain.where(name: domain_names) end + def output_reserved_domains + reserved_domains.map do |domain| + { + name: domain.name, + password: domain.password, + expire_at: domain.expire_at + } + end + end + private def set_user_unique_id self.user_unique_id = SecureRandom.uuid[0..9] end - end diff --git a/config/routes.rb b/config/routes.rb index 67d3d9097..f366c1790 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -181,6 +181,7 @@ get 'domain_names/:organization_name', to: 'domain_names#show', as: 'domain_names' get 'long_reserve_domains_status', to: 'long_reserve_domains_status#show', as: 'long_reserve_domains_status' post 'reserve_domains', to: 'reserve_domains#create', as: 'reserve_domains' + get 'reserve_domains/:user_unique_id', to: 'reserve_domains#show', as: 'reserve_domains_data' post 'long_reserve_domains', to: 'long_reserve_domains#create', as: 'long_reserve_domains' get 'registration_code', to: 'registration_code#show', as: 'registration_code' end diff --git a/test/integration/api/business_registry/reserve_domains_controller_test.rb b/test/integration/api/business_registry/reserve_domains_controller_test.rb index 945b4706a..07a609bc1 100644 --- a/test/integration/api/business_registry/reserve_domains_controller_test.rb +++ b/test/integration/api/business_registry/reserve_domains_controller_test.rb @@ -143,4 +143,35 @@ def setup assert_equal "No available domains", json_response['error'] end end + + test "should get reserved domains info by user_unique_id" do + # Create test data + domain_names = ["test1.test", "test2.test"] + result = ReservedDomain.reserve_domains_without_payment(domain_names) + + # Make request + get api_v1_business_registry_reserve_domains_data_path(user_unique_id: result.user_unique_id), + headers: @auth_headers + + assert_response :success + json_response = JSON.parse(response.body) + + # Check response structure + assert_equal 2, json_response.length + json_response.each do |domain| + assert_includes domain_names, domain['name'] + assert_not_nil domain['password'] + assert_not_nil domain['expire_at'] + assert Time.parse(domain['expire_at']) > Time.current + end + end + + test "should return error for non-existent user_unique_id" do + get api_v1_business_registry_reserve_domains_data_path(user_unique_id: 'non-existent'), + headers: @auth_headers + + assert_response :not_found + json_response = JSON.parse(response.body) + assert_equal "Reserved domains not found. Invalid user_unique_id", json_response['error'] + end end \ No newline at end of file diff --git a/test/models/free_domain_reservation_holder_test.rb b/test/models/free_domain_reservation_holder_test.rb index 6aae40295..73c7b92f0 100644 --- a/test/models/free_domain_reservation_holder_test.rb +++ b/test/models/free_domain_reservation_holder_test.rb @@ -30,4 +30,28 @@ def setup ) assert_includes holder.reserved_domains, reserved_domain end + + test "output_reserved_domains should return correct structure" do + # Create test data + domain_names = ['test1.test', 'test2.test'] + holder = FreeDomainReservationHolder.create!(domain_names: domain_names) + + domain_names.each do |name| + ReservedDomain.create!( + name: name, + password: 'test-password', + expire_at: Time.current + 7.days + ) + end + + # Test the method + output = holder.output_reserved_domains + + assert_equal domain_names.length, output.length + output.each do |domain| + assert_includes domain_names, domain[:name] + assert_not_nil domain[:password] + assert_not_nil domain[:expire_at] + end + end end \ No newline at end of file