# frozen_string_literal: true require 'active_support/all' require 'graphlient' module Billfixers module Partner class Client def initialize(email:, token:, test_mode: false) endpoint = test_mode ? 'https://billfixers-staging.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-PartnerUser-Email": email, "X-PartnerUser-Token": token }) 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(params) response = @gql.query(CREATE_BILL_MUTATION, { bill: camelize(params) }) result = response.data.create_bill raise Error, result.errors.join(' ') unless result.success result.bill end def cancel_bill(bill_id) response = @gql.query(CANCEL_BILL_MUTATION, { id: bill_id }) response.data.cancel_bill.success 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 raise Error, result.errors.join(' ') unless result.success result.offer end private def camelize(hsh) hsh.deep_transform_keys { |key| key.to_s.camelize(:lower) } end end end end