From fd6e625d251399e7cec891f033c4d07d1a7bffce Mon Sep 17 00:00:00 2001 From: Thiago Youssef Date: Tue, 5 Apr 2022 14:18:04 +0300 Subject: [PATCH] Merge branch 'master' into 499-admin-wildcard-search --- .../workflows/build_baseimage_with_gems.yml | 2 +- .github/workflows/ruby.yml | 6 +-- .gitignore | 1 + CHANGELOG.md | 6 +++ Gemfile | 6 +-- Gemfile.lock | 22 ++++----- .../admin/account_activities_controller.rb | 3 +- app/controllers/admin/base_controller.rb | 2 +- .../admin/contact_versions_controller.rb | 13 ++++- .../admin/domain_versions_controller.rb | 7 +-- .../api/v1/registrant/contacts_controller.rb | 11 +++++ .../account_activities_controller.rb | 3 +- .../registrar/contacts_controller.rb | 2 +- app/helpers/application_helper.rb | 24 ++-------- app/helpers/object_versions_helper.rb | 19 -------- app/lib/to_csv.rb | 10 ---- app/models/account.rb | 9 +++- app/models/account_activity.rb | 19 +++----- app/models/api_log/epp_log.rb | 4 +- app/models/api_log/repp_log.rb | 4 +- app/models/blocked_domain.rb | 1 - app/models/contact.rb | 32 +++++++++---- app/models/dispute.rb | 1 - app/models/domain.rb | 47 +++++++++++++------ app/models/invoice.rb | 28 ++++++++++- app/models/registrant_user.rb | 25 +++++++--- app/models/reserved_domain.rb | 1 - app/models/version/contact_version.rb | 18 ++++++- app/models/version/domain_version.rb | 17 ++++++- app/services/csv_generator.rb | 29 ++++++++++++ app/services/object_versions_parser.rb | 28 +++++++++++ app/views/admin/contact_versions/index.haml | 4 +- app/views/admin/contact_versions/show.haml | 5 +- app/views/admin/domain_versions/archive.haml | 4 +- app/views/admin/domain_versions/show.haml | 4 +- config/routes.rb | 5 +- test/fixtures/files/accounts.csv | 4 ++ test/fixtures/files/contact_versions.csv | 3 ++ test/fixtures/files/contacts.csv | 2 + test/fixtures/files/domain_versions.csv | 3 ++ test/fixtures/files/domains.csv | 2 + test/fixtures/files/invoices.csv | 3 ++ test/models/registrant_user_test.rb | 39 +++++++++++++-- test/system/admin_area/accounts_test.rb | 12 +++++ .../admin_area/contact_versions_test.rb | 3 +- test/system/admin_area/contacts/csv_test.rb | 22 +++++++++ .../system/admin_area/domain_versions_test.rb | 2 +- test/system/admin_area/domains/csv_test.rb | 15 +++--- test/system/admin_area/invoices_test.rb | 10 ++++ 49 files changed, 382 insertions(+), 160 deletions(-) delete mode 100644 app/helpers/object_versions_helper.rb delete mode 100644 app/lib/to_csv.rb create mode 100644 app/services/csv_generator.rb create mode 100644 app/services/object_versions_parser.rb create mode 100644 test/fixtures/files/accounts.csv create mode 100644 test/fixtures/files/contact_versions.csv create mode 100644 test/fixtures/files/contacts.csv create mode 100644 test/fixtures/files/domain_versions.csv create mode 100644 test/fixtures/files/domains.csv create mode 100644 test/fixtures/files/invoices.csv create mode 100644 test/system/admin_area/contacts/csv_test.rb diff --git a/.github/workflows/build_baseimage_with_gems.yml b/.github/workflows/build_baseimage_with_gems.yml index 3487e838c..e6f72594a 100644 --- a/.github/workflows/build_baseimage_with_gems.yml +++ b/.github/workflows/build_baseimage_with_gems.yml @@ -17,7 +17,7 @@ jobs: steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Login to container registry env: diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index dd71c4bee..d50636e26 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -22,7 +22,7 @@ jobs: continue-on-error: ${{ endsWith(matrix.ruby, 'head') || matrix.ruby == 'debug' }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} @@ -79,7 +79,7 @@ jobs: - name: Save coverage run: ./cc-test-reporter format-coverage --output coverage/codeclimate.${{ matrix.ruby }}.json - - uses: actions/upload-artifact@v2.3.1 + - uses: actions/upload-artifact@v3.0.0 with: name: coverage-${{ matrix.ruby }} path: coverage/codeclimate.${{ matrix.ruby }}.json @@ -104,7 +104,7 @@ jobs: - name: Give test coverage reporter executable permissions run: chmod +x cc-test-reporter - - uses: actions/download-artifact@v2.1.0 + - uses: actions/download-artifact@v3.0.0 with: name: coverage-${{ matrix.ruby }} path: coverage diff --git a/.gitignore b/.gitignore index 3f23f1277..1c4a85f46 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ /config/application.yml /config/environments/development.rb /config/deploy.rb +/config/master.key /.idea # Do not commit one. Instead, download the latest from https://github.com/internetee/style-guide. diff --git a/CHANGELOG.md b/CHANGELOG.md index 0305aff95..ea1333016 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +31.03.2022 +* Sidekiq update to 6.4.1 [#2322](https://github.com/internetee/registry/pull/2322) + +25.03.2022 +* Bulk change of business contacts' names requires now user confirmation [#2309](https://github.com/internetee/registry/pull/2309) + 23.02.2022 * FD notes are updated when basis for FD changes [#2216](https://github.com/internetee/registry/issues/2216) * Admin: date filter end date in domain hostory is now inclusive [#2274](https://github.com/internetee/registry/issues/2274) diff --git a/Gemfile b/Gemfile index 2e728f99f..c907eb267 100644 --- a/Gemfile +++ b/Gemfile @@ -17,7 +17,7 @@ gem 'figaro', '~> 1.2' # model related gem 'paper_trail', '~> 12.1' -gem 'pg', '1.3.3' +gem 'pg', '1.3.5' # 1.8 is for Rails < 5.0 gem 'ransack', '~> 2.6.0' gem 'truemail', '~> 2.4' # validates email by regexp, mail server existence and address existence @@ -40,7 +40,7 @@ gem 'select2-rails', '4.0.13' # for autocomplete gem 'selectize-rails', '0.12.6' # include selectize.js for select # registry specfic -gem 'data_migrate', '~> 7.0' +gem 'data_migrate', '~> 8.0' gem 'dnsruby', '~> 1.61' gem 'isikukood' # for EE-id validation gem 'money-rails' @@ -70,7 +70,7 @@ gem 'jquery-ui-rails', '6.0.1' gem 'pdfkit' gem 'que' gem 'que-web' -gem 'sidekiq' +gem 'sidekiq', '>= 6.4.1' gem 'company_register', github: 'internetee/company_register', branch: 'master' diff --git a/Gemfile.lock b/Gemfile.lock index 10ae5358f..f7e9afa7e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -193,7 +193,7 @@ GEM coffee-script-source execjs coffee-script-source (1.12.2) - concurrent-ruby (1.1.9) + concurrent-ruby (1.1.10) connection_pool (2.2.5) countries (4.0.1) i18n_data (~> 0.13.0) @@ -205,7 +205,7 @@ GEM daemons-rails (1.2.1) daemons multi_json (~> 1.0) - data_migrate (7.0.2) + data_migrate (8.0.0) activerecord (>= 5.0) railties (>= 5.0) database_cleaner (2.0.1) @@ -289,7 +289,7 @@ GEM kaminari-core (1.2.1) libxml-ruby (3.2.1) logger (1.4.3) - loofah (2.12.0) + loofah (2.16.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) mail (2.7.1) @@ -351,7 +351,7 @@ GEM activerecord (>= 5.2) request_store (~> 1.1) pdfkit (0.8.5) - pg (1.3.3) + pg (1.3.5) pg_query (2.1.2) google-protobuf (>= 3.17.1) pghero (2.8.1) @@ -360,7 +360,7 @@ GEM coderay (~> 1.1) method_source (~> 1.0) public_suffix (4.0.6) - puma (5.6.2) + puma (5.6.4) nio4r (~> 2.0) que (0.14.3) que-web (0.7.2) @@ -449,7 +449,7 @@ GEM selenium-webdriver (3.142.7) childprocess (>= 0.5, < 4.0) rubyzip (>= 1.2.2) - sidekiq (6.4.0) + sidekiq (6.4.1) connection_pool (>= 2.2.2) rack (~> 2.0) redis (>= 4.2.0) @@ -480,7 +480,7 @@ GEM attr_required (>= 0.0.5) httpclient (>= 2.4) temple (0.8.2) - thor (1.1.0) + thor (1.2.1) tilt (2.0.10) truemail (2.4.9) simpleidn (~> 0.2.1) @@ -544,7 +544,7 @@ DEPENDENCIES company_register! countries daemons-rails (= 1.2.1) - data_migrate (~> 7.0) + data_migrate (~> 8.0) database_cleaner devise (~> 4.8) digidoc_client! @@ -574,7 +574,7 @@ DEPENDENCIES omniauth-tara! paper_trail (~> 12.1) pdfkit - pg (= 1.3.3) + pg (= 1.3.5) pg_query (>= 0.9.0) pghero pry (= 0.14.1) @@ -588,7 +588,7 @@ DEPENDENCIES sass-rails select2-rails (= 4.0.13) selectize-rails (= 0.12.6) - sidekiq + sidekiq (>= 6.4.1) simplecov (= 0.17.1) simpleidn (= 0.2.1) spy @@ -601,4 +601,4 @@ DEPENDENCIES wkhtmltopdf-binary (~> 0.12.5.1) BUNDLED WITH - 2.2.31 + 2.3.9 diff --git a/app/controllers/admin/account_activities_controller.rb b/app/controllers/admin/account_activities_controller.rb index ebd44e28e..452acaee1 100644 --- a/app/controllers/admin/account_activities_controller.rb +++ b/app/controllers/admin/account_activities_controller.rb @@ -35,7 +35,8 @@ module Admin respond_to do |format| format.html format.csv do - send_data @q.result.to_csv, filename: "account_activities_#{Time.zone.now.to_formatted_s(:number)}.csv" + raw_csv = CsvGenerator.generate_csv(@q.result) + send_data raw_csv, filename: "account_activities_#{Time.zone.now.to_formatted_s(:number)}.csv" end end diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index 016c0a750..56806ba3e 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -26,7 +26,7 @@ module Admin respond_to do |format| format.html { render page } format.csv do - raw_csv = @q.result.to_csv + raw_csv = CsvGenerator.generate_csv(@q.result) send_data raw_csv, filename: "#{filename}_#{Time.zone.now.to_formatted_s(:number)}.csv", type: "#{Mime[:csv]}; charset=utf-8" diff --git a/app/controllers/admin/contact_versions_controller.rb b/app/controllers/admin/contact_versions_controller.rb index c53a020ee..43d43d5e8 100644 --- a/app/controllers/admin/contact_versions_controller.rb +++ b/app/controllers/admin/contact_versions_controller.rb @@ -1,13 +1,13 @@ module Admin class ContactVersionsController < BaseController - include ObjectVersionsHelper + include ApplicationHelper load_and_authorize_resource class: Version::ContactVersion def index params[:q] ||= {} - search_params = PartialSearchFormatter.format(params[:q]) + search_params = PartialSearchFormatter.format(fix_date_params) versions = Version::ContactVersion.includes(:item).order(created_at: :desc, id: :desc) @q = versions.ransack(polymorphic_association(search_params)) @@ -48,5 +48,14 @@ module Admin record_type end + + def fix_date_params + params_copy = params[:q].deep_dup + if params_copy['created_at_lteq'].present? + params_copy['created_at_lteq'] = Date.parse(params_copy['created_at_lteq']) + 1.day + end + + params_copy + end end end diff --git a/app/controllers/admin/domain_versions_controller.rb b/app/controllers/admin/domain_versions_controller.rb index c6bba6d51..c82347ff9 100644 --- a/app/controllers/admin/domain_versions_controller.rb +++ b/app/controllers/admin/domain_versions_controller.rb @@ -1,7 +1,5 @@ module Admin class DomainVersionsController < BaseController - include ObjectVersionsHelper - load_and_authorize_resource class: Version::DomainVersion def index @@ -82,9 +80,8 @@ module Admin def fix_date_params params_copy = params[:q].deep_dup - if params_copy['created_at_lteq'].present? - params_copy['created_at_lteq'] = Date.parse(params_copy['created_at_lteq']) + 1.day - end + created_at = params_copy['created_at_lteq'] + params_copy['created_at_lteq'] = Date.parse(created_at) + 1.day if created_at.present? params_copy end diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index 30096ab8a..8e8b46631 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -34,6 +34,17 @@ module Api end end + def do_need_update_contact + result = current_registrant_user.do_need_update_contact? + render json: { update_contacts: result[:result], counter: result[:counter] } + end + + def update_company_contacts + companies = current_registrant_user.update_company_contacts + + render json: { message: 'get it', companies: companies } + end + def update logger.debug 'Received update request' logger.debug params diff --git a/app/controllers/registrar/account_activities_controller.rb b/app/controllers/registrar/account_activities_controller.rb index 55e53f8fc..0ad8c3d5a 100644 --- a/app/controllers/registrar/account_activities_controller.rb +++ b/app/controllers/registrar/account_activities_controller.rb @@ -20,7 +20,8 @@ class Registrar respond_to do |format| format.html { @account_activities = @q.result.page(params[:page]) } format.csv do - send_data @q.result.to_csv, filename: "account_activities_#{Time.zone.now.to_formatted_s(:number)}.csv" + raw_csv = CsvGenerator.generate_csv(@q.result) + send_data raw_csv, filename: "account_activities_#{Time.zone.now.to_formatted_s(:number)}.csv" end end diff --git a/app/controllers/registrar/contacts_controller.rb b/app/controllers/registrar/contacts_controller.rb index ec4ce5129..812e278e5 100644 --- a/app/controllers/registrar/contacts_controller.rb +++ b/app/controllers/registrar/contacts_controller.rb @@ -40,7 +40,7 @@ class Registrar @contacts = @contacts.per(contacts_per_page) if contacts_per_page.positive? end format.csv do - raw_csv = contacts.to_csv + raw_csv = CsvGenerator.generate_csv(contacts) send_data raw_csv, filename: 'contacts.csv', type: "#{Mime[:csv]}; charset=utf-8" end format.pdf do diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 3de98b88c..122fc40d4 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -10,27 +10,11 @@ module ApplicationHelper end def ident_for(contact) - if contact.is_a? Hash - ident_country_code = contact[:ident_country_code] - ident_type = contact[:ident_type] - ident = contact[:ident] - else - ident_country_code = contact.ident_country_code - ident_type = contact.ident_type - ident = contact.ident - end + ident = contact.ident + description = "[#{contact.ident_country_code} #{contact.ident_type}]" + description.prepend("#{ident} ") if ident.present? - case ident_type - when 'birthday' - "#{ident} [#{ident_country_code} #{ident_type}]" - else - if ident.present? - "#{ident} [#{ident_country_code} #{ident_type}]" - else - "[#{ident_country_code} #{ident_type}]" - end - - end + description end def current_commit_link diff --git a/app/helpers/object_versions_helper.rb b/app/helpers/object_versions_helper.rb deleted file mode 100644 index dae357cf6..000000000 --- a/app/helpers/object_versions_helper.rb +++ /dev/null @@ -1,19 +0,0 @@ -module ObjectVersionsHelper - def attach_existing_fields(version, new_object) - version.object_changes.to_h.each do |key, value| - method_name = "#{key}=".to_sym - new_object.public_send(method_name, event_value(version, value)) if new_object.respond_to?(method_name) - end - end - - def only_present_fields(version, model) - field_names = model.column_names - version.object.to_h.select { |key, _value| field_names.include?(key) } - end - - private - - def event_value(version, val) - version.event == 'destroy' ? val.first : val.last - end -end diff --git a/app/lib/to_csv.rb b/app/lib/to_csv.rb deleted file mode 100644 index 32c288978..000000000 --- a/app/lib/to_csv.rb +++ /dev/null @@ -1,10 +0,0 @@ -module ToCsv - def to_csv - CSV.generate do |csv| - csv << column_names - all.find_each do |item| - csv << item.attributes.values_at(*column_names) - end - end - end -end diff --git a/app/models/account.rb b/app/models/account.rb index fe0820888..7639c61dd 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -1,5 +1,4 @@ class Account < ApplicationRecord - extend ToCsv include Versions belongs_to :registrar, required: true @@ -12,4 +11,12 @@ class Account < ApplicationRecord def activities account_activities end + + def as_csv_row + [id, balance, currency, registrar] + end + + def self.csv_header + ['Id', 'Balance', 'Currency', 'Registrar'] + end end diff --git a/app/models/account_activity.rb b/app/models/account_activity.rb index 05873a2a6..38fa70358 100644 --- a/app/models/account_activity.rb +++ b/app/models/account_activity.rb @@ -11,6 +11,7 @@ class AccountActivity < ApplicationRecord UPDATE_CREDIT = 'update_credit'.freeze after_create :update_balance + def update_balance account.balance += sum account.save @@ -19,23 +20,17 @@ class AccountActivity < ApplicationRecord save end + def as_csv_row + [account.registrar.try(:code), description, I18n.t(activity_type), I18n.l(created_at), sum] + end + class << self def types_for_select [CREATE, RENEW, ADD_CREDIT, UPDATE_CREDIT].map { |x| [I18n.t(x), x] } end - def to_csv - attributes = %w(description activity_type created_at sum) - - CSV.generate(headers: true) do |csv| - csv << %w(registrar description activity_type receipt_date sum) - - all.each do |x| - attrs = [x.account.registrar.try(:code)] - attrs += attributes.map { |attr| x.send(attr) } - csv << attrs - end - end + def csv_header + ['Registrar', 'Description', 'Activity Type', 'Receipt Date', 'Sum'] end end end diff --git a/app/models/api_log/epp_log.rb b/app/models/api_log/epp_log.rb index feed1ecad..c89569be5 100644 --- a/app/models/api_log/epp_log.rb +++ b/app/models/api_log/epp_log.rb @@ -1,5 +1,3 @@ module ApiLog - class EppLog < Db - extend ToCsv - end + class EppLog < Db; end end diff --git a/app/models/api_log/repp_log.rb b/app/models/api_log/repp_log.rb index 62dcee238..540a9043a 100644 --- a/app/models/api_log/repp_log.rb +++ b/app/models/api_log/repp_log.rb @@ -1,5 +1,3 @@ module ApiLog - class ReppLog < Db - extend ToCsv - end + class ReppLog < Db; end end diff --git a/app/models/blocked_domain.rb b/app/models/blocked_domain.rb index fb50a8b52..f82faa771 100644 --- a/app/models/blocked_domain.rb +++ b/app/models/blocked_domain.rb @@ -1,6 +1,5 @@ class BlockedDomain < ApplicationRecord include Versions - extend ToCsv before_save :generate_data after_destroy :remove_data diff --git a/app/models/contact.rb b/app/models/contact.rb index 84d4ba962..dab2dd6d9 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -188,15 +188,6 @@ class Contact < ApplicationRecord ] end - def to_csv - CSV.generate do |csv| - csv << column_names - all.each do |contact| - csv << contact.attributes.values_at(*column_names) - end - end - end - def pdf(html) kit = PDFKit.new(html) kit.to_pdf @@ -569,4 +560,27 @@ class Contact < ApplicationRecord def deletable? !linked? end + + def ident_human_description + description = "[#{ident_country_code} #{ident_type}]" + description.prepend("#{ident} ") if ident.present? + + description + end + + def as_csv_row + [ + name, + code, + ident_human_description, + email, + created_at.to_formatted_s(:db), + registrar, + phone, + ] + end + + def self.csv_header + ['Name', 'ID', 'Ident', 'E-mail', 'Created at', 'Registrar', 'Phone'] + end end diff --git a/app/models/dispute.rb b/app/models/dispute.rb index cbf93566a..f5a948355 100644 --- a/app/models/dispute.rb +++ b/app/models/dispute.rb @@ -1,5 +1,4 @@ class Dispute < ApplicationRecord - extend ToCsv include WhoisStatusPopulate validates :domain_name, :password, :starts_at, :expires_at, presence: true before_validation :fill_empty_passwords, :set_expiry_date diff --git a/app/models/domain.rb b/app/models/domain.rb index 873216cf1..7afd8046e 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -289,21 +289,6 @@ class Domain < ApplicationRecord ) end - def to_csv - CSV.generate do |csv| - headers = column_names.dup - swap_elements(headers, [[0, 1], [1, 5]]) - headers[0] = 'Domain' - headers[1] = headers[1].humanize - csv << headers - all.find_each do |item| - row = item.attributes.values_at(*column_names) - swap_elements(row, [[0, 1], [1, 5]]) - csv << row - end - end - end - private def registrant_user_domains_by_registrant(registrant_user) @@ -753,6 +738,38 @@ class Domain < ApplicationRecord contacts.select(&:email_verification_failed?)&.map(&:email)&.uniq end + def as_csv_row + [ + name, + registrant_name, + valid_to.to_formatted_s(:db), + registrar, + created_at.to_formatted_s(:db), + statuses, + contacts.pluck(:code), + force_delete_date, + force_delete_data, + ] + end + + def registrant_name + return registrant.name if registrant + + ver = Version::ContactVersion.where(item_id: registrant_id).last + contact = Contact.all_versions_for([registrant_id], created_at).first + + contact = ObjectVersionsParser.new(ver).parse if contact.nil? && ver + + contact.try(:name) || 'Deleted' + end + + def self.csv_header + [ + 'Domain', 'Registrant', 'Valid to', 'Registrar', 'Created at', + 'Statuses', 'Contacts code', 'Force delete date', 'Force delete data' + ] + end + def self.pdf(html) kit = PDFKit.new(html) kit.to_pdf diff --git a/app/models/invoice.rb b/app/models/invoice.rb index df8b7aff8..6ba3a158d 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -3,7 +3,6 @@ class Invoice < ApplicationRecord include Invoice::Cancellable include Invoice::Payable include Invoice::BookKeeping - extend ToCsv belongs_to :buyer, class_name: 'Registrar' has_one :account_activity @@ -117,6 +116,23 @@ class Invoice < ApplicationRecord e_invoice_sent_at.present? end + def as_csv_row + [ + number, + buyer, + cancelled? ? I18n.t(:cancelled) : due_date, + receipt_date_status, + issue_date, + total, + currency, + seller_name, + ] + end + + def self.csv_header + ['Number', 'Buyer', 'Due Date', 'Receipt Date', 'Issue Date', 'Total', 'Currency', 'Seller Name'] + end + def self.create_from_transaction!(transaction) registrar_user = Registrar.find_by(reference_no: transaction.parsed_ref_number) return unless registrar_user @@ -128,6 +144,16 @@ class Invoice < ApplicationRecord private + def receipt_date_status + if paid? + receipt_date + elsif cancelled? + I18n.t(:cancelled) + else + I18n.t(:unpaid) + end + end + def apply_default_buyer_vat_no self.buyer_vat_no = buyer.vat_no end diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index 5fe508125..80b8ecab9 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -20,16 +20,27 @@ class RegistrantUser < User def companies(company_register = CompanyRegister::Client.new) return [] if ident.include?('-') - companies = company_register.representation_rights(citizen_personal_code: ident, - citizen_country_code: country.alpha3) - - companies = update_contacts_before_receive(companies) - companies + company_register.representation_rights(citizen_personal_code: ident, + citizen_country_code: country.alpha3) rescue CompanyRegister::NotAvailableError - return [] + [] end - def update_contacts_before_receive(companies) + def do_need_update_contact? + return { result: false, counter: 0 } if companies.blank? + + counter = 0 + companies.each do |company| + counter += Contact.where(ident: company.registration_number, ident_country_code: 'EE')&. + reject { |contact| contact.name == company.company_name }.size + end + + return { result: true, counter: counter } if counter.positive? + + { result: false, counter: 0 } + end + + def update_company_contacts return [] if companies.blank? companies.each do |company| diff --git a/app/models/reserved_domain.rb b/app/models/reserved_domain.rb index 5d585e5d9..10d5edd11 100644 --- a/app/models/reserved_domain.rb +++ b/app/models/reserved_domain.rb @@ -1,5 +1,4 @@ class ReservedDomain < ApplicationRecord - extend ToCsv include Versions # version/reserved_domain_version.rb include WhoisStatusPopulate before_save :fill_empty_passwords diff --git a/app/models/version/contact_version.rb b/app/models/version/contact_version.rb index 95bd4b677..d3ff1130c 100644 --- a/app/models/version/contact_version.rb +++ b/app/models/version/contact_version.rb @@ -1,7 +1,23 @@ class Version::ContactVersion < PaperTrail::Version - extend ToCsv include VersionSession self.table_name = :log_contacts self.sequence_name = :log_contacts_id_seq + + def as_csv_row + contact = ObjectVersionsParser.new(self).parse + + [ + contact.name, + contact.code, + contact.ident_human_description, + contact.registrar, + event, + created_at.to_formatted_s(:db) + ] + end + + def self.csv_header + ['Name', 'ID', 'Ident', 'Registrar', 'Action', 'Created at'] + end end diff --git a/app/models/version/domain_version.rb b/app/models/version/domain_version.rb index 2c6848d4b..e84fc9da2 100644 --- a/app/models/version/domain_version.rb +++ b/app/models/version/domain_version.rb @@ -1,5 +1,4 @@ class Version::DomainVersion < PaperTrail::Version - extend ToCsv include VersionSession self.table_name = :log_domains @@ -7,6 +6,18 @@ class Version::DomainVersion < PaperTrail::Version scope :deleted, -> { where(event: 'destroy') } + def as_csv_row + domain = ObjectVersionsParser.new(self).parse + + [ + domain.name, + domain.registrant_name, + domain.registrar, + event, + created_at.to_formatted_s(:db) + ] + end + def self.was_contact_linked?(contact_id) sql = <<-SQL SELECT @@ -43,4 +54,8 @@ class Version::DomainVersion < PaperTrail::Version count_by_sql(sql).nonzero? end + + def self.csv_header + ['Name', 'Registrant', 'Registrar', 'Action', 'Created at'] + end end diff --git a/app/services/csv_generator.rb b/app/services/csv_generator.rb new file mode 100644 index 000000000..d92beeddc --- /dev/null +++ b/app/services/csv_generator.rb @@ -0,0 +1,29 @@ +class CsvGenerator + class << self + def generate_csv(objects) + class_name = objects.first.class + return default_generation(objects) unless custom_csv?(class_name) + + CSV.generate do |csv| + csv << class_name.csv_header + objects.each { |object| csv << object.as_csv_row } + end + end + + private + + def default_generation(objects) + CSV.generate do |csv| + csv << objects.column_names + objects.all.find_each { |object| csv << object.attributes.values_at(*objects.column_names) } + end + end + + def custom_csv?(class_name) + [ + Version::DomainVersion, Version::ContactVersion, Domain, + Contact, Invoice, Account, AccountActivity + ].include?(class_name) + end + end +end diff --git a/app/services/object_versions_parser.rb b/app/services/object_versions_parser.rb new file mode 100644 index 000000000..5871a6bd6 --- /dev/null +++ b/app/services/object_versions_parser.rb @@ -0,0 +1,28 @@ +class ObjectVersionsParser + def initialize(version) + @version = version + end + + def parse + model = @version.item_type.constantize + attributes = only_present_fields(model) + history_object = model.new(attributes) + attach_existing_fields(history_object) unless @version.event == 'destroy' + + history_object + end + + private + + def attach_existing_fields(history_object) + @version.object_changes.to_h.each do |key, value| + method_name = "#{key}=".to_sym + history_object.public_send(method_name, value.last) if history_object.respond_to?(method_name) + end + end + + def only_present_fields(model) + field_names = model.column_names + @version.object.to_h.select { |key, _value| field_names.include?(key) } + end +end diff --git a/app/views/admin/contact_versions/index.haml b/app/views/admin/contact_versions/index.haml index 581402197..e181673f2 100644 --- a/app/views/admin/contact_versions/index.haml +++ b/app/views/admin/contact_versions/index.haml @@ -64,9 +64,7 @@ %tbody - @versions.each do |version| - if version - - attributes = only_present_fields(version, Contact) - - contact = Contact.new(attributes) - - attach_existing_fields(version, contact) + - contact = ObjectVersionsParser.new(version).parse %tr %td= link_to(contact.name, admin_contact_version_path(version.id)) diff --git a/app/views/admin/contact_versions/show.haml b/app/views/admin/contact_versions/show.haml index 901f5ee1a..546ff4287 100644 --- a/app/views/admin/contact_versions/show.haml +++ b/app/views/admin/contact_versions/show.haml @@ -1,6 +1,5 @@ -- attributes = only_present_fields(@version, Contact) -- contact = Contact.new(attributes) -- attach_existing_fields(@version, contact) +- contact = ObjectVersionsParser.new(@version).parse + = render 'shared/title', name: contact.name .row diff --git a/app/views/admin/domain_versions/archive.haml b/app/views/admin/domain_versions/archive.haml index ec2034ed1..85105b9f0 100644 --- a/app/views/admin/domain_versions/archive.haml +++ b/app/views/admin/domain_versions/archive.haml @@ -62,9 +62,7 @@ %tbody - @versions.each do |version| - if version - - attributes = only_present_fields(version, Domain) - - domain = Domain.new(attributes) - - attach_existing_fields(version, domain) unless version.event == 'destroy' + - domain = ObjectVersionsParser.new(version).parse %tr %td= link_to(domain.name, admin_domain_version_path(version.id)) diff --git a/app/views/admin/domain_versions/show.haml b/app/views/admin/domain_versions/show.haml index 11f70599f..ab49dffee 100644 --- a/app/views/admin/domain_versions/show.haml +++ b/app/views/admin/domain_versions/show.haml @@ -1,6 +1,4 @@ -- present_fields = only_present_fields(@version, Domain) -- domain = Domain.new(present_fields) -- attach_existing_fields(@version, domain) unless @version.event == 'destroy' +- domain = ObjectVersionsParser.new(@version).parse - if @version - children = HashWithIndifferentAccess.new(@version.children) diff --git a/config/routes.rb b/config/routes.rb index 66debd4b4..79807729a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -128,7 +128,10 @@ Rails.application.routes.draw do resources :domains, only: %i[index show], param: :uuid do resource :registry_lock, only: %i[create destroy] end - resources :contacts, only: %i[index show update], param: :uuid + resources :contacts, only: %i[index show update], param: :uuid do + get 'do_need_update_contact', to: 'contacts#do_need_update_contact', as: :do_need_update_contact + post 'update_company_contacts', to: 'contacts#update_company_contacts', as: :update_company_contacts + end resources :companies, only: %i[index] end diff --git a/test/fixtures/files/accounts.csv b/test/fixtures/files/accounts.csv new file mode 100644 index 000000000..5bc44fc48 --- /dev/null +++ b/test/fixtures/files/accounts.csv @@ -0,0 +1,4 @@ +Id,Balance,Currency,Registrar +112846265,100.0,EUR,Best Names +298486374,100.0,EUR,Good Names +597560588,0.0,EUR,Not in use diff --git a/test/fixtures/files/contact_versions.csv b/test/fixtures/files/contact_versions.csv new file mode 100644 index 000000000..c794026c2 --- /dev/null +++ b/test/fixtures/files/contact_versions.csv @@ -0,0 +1,3 @@ +Name,ID,Ident,Registrar,Action,Created at +,test_code,[ ],Best Names,update,2018-04-23 15:50:48 +,,[ ],,update,2010-07-04 21:00:00 diff --git a/test/fixtures/files/contacts.csv b/test/fixtures/files/contacts.csv new file mode 100644 index 000000000..9d8ca5357 --- /dev/null +++ b/test/fixtures/files/contacts.csv @@ -0,0 +1,2 @@ +Name,ID,Ident,E-mail,Created at,Registrar,Phone +Acme Ltd,acme-ltd-001,1234567 [US org],acme@outlook.test,2010-07-05 07:30:00,Best Names,+555.555 diff --git a/test/fixtures/files/domain_versions.csv b/test/fixtures/files/domain_versions.csv new file mode 100644 index 000000000..8fa7fa9fe --- /dev/null +++ b/test/fixtures/files/domain_versions.csv @@ -0,0 +1,3 @@ +Name,Registrant,Registrar,Action,Created at +,test_code,Best Names,update,2018-04-23 15:50:48 +,John,,update,2010-07-04 21:00:00 diff --git a/test/fixtures/files/domains.csv b/test/fixtures/files/domains.csv new file mode 100644 index 000000000..b8261ebc2 --- /dev/null +++ b/test/fixtures/files/domains.csv @@ -0,0 +1,2 @@ +Domain,Registrant,Valid to,Registrar,Created at,Statuses,Contacts code,Force delete date,Force delete data +metro.test,Jack,2010-07-05 00:00:00,Good Names,2010-07-05 07:30:00,[],"[""jack-001"", ""jack-001""]",, diff --git a/test/fixtures/files/invoices.csv b/test/fixtures/files/invoices.csv new file mode 100644 index 000000000..641cfe036 --- /dev/null +++ b/test/fixtures/files/invoices.csv @@ -0,0 +1,3 @@ +Number,Buyer,Due Date,Receipt Date,Issue Date,Total,Currency,Seller Name +2,Best Names,2010-07-06,Unpaid,2010-07-05,16.5,EUR,Seller Ltd +1,Best Names,2010-07-06,2010-07-05,2010-07-05,16.5,EUR,Seller Ltd diff --git a/test/models/registrant_user_test.rb b/test/models/registrant_user_test.rb index 987e80c03..2b1d6a880 100644 --- a/test/models/registrant_user_test.rb +++ b/test/models/registrant_user_test.rb @@ -41,10 +41,8 @@ class RegistrantUserTest < ActiveSupport::TestCase company = Company.new(org.ident, "ace") - company_register = Minitest::Mock.new - company_register.expect(:representation_rights, [company], [{ citizen_personal_code: '1234', - citizen_country_code: 'USA' }]) - @user.companies(company_register) + Spy.on(@user, :companies).and_return([company]) + @user.update_company_contacts org.reload assert_equal org.name, company.company_name @@ -63,6 +61,39 @@ class RegistrantUserTest < ActiveSupport::TestCase company_register.verify end + def test_should_return_zero_count_of_companies + assert_equal 'US-1234', @user.registrant_ident + org = contacts(:acme_ltd) + org.ident_country_code = 'EE' + org.save(validate: false) + org.reload + + company_one = Company.new(org.ident, 'Acme Ltd') + + Spy.on(@user, :companies).and_return([company_one]) + response = @user.do_need_update_contact? + org.reload + + assert_equal response[:counter], 0 + end + + def test_should_return_count_of_contact_which_should_be_updated + assert_equal 'US-1234', @user.registrant_ident + org = contacts(:acme_ltd) + org.ident_country_code = 'EE' + org.save(validate: false) + org.reload + + company_one = Company.new(org.ident, 'ace') + company_two = Company.new(org.ident, 'acer') + + Spy.on(@user, :companies).and_return([company_one, company_two]) + response = @user.do_need_update_contact? + org.reload + + assert_equal response[:counter], 2 + end + def test_returns_contacts Contact.stub(:registrant_user_contacts, %w(john jane)) do assert_equal %w(john jane), @user.contacts diff --git a/test/system/admin_area/accounts_test.rb b/test/system/admin_area/accounts_test.rb index f07ced9c3..393c3445a 100644 --- a/test/system/admin_area/accounts_test.rb +++ b/test/system/admin_area/accounts_test.rb @@ -29,4 +29,16 @@ class AdminAccountsSystemTest < ApplicationSystemTestCase assert_text 'Account has been successfully updated' assert_text '234' end + + def test_download_accounts_list_as_csv + travel_to Time.zone.parse('2010-07-05 10:30') + + get admin_accounts_path(format: :csv) + + assert_response :ok + assert_equal 'text/csv; charset=utf-8', response.headers['Content-Type'] + assert_equal %(attachment; filename="accounts_#{Time.zone.now.to_formatted_s(:number)}.csv"; filename*=UTF-8''accounts_#{Time.zone.now.to_formatted_s(:number)}.csv), + response.headers['Content-Disposition'] + assert_equal file_fixture('accounts.csv').read, response.body + end end diff --git a/test/system/admin_area/contact_versions_test.rb b/test/system/admin_area/contact_versions_test.rb index f040646bb..e199f5768 100644 --- a/test/system/admin_area/contact_versions_test.rb +++ b/test/system/admin_area/contact_versions_test.rb @@ -62,11 +62,10 @@ class ContactVersionsTest < ApplicationSystemTestCase travel_to now get admin_contact_versions_path(format: :csv) - assert_response :ok assert_equal 'text/csv; charset=utf-8', response.headers['Content-Type'] assert_equal %(attachment; filename="contact_history_#{Time.zone.now.to_formatted_s(:number)}.csv"; filename*=UTF-8''contact_history_#{Time.zone.now.to_formatted_s(:number)}.csv), response.headers['Content-Disposition'] - assert_not_empty response.body + assert_equal file_fixture('contact_versions.csv').read, response.body end end diff --git a/test/system/admin_area/contacts/csv_test.rb b/test/system/admin_area/contacts/csv_test.rb new file mode 100644 index 000000000..de5bd440e --- /dev/null +++ b/test/system/admin_area/contacts/csv_test.rb @@ -0,0 +1,22 @@ +require 'application_system_test_case' + +class ContactsCsvTest < ApplicationSystemTestCase + setup do + sign_in users(:admin) + Domain.destroy_all + Contact.all.each { |contact| contact.destroy unless contact.name == 'Acme Ltd' } + end + + def test_download_contacts_list_as_csv + travel_to Time.zone.parse('2010-07-05 10:30') + contact = Contact.first + contact.created_at = Time.zone.now + contact.save(validate: false) + + visit admin_contacts_url + click_link('CSV') + + assert_equal "attachment; filename=\"contacts_#{Time.zone.now.to_formatted_s(:number)}.csv\"; filename*=UTF-8''contacts_#{Time.zone.now.to_formatted_s(:number)}.csv", response_headers['Content-Disposition'] + assert_equal file_fixture('contacts.csv').read, page.body + end +end diff --git a/test/system/admin_area/domain_versions_test.rb b/test/system/admin_area/domain_versions_test.rb index a8aeb0cd4..75b66b1bb 100644 --- a/test/system/admin_area/domain_versions_test.rb +++ b/test/system/admin_area/domain_versions_test.rb @@ -98,7 +98,7 @@ class DomainVersionsTest < ApplicationSystemTestCase assert_equal 'text/csv; charset=utf-8', response.headers['Content-Type'] assert_equal %(attachment; filename="domain_history_#{Time.zone.now.to_formatted_s(:number)}.csv"; filename*=UTF-8''domain_history_#{Time.zone.now.to_formatted_s(:number)}.csv), response.headers['Content-Disposition'] - assert_not_empty response.body + assert_equal file_fixture('domain_versions.csv').read, response.body end def test_search_event_param diff --git a/test/system/admin_area/domains/csv_test.rb b/test/system/admin_area/domains/csv_test.rb index c84e2cf0e..691a8dc50 100644 --- a/test/system/admin_area/domains/csv_test.rb +++ b/test/system/admin_area/domains/csv_test.rb @@ -1,18 +1,21 @@ require 'application_system_test_case' -class AdminAreaCsvTest < ApplicationSystemTestCase +class DomainsCsvTest < ApplicationSystemTestCase setup do sign_in users(:admin) + Domain.all.each { |domain| domain.destroy unless domain.name == 'metro.test' } end - def test_downloads_domain_list_as_csv - search_params = {"valid_to_lteq"=>nil} - expected_csv = Domain.includes(:registrar, :registrant).search(search_params).result.to_csv - + def test_download_domains_list_as_csv travel_to Time.zone.parse('2010-07-05 10:30') + domain = Domain.first + domain.created_at = Time.zone.now + domain.save(validate: false) + visit admin_domains_url click_link('CSV') + assert_equal "attachment; filename=\"domains_#{Time.zone.now.to_formatted_s(:number)}.csv\"; filename*=UTF-8''domains_#{Time.zone.now.to_formatted_s(:number)}.csv", response_headers['Content-Disposition'] - assert_equal expected_csv, page.body + assert_equal file_fixture('domains.csv').read, page.body end end diff --git a/test/system/admin_area/invoices_test.rb b/test/system/admin_area/invoices_test.rb index 40a50e1c7..a8d0a8a75 100644 --- a/test/system/admin_area/invoices_test.rb +++ b/test/system/admin_area/invoices_test.rb @@ -40,4 +40,14 @@ class AdminAreaInvoicesTest < ApplicationSystemTestCase assert_current_path admin_invoice_path(@invoice) assert_text 'Invoice has been sent' end + + def test_download_invoices_list_as_csv + travel_to Time.zone.parse('2010-07-05 10:30') + + visit admin_invoices_url + click_link('CSV') + + assert_equal "attachment; filename=\"invoices_#{Time.zone.now.to_formatted_s(:number)}.csv\"; filename*=UTF-8''invoices_#{Time.zone.now.to_formatted_s(:number)}.csv", response_headers['Content-Disposition'] + assert_equal file_fixture('invoices.csv').read, page.body + end end