From 6c7abf11238d58e8070507bb9948cd6f01698be9 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Tue, 31 Oct 2023 12:42:40 +0200 Subject: [PATCH] updated request for get reference user data --- .../admin/registrars_controller.rb | 2 +- app/models/billing/reference_no.rb | 4 +-- .../eis_billing/get_reference_number.rb | 26 ++++++++++++------- test/models/billing/reference_no/base_test.rb | 4 +++ test/models/billing/reference_no_test.rb | 6 ++++- 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb index 7479d53aca..3129f3585f 100644 --- a/app/controllers/admin/registrars_controller.rb +++ b/app/controllers/admin/registrars_controller.rb @@ -23,7 +23,7 @@ def new def create @registrar = Registrar.new(registrar_params) - @registrar.reference_no = ::Billing::ReferenceNo.generate + @registrar.reference_no = ::Billing::ReferenceNo.generate(owner: @registrar.name) if @registrar.valid? @registrar.transaction do diff --git a/app/models/billing/reference_no.rb b/app/models/billing/reference_no.rb index 050db73d40..80122bd5c8 100644 --- a/app/models/billing/reference_no.rb +++ b/app/models/billing/reference_no.rb @@ -3,8 +3,8 @@ class ReferenceNo REGEXP = /\A\d{2,20}\z/ MULTI_REGEXP = /(\d{2,20})/ - def self.generate - result = EisBilling::GetReferenceNumber.send_request + def self.generate(owner:) + result = EisBilling::GetReferenceNumber.call(owner: owner) JSON.parse(result.body)['reference_number'] end diff --git a/app/services/eis_billing/get_reference_number.rb b/app/services/eis_billing/get_reference_number.rb index e200a8dfff..f3024f08a1 100644 --- a/app/services/eis_billing/get_reference_number.rb +++ b/app/services/eis_billing/get_reference_number.rb @@ -1,21 +1,29 @@ module EisBilling class GetReferenceNumber < EisBilling::Base - def self.send_request - send_it + attr_reader :owner + + # rubocop:disable Lint/MissingSuper + def initialize(owner:) + @owner = owner end - def self.obj_data - { - initiator: INITIATOR, - } + def self.call(owner:) + new(owner: owner).call end - def self.send_it + def call http = EisBilling::Base.base_request(url: reference_number_generator_url) - http.post(reference_number_generator_url, obj_data.to_json, EisBilling::Base.headers) + http.post(reference_number_generator_url, payload.to_json, EisBilling::Base.headers) + end + + def payload + { + initiator: INITIATOR, + owner: owner + } end - def self.reference_number_generator_url + def reference_number_generator_url "#{EisBilling::Base::BASE_URL}/api/v1/invoice_generator/reference_number_generator" end end diff --git a/test/models/billing/reference_no/base_test.rb b/test/models/billing/reference_no/base_test.rb index bc14ba2c8c..4f2e2672a2 100644 --- a/test/models/billing/reference_no/base_test.rb +++ b/test/models/billing/reference_no/base_test.rb @@ -2,6 +2,10 @@ # https://www.pangaliit.ee/settlements-and-standards/reference-number-of-the-invoice class ReferenceNoBaseTest < ActiveSupport::TestCase + setup do + @registrar = registrars(:bestnames) + end + def test_generates_random_base assert_not_equal Billing::ReferenceNo::Base.generate, Billing::ReferenceNo::Base.generate end diff --git a/test/models/billing/reference_no_test.rb b/test/models/billing/reference_no_test.rb index 5f22ff780a..06cfc4a79a 100644 --- a/test/models/billing/reference_no_test.rb +++ b/test/models/billing/reference_no_test.rb @@ -1,6 +1,10 @@ require 'test_helper' class ReferenceNoTest < ActiveSupport::TestCase + setup do + @registrar = registrars(:bestnames) + end + def test_returns_format_regexp format = /\A\d{2,20}\z/ assert_equal format, Billing::ReferenceNo::REGEXP @@ -10,7 +14,7 @@ def test_generated_reference_number_conforms_to_format stub_request(:post, "https://eis_billing_system:3000/api/v1/invoice_generator/reference_number_generator") .to_return(status: 200, body: "{\"reference_number\":\"12332\"}", headers: {}) - reference_no = Billing::ReferenceNo.generate + reference_no = Billing::ReferenceNo.generate(owner: @registrar.name) assert_match Billing::ReferenceNo::REGEXP, reference_no end end