From b8aeba6beb8d582bc04666412d21ae643476a14b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Fri, 18 Dec 2020 14:41:34 +0200 Subject: [PATCH] Move bulk domain renew BL to REPP --- .../registrar/bulk_change_controller.rb | 16 +++--- .../repp/v1/domains/renews_controller.rb | 51 +++++++++++++++++++ app/models/depp/domain.rb | 9 ++++ .../bulk_change/_bulk_renew_form.html.erb | 4 +- config/routes.rb | 1 + 5 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 app/controllers/repp/v1/domains/renews_controller.rb diff --git a/app/controllers/registrar/bulk_change_controller.rb b/app/controllers/registrar/bulk_change_controller.rb index 1dc5282a7..1c3d85cc1 100644 --- a/app/controllers/registrar/bulk_change_controller.rb +++ b/app/controllers/registrar/bulk_change_controller.rb @@ -13,12 +13,14 @@ class Registrar set_form_data if ready_to_renew? - domains = Epp::Domain.where(id: domain_ids_for_bulk_renew).to_a - task = renew_task(domains) - flash[:notice] = flash_message(task) + res = Depp::Domain.bulk_renew(domain_ids_for_bulk_renew, params[:period], + current_registrar_user.registrar) + + flash_message(JSON.parse(res)) else flash[:notice] = nil end + render file: 'registrar/bulk_change/new', locals: { active_tab: :bulk_renew } end @@ -60,12 +62,8 @@ class Registrar registrar: current_registrar_user.registrar) end - def flash_message(task) - if task.valid? - t(:bulk_renew_completed) - else - task.errors.full_messages.join(' and ') - end + def flash_message(res) + flash[:notice] = res['code'] == 1000 ? t(:bulk_renew_completed) : res['message'] end end end diff --git a/app/controllers/repp/v1/domains/renews_controller.rb b/app/controllers/repp/v1/domains/renews_controller.rb new file mode 100644 index 000000000..46dd9fbba --- /dev/null +++ b/app/controllers/repp/v1/domains/renews_controller.rb @@ -0,0 +1,51 @@ +module Repp + module V1 + module Domains + class RenewsController < BaseController + def bulk_renew + @epp_errors ||= [] + + if bulk_renew_params[:domains].instance_of?(Array) + domains = bulk_renew_domains + else + @epp_errors << { code: 2005, msg: 'Domains attribute must be an array' } + end + + return handle_errors if @epp_errors.any? + + renew = run_bulk_renew_task(domains, bulk_renew_params[:renew_period]) + return render_success(data: { updated_domains: domains.map(&:name) }) if renew.valid? + + @epp_errors << { code: 2304, msg: renew.errors.full_messages.join(',') } + handle_errors + end + + private + + def run_bulk_renew_task(domains, period) + ::Domains::BulkRenew::Start.run(domains: domains, period_element: period, + registrar: current_user.registrar) + end + + def bulk_renew_params + params do + params.require(%i[domains renew_period]) + params.permit(:domains, :renew_period) + end + end + + def bulk_renew_domains + @epp_errors ||= [] + domains = [] + bulk_renew_params[:domains].each do |idn| + domain = Epp::Domain.find_by_idn(idn) + domains << domain if domain + @epp_errors << { code: 2304, msg: "Object does not exist: #{idn}" } unless domain + end + + domains + end + end + end + end +end diff --git a/app/models/depp/domain.rb b/app/models/depp/domain.rb index 3bb3b7473..a2acde07a 100644 --- a/app/models/depp/domain.rb +++ b/app/models/depp/domain.rb @@ -30,6 +30,15 @@ module Depp ['10 years', '10y'], ] + def self.bulk_renew(domains, period, _registrar) + payload = { domains: domains, renew_period: period } + headers = { Authorization: 'Basic dGVzdDp0ZXN0MTIz' } + + RestClient.post("#{ENV['repp_url']}domains/renew/bulk", payload, headers).response + rescue RestClient::ExceptionWithResponse => e + e.response + end + def initialize(args = {}) self.current_user = args[:current_user] self.epp_xml = EppXml::Domain.new(cl_trid_prefix: current_user.tag) diff --git a/app/views/registrar/bulk_change/_bulk_renew_form.html.erb b/app/views/registrar/bulk_change/_bulk_renew_form.html.erb index 239a36815..5db40365f 100644 --- a/app/views/registrar/bulk_change/_bulk_renew_form.html.erb +++ b/app/views/registrar/bulk_change/_bulk_renew_form.html.erb @@ -37,8 +37,8 @@ <%= f.label :domain_ids, t('.domain_ids') %>
- <%= f.collection_check_boxes :domain_ids, @domains, :id, :name, - checked: @domains.map(&:id) do |b|%> + <%= f.collection_check_boxes :domain_ids, @domains, :name, :name, + checked: @domains.map(&:name) do |b|%>
<%= b.check_box %> <%= b.label %> diff --git a/config/routes.rb b/config/routes.rb index 7bf1ecd39..2a2de1863 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -64,6 +64,7 @@ Rails.application.routes.draw do get ':id/transfer_info', to: 'domains#transfer_info', constraints: { id: /.*/ } post 'transfer', to: 'domains#transfer' patch 'contacts', to: 'domains/contacts#update' + post 'renew/bulk', to: 'domains/renews#bulk_renew' end end end