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.4.27 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.4 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.26 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.3 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.25 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.2 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.24 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.1 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.23 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.22 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.0 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.21 app/models/workarea/fulfillment/package.rb
workarea-core-3.5.0.beta.1 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.20 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.19 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.18 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.17 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.16 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.15 app/models/workarea/fulfillment/package.rb
workarea-core-3.4.14 app/models/workarea/fulfillment/package.rb