Sha256: 5a5d1bbdf425c58eeae5875f95459c59b75a1d55e7872d0de92f77d813ad9724

Contents?: true

Size: 1.57 KB

Versions: 11

Compression:

Stored size: 1.57 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:}, **to)
          return query.code
        end

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

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
comee_core-0.1.23 app/services/comee/core/product_lookup_service.rb
comee_core-0.1.22 app/services/comee/core/product_lookup_service.rb
comee_core-0.1.21 app/services/comee/core/product_lookup_service.rb
comee_core-0.1.20 app/services/comee/core/product_lookup_service.rb
comee_core-0.1.19 app/services/comee/core/product_lookup_service.rb
comee_core-0.1.18 app/services/comee/core/product_lookup_service.rb
comee_core-0.1.17 app/services/comee/core/product_lookup_service.rb
comee_core-0.1.16 app/services/comee/core/product_lookup_service.rb
comee_core-0.1.15 app/services/comee/core/product_lookup_service.rb
comee_core-0.1.14 app/services/comee/core/product_lookup_service.rb
comee_core-0.1.13 app/services/comee/core/product_lookup_service.rb