Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
olegphenomenon committed Apr 27, 2022
1 parent 0764d07 commit 8c697cb
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 17 deletions.
65 changes: 55 additions & 10 deletions app/controllers/admin/auctions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/reserved_domains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/models/auction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion app/models/dns/domain_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions app/views/admin/auctions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
<th class="col-xs-2">
<%= sort_link(@q, 'created_at') %>
</th>
<th class="col-xs-2">
<th class="col-xs-2" style="width: 100px !important; word-break: break-all;">
<%= sort_link(@q, 'registration_code') %>
</th>
<th class="col-xs-2">
Expand All @@ -126,9 +126,9 @@
<td><%= colorize_auction(auction) %></td>
<td><%= auction.status %></td>
<td><%= auction.created_at %></td>
<td><%= auction.registration_code %></td>
<td style="width: 100px !important; word-break: break-all;"><%= auction.registration_code %></td>
<td><%= auction.registration_deadline %></td>
<td><%= auction.platform.nil? ? 'automatically' : auction.platform %></td>
<td><%= auction.platform.nil? ? 'auto' : auction.platform %></td>
</tr>
<% end %>
</tbody>
Expand Down
5 changes: 4 additions & 1 deletion app/views/admin/reserved_domains/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@
<hr/>

<%= 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;' %>
<div style="display: flex; flex-direction: row; align-items: center">
<%= f.submit 'Send to the auction list', class: 'btn btn-primary', style: 'margin: 10px 0 20px 0;' %>
<span style="margin-left: 10px; font-weight: bold">Domains will be removed from reserved list!</span>
</div>

<div class="row">
<div class="col-md-12">
Expand Down

0 comments on commit 8c697cb

Please sign in to comment.