Skip to content

Commit

Permalink
added pending actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Hasjanov authored and Oleg Hasjanov committed Jan 31, 2024
1 parent ae9a80d commit 72f74f2
Show file tree
Hide file tree
Showing 25 changed files with 382 additions and 106 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ gem 'turbo-rails'
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
gem 'uuidtools' # For unique IDs (used by the epp gem)
gem 'view_component'
gem 'aasm'

# gem "kredis"
# gem "image_processing", "~> 1.2"
Expand Down
7 changes: 6 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ GIT
GEM
remote: https://rubygems.org/
specs:
aasm (5.5.0)
concurrent-ruby (~> 1.0)
actioncable (7.0.8)
actionpack (= 7.0.8)
activesupport (= 7.0.8)
Expand Down Expand Up @@ -235,6 +237,8 @@ GEM
net-smtp (0.4.0)
net-protocol
nio4r (2.5.9)
nokogiri (1.15.4-aarch64-linux)
racc (~> 1.4)
nokogiri (1.15.4-x86_64-linux)
racc (~> 1.4)
omniauth (2.1.1)
Expand Down Expand Up @@ -443,10 +447,11 @@ GEM
zeitwerk (2.6.12)

PLATFORMS
x86_64-linux
aarch64-linux-musl
x86_64-linux

DEPENDENCIES
aasm
annotate
attr_encrypted
bcrypt (~> 3.1.7)
Expand Down
4 changes: 2 additions & 2 deletions app/adapters/estonian_repp_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class EstonianReppAdapter
include AdapterInterface

def create_domain(payload:)
EstonianTld::DomainService.new(tld: Tld.first).create(payload:)
def create_domain(domain:, pending_action:)
EstonianTld::DomainService.new(tld: Tld.first).create(domain, pending_action)
end
end
24 changes: 24 additions & 0 deletions app/broadcasts/estonian_tld/inform_registrant_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module EstonianTld
class InformRegistrantService < ApplicationService
attr_reader :message, :tld

def initialize(params)
super

@message = params[:message]
@tld = params[:tld]
end

def call
post_call
end

private

def post_call
broadcast_later "dashboards_#{tld.uuid}",
'dashboards/streams/registrant_replaced',
locals: { message: }
end
end
end
37 changes: 11 additions & 26 deletions app/controllers/registrant/domains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,22 @@ def new
end

def index
@pending_actions = current_user.pending_actions
pending_actions = current_user.pending_actions.order(created_at: :desc)
@pagy, @pending_actions = pagy(pending_actions, items: 10, link_extra: 'data-turbo-action="advance"')

@pagy_domain, @domains = pagy(current_user.domains.order(created_at: :desc), items: 10, link_extra: 'data-turbo-action="advance"')
end

def show; end

def edit; end

def create
# result = current_api_adapter.create_domain(payload: params[:domain])

# if result.success?
# redirect_to registrant_domains_path, notice: t('.success')
# else
# @domain = Domain.build_from_registrar(params[:domain])
# flash.now[:alert] = result.errors
# render :new
# end

# ==============
# TODO:
# + Create pending action "Create Domain" (Table i created)
# + If user not in contact list or he doesn't have a code, create contact with code (I implement this jov)
# + Generate Invoice for this action (Need implement this method in PendingAction model)
# + User pay and he redirected back (I created oneoff service object)

# SHow banner that domain is creating (Need to implement)
# Create domain and Send this info to EPP (implement this job)

pending = PendingAction.create(
user: current_user,
action: :domain_create,
status: :pending,
info: {
domain: params[:name],
reserved_pw: params[:reserved_pw],
period: params[:period],
},
info: domain_params.to_h,
)

@invoice = pending.create_invoice_by_pending_action
Expand All @@ -65,5 +44,11 @@ def create
def update; end

def destroy; end

private

def domain_params
params.require(:domain).permit(:name, :period, :reserved_pw)
end
end
end
11 changes: 9 additions & 2 deletions app/controllers/registrant/invoices/pay_invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,16 @@ def callback
status = parsed_response[:payment_state] == 'settled' ? :paid : :failed

invoice = Invoice.find_by!(number: invoice_number)
invoice.update!(status: status)

if invoice.update(status: status)
EstonianTld::CreateDomainJob.perform_later(invoice.pending_action)

redirect_to registrant_domains_path
flash.notice = t('.domain_is_creating')
redirect_to registrant_domains_path, status: :see_other
else
flash.alert = t('.failed')
redirect_to registrant_domains_path, status: :see_other
end
end

private
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/registrant/pending_actions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Registrant
class PendingActionsController < ApplicationController
include Roles::RegistrantAbilitable

def show
@pending_action = current_user.pending_actions.find_by(uuid: params[:uuid])
end
end
end
12 changes: 0 additions & 12 deletions app/jobs/estonian_tld/create_contact_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ class EstonianTld::CreateContactJob < ApplicationJob
def perform(user)
contact = Contact.find_by(ident: user.ident)

puts '===='
puts contact
puts contact.code.present?

if contact && contact.code.present?
puts '== AU PIDAR'
user.update!(code: contact.code) if user.code.blank?
Expand All @@ -17,31 +13,23 @@ def perform(user)
payload = contact_payload(user)
response = EstonianTld::ContactService.new(tld: Tld.first).create_contact(payload:)

