refactoring

This commit is contained in:
olegphenomenon 2023-01-11 12:46:03 +02:00
parent 0a4444b556
commit 7b05e1e28a
2 changed files with 31 additions and 25 deletions

View file

@ -1,6 +1,10 @@
module EisBilling
class InvoicesController < BaseController
TYPE = 'PaymentOrders::EveryPay'.freeze
PAID = 'paid'.freeze
CANCELLED = 'cancelled'.freeze
ISSUED = 'issued'.freeze
FAILED = 'failed'.freeze
before_action :load_invoice, only: :update
@ -9,11 +13,11 @@ module EisBilling
payment_orders_handler
render json: {
message: 'Invoice data was successfully updated'
message: 'Invoice data was successfully updated',
}, status: :ok
else
render json: {
error: @message.errors.full_messages
error: @message.errors.full_messages,
}, status: :unprocessable_entity
end
end
@ -22,49 +26,51 @@ module EisBilling
def payment_orders_handler
if @invoice.payment_orders.present?
return if (@invoice.paid? && status == 'paid') || (@invoice.cancelled? && status == 'cancelled')
return if (@invoice.paid? && status.paid?) || (@invoice.cancelled? && status.cancelled?)
if status == 'cancelled' || status == 'failed'
if status.cancelled? || status.failed?
@invoice.cancel_manualy
elsif status == 'paid'
elsif status.paid?
@invoice.autobind_manually
end
else
return unless status == 'paid'
return unless status.paid?
@invoice.autobind_manually
end
end
def status
case params[:invoice][:status]
when 'paid'
'paid'
when 'cancelled'
'cancelled'
when 'failed'
'failed'
else
'issued'
end
status = case params[:invoice][:status]
when 'paid'
'paid'
when 'cancelled'
'cancelled'
when 'failed'
'failed'
else
'issued'
end
Struct.new(:paid?, :cancelled?, :issued?, :failed?)
.new(status == PAID, status == CANCELLED, status == ISSUED, status == FAILED)
end
def load_invoice
@invoice = Invoice.find_by(number: params[:invoice][:invoice_number])
return if @invoice.present?
if @invoice.nil?
render json: {
error: {
message: "Invoice with #{params[:invoice][:invoice_number]} number not found"
}
}, status: :not_found and return
end
render json: {
error: {
message: "Invoice with #{params[:invoice][:invoice_number]} number not found",
}
}, status: :not_found and return
end
def modified_params
{
in_directo: params[:invoice][:in_directo],
e_invoice_sent_at: params[:invoice][:sent_at_omniva]
e_invoice_sent_at: params[:invoice][:sent_at_omniva],
}
end
end