# frozen_string_literal: true require 'active_support/all' require 'graphlient' module Billfixers module Partner class Client def initialize(api_key:, test_mode: false) endpoint = test_mode ? 'https://billfixers-partner-sandbox.herokuapp.com/partner/graphql' : 'https://billfixers.herokuapp.com/partner/graphql' @gql = Graphlient::Client.new(endpoint, schema_path: File.join(File.dirname(__FILE__), '../../../partner_schema.json'), headers: { "X-Partner-ApiKey": api_key }) end def list_providers response = @gql.query(LIST_PROVIDERS_QUERY) response.data.list_providers end def list_customers(limit: 25, offset: 0) response = @gql.query(LIST_CUSTOMERS_QUERY, { limit: limit, offset: offset }) result = response.data.list_customers [result.total_count, result.nodes] end def list_bills(limit: 25, offset: 0, customer_id: nil) response = @gql.query(LIST_BILLS_QUERY, { limit: limit, offset: offset, customer_id: customer_id }) result = response.data.list_bills [result.total_count, result.nodes] end def list_offers(limit: 25, offset: 0, customer_id: nil, bill_id: nil, status: nil) response = @gql.query(LIST_OFFERS_QUERY, { limit: limit, offset: offset, customer_id: customer_id, bill_id: bill_id, status: status }) result = response.data.list_offers [result.total_count, result.nodes] end def list_information_requests(limit: 25, offset: 0, customer_id: nil) response = @gql.query(LIST_INFORMATION_REQUESTS_QUERY, { limit: limit, offset: offset, customer_id: customer_id }) result = response.data.list_information_requests [result.total_count, result.nodes] end def find_customer(customer_id) response = @gql.query(FIND_CUSTOMER_QUERY, { id: customer_id }) response.data.find_customer end def find_bill(bill_id) response = @gql.query(FIND_BILL_QUERY, { id: bill_id }) response.data.find_bill end def find_offer(offer_id) response = @gql.query(FIND_OFFER_QUERY, { id: offer_id }) response.data.find_offer end def find_information_request(information_request_id) response = @gql.query(FIND_INFORMATION_REQUEST_QUERY, { id: information_request_id }) response.data.find_information_request end def create_customer(params) response = @gql.query(CREATE_CUSTOMER_MUTATION, { customer: camelize(params) }) result = response.data.create_customer raise Error, result.errors.join(' ') unless result.success result.customer end def create_bill(customer_id, provider_id, params) response = @gql.query(CREATE_BILL_MUTATION, { customer_id: customer_id, provider_id: provider_id, bill: camelize(params) }) result = response.data.create_bill raise Error, result.errors.join(' ') unless result.success result.bill end def update_bill(bill_id, params) response = @gql.query(UPDATE_BILL_MUTATION, { bill_id: bill_id, bill: camelize(params) }) result = response.data.update_bill raise Error, result.errors.join(' ') unless result.success result.bill end def renegotiate_bill(bill_id) response = @gql.query(RENEGOTIATE_BILL_MUTATION, { id: bill_id }) response.data.renegotiate_bill end def stop_negotiating(bill_id) response = @gql.query(STOP_NEGOTIATING_MUTATION, { id: bill_id }) result = response.data.stop_negotiating raise Error, result.errors.join(' ') unless result.success result end def accept_offer(offer_id) response = @gql.query(ACCEPT_OFFER_MUTATION, { id: offer_id }) result = response.data.accept_offer raise Error, result.errors.join(' ') unless result.success result.offer end def reject_offer(offer_id) response = @gql.query(REJECT_OFFER_MUTATION, { id: offer_id }) result = response.data.reject_offer # binding.pry raise Error, result.errors.join(' ') unless result.success result.offer end def respond_to_information_request(information_request_id, params) response = @gql.query( RESPOND_TO_INFORMATION_REQUEST, { id: information_request_id, information_request: camelize(params) } ) result = response.data.respond_to_information_request raise Error, result.errors.join(' ') unless result.success result.information_request end def calculate_savings_estimate(provider_id, current_monthly_payment) response = @gql.query( CALCULATE_SAVINGS_ESTIMATE, { provider_id: provider_id, current_monthly_amount: current_monthly_payment } ) response.data.calculate_savings_estimate end private def camelize(hsh) hsh.deep_transform_keys { |key| key.to_s.camelize(:lower) } end end end end