puts response

if response.success
code = response.body["data"]["contact"]["code"]

if contact && contact.code.blank?
ActiveRecord::Base.transaction do
contact.update!(code: code)
user.update!(code: code)
puts "ai ame here"
end
else
ActiveRecord::Base.transaction do
user.update!(code: code)
create_contact(user)

puts "ai ame here"

end
end
else
Rails.logger.info response.body["message"]
end

puts '====='
end

def create_contact(registrant_user)
Expand Down
93 changes: 90 additions & 3 deletions app/jobs/estonian_tld/create_domain_job.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,94 @@
class EstonianTld::CreateDomainJob < ApplicationJob
queue_as :critical

def perform(domain)
# TODO: Create domain in local database and send by EPP to registry
def perform(pending_action)
return if pending_action.completed?

admin_contact = Contact.find_by(code: pending_action.user.code)
tech_contact = Contact.find_by(code: Setting['code_of_technical_contact'] || pending_action.user.code)

expire_at = calculate_expiry(pending_action.info['period'].to_i)

registrant_contact = Contact.find_by(code: pending_action.user.code)

domain = Domain.find_by(name: pending_action.info['name'])

if domain.nil?
domain = Domain.new(
name: pending_action.info['name'],
registrant_id: registrant_contact.id,
expire_at: expire_at,
reserved_pw: pending_action.info['reserved_pw'],
domain_contacts: [
AdminDomainContact.new(
contact_id: admin_contact.id,
),
TechDomainContact.new(
contact_id: tech_contact.id,
),
],
nameservers: Setting['default_nameserver_records'] || [],
dnssec_keys: Setting['dnssec_enabled'] ? Setting['default_dnssec_keys'] : [],
)
REPP_ENDPOINT

if domain.save
# response = EstonianTld::DomainService.new(tld: Tld.first).create(domain, pending_action)

response = current_api_adapter.create_domain(domain: domain, pending_action: pending_action)

if response.success
pending_action.update(status: :completed)
domain.update(state: :active)
else
pending_action.update(status: :failed, errors_in_response: response.body)
inform_registrant_service(tld: Tld.first, message: response.body)

Rails.logger.info '------ error -----'
Rails.logger.info response.body
Rails.logger.info '------ error -----'
end

Rails.logger.info "Domain #{domain.name} was created!"
else
Rails.logger.info domain.errors.inspect
end
end

private

def inform_registrant_service(tld:, message:)
EstonianTld::InformRegistrantService.call({ tld:, message: })
end

def ns_attrs(nameservers)
nameservers.map { |n| n.extract!(:hostname, :ipv4, :ipv6) }
end

def contacts(contacts, type)
return [] unless contacts

contacts.select { |c| c[:type] == type }.pluck(:code)
end

# TODO: Need to complete this method
def domain_params(domain)
{
domain: {
name: domain.name,
reserved_pw: domain.reserved_pw,
registrant: domain.registrant.code,
# TODO: domain no store period unit value !!! period_unit: payload[:period][-1].to_s,
# TODO: domain no store perion value !!! period: payload[:period].to_i,
nameservers_attributes: ns_attrs(payload[:nameservers]),
admin_contacts: contacts(payload[:contacts], 'admin'),
tech_contacts: contacts(payload[:contacts], 'tech'),
dnskeys_attributes: payload[:dns_keys],
},
}
end

def calculate_expiry(period)
(Time.zone.now.advance(months: period) + 1.day).beginning_of_day
end
end
end
35 changes: 35 additions & 0 deletions app/models/pending_action.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
class PendingAction < ApplicationRecord
include AASM

belongs_to :user
belongs_to :invoice, optional: true

enum status: { pending: 100, completed: 200, declined: 300, failed: 400 }

aasm column: :status, enum: true do
state :pending, initial: true
state :completed
state :declined
state :failed

event :complete do
transitions from: :pending, to: :completed
end

event :decline do
transitions from: :pending, to: :declined
end

event :fail do
transitions from: :pending, to: :failed
end
end

validate :unable_to_update

def unable_to_update
return if pending?
return if invoice.issued?

errors.add(:base, I18n.t('errors.messages.unable_to_update'))
end

def can_i_edit_it?
pending? && invoice.issued?
end

enum action: {
domain_create: 100,
domain_update: 200,
Expand Down
9 changes: 8 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ class User < ApplicationRecord
after_find :split_phone_into_code_and_number

def self.from_omniauth(tara_params)
full_name = "#{tara_params.dig('info', 'given_name')} #{tara_params.dig('info', 'family_name')}"
full_name = "#{tara_params.dig('info', 'first_name')} #{tara_params.dig('info', 'last_name')}"
ident = tara_params['uid'][2..]

print("TARA params: #{tara_params}")

user = User.find_or_initialize_by(ident:)
user.name = full_name
user.ident = ident
Expand All @@ -55,4 +57,9 @@ def birthday(identity_code:)
Date.parse(identity_code.slice(1..6))
end

def domains
contact = Contact.find_by(code: code)
Domain.where(registrant_id: contact.id)
end

end
Loading

0 comments on commit 72f74f2

Please sign in to comment.