Sha256: e7f001c96deb283fb5a967dcb1506986f03e09e69b4160c6f301377fb106dc68

Contents?: true

Size: 1.34 KB

Versions: 7

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module DefraRubyMocks
  class WorldpayResourceService < BaseService

    def run(reference:)
      @reference = reference

      raise(MissingResourceError, @reference) if resource.nil?

      WorldpayResource.new(resource, order, company_name)
    end

    private

    WorldpayResource = Struct.new(:resource, :order, :company_name)

    def resource
      @_resource ||= locate_transient_registration || locate_completed_registration
    end

    def locate_transient_registration
      "WasteCarriersEngine::TransientRegistration"
        .constantize
        .where(token: @reference)
        .first
    end

    def locate_completed_registration
      "WasteCarriersEngine::Registration"
        .constantize
        .where(reg_uuid: @reference)
        .first
    end

    def locate_original_registration(reg_identifier)
      "WasteCarriersEngine::Registration"
        .constantize
        .where(reg_identifier: reg_identifier)
        .first
    end

    def order
      @_order ||= resource.finance_details&.orders&.order_by(dateCreated: :desc)&.first
    end

    def company_name
      if resource.class.to_s == "WasteCarriersEngine::OrderCopyCardsRegistration"
        locate_original_registration(resource.reg_identifier).company_name.downcase
      else
        resource.company_name.downcase
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
defra_ruby_mocks-2.3.0 app/services/defra_ruby_mocks/worldpay_resource_service.rb
defra_ruby_mocks-2.2.0 app/services/defra_ruby_mocks/worldpay_resource_service.rb
defra_ruby_mocks-2.1.0 app/services/defra_ruby_mocks/worldpay_resource_service.rb
defra_ruby_mocks-2.0.0 app/services/defra_ruby_mocks/worldpay_resource_service.rb
defra_ruby_mocks-1.5.0 app/services/defra_ruby_mocks/worldpay_resource_service.rb
defra_ruby_mocks-1.4.1 app/services/defra_ruby_mocks/worldpay_resource_service.rb
defra_ruby_mocks-1.4.0 app/services/defra_ruby_mocks/worldpay_resource_service.rb