Skip to content

Commit

Permalink
added new status - overdue
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Nov 27, 2023
1 parent f822d1b commit 5ce69e6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
5 changes: 5 additions & 0 deletions app/controllers/dashboards/invoice_status_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ def update
temporary_unavailable and return unless @invoice.allow_to_synchronize?

resp = @invoice.synchronize(status: params[:status])

puts '----'
puts resp
puts '---- '

if resp.result?
@invoice.update(status: params[:status])

Expand Down
8 changes: 6 additions & 2 deletions app/controllers/everypay_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ def index; end

def everypay_data
response = EverypayResponse.call(params[:payment_reference])
Notify.call(response: response)
Notify.call(response:)

respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace('response_everypay',
html: "<h2 class='mt-3'>Everypay response result</h2><div class='bg-gray-300 p-4 mt-1 mb-3'><code>#{response}</code></div>".html_safe),
html: html_response(response).html_safe)
]
end
end
end

def html_response(response)
"<h2 class='mt-3'>Everypay response result</h2><div class='bg-gray-300 p-4 mt-1 mb-3'>
<code>#{response}</code></div>"
end
end
49 changes: 24 additions & 25 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,32 @@ class Invoice < ApplicationRecord
EEID = 'eeid'.freeze

pg_search_scope :search_by_number, against: [:invoice_number],
using: {
tsearch: {
prefix: true
}
}
using: {
tsearch: {
prefix: true
}
}

enum affiliation: %i[regular auction_deposit]
enum status: %i[unpaid paid cancelled failed refunded]
enum affiliation: { regular: 0, auction_deposit: 1 }
enum status: { unpaid: 0, paid: 1, cancelled: 2, failed: 3, refunded: 4, overdue: 5 }

scope :with_status, ->(status) {
where(status: status) if status.present?
scope :with_status, lambda { |status|
where(status:) if status.present?
}

scope :with_number, ->(invoice_number) {
scope :with_number, lambda { |invoice_number|
search_by_number(invoice_number) if invoice_number.present?
}

scope :with_amount_between, ->(low, high) {
scope :with_amount_between, lambda { |low, high|
where(transaction_amount: low.to_f..high.to_f) if low.present? && high.present?
}

def self.search(params={})
sort_column = params[:sort].presence_in(%w{ invoice_number status affiliation }) || "id"
sort_direction = params[:direction].presence_in(%w{ asc desc }) || "desc"
def self.search(params = {})
sort_column = params[:sort].presence_in(%w[invoice_number status affiliation]) || 'id'
sort_direction = params[:direction].presence_in(%w[asc desc]) || 'desc'

self
.with_number(params[:invoice_number].to_s.downcase)
with_number(params[:invoice_number].to_s.downcase)
.with_status(params[:status])
.with_amount_between(params[:min_amount], params[:max_amount])
.order(sort_column => sort_direction)
Expand All @@ -59,15 +58,15 @@ def auction?

def to_h
{
invoice_number: invoice_number,
initiator: initiator,
payment_reference: payment_reference,
transaction_amount: transaction_amount,
status: status,
in_directo: in_directo,
everypay_response: everypay_response,
transaction_time: transaction_time,
sent_at_omniva: sent_at_omniva
invoice_number:,
initiator:,
payment_reference:,
transaction_amount:,
status:,
in_directo:,
everypay_response:,
transaction_time:,
sent_at_omniva:
}
end
end
12 changes: 11 additions & 1 deletion app/models/invoice/synchronization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ module Invoice::Synchronization
extend ActiveSupport::Concern

def synchronize(status:)
InvoiceDataSenderService.call(invoice: self, status: status)
return error_structure if restricted_statuses.include?(status) || !allow_to_synchronize?

InvoiceDataSenderService.call(invoice: self, status:)
end

def allow_to_synchronize?
initiator == 'registry' || initiator == 'auction'
end

def restricted_statuses
['overdue']
end

def error_structure
Struct.new(:result?, :instance, :errors).new(false, nil, 'You can not change to this status!')
end
end

0 comments on commit 5ce69e6

Please sign in to comment.