From 8c697cbe09b19106d6460a375896e33b01de511b Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Wed, 27 Apr 2022 11:55:36 +0300 Subject: [PATCH] refactoring --- app/controllers/admin/auctions_controller.rb | 65 ++++++++++++++++--- .../admin/reserved_domains_controller.rb | 2 +- app/models/auction.rb | 2 +- app/models/dns/domain_name.rb | 2 +- app/views/admin/auctions/index.html.erb | 6 +- .../admin/reserved_domains/index.html.erb | 5 +- 6 files changed, 65 insertions(+), 17 deletions(-) diff --git a/app/controllers/admin/auctions_controller.rb b/app/controllers/admin/auctions_controller.rb index 80793e086c..92ce6f76e9 100644 --- a/app/controllers/admin/auctions_controller.rb +++ b/app/controllers/admin/auctions_controller.rb @@ -9,6 +9,7 @@ def index .with_status(params[:statuses_contains]) .with_start_created_at_date(params[:created_at_start]) .with_end_created_at_date(params[:created_at_end]) + .order(created_at: :desc) @auction = Auction.new @@ -28,7 +29,18 @@ def index end def create - auction = Auction.new(domain: params[:domain], status: Auction.statuses[:started], platform: 'manually') + auction = Auction.new(domain: params[:domain], status: Auction.statuses[:started], platform: 'manual') + + if domain_exists_in_blocked_disputed_and_registered?(params[:domain]) + flash[:alert] = "Adding #{params[:domain]} failed - domain registered or regsitration is blocked" + redirect_to admin_auctions_path and return + end + + result = check_availability(params[:domain])[0] + if result[:avail] == 0 + flash[:alert] = "Cannot generate domain. Reason: #{result[:reason]}" + redirect_to admin_auctions_path and return + end if auction.save remove_from_reserved(auction) @@ -41,33 +53,66 @@ def create end def upload_spreadsheet + if params[:q].nil? + flash[:alert] = "No file upload! Look at the left of upload button!" + redirect_to admin_auctions_path and return + end + filename = params[:q][:file] table = CSV.parse(File.read(filename), headers: true) + failed_names = [] + if validate_table(table) table.each do |row| record = row.to_h - auction = Auction.new(domain: record['name'], status: Auction.statuses[:started], platform: 'manually') + + if domain_exists_in_blocked_disputed_and_registered?(record['name']) + failed_names << record['name'] + + next + end + + result = check_availability(record['name'])[0] + if result[:avail] == 0 + failed_names << record['name'] + + next + end + + auction = Auction.new(domain: record['name'], status: Auction.statuses[:started], platform: 'manual') remove_from_reserved(auction) if auction.save! end - flash[:notice] = "Domains added" + + if failed_names.empty? + flash[:notice] = "Domains added!" + else + flash[:notice] = "Domains added! But these domains were ignored: #{failed_names.join(' ')}" + end + redirect_to admin_auctions_path else - flash[:alert] = "Invalid CSV format." + flash[:alert] = "Invalid CSV format. Should be column with 'name' where is the list of name of domains!" redirect_to admin_auctions_path end end private + def check_availability(domain_name) + Epp::Domain.check_availability(domain_name) + end + + def domain_exists_in_blocked_disputed_and_registered?(domain_name) + Domain.exists?(name: domain_name) || + BlockedDomain.exists?(name: domain_name) || + Dispute.exists?(domain_name: domain_name) || + Auction.exists?(domain: domain_name) + end + def validate_table(table) first_row = table.headers - first_row[0] == 'id' && - first_row[1] == 'created_at' && - first_row[2] == 'updated_at' && - first_row[3] == 'creator_str' && - first_row[4] == 'updator_str' && - first_row[5] == 'name' + first_row.include? 'name' end def remove_from_reserved(auction) diff --git a/app/controllers/admin/reserved_domains_controller.rb b/app/controllers/admin/reserved_domains_controller.rb index 3812a23948..20957dec4c 100644 --- a/app/controllers/admin/reserved_domains_controller.rb +++ b/app/controllers/admin/reserved_domains_controller.rb @@ -58,7 +58,7 @@ def release_to_auction reserved_domains = ReservedDomain.where(id: reserved_domains_ids) reserved_domains.each do |domain| - Auction.create!(domain: domain.name, status: Auction.statuses[:started], platform: 'manually') + Auction.create!(domain: domain.name, status: Auction.statuses[:started], platform: 'manual') domain.destroy! end diff --git a/app/models/auction.rb b/app/models/auction.rb index d7d1e1c237..202e6ac0b9 100644 --- a/app/models/auction.rb +++ b/app/models/auction.rb @@ -9,7 +9,7 @@ class Auction < ApplicationRecord domain_not_registered: 'domain_not_registered', } - enum platform: %i[automatically manually] + enum platform: %i[auto manual] PENDING_STATUSES = [statuses[:started], statuses[:awaiting_payment], diff --git a/app/models/dns/domain_name.rb b/app/models/dns/domain_name.rb index 5d89868ffa..6c68c37971 100644 --- a/app/models/dns/domain_name.rb +++ b/app/models/dns/domain_name.rb @@ -35,7 +35,7 @@ def unavailability_reason def sell_at_auction auction = Auction.new auction.domain = name - auction.platform = 'automatically' + auction.platform = 'auto' auction.start ToStdout.msg "Created the auction: #{auction.inspect}" update_whois_from_auction(auction) diff --git a/app/views/admin/auctions/index.html.erb b/app/views/admin/auctions/index.html.erb index 9d530b80f4..59063865e4 100644 --- a/app/views/admin/auctions/index.html.erb +++ b/app/views/admin/auctions/index.html.erb @@ -108,7 +108,7 @@ <%= sort_link(@q, 'created_at') %> - + <%= sort_link(@q, 'registration_code') %> @@ -126,9 +126,9 @@ <%= colorize_auction(auction) %> <%= auction.status %> <%= auction.created_at %> - <%= auction.registration_code %> + <%= auction.registration_code %> <%= auction.registration_deadline %> - <%= auction.platform.nil? ? 'automatically' : auction.platform %> + <%= auction.platform.nil? ? 'auto' : auction.platform %> <% end %> diff --git a/app/views/admin/reserved_domains/index.html.erb b/app/views/admin/reserved_domains/index.html.erb index 6f1018875b..109dbad9a1 100644 --- a/app/views/admin/reserved_domains/index.html.erb +++ b/app/views/admin/reserved_domains/index.html.erb @@ -50,7 +50,10 @@
<%= form_for :reserved_elements, url: release_to_auction_admin_reserved_domains_path, html: { class: 'form-horizontal', autocomplete: 'off' } do |f| %> - <%= f.submit 'Send to the auction list', class: 'btn btn-primary', style: 'margin: 10px 0 20px 0;' %> +
+ <%= f.submit 'Send to the auction list', class: 'btn btn-primary', style: 'margin: 10px 0 20px 0;' %> + Domains will be removed from reserved list! +