lib/wolf_core/application/application_service.rb



module WolfCore
  class ApplicationService
    def call
      process
    rescue => e
      if e.instance_of?(WolfCore::ServiceException)
        return Result.failure(
          error: e.error.to_h.merge({ backtrace: e.backtrace })
        )
      end

      # Honeybadger.notify(e)
      return Result.failure(error: { message: e.message, backtrace: e.backtrace })
    end

    def process
      raise NotImplementedError
    end

    def raise_failed_result(result)
      return if result.success?

      raise_service_error(result.error.to_h)
    end

    def validate_presence(object, message)
      return if object.present?

      raise_service_error({ message: message })
    end

    def raise_service_error(error_data)
      raise WolfCore::ServiceException, error_data
    end

    def remove_non_permitted_parameters(params, allowed_params)
      permitted = params.slice(*allowed_params)
      if permitted.blank?
        raise_service_error({ message: "There are not permitted parameters"})
      end
      return permitted
    end

    def http_get(url:, headers: {}, query: nil)
    WolfCore::HttpDataSource.http_get(url: url, headers: headers, query: query)
    end

    def http_post(url:, headers: {}, body: nil, query: nil)
    WolfCore::HttpDataSource.http_post(url: url, headers: headers, query: query, body: body)
    end

    def barton_integration_http_post(path:, body:, error_message:)
      domain_url = ENV['CURRENT_SAM_URL']
      response = http_post(url: "#{domain_url}/#{path}", body: body)
      validate_http_response(
        response: response, message: error_message, error_data: { url: path }
      )
      response
    end

    def validate_http_response(response:, message:, error_data: nil)
      unless response.code == 200
        error_data = {
          message: message,
          response: parse_http_response(response)
        }.merge(error_data || {})
        raise_service_error(error_data)
      end
    end

    def parse_http_response(response)
      body = JSON.parse(response.body) rescue response.body
      {
        code: response.code,
        body: body,
        message: response.message,
      }
    end

    def get_salesforce_access_token
      result = WolfCore::SalesforceOauthService.new.call
      raise_failed_result(result)
      result.data.access_token
    end

    def get_salesforce_foreign_object(record_id:)
      data = saleforce_http_get(
        query: { calltype: 'getRecord', RecordId: record_id }
      )
      foreign_object = data.first
      puts "foreign object is"
      pp foreign_object
      foreign_object
    end

    def saleforce_http_get(query: nil)
      response = http_get(
        url: ENV['SALESFORCE_URL'],
        headers: { 'Authorization' => "Bearer #{@salesforce_access_token}" },
        query: query
      )
      validate_salesforce_response(response)
      JSON.parse(response.parsed_response)
    end

    def validate_salesforce_response(response)
      return if response.code == 200
      message = JSON.parse(response.body).first['message']
      raise_service_error({ message: message })
    end

    def get_wolf_token
      response = http_post(
        query: { tenant: ENV['TENANT'] },
        url: "#{ENV['WOLF_PLATFORM_URL']}/api/v1/sign_in",
        body: {
          email: ENV['WOLF_ADMIN_EMAIL'],
          password: ENV['WOLF_ADMIN_PASSWORD']
        }
      )
      validate_http_response(
        response: response, message: 'Failed to get wolf token'
      )
      response_body = JSON.parse(response.body)
      wolf_token = response_body.dig('user', 'authentication_token')
      puts "wolf token is #{wolf_token}"
      wolf_token
    end
  end
end