Skip to content

Commit

Permalink
Merge pull request #131 from internetee/verify-contacts
Browse files Browse the repository at this point in the history
Added contact verification request feature
  • Loading branch information
vohmar authored Nov 19, 2024
2 parents 6c4b9a4 + 43b05c0 commit 12b01d4
Show file tree
Hide file tree
Showing 27 changed files with 2,068 additions and 59 deletions.
3 changes: 2 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def respond(msg, dialog: false)
respond_to do |format|
format.html { respond_html(msg) }
format.turbo_stream { respond_turbo_stream(msg, dialog) }
format.json { render json: { error: msg }, status: :unprocessable_entity }
end
end

Expand All @@ -53,7 +54,7 @@ def handle_error_response(res, dialog: false)
msg = res.body[:message]

case res.body[:code]
when 2202, 503, 401
when 2202, 401
respond_with_log_out(msg)
when 2000..2500
respond(msg, dialog: dialog)
Expand Down
123 changes: 81 additions & 42 deletions app/controllers/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
class ContactsController < BaseController # rubocop:disable Metrics/ClassLength
before_action :set_pagy_params, only: %i[index show]

# rubocop:disable Metrics/MethodLength
# Get all the contacts
def index
conn = ApiConnector::Contacts::All.new(**auth_info)
Expand All @@ -13,17 +12,9 @@ def index
offset: pdf_or_csv_request? ? nil : @offset)
handle_response(result); return if performed?

@ident_types = [['Vali', '']] + @response.ident_types
@statuses = @response.statuses
@contacts = @response.contacts
@pagy = Pagy.new(count: @response.count, items: session[:page_size], page: @page)
respond_to do |format|
format.html
format.csv { format_csv }
format.pdf { format_pdf }
end
set_instance_variables
respond_to_formats
end
# rubocop:enable Metrics/MethodLength

def search
conn = ApiConnector::Contacts::Finder.new(**auth_info)
Expand All @@ -38,13 +29,9 @@ def search

# Show the contact info
def show
conn = ApiConnector::Contacts::Reader.new(**auth_info)
result = conn.call_action(id: params[:contact_code], per_page: session[:page_size],
page: @page, domain_filter: params[:contact_types],
q: search_params)
handle_response(result); return if performed?
fetch_contact
return if performed?

@contact = @response.contact
@domains = @contact[:domains]
@contact_types = ApplicationHelper::CONTACT_TYPE.merge(registrant: 'Registrant')
@pagy = Pagy.new(count: @contact[:domains_count], items: session[:page_size], page: @page)
Expand Down Expand Up @@ -72,12 +59,7 @@ def new
end

def create
conn = ApiConnector::Contacts::Creator.new(**auth_info)
result = conn.call_action(payload: contact_payload)
handle_response(result); return if performed?

flash.notice = @message
redirect_to contact_path(contact_code: @response.contact[:code])
handle_contact_action(ApiConnector::Contacts::Creator)
end

def edit
Expand All @@ -89,14 +71,28 @@ def edit
end

def update
conn = ApiConnector::Contacts::Updater.new(**auth_info)
result = conn.call_action(payload: contact_payload)
handle_contact_action(ApiConnector::Contacts::Updater)
end

def verify
conn = ApiConnector::Contacts::Verifier.new(**auth_info)
result = conn.call_action(id: params[:contact_code])
handle_response(result); return if performed?

flash.notice = @message
flash.notice = 'Successfully sent identification request'
redirect_to contact_path(contact_code: @response.contact[:code])
end

def download_poi
conn = ApiConnector::Contacts::PoiDownloader.new(**auth_info)
result = conn.call_action(id: params[:contact_code])
handle_response(result); return if performed?

send_data(@response, type: 'application/pdf',
disposition: 'attachment',
filename: @message.match(/filename=(\"?)(.+)\1/)[2])
end

private

def contact_params
Expand All @@ -105,31 +101,74 @@ def contact_params
:city, :street, :state, :zip, :legal_document)
end

# rubocop:disable Metrics/MethodLength
def set_instance_variables
@ident_types = [['Vali', '']] + @response.ident_types
@statuses = @response.statuses
@contacts = @response.contacts
@pagy = Pagy.new(count: @response.count, items: session[:page_size], page: @page)
end

def respond_to_formats
respond_to do |format|
format.html
format.csv { format_csv }
format.pdf { format_pdf }
end
end

def handle_contact_action(conn_class)
conn = conn_class.new(**auth_info)
result = conn.call_action(payload: contact_payload)
handle_response(result); return if performed?

flash.notice = @message
redirect_to contact_path(contact_code: @response.contact[:code])
end

def fetch_contact
conn = ApiConnector::Contacts::Reader.new(**auth_info)
result = conn.call_action(
id: params[:contact_code],
per_page: session[:page_size],
page: @page,
domain_filter: params[:contact_types],
q: search_params
)
handle_response(result)
@contact = @response&.contact
end

def contact_payload
{
id: contact_params[:code],
name: contact_params[:name],
email: contact_params[:email],
phone: contact_params[:phone],
ident: {
ident: contact_params[:ident],
ident_type: contact_params[:ident_type],
ident_country_code: contact_params[:ident_country_code],
},
addr: if current_user.address_processing
{
country_code: contact_params[:country_code],
city: contact_params[:city],
street: contact_params[:street],
zip: contact_params[:zip],
state: contact_params[:state],
}
end,
ident: contact_ident_payload,
addr: contact_address_payload,
legal_document: transform_file_params(contact_params[:legal_document]),
}
end
# rubocop:enable Metrics/MethodLength

def contact_ident_payload
{
ident: contact_params[:ident],
ident_type: contact_params[:ident_type],
ident_country_code: contact_params[:ident_country_code],
}
end

def contact_address_payload
return unless current_user.address_processing

{
country_code: contact_params[:country_code],
city: contact_params[:city],
street: contact_params[:street],
zip: contact_params[:zip],
state: contact_params[:state],
}
end

def format_csv
raw_csv = ContactListCsvPresenter.new(objects: @contacts,
Expand Down
5 changes: 4 additions & 1 deletion app/services/api_connector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ def self.call(**args)
end

def call_action(**args)
Rails.logger.debug("Calling action: #{self.class::ACTION} with args: #{args}")
__send__(self.class::ACTION, **args)
end

private

def request(url:, method:, params: nil, headers: nil)
Rails.logger.debug("Sending #{method} request to #{url} with params: #{params} and headers: #{headers}")
request = faraday_request(url: url, headers: headers)
response = send_request(request, method, url, params)
success = response.status == 200
success = [200, 201].include?(response.status)
body = process_response_body(response)

OpenStruct.new(body: body, success: success, type: response['content-type'])
Expand All @@ -52,6 +54,7 @@ def send_non_get_request(request, method, params)
end

def process_response_body(response)
Rails.logger.debug("Processing response with content type: #{response['content-type']}")
case response['content-type']
when 'application/pdf', 'application/octet-stream'
{ data: response.body, message: response.headers['content-disposition'] }
Expand Down
18 changes: 18 additions & 0 deletions app/services/api_connector/contacts/poi_downloader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

class ApiConnector
module Contacts
class PoiDownloader < ApiConnector
ACTION = 'download_poi'
ENDPOINT = {
method: 'get',
endpoint: '/contacts/download_poi',
}.freeze

def download_poi(params = {})
request(url: url_with_id(params[:id]),
method: method)
end
end
end
end
18 changes: 18 additions & 0 deletions app/services/api_connector/contacts/verifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

class ApiConnector
module Contacts
class Verifier < ApiConnector
ACTION = 'verify_contact'
ENDPOINT = {
method: 'post',
endpoint: '/contacts/verify',
}.freeze

def verify_contact(params = {})
request(url: url_with_id(params[:id]),
method: method)
end
end
end
end
13 changes: 13 additions & 0 deletions app/views/contacts/partials/_general.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,18 @@
<th><%= t('contacts.show.phone') %></th>
<td><%= @contact[:phone] %></td>
</tr>
<tr>
<th><%= t('contacts.show.verification') %></th>
<td>
<% if @contact[:verified_at].present? %>
<%= t('contacts.show.verified_on') %> <%= l(@contact[:verified_at].to_datetime, format: :long) %>
<%= icon_link_to 'fas fa-file-pdf', download_poi_contact_path(contact_code: @contact[:code]), class: 'simple_tooltip', data: { 'tippy-content': t('contacts.show.download_poi') } %>
<% elsif @contact[:ident_request_sent_at].present? %>
<%= t('contacts.show.ident_requested_on') %> <%= l(@contact[:ident_request_sent_at].to_datetime, format: :long) %>
<% else %>
<%= t('contacts.show.unverified') %>
<% end %>
</td>
</tr>
</tbody>
</table>
15 changes: 15 additions & 0 deletions app/views/contacts/partials/_verify_confirm_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div class="dialog" id="contact_verify">
<div class="wrapper wrapper__small">
<div class="paper paper--text__left">
<div class="paper--header">
<%= button_tag(class: 'button--close', type: '') do %>
<i class="fas fa-times"></i>
<% end %>
<h2 id="title"><%= t('contacts.show.verify_confirm_html', code: @contact[:code]) %></h2>
</div>
<div class="paper--content">
<%= button_to t(:send), verify_contact_path(contact_code: @contact[:code]), data: { turbo: false }, class: 'button button--primary' %>
</div>
</div>
</div>
</div>
7 changes: 7 additions & 0 deletions app/views/contacts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
<div class="col-xs-auto">
<%= link_to t(:edit), edit_contact_path(contact_code: @contact[:code]), class: 'button button--primary' %>
</div>
<% if @contact[:ident][:type] == "priv" %>
<div class="col-xs-auto">
<%= link_to t(:verify), 'javascript: void(0);', class: 'button button--secondary',
'data-dialog': 'contact_verify' %>
</div>
<% end %>
<div class="col-xs-auto">
<%= link_to t(:delete), delete_contact_path(contact_code: @contact[:code]), class: 'button button--danger' %>
</div>
Expand Down Expand Up @@ -53,3 +59,4 @@
</div>
</div>
</div>
<%= render 'contacts/partials/verify_confirm_form' %>
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
config.cache_store = :null_store
end

config.log_level = :debug

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false

Expand Down
9 changes: 9 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ en:
enable: "Enable"
disable: "Disable"
transfer: "Transfer"
verify: "Verify"
add_credit: "Add credit"
choose_file: "Choose file"
created_at: "Created"
Expand Down Expand Up @@ -377,10 +378,18 @@ en:
registrar_name: "Registrar"
valid_to: "Valid to"
roles: "Roles"
verification: "Verification status"
requested_at: "Requested at"
verified_at: "Verified at"
verify_confirm_html: "Are you sure you want to send identification request to contact <span>%{code}</span>?"
verified_on: "Verified on"
ident_requested_on: "Identification requested on"
unverified: "Unverified"
contact_types:
registrant: "Registrant"
tech: "Technical contact"
admin: "Admin contact"
download_poi: "Download Proof of Identity"

delete:
title: "Delete contact"
Expand Down
9 changes: 9 additions & 0 deletions config/locales/et.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ et:
enable: "Aktiveeri"
disable: "Deaktiveeri"
transfer: "Too üle"
verify: "Verifitseeri"
add_credit: "Lisa krediiti"
choose_file: "Vali fail arvutist"
created_at: "Loodud"
Expand Down Expand Up @@ -379,10 +380,18 @@ et:
registrar_name: "Registripidaja"
valid_to: "Kehtiv kuni"
roles: "Rollid"
verification: "Tuvastuse staatus"
requested_at: "Taotletud"
verified_at: "Verifitseeritud"
verify_confirm_html: "Kas olete kindel, et soovite saata kontaktile <span>%{code}</span> tuvastamistaotlust?"
verified_on: "Verifitseeritud"
ident_requested_on: "Identifitseerimine taotletud"
unverified: "Verifitseerimata"
contact_types:
registrant: "Registreerija"
tech: "Tehniline kontakt"
admin: "Admin kontakt"
download_poi: "Laadi alla isikutõend"

delete:
title: "Kustuta kontakt"
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
post 'contacts/update', to: 'contacts#update', as: :update_contact
get 'contact/delete', to: 'contacts#delete', as: :delete_contact
delete 'contacts/destroy', to: 'contacts#destroy', as: :destroy_contact
post 'contact/verify', to: 'contacts#verify', as: :verify_contact
get 'contact/download_poi', to: 'contacts#download_poi', as: :download_poi_contact
resources :contacts, except: %i[destroy update show edit]

get 'invoices/:id/download', to: 'invoices#download', as: :download_invoice
Expand Down
Loading

0 comments on commit 12b01d4

Please sign in to comment.