Sha256: 54357c7f5b45b3d9604383db8f7e57745a1bfaab2e64f60c38f33f062eabbc5f

Contents?: true

Size: 1.11 KB

Versions: 62

Compression:

Stored size: 1.11 KB

Contents

module Workarea
  class Fulfillment
    class Package
      def self.create(events)
        tracking_numbers = events
                            .select { |e| e.status == 'shipped' }
                            .map { |e| e.data['tracking_number'] }
                            .uniq

        tracking_numbers.map do |tracking_number|
          matching_events = events.select do |event|
            event.status == 'shipped' &&
              event.data['tracking_number'] == tracking_number
          end

          Package.new(tracking_number, matching_events)
        end
      end

      attr_reader :tracking_number, :events

      def initialize(tracking_number, events = [])
        @tracking_number = tracking_number
        @events = events
      end

      def events_by_item
        @events.group_by(&:order_item_id)
      end

      def created_at
        sorted_event_creation_dates.first
      end

      def updated_at
        sorted_event_creation_dates.last
      end

      private

      def sorted_event_creation_dates
        @sorted_event_creation_dates ||= events.map(&:created_at)
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.26 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.45 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.25 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.23 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.44 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.22 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.43 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.21 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.42 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.20 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.41 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.19 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.40 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.18 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.39 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.17 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.38 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.16 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.37 app/models/workarea/fulfillment/package.rb