Sha256: 9a840ab376aa95663cb5dcb64664dc460dd45e9bec4a21643e6af8822de706b1

Contents?: true

Size: 1.85 KB

Versions: 46

Compression:

Stored size: 1.85 KB

Contents

module ForestLiana
  class IntercomConversationsGetter
    def initialize(params)
      @params = params
      @app_id = ForestLiana.integrations[:intercom][:app_id]
      @api_key = ForestLiana.integrations[:intercom][:api_key]

      @intercom = ::Intercom::Client.new(app_id: @app_id, api_key: @api_key)
    end

    def count
      @records.count
    end

    def records
      @records[pagination].map do |conversation|
        if conversation.assignee.is_a?(::Intercom::Admin)
          admins = @intercom.admins.all.detect(id: conversation.assignee.id)
          conversation.assignee = admins.first
        end

        conversation.link = link(conversation)

        conversation
      end
    end

    def perform
      begin
        resource = user_collection.find(@params[:id])
        @records = @intercom.conversations.find_all(
          email: resource.email,
          type: 'user',
          display_as: 'plaintext',
        ).entries
      rescue Intercom::ResourceNotFound
        @records = []
      end
    end

    private

    def user_collection
      ForestLiana.integrations
        .try(:[], :intercom)
        .try(:[], :user_collection)
        .try(:constantize)
    end

    def user_collection?
      user_collection.present?
    end

    def link(conversation)
      "#{@intercom.base_url}/a/apps/#{@app_id}/inbox/all/conversations/#{conversation.id}"
    end

    def pagination
      offset..(offset + limit - 1)
    end

    def offset
      return 0 unless pagination?

      number = @params[:page][:number]
      if number && number.to_i > 0
        (number.to_i - 1) * limit
      else
        0
      end
    end

    def limit
      return 10 unless pagination?

      if @params[:page][:size]
        @params[:page][:size].to_i
      else
        10
      end
    end

    def pagination?
      @params[:page] && @params[:page][:number]
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
forest_liana-1.3.28 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.27 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.26 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.25 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.24 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.23 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.22 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.21 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.20 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.19 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.18 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.17 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.16 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.15 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.14 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.13 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.12 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.11 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.10 app/services/forest_liana/intercom_conversations_getter.rb
forest_liana-1.3.9 app/services/forest_liana/intercom_conversations_getter.rb