From f6580bd79a6e4bf8d1d7579a84f84c8420b70ad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Fri, 9 Oct 2020 13:10:28 +0300 Subject: [PATCH] REPP: custom success response handler --- app/controllers/repp/v1/base_controller.rb | 7 ++ .../repp/v1/contacts_controller.rb | 85 +++++++------------ 2 files changed, 38 insertions(+), 54 deletions(-) diff --git a/app/controllers/repp/v1/base_controller.rb b/app/controllers/repp/v1/base_controller.rb index f1000849f..81f812761 100644 --- a/app/controllers/repp/v1/base_controller.rb +++ b/app/controllers/repp/v1/base_controller.rb @@ -13,6 +13,13 @@ module Repp private + def render_success(code: nil, message: nil, data: nil) + resp = { code: code || 1000, message: message || 'Command completed successfully', + data: data || {} } + + render(json: resp, status: :ok) + end + def epp_errors @errors ||= [] end diff --git a/app/controllers/repp/v1/contacts_controller.rb b/app/controllers/repp/v1/contacts_controller.rb index 70f5be1b5..66ab3712f 100644 --- a/app/controllers/repp/v1/contacts_controller.rb +++ b/app/controllers/repp/v1/contacts_controller.rb @@ -22,76 +22,49 @@ module Repp ## GET /repp/v1/contacts/1 def show - render(json: @contact.as_json, status: :ok) + render_success(data: @contact.as_json) end ## GET /repp/v1/contacts/check/1 def check contact = Epp::Contact.find_by(code: params[:id]) + data = { contact: { id: params[:id], available: contact.nil? } } - render json: { - code: 1000, message: I18n.t('epp.contacts.completed'), - data: { contact: { - id: params[:id], - available: contact.nil? - } } - }, status: :ok + render_success(data: data) end ## POST /repp/v1/contacts def create @legal_doc = params[:legal_documents] - @contact_params = contact_params_with_address - @ident = contact_ident_params - address_present = contact_addr_params.keys.any? - @contact = Epp::Contact.new(@contact_params, current_user.registrar, epp: false) + @contact = Epp::Contact.new(contact_params_with_address, current_user.registrar, epp: false) + action = Actions::ContactCreate.new(@contact, @legal_doc, contact_ident_params) + handle_errors(@contact) and return unless action.call - action = Actions::ContactCreate.new(@contact, @legal_doc, @ident) - - if action.call - if !Contact.address_processing? && address_present - @response_code = 1100 - @response_description = I18n.t('epp.contacts.completed_without_address') - else - @response_code = 1000 - @response_description = I18n.t('epp.contacts.completed') - end - - render(json: { code: @response_code, - message: @response_description, - data: { contact: { id: @contact.code } } }, - status: :created) - else - handle_errors(@contact) - end + render_success(code: opt_addr? ? 1100 : nil, data: { contact: { id: @contact.code } }, + message: opt_addr? ? I18n.t('epp.contacts.completed_without_address') : nil) end ## PUT /repp/v1/contacts/1 def update - @update = contact_params_with_address + action = Actions::ContactUpdate.new(@contact, contact_params_with_address, + params[:legal_document], + contact_ident_params(required: false), current_user) - @legal_doc = params[:legal_document] - @ident = contact_ident_params || {} - address_present = contact_addr_params.keys.any? - action = Actions::ContactUpdate.new(@contact, @update, @legal_doc, @ident, current_user) + handle_errors(@contact) and return unless action.call - if action.call - if !Contact.address_processing? && address_present - @response_code = 1100 - @response_description = I18n.t('epp.contacts.completed_without_address') - else - @response_code = 1000 - @response_description = I18n.t('epp.contacts.completed') - end + render_success(code: opt_addr? ? 1100 : nil, data: { contact: { id: @contact.code } }, + message: opt_addr? ? I18n.t('epp.contacts.completed_without_address') : nil) + end - render(json: { code: @response_code, - message: @response_description, - data: { contact: { id: @contact.code } } }, - status: :ok) - else - handle_errors(@contact) - end + def contact_addr_present? + return false unless contact_addr_params.key?(:addr) + + contact_addr_params[:addr].keys.any? + end + + def opt_addr? + !Contact.address_processing? && contact_addr_present? end def find_contact @@ -100,9 +73,9 @@ module Repp end def contact_params_with_address - addr = {} return contact_create_params unless contact_addr_params.key?(:addr) + addr = {} contact_addr_params[:addr].each_key { |k| addr[k] = contact_addr_params[:addr][k] } contact_create_params.merge(addr) end @@ -112,9 +85,13 @@ module Repp params.require(:contact).permit(:name, :email, :phone) end - def contact_ident_params - params.require(:contact).require(:ident).require(%i[ident ident_type ident_country_code]) - params.require(:contact).require(:ident).permit(:ident, :ident_type, :ident_country_code) + def contact_ident_params(required: true) + if required + params.require(:contact).require(:ident).require(%i[ident ident_type ident_country_code]) + params.require(:contact).require(:ident).permit(:ident, :ident_type, :ident_country_code) + else + params.permit(ident: %i[ident ident_type ident_country_code]) + end end def contact_addr_params