Class: Edi::Wayfair::PackingSlipRetriever

Inherits:
BaseEdiService show all
Defined in:
app/services/edi/wayfair/packing_slip_retriever.rb

Overview

Retrieves Wayfair packing-slip PDFs and attaches them to orders as the
custom packing slip, releasing the order from CR hold once attached.

Constant Summary

Constants included from RequestIdentifiable

RequestIdentifiable::REQUEST_ID_HEADERS

Constants included from AddressAbbreviator

AddressAbbreviator::MAX_LENGTH

Instance Attribute Summary

Attributes inherited from BaseEdiService

#orchestrator

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseEdiService

#amazon_feed_product_type, #duplicate_po_already_notified?, #initialize, #mark_duplicate_po_as_notified, #onboard_ordered_catalog_items, #report_order_creation_issues, #safe_process_edi_communication_log

Methods included from RequestIdentifiable

#partner_request_id

Methods included from AddressAbbreviator

#abbreviate_street, #collect_street_originals, #record_address_abbreviation_notes

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

This class inherits a constructor from Edi::BaseEdiService

Instance Method Details

#instantiate_transporter(transporter, transporter_profile = nil) ⇒ Edi::Wayfair::Transport::HttpGraphqlApiConnection

Builds the transport connection for the given transporter symbol.

Parameters:

  • transporter (Symbol)

    transporter type (only :http_graphql_api is supported)

  • transporter_profile (Symbol, nil) (defaults to: nil)

    connection profile (e.g. :wayfair_api)

Returns:

  • (Edi::Wayfair::Transport::HttpGraphqlApiConnection)

Raises:

  • (RuntimeError)

    when the transporter symbol is unknown



43
44
45
46
47
48
49
50
# File 'app/services/edi/wayfair/packing_slip_retriever.rb', line 43

def instantiate_transporter(transporter, transporter_profile = nil)
  case transporter
  when :http_graphql_api
    Transport::HttpGraphqlApiConnection.new({ profile: transporter_profile })
  else
    raise "Unknown transporter: #{transporter}"
  end
end

#process(orders, force: false) ⇒ void

This method returns an undefined value.

Retrieve a packing list for each order in the given BatchProcessResult

Parameters:

  • orders (Array<Order>)

    orders to fetch packing slips for

  • force (Boolean) (defaults to: false)

    re-fetch even when the order already has a custom packing slip

Raises:

  • (RuntimeError)

    when a packing slip fails to upload for an order



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/edi/wayfair/packing_slip_retriever.rb', line 18

def process(orders, force: false)
  transport = instantiate_transporter(orchestrator.transporter, orchestrator.transporter_profile)
  orders.each do |order|
    if order.has_custom_packing_slip? && !force
      logger.info "Order #{order.id} already has a custom packing slip"
      next
    end
    packing_slip_pdf_url = Rails.env.production? ? "#{orchestrator.packing_slip_remote_path}/#{order.po_number}" : 'https://www.warmlyyours.com/images/pdf/saia_sample_bol.pdf'
    upload = transport.fetch_url_to_upload(packing_slip_pdf_url, 'packing_list.pdf', 'custom_packing_slip_pdf', order)
    if upload&.valid? && upload.persisted?
      logger.info "packing list uploaded to order id: #{order.id}, reference_number: #{order.reference_number}, upload id #{upload.id}, attachment_uid: #{upload.attachment_uid}"
      order.release_order_or_hold if order.in_cr_hold?
    else
      logger.info "packing list could not be uploaded to order id: #{order.id}, reference_number: #{order.reference_number}, upload.errors #{upload.errors.full_messages}"
      raise "packing list could not be uploaded to order id: #{order.id}, reference_number: #{order.reference_number}, upload.errors #{upload.errors.full_messages}"
    end
  end
end