Skip to content

Commit

Permalink
Merge pull request #62 from internetee/61-oneoff-request-for-non-exis…
Browse files Browse the repository at this point in the history
…ted-invoice

return an error for non existed invoice due one off generation
  • Loading branch information
vohmar authored May 11, 2023
2 parents 01422d1 + d032ef4 commit c9b0ede
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ def create

if invoice.nil?
message = "Invoice with #{params[:invoice_number]} number not found in Invoice Status Controller"
NotifierMailer.inform_admin("Invoice with #{params[:invoice_number]} number not found",
message).deliver_now
raise ActiveRecord::RecordNotFound, "Invoice with #{params[:invoice_number]} number not found"
NotifierMailer.inform_admin(title: "Invoice with #{params[:invoice_number]} number not found",
error_message: message).deliver_now
# raise ActiveRecord::RecordNotFound, "Invoice with #{params[:invoice_number]} number not found"

render json: { 'message' => message } and return
end

if invoice.update(status: params[:status])
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/dashboards/invoice_synchronizes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ def update

respond_to do |format|
format.turbo_stream do
if @invoice.synchronize(status: @invoice.status).result?
res = @invoice.synchronize(status: @invoice.status)

if res.result?
flash[:notice] = 'Invoice data was successfully updated'
else
flash[:alert] = response.errors['message']
flash[:alert] = res.errors['message']
end

render turbo_stream: [render_turbo_flash]
Expand Down
1 change: 1 addition & 0 deletions app/jobs/save_invoice_data_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def generate_invoice(attrs:)
invoice.in_directo = attrs[:in_directo]
invoice.sent_at_omniva = attrs[:e_invoice_sent_at]
invoice.transaction_time = attrs[:transaction_time]
invoice.description = attrs.fetch(:description, 'prepended')

invoice.save!
end
Expand Down
7 changes: 4 additions & 3 deletions app/services/notify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Notify

SETTLED = 'settled'.freeze
PREPENDED = 'prepended'.freeze
AUCTION = 'auction'.freeze

attr_reader :response

Expand All @@ -11,7 +12,7 @@ def initialize(response:)
end

def self.call(response:)
notifier = new(response: response)
notifier = new(response:)

parsed_response = notifier.parse_response(response)

Expand All @@ -29,7 +30,7 @@ def self.call(response:)
end
return if invoice.paid?

notifier.update_invoice_state(parsed_response: parsed_response, invoice: invoice)
notifier.update_invoice_state(parsed_response:, invoice:)
return unless invoice.paid?

url = notifier.get_update_payment_url[invoice.initiator.to_sym]
Expand Down Expand Up @@ -76,7 +77,7 @@ def update_invoice_state(parsed_response:, invoice:)
end

def invoice_numbers_from_multi_payment(invoice)
return if !invoice.auction? || invoice.description == PREPENDED
return if !invoice.initiator == AUCTION || invoice.description == PREPENDED || invoice.description == ''

numbers = invoice.description.split(' ')
results = Invoice.where(invoice_number: numbers).pluck(:invoice_number, :payment_reference)
Expand Down
10 changes: 10 additions & 0 deletions app/services/oneoff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ def self.call(invoice_number:, customer_url:, reference_number:, bulk: false, bu
end

def call
if @invoice.nil?
if invoice_number.nil?
errors = 'Internal error: called invoice withour number. Please contact to administrator'
else
errors = "Invoice with #{invoice_number} not found in internal system"
end

return wrap(result: false, instance: nil, errors: errors)
end

contract = OneoffParamsContract.new
result = contract.call(invoice_number: invoice_number,
customer_url: customer_url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
}

expect { post api_v1_invoice_generator_invoice_status_index_path, params: params }
.to change { ActionMailer::Base.deliveries.count }.by(2)
.to change { ActionMailer::Base.deliveries.count }.by(1)
end
end
end
8 changes: 8 additions & 0 deletions spec/services/notify_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'rails_helper'

RSpec.describe 'Notify' do
let(:invoice) { create(:invoice) }
let(:user) { create(:user) }
Expand Down Expand Up @@ -43,9 +45,15 @@
invoice_one.save
invoice_two.save

invoice_one.update(initiator: 'auction')
invoice_two.update(initiator: 'auction')
invoice_three.update(initiator: 'auction')

invoice_three.description = "#{invoice_one.invoice_number} #{invoice_two.invoice_number}"
invoice_three.save

invoice_one.reload && invoice_two.reload && invoice_three.reload

expect(invoice_three.description).to eq("#{invoice_one.invoice_number} #{invoice_two.invoice_number}")
expect(invoice_one.status).to eq('unpaid')
expect(invoice_two.status).to eq('unpaid')
Expand Down
4 changes: 1 addition & 3 deletions spec/services/oneoff_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@
customer_url: GlobalVariable::BASE_REGISTRY,
reference_number: nil)

expect(response.errors).to a_hash_including(
invoice_number: ['must be filled']
)
expect(response.errors).to eq 'Internal error: called invoice withour number. Please contact to administrator'
end
end
end

0 comments on commit c9b0ede

Please sign in to comment.