Class: Comee::Core::OrderSource

Inherits:
ApplicationRecord show all
Defined in:
app/models/comee/core/order_source.rb

Instance Method Summary collapse

Instance Method Details

#validate_field_mappingObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/comee/core/order_source.rb', line 8

def validate_field_mapping
  return unless field_mapping

  mappings = field_mapping.map(&:with_indifferent_access)
  order_fields = %w[order_number order_date delivery_address invoice_address]
  item_fields = %w[customer_item_no customer_item_description unit_code quantity price delivery_date]
  order_dest = mappings.select { |mapping| mapping[:level] == "order" }.map { |m| m[:destination] }
  item_dest = mappings.select { |mapping| mapping[:level] == "item" }.map { |m| m[:destination] }
  order_diff = order_fields - order_dest
  item_diff = item_fields - item_dest
  errors.add(:field_mapping, "has fields #{order_diff} missing at order level.") if order_diff.count.positive?

  errors.add(:field_mapping, "has fields #{item_diff} missing at item level.") if item_diff.count.positive?
end