From 5ce69e6e195b35ef080127ae9120634d12d3ab94 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Mon, 27 Nov 2023 13:55:51 +0200 Subject: [PATCH] added new status - overdue --- .../dashboards/invoice_status_controller.rb | 5 ++ app/controllers/everypay_controller.rb | 8 ++- app/models/invoice.rb | 49 +++++++++---------- app/models/invoice/synchronization.rb | 12 ++++- 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/app/controllers/dashboards/invoice_status_controller.rb b/app/controllers/dashboards/invoice_status_controller.rb index 7975a05..55b9cd1 100644 --- a/app/controllers/dashboards/invoice_status_controller.rb +++ b/app/controllers/dashboards/invoice_status_controller.rb @@ -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]) diff --git a/app/controllers/everypay_controller.rb b/app/controllers/everypay_controller.rb index 8578be0..4ca999d 100644 --- a/app/controllers/everypay_controller.rb +++ b/app/controllers/everypay_controller.rb @@ -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: "

Everypay response result

#{response}
".html_safe), + html: html_response(response).html_safe) ] end end end + def html_response(response) + "

Everypay response result

+ #{response}
" + end end diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 292055c..fbecb74 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -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) @@ -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 diff --git a/app/models/invoice/synchronization.rb b/app/models/invoice/synchronization.rb index 42b326f..b71618f 100644 --- a/app/models/invoice/synchronization.rb +++ b/app/models/invoice/synchronization.rb @@ -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