From ef03166feec13abeef7c4db15baad8def4dd2f3f Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Thu, 10 Feb 2022 14:36:35 +0200 Subject: [PATCH] added toggle feature --- .../eis_billing/base_controller.rb | 7 ++++ app/models/billing/reference_no.rb | 11 ++++-- app/models/feature.rb | 16 +++----- app/models/invoice.rb | 38 ++++++++++++++----- app/models/registrar.rb | 25 ++++++------ config/application.yml.sample | 2 +- 6 files changed, 61 insertions(+), 38 deletions(-) diff --git a/app/controllers/eis_billing/base_controller.rb b/app/controllers/eis_billing/base_controller.rb index 25e921482..93d33c85a 100644 --- a/app/controllers/eis_billing/base_controller.rb +++ b/app/controllers/eis_billing/base_controller.rb @@ -3,6 +3,7 @@ module EisBilling protect_from_forgery with: :null_session skip_authorization_check # Temporary solution # skip_before_action :verify_authenticity_token # Temporary solution + before_action :persistent before_action :authorized def encode_token(payload) @@ -47,5 +48,11 @@ module EisBilling def logger @logger ||= Rails.logger end + + def persistent + return true if Feature.billing_system_integrated? + + render json: { message: "We don't work yet!" }, status: :unauthorized + end end end diff --git a/app/models/billing/reference_no.rb b/app/models/billing/reference_no.rb index b663da36c..3ed84d7f1 100644 --- a/app/models/billing/reference_no.rb +++ b/app/models/billing/reference_no.rb @@ -4,10 +4,13 @@ module Billing MULTI_REGEXP = /(\d{2,20})/ def self.generate - # base = Base.generate - # "#{base}#{base.check_digit}" - result = EisBilling::GetReferenceNumber.send_request - JSON.parse(result.body)['reference_number'] + if Feature.billing_system_integrated? + result = EisBilling::GetReferenceNumber.send_request + JSON.parse(result.body)['reference_number'] + else + base = Base.generate + "#{base}#{base.check_digit}" + end end def self.valid?(ref) diff --git a/app/models/feature.rb b/app/models/feature.rb index 4143f108e..7a0d6d78b 100644 --- a/app/models/feature.rb +++ b/app/models/feature.rb @@ -1,13 +1,7 @@ class Feature - # def self.obj_and_extensions_statuses_enabled? - # return false if ENV['obj_and_extensions_prohibited'] == 'false' - # - # ENV['obj_and_extensions_prohibited'] || false - # end - # - # def self.enable_lock_domain_with_new_statuses? - # return false if ENV['enable_lock_domain_with_new_statuses'] == 'false' - # - # ENV['enable_lock_domain_with_new_statuses'] || false - # end + def self.billing_system_integrated? + return false if ENV['billing_system_integrated'] == 'false' + + ENV['billing_system_integrated'] || false + end end diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 449045a3e..e6cfdb7c6 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -41,21 +41,41 @@ class Invoice < ApplicationRecord attribute :vat_rate, ::Type::VatRate.new def set_invoice_number - result = EisBilling::GetInvoiceNumber.send_invoice + if Feature.billing_system_integrated? + result = EisBilling::GetInvoiceNumber.send_invoice - if JSON.parse(result.body)['code'] == '403' - errors.add(:base, I18n.t('cannot get access')) - logger.error('PROBLEM WITH TOKEN') - throw(:abort) - end + if JSON.parse(result.body)['code'] == '403' + errors.add(:base, I18n.t('cannot get access')) + logger.error('PROBLEM WITH TOKEN') + throw(:abort) + end + + if JSON.parse(result.body)['error'] == 'out of range' + errors.add(:base, I18n.t('failed_to_generate_invoice_invoice_number_limit_reached')) + logger.error('INVOICE NUMBER LIMIT REACHED, COULD NOT GENERATE INVOICE') + throw(:abort) + end + + self.number = JSON.parse(result.body)['invoice_number'].to_i + else + last_no = Invoice.all + .where(number: Setting.invoice_number_min.to_i...Setting.invoice_number_max.to_i) + .order(number: :desc) + .limit(1) + .pick(:number) + + if last_no && last_no >= Setting.invoice_number_min.to_i + self.number = last_no + 1 + else + self.number = Setting.invoice_number_min.to_i + end + + return if number <= Setting.invoice_number_max.to_i - if JSON.parse(result.body)['error'] == 'out of range' errors.add(:base, I18n.t('failed_to_generate_invoice_invoice_number_limit_reached')) logger.error('INVOICE NUMBER LIMIT REACHED, COULD NOT GENERATE INVOICE') throw(:abort) end - - self.number = JSON.parse(result.body)['invoice_number'].to_i end def to_s diff --git a/app/models/registrar.rb b/app/models/registrar.rb index d7fc255d5..f84f01d1a 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -107,21 +107,20 @@ class Registrar < ApplicationRecord .deliver_later(wait: 1.minute) end - unless Rails.env.staging? - SendEInvoiceJob.set(wait: 1.minute).perform_now(invoice.id, payable: payable) + if Feature.billing_system_integrated? + add_invoice_instance = EisBilling::AddDeposits.new(invoice) + result = add_invoice_instance.send_invoice + + link = JSON.parse(result.body)['everypay_link'] + + invoice.update(payment_link: link) end - add_invoice_instance = EisBilling::AddDeposits.new(invoice) - result = add_invoice_instance.send_invoice - - link = JSON.parse(result.body)['everypay_link'] - - invoice.update(payment_link: link) - - Rails.logger.info "Invoice created from transaction ------->" - Rails.logger.info invoice - Rails.logger.info result.body - Rails.logger.info "-----------------------------------------" + if Feature.billing_system_integrated? + SendEInvoiceTwoJob.set(wait: 1.minute).perform_now(invoice.id, payable: payable) + else + SendEInvoiceJob.set(wait: 1.minute).perform_now(invoice.id, payable: payable) + end invoice end diff --git a/config/application.yml.sample b/config/application.yml.sample index b565f5acc..e8c87d566 100644 --- a/config/application.yml.sample +++ b/config/application.yml.sample @@ -237,4 +237,4 @@ nameserver_validation_timeout: '1' eis_billing_system_base_url_dev: 'http://eis_billing_system:3000' eis_billing_system_base_url_staging: 'https://st-billing.infra.tld.ee' -eis_token: +billing_system_integrated: 'true'