Sha256: 3ef724a9caefa77a428cdb27f42e9e04a720c2ce8b2ae7facbbba8aae10c069b

Contents?: true

Size: 1.55 KB

Versions: 191

Compression:

Stored size: 1.55 KB

Contents

module Comee
  module Core
    class ProductLookupService
      ORGANIZATION = "ORG".freeze
      ##
      # This method returns a translated code for a given +code+. The +code+ can be
      # for a supplier or a client. The source of the +code+ is described using the +from+
      # parameter. The +from+ parameter is a hash which specifies the id of the source
      # supplier/client as `itemable_id`, and the data type of the source as itemable_type.
      # We specify the id and type because the ProductLookup model has a polymorphic relationship
      # with supplier and client.
      #
      # The +to+ parameter uses a similar format to specify the target supplier/client we want to
      # conduct the lookup for.
      def lookup_product(code, from, to)
        error = "The 'from' parameter cannot be assigned any string other than '#{ORGANIZATION}'."
        raise(StandardError, error) if from.instance_of?(String) && from != ORGANIZATION

        error = "The 'to' parameter cannot be assigned any string other than '#{ORGANIZATION}'."
        raise(StandardError, error) if to.instance_of?(String) && to != ORGANIZATION

        if from == ORGANIZATION
          query = ProductLookup.joins(:product).find_by(product: {code: code}, **to)
          return query
        end

        if to == ORGANIZATION
          product = ProductLookup.find_by(code: code, **from).product
          return product.code
        end

        product = ProductLookup.find_by(code: code, **from).product
        ProductLookup.find_by(product: product, **to)
      end
    end
  end
end

Version data entries

191 entries across 191 versions & 1 rubygems

Version Path
comee_core-0.2.56 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.55 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.54 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.53 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.52 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.51 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.50 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.49 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.48 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.47 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.46 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.45 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.44 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.43 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.42 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.41 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.40 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.39 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.38 app/services/comee/core/product_lookup_service.rb
comee_core-0.2.37 app/services/comee/core/product_lookup_service.rb