refactoring

This commit is contained in:
olegphenomenon 2022-04-27 11:55:36 +03:00
parent 0764d0735d
commit 9f69370a71
7 changed files with 69 additions and 25 deletions

View file

@ -9,6 +9,7 @@ module Admin
.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 @@ module Admin
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].zero?
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,62 @@ module Admin
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].zero?
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"
redirect_to admin_auctions_path
flash[:notice] = 'Domains added!'
flash[:notice] = "Domains added! But these domains were ignored: #{failed_names.join(' ')}" if failed_names.present?
else
flash[:alert] = "Invalid CSV format."
redirect_to admin_auctions_path
flash[:alert] = "Invalid CSV format. Should be column with 'name' where is the list of name of domains!"
end
redirect_to admin_auctions_path
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)

View file

@ -58,7 +58,7 @@ module Admin
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

View file

@ -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],
@ -21,15 +21,15 @@ class Auction < ApplicationRecord
where(status: status) if status.present?
}
scope :with_start_created_at_date, -> (start_created_at) {
where("created_at >= ?", start_created_at) if start_created_at.present?
scope :with_start_created_at_date, ->(start_created_at) {
where('created_at >= ?', start_created_at) if start_created_at.present?
}
scope :with_end_created_at_date, -> (end_created_at) {
where("created_at <= ?", end_created_at) if end_created_at.present?
scope :with_end_created_at_date, ->(end_created_at) {
where('created_at <= ?', end_created_at) if end_created_at.present?
}
scope :with_domain_name, -> (domain_name) {
scope :with_domain_name, ->(domain_name) {
where('domain ilike ?', "%#{domain_name.strip}%") if domain_name.present?
}

View file

@ -35,7 +35,7 @@ module DNS
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)

View file

@ -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">
@ -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>

View file

@ -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| %>
<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">

View file

@ -5090,5 +5090,5 @@ INSERT INTO "schema_migrations" (version) VALUES
('20220413084748'),
('20220124105717'),
('20220216113112'),
('20220228093211');
('20220228093211'),
('20220412130856');