diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..943ae65 Binary files /dev/null and b/.DS_Store differ diff --git a/app/components/admin/settings/contact_group/component.html.erb b/app/components/admin/settings/contact_group/component.html.erb new file mode 100644 index 0000000..9ce45ae --- /dev/null +++ b/app/components/admin/settings/contact_group/component.html.erb @@ -0,0 +1,9 @@ +<% @contact_group.each do |setting| %> + <% if setting.format == 'array' and setting.code == 'code_of_technical_contact' %> + <%= component 'settings/tech_contact_field', form: form, setting: setting %> + <% else %> +
+ <%= component 'form/checkbox_with_description', form: form, attribute: "[settings][#{setting.id}", label_name: setting.code, description: setting.description, value: setting.value == 'true' %> +
+ <% end %> +<% end %> diff --git a/app/components/admin/settings/contact_group/component.rb b/app/components/admin/settings/contact_group/component.rb new file mode 100644 index 0000000..5db6980 --- /dev/null +++ b/app/components/admin/settings/contact_group/component.rb @@ -0,0 +1,14 @@ +module Admin + module Settings + module ContactGroup + class Component < ApplicationViewComponent + attr_reader :contact_group, :form + + def initialize(contact_group:, form:) + @contact_group = contact_group + @form = form + end + end + end + end +end diff --git a/app/components/admin/settings/invoice_group/component.html.erb b/app/components/admin/settings/invoice_group/component.html.erb new file mode 100644 index 0000000..dd58bd9 --- /dev/null +++ b/app/components/admin/settings/invoice_group/component.html.erb @@ -0,0 +1,15 @@ +<% @invoice_group.each do |setting| %> +
+ +
+ <%= form.label setting.code, class: 'block text-sm font-bold text-gray-700' %> +
+
+
+ <%= heroicon 'cog', variant: :solid, options: { class: 'h-5 w-5 text-gray-400' } %> +
+ <%= form.text_field "[settings][#{setting.id}", placeholder: t(".#{setting.code}"), value: setting.value, class: 'block w-full rounded-md border-gray-300 pl-10 focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm py-2' %> +
+ <%= form.hidden_field :id, value: "[settings][#{setting.id}" %> +
+<% end %> \ No newline at end of file diff --git a/app/components/admin/settings/invoice_group/component.rb b/app/components/admin/settings/invoice_group/component.rb new file mode 100644 index 0000000..08b9f03 --- /dev/null +++ b/app/components/admin/settings/invoice_group/component.rb @@ -0,0 +1,14 @@ +module Admin + module Settings + module InvoiceGroup + class Component < ApplicationViewComponent + attr_reader :invoice_group, :form + + def initialize(invoice_group:, form:) + @invoice_group = invoice_group + @form = form + end + end + end + end +end diff --git a/app/components/admin/settings/registrar_group/component.html.erb b/app/components/admin/settings/registrar_group/component.html.erb new file mode 100644 index 0000000..1c49de3 --- /dev/null +++ b/app/components/admin/settings/registrar_group/component.html.erb @@ -0,0 +1,32 @@ +<% @registrar_group.each do |setting| %> + +
+
+ <%= form.label setting.code, class: 'block text-sm font-bold text-gray-700' %> +
+ <% if setting.format == 'array' and setting.code == 'default_nameserver_records' %> + <%= component 'settings/array_field', form: form, setting: setting %> + + <% elsif setting.format == 'hash' %> + <%= component 'settings/hash_field', form: form, setting: setting %> + + <% elsif setting.format == 'boolean' %> + +
+ <%= component 'form/checkbox_with_description', form: form, attribute: "[settings][#{setting.id}", label_name: setting.code, description: setting.description, value: setting.value == 'true' %> +
+ + <% else %> + +
+
+ <%= heroicon 'cog', variant: :solid, options: { class: 'h-5 w-5 text-gray-400' } %> +
+ <%= form.text_field "[settings][#{setting.id}", placeholder: t(".#{setting.code}"), value: setting.value, class: 'block w-full rounded-md border-gray-300 pl-10 focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm py-2' %> +
+ + <% end %> + + <%= form.hidden_field :id, value: "[settings][#{setting.id}" %> +
+ <% end %> \ No newline at end of file diff --git a/app/components/admin/settings/registrar_group/component.rb b/app/components/admin/settings/registrar_group/component.rb new file mode 100644 index 0000000..d12d813 --- /dev/null +++ b/app/components/admin/settings/registrar_group/component.rb @@ -0,0 +1,14 @@ +module Admin + module Settings + module RegistrarGroup + class Component < ApplicationViewComponent + attr_reader :registrar_group, :form + + def initialize(registrar_group:, form:) + @registrar_group = registrar_group + @form = form + end + end + end + end +end diff --git a/app/components/form_domain_contact/component.html.erb b/app/components/form_domain_contact/component.html.erb index 89e1040..ce98f01 100644 --- a/app/components/form_domain_contact/component.html.erb +++ b/app/components/form_domain_contact/component.html.erb @@ -23,7 +23,7 @@ @@ -34,7 +34,7 @@ <%= form.fields_for contact_type_objects.to_sym, constanicize_contact_type.new, child_index: "new_contact" do |builder| %> <% builder.fields_for :contact, Contact.new do |contact_form| %> - <%= render 'contact_fields', form: contact_form, remove_button: true %> + <%= render 'contact_fields', form: contact_form, remove_button: true, contact_type: contact_type %> <% end %> <% end %> diff --git a/app/components/settings/array_field/component.html.erb b/app/components/settings/array_field/component.html.erb new file mode 100644 index 0000000..819aa7a --- /dev/null +++ b/app/components/settings/array_field/component.html.erb @@ -0,0 +1,48 @@ +
+ +
+ <% @parsed_data.each_with_index do |item, index| %> + +
+ <% item.each do |k, v| %> + <%= form.label k, "#{k.to_s.humanize} #{index + 1}", class: 'block text-sm font-medium text-gray-700 w-64' %> + <%= form.text_field "settings[#{setting.id}][][#{k}]", value: v, class: 'mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm' %> + <% end %> + + + delete + +
+ <% end %> +
+ + +
+ <%= t('.add_contact') %> +
+ + +
diff --git a/app/components/settings/array_field/component.rb b/app/components/settings/array_field/component.rb new file mode 100644 index 0000000..f02aa19 --- /dev/null +++ b/app/components/settings/array_field/component.rb @@ -0,0 +1,13 @@ +module Settings + module ArrayField + class Component < ApplicationViewComponent + attr_reader :setting, :form + + def initialize(setting:, form:) + @setting = setting + @form = form + @parsed_data = eval(setting.value) + end + end + end +end diff --git a/app/components/settings/hash_field/component.html.erb b/app/components/settings/hash_field/component.html.erb new file mode 100644 index 0000000..7f7fb00 --- /dev/null +++ b/app/components/settings/hash_field/component.html.erb @@ -0,0 +1,8 @@ +
+ + <% @parsed_value.each do |k, v| %> + <%= form.label k.to_sym, k.to_s.humanize, class: 'block text-sm font-medium text-gray-700 w-64' %> + <%= form.text_field "settings[#{setting.id}][#{k}]", value: v, class: 'mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm' %> + <% end %> + +
\ No newline at end of file diff --git a/app/components/settings/hash_field/component.rb b/app/components/settings/hash_field/component.rb new file mode 100644 index 0000000..7e07145 --- /dev/null +++ b/app/components/settings/hash_field/component.rb @@ -0,0 +1,13 @@ +module Settings + module HashField + class Component < ApplicationViewComponent + attr_reader :setting, :form + + def initialize(setting:, form:) + @setting = setting + @form = form + @parsed_value = eval(setting.value) + end + end + end +end diff --git a/app/components/settings/tech_contact_field/component.html.erb b/app/components/settings/tech_contact_field/component.html.erb new file mode 100644 index 0000000..66c024f --- /dev/null +++ b/app/components/settings/tech_contact_field/component.html.erb @@ -0,0 +1,79 @@ + + + +
+ +
+ <% @parsed_data.each_with_index do |item, index| %> + +
+ <%= form.label "tech contact code #{index}", class: 'block text-sm font-medium text-gray-700 w-64' %> + + <%= component 'form/autocomplete_input', form: form, attribute: :value, heroicon_name: 'user', placeholder: 'ABC:123', + data_attributes: { action: "input->form--autocomplete#search", form__autocomplete_target: "input" }, + value: item %> + + <%= form.hidden_field "settings[#{setting.id}][]", data: { form__autocomplete_target: "hiddenInput" }, value: item %> + + + delete + +
+ <% end %> +
+ + +
+ <%= t('.add_contact') %> +
+ + +
diff --git a/app/components/settings/tech_contact_field/component.rb b/app/components/settings/tech_contact_field/component.rb new file mode 100644 index 0000000..88be1b7 --- /dev/null +++ b/app/components/settings/tech_contact_field/component.rb @@ -0,0 +1,13 @@ +module Settings + module TechContactField + class Component < ApplicationViewComponent + attr_reader :setting, :form + + def initialize(setting:, form:) + @setting = setting + @form = form + @parsed_data = eval(setting.value) + end + end + end +end diff --git a/app/controllers/admin/contacts/searches_controller.rb b/app/controllers/admin/contacts/searches_controller.rb new file mode 100644 index 0000000..8f581b0 --- /dev/null +++ b/app/controllers/admin/contacts/searches_controller.rb @@ -0,0 +1,21 @@ +module Admin + module Contacts + class SearchesController < ApplicationController + include Roles::AdminAbilitable + + def show + puts '----' + puts params[:query] + puts '-----' + + @contacts = Contact.search(params[:query]) + + puts '==== contacts' + puts @contacts.inspect + puts '=====' + + render json: @contacts.limit(5).map(&:as_json) + end + end + end +end diff --git a/app/controllers/admin/settings_controller.rb b/app/controllers/admin/settings_controller.rb index 60ebe04..c79f69d 100644 --- a/app/controllers/admin/settings_controller.rb +++ b/app/controllers/admin/settings_controller.rb @@ -5,27 +5,18 @@ class SettingsController < ApplicationController def show @invoice_group = Setting.with_group('invoice') @contact_group = Setting.with_group('contacts') + @registrar_group = Setting.with_group('registrar') end def update - if Setting.update(casted_settings.keys, casted_settings.values) + updated, errors = Admin::SettingUpdaterService.new(params).update + + if updated redirect_to root_path, status: :see_other, notice: t('.success') else - flash[:alert] = update.errors.values.uniq.join(', ') + flash[:alert] = errors.uniq.join(', ') render :show, status: :unprocessable_entity end end - - private - - def casted_settings - settings = {} - - params[:settings].each do |k, v| - settings[k] = { value: v } - end - - settings - end end end diff --git a/app/controllers/registrant/domains_controller.rb b/app/controllers/registrant/domains_controller.rb index 8eb5009..316ae72 100644 --- a/app/controllers/registrant/domains_controller.rb +++ b/app/controllers/registrant/domains_controller.rb @@ -6,8 +6,8 @@ def new @domain = Domain.new Tld::Estonian::MIN_DOMAIN_CONTACT_COUNT.times do |i| - admin_contact = Contact.find_by(code: current_user.code) - tech_contact = Contact.find_by(code: Setting.code_of_technical_contact) + admin_contact = Contact.find_by(code: current_user.code, role: :priv) + tech_contact = Contact.find_by(code: Setting.code_of_technical_contact || current_user.code) if i % 2 == 0 @domain.admin_domain_contacts.build(contact: admin_contact) @@ -79,13 +79,15 @@ def create @invoice = pending.create_invoice_by_pending_action(domain_price.price.to_f) if @invoice - flash.now[:notice] = t('.success') + flash[:notice] = t('.success') - render turbo_stream: [ - turbo_stream.append('flash', partial: 'layouts/flash'), - turbo_stream.append('payment_method', partial: 'registrant/domains/payment_form', - locals: { invoice: @invoice}) - ] + redirect_to registrant_domains_path, status: :see_other + + # render turbo_stream: [ + # turbo_stream.append('flash', partial: 'layouts/flash'), + # turbo_stream.append('payment_method', partial: 'registrant/domains/payment_form', + # locals: { invoice: @invoice}) + # ] else flash[:alert] = @invoice.errors.full_messages render turbo_stream: turbo_stream.replace('flash', partial: 'layouts/flash') diff --git a/app/javascript/controllers/behaviour/auto_removeable_controller.js b/app/javascript/controllers/behaviour/auto_removeable_controller.js new file mode 100644 index 0000000..6594a01 --- /dev/null +++ b/app/javascript/controllers/behaviour/auto_removeable_controller.js @@ -0,0 +1,25 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + static values = { delay: { type: Number, default: 2 } }; + + connect() { + if (this.delayValue) { + this.delayTid = setTimeout(() => { + this.hide(); + delete this.delayTid; + }, this.delayValue * 1000); + + } + } + + disconnect() { + if (this.delayTid) { + clearTimeout(this.delayTid); + } + } + + hide() { + this.element.remove(); + } +} diff --git a/app/javascript/controllers/behaviour/removeable_controller.js b/app/javascript/controllers/behaviour/removeable_controller.js index 6594a01..4363513 100644 --- a/app/javascript/controllers/behaviour/removeable_controller.js +++ b/app/javascript/controllers/behaviour/removeable_controller.js @@ -1,25 +1,8 @@ import { Controller } from "@hotwired/stimulus" export default class extends Controller { - static values = { delay: { type: Number, default: 2 } }; - - connect() { - if (this.delayValue) { - this.delayTid = setTimeout(() => { - this.hide(); - delete this.delayTid; - }, this.delayValue * 1000); - - } - } - - disconnect() { - if (this.delayTid) { - clearTimeout(this.delayTid); - } - } - - hide() { + remove(event) { + event.preventDefault(); this.element.remove(); } } diff --git a/app/javascript/controllers/form/autocomplete_controller.js b/app/javascript/controllers/form/autocomplete_controller.js index 6fda4b5..367237b 100644 --- a/app/javascript/controllers/form/autocomplete_controller.js +++ b/app/javascript/controllers/form/autocomplete_controller.js @@ -4,7 +4,8 @@ export default class extends Controller { static targets = ["input", "results", "hiddenInput"] static values = { searchUrl: String, - initiator: { type: String, default: "registrar" } + initiator: { type: String, default: "registrar" }, + setCode: { type: Boolean, default: false } } connect() { @@ -46,21 +47,7 @@ export default class extends Controller { .then((contacts) => { console.log(contacts); - let html = "" - - if (this.initiatorValue === "registrant") { - html = contacts.map(contact => ` -
  • - ${contact.name} - ${contact.ident} -
  • - `).join('') - } else { - html = contacts.map(contact => ` -
  • - ${contact.name} - ${contact.code} -
  • - `).join('') - } + let html = this.assignDropboxValue(contacts); this.resultsTarget.innerHTML = html this.resultsTarget.style.display = contacts.length > 0 ? 'block' : 'none'; @@ -76,6 +63,47 @@ export default class extends Controller { this.highlightFirstItem(); } + assignDropboxValue(contacts) { + let html = "" + + console.log(this.setCodeValue); + + if (this.initiatorValue === "registrant") { + if (this.setCodeValue) { + html = contacts.map(contact => ` +
  • + ${contact.name} - ${contact.code} +
  • + `).join('') + } + else { + html = contacts.map(contact => ` +
  • + ${contact.name} - ${contact.ident} +
  • + `).join('') + } + + } else { + if (this.setCodeValue) { + html = contacts.map(contact => ` +
  • + ${contact.name} - ${contact.code} +
  • + `).join('') + } + else { + html = contacts.map(contact => ` +
  • + ${contact.name} - ${contact.code} +
  • + `).join('') + } + } + + return html; + } + highlightFirstItem() { const firstItem = this.resultsTarget.querySelector('li'); if (firstItem) firstItem.classList.add('bg-indigo-200'); diff --git a/app/javascript/controllers/index.js b/app/javascript/controllers/index.js index 3feb5cc..a9d157f 100644 --- a/app/javascript/controllers/index.js +++ b/app/javascript/controllers/index.js @@ -22,6 +22,9 @@ application.register("form--multi-select", Form__MultiSelectController); import Form__AddInputController from "./form/add_input_controller"; application.register("form--add-input", Form__AddInputController); +import Behaviour__AutoRemoveableController from "./behaviour/auto_removeable_controller"; +application.register("behaviour--auto-removeable", Behaviour__AutoRemoveableController); + import Behaviour__RemoveableController from "./behaviour/removeable_controller"; application.register("behaviour--removeable", Behaviour__RemoveableController); diff --git a/app/jobs/estonian_tld/domain_creation_process/create_contacts_job.rb b/app/jobs/estonian_tld/domain_creation_process/create_contacts_job.rb index 3440dc8..6cb723b 100644 --- a/app/jobs/estonian_tld/domain_creation_process/create_contacts_job.rb +++ b/app/jobs/estonian_tld/domain_creation_process/create_contacts_job.rb @@ -26,6 +26,7 @@ def perform(pending_action) next if v['contact']['_destroy'] == 'true' contact = Contact.find_by(ident: v['contact']['ident']) + if contact.nil? contacts_what_not_exists << v elsif contact.code.blank? @@ -81,7 +82,7 @@ def create_contact(contact) end after_perform do |job| - EstonianTld::DomainCreationProcess::CreateDomainJob.perform_later(@pending_action) + EstonianTld::DomainCreationProcess::CreateDomainJob.perform_now(@pending_action) end end end diff --git a/app/models/contact.rb b/app/models/contact.rb index 3ec1b8e..e644bc9 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -34,11 +34,11 @@ class Contact < ApplicationRecord validates :name, presence: true validates :role, inclusion: { in: Contact.roles } - def self.create_contact_from_registrant_perform_later(registrant_user:) + def self.create_contact_from_registrant_perform_now(registrant_user:) c = Contact.find_by(ident: registrant_user.ident) return c if c && c.code.present? - CreateContactJob.perform_later(registrant_user) + CreateContactJob.perform_now(registrant_user) end def self.search(query) diff --git a/app/models/setting.rb b/app/models/setting.rb index ceccfb3..6b91468 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -86,6 +86,7 @@ def hash_format end def array_format - JSON.parse(value).to_a + # JSON.parse(value).to_a + eval(value) end end diff --git a/app/services/admin/setting_updater_service.rb b/app/services/admin/setting_updater_service.rb new file mode 100644 index 0000000..09d4ca5 --- /dev/null +++ b/app/services/admin/setting_updater_service.rb @@ -0,0 +1,67 @@ +module Admin + class SettingUpdaterService + attr_reader :params + + def initialize(params) + @params = params + end + + def update + updated = true + errors = [] + + casted_settings.each do |id, setting_params| + setting = Setting.find_by(id: id) + + if setting.update!(setting_params) + next + else + updated = false + errors += setting.errors.full_messages + end + end + + return updated, errors + end + + private + + def casted_settings + settings_values = {} + + params.require(:settings).permit! + + params[:settings].each do |k, v| + setting = Setting.find_by(id: k) + + next unless setting + + formatted_value = format_setting_value(v, setting.format) + settings_values[setting.id] = { value: formatted_value } + end + + settings_values + end + + def format_setting_value(value, format_type) + case format_type + when 'boolean' + (value.to_s == 'true').to_s + when 'integer' + value.to_i + when 'float' + value.to_f + when 'hash' + value.is_a?(ActionController::Parameters) ? value.to_unsafe_h.to_json : value + when 'array' + if value.is_a?(Array) && value.first.is_a?(ActionController::Parameters) + value.map(&:to_unsafe_h).to_json + else + value.to_json + end + else + value + end + end + end +end \ No newline at end of file diff --git a/app/services/domain_services/create_domain_service.rb b/app/services/domain_services/create_domain_service.rb index 6c9fa7a..1975f7d 100644 --- a/app/services/domain_services/create_domain_service.rb +++ b/app/services/domain_services/create_domain_service.rb @@ -7,7 +7,7 @@ def initialize(pending_action) end def call - EstonianTld::DomainCreationProcess::CreateContactsJob.perform_later(pending_action) + EstonianTld::DomainCreationProcess::CreateContactsJob.perform_now(pending_action) # EstonianTld::DomainCreationProcess::CreateDomainJob.perform_later(pending_action) end end diff --git a/app/views/admin/settings/show.html.erb b/app/views/admin/settings/show.html.erb index fd8a47d..1654159 100644 --- a/app/views/admin/settings/show.html.erb +++ b/app/views/admin/settings/show.html.erb @@ -8,31 +8,13 @@
    <%= form_with url: admin_settings_path, method: :patch, class: 'w-full mt-8 space-y-4' do |form| %> - <% @invoice_group.each do |setting| %> -
    + <%= component 'admin/settings/invoice_group', form: form, invoice_group: @invoice_group %> + <%= component 'admin/settings/registrar_group', form: form, registrar_group: @registrar_group %> + <%= component 'admin/settings/contact_group', form: form, contact_group: @contact_group %> -
    - <%= form.label setting.code, class: 'block text-sm font-bold text-gray-700' %> -
    -
    -
    - <%= heroicon 'cog', variant: :solid, options: { class: 'h-5 w-5 text-gray-400' } %> -
    - <%= form.text_field "[settings][#{setting.id}", placeholder: t(".#{setting.code}"), value: setting.value, class: 'block w-full rounded-md border-gray-300 pl-10 focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm py-2' %> -
    - <%= form.hidden_field :id, value: "[settings][#{setting.id}" %> -
    +
    + <%= form.submit t('.submit'), class: "text-gray-600 inline-flex w-72 justify-center rounded-md border border-[#DDA77B] bg-white py-2 px-4 text-sm font-medium text-[#DDA77B] shadow-sm hover:bg-[#DDA77B] hover:text-white mt-2 cursor-pointer" %> +
    <% end %> - - <% @contact_group.each do |setting| %> -
    - <%= component 'form/checkbox_with_description', form: form, attribute: "[settings][#{setting.id}", label_name: setting.code, description: setting.description, value: setting.value == 'true' %> -
    - <% end %> - -
    - <%= form.submit t('.submit'), class: "text-gray-600 inline-flex w-72 justify-center rounded-md border border-[#DDA77B] bg-white py-2 px-4 text-sm font-medium text-[#DDA77B] shadow-sm hover:bg-[#DDA77B] hover:text-white mt-2 cursor-pointer" %> -
    - <% end %>
    diff --git a/app/views/layouts/_flash.html.erb b/app/views/layouts/_flash.html.erb index 5927f81..aa2d1bf 100644 --- a/app/views/layouts/_flash.html.erb +++ b/app/views/layouts/_flash.html.erb @@ -1,13 +1,13 @@ <%# locals: () -%>
    <% if flash[:notice] %> -