Sha256: 01a9e8c55c654744ad78b66acc7e505a395cd6e48d984f3689744c856a7e8382

Contents?: true

Size: 1.01 KB

Versions: 5

Compression:

Stored size: 1.01 KB

Contents

require 'interactor'

module DiscoApp
  module Flow
    class UpdateTriggerUsage

      include Interactor

      delegate :shop, :flow_trigger_definition_id, :has_enabled_flow, :timestamp, to: :context
      delegate :trigger_usage, to: :context

      def call
        find_or_create_trigger_usage
        update_trigger_usage
      end

      private

        def find_or_create_trigger_usage
          context.trigger_usage = shop.flow_trigger_usages.create_or_find_by!(
            flow_trigger_definition_id: flow_trigger_definition_id
          )
        rescue ActiveRecord::RecordNotUnique, PG::UniqueViolation
          context.fail!
        end

        def update_trigger_usage
          return if existing_timestamp_is_newer?

          trigger_usage.update(
            has_enabled_flow: has_enabled_flow,
            timestamp: timestamp
          )
        end

        def existing_timestamp_is_newer?
          trigger_usage.timestamp.present? && timestamp < trigger_usage.timestamp
        end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
disco_app-0.18.2 app/services/disco_app/flow/update_trigger_usage.rb
disco_app-0.18.3 app/services/disco_app/flow/update_trigger_usage.rb
disco_app-0.18.6 app/services/disco_app/flow/update_trigger_usage.rb
disco_app-0.18.4 app/services/disco_app/flow/update_trigger_usage.rb
disco_app-0.18.1 app/services/disco_app/flow/update_trigger_usage.rb