Class: Comee::Core::ProductLookupService

Inherits:
Object
  • Object
show all
Defined in:
app/services/comee/core/product_lookup_service.rb

Constant Summary collapse

ORGANIZATION =
"ORG".freeze

Instance Method Summary collapse

Instance Method Details

#lookup_product(code, from, to) ⇒ Object

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.

Raises:

  • (StandardError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/comee/core/product_lookup_service.rb', line 15

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