Skip to content

Commit

Permalink
updated request for get reference user data
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Oct 31, 2023
1 parent c7c8ae9 commit 6c7abf1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/registrars_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/models/billing/reference_no.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 17 additions & 9 deletions app/services/eis_billing/get_reference_number.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/models/billing/reference_no/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion test/models/billing/reference_no_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

0 comments on commit 6c7abf1

Please sign in to comment.