Skip to content

Commit

Permalink
ap-5119: compare results of new Provider Details API call
Browse files Browse the repository at this point in the history
with ccms provider details api
  • Loading branch information
kmahern committed Aug 22, 2024
1 parent 413fff6 commit 45fbae1
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
63 changes: 63 additions & 0 deletions app/services/pda/compare_provider_details.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module PDA
class CompareProviderDetails
# NOTE: This is intended as a temporary class while we switch from CCMS Provider Details API
# to the new Provider Details API.
# Once that change over is complete, the aim is that this can be removed.
OfficeStruct = Struct.new(:id, :code)

def initialize(provider_id)
@provider = Provider.find(provider_id)
end

def self.call(provider_id)
new(provider_id).call
end

def call
compare_result
end

private

def compare_result
if match?
Rails.logger.info("Compare provider details for Provider #{@provider.id}. Provider details API results match.")
else
Rails.logger.info("Compare provider details for Provider #{@provider.id}. Provider details API results do not match.")
end
end

def result
@result ||= PDA::ProviderDetailsRetriever.call(@provider.username)
end

def match?
@match ||= [
firm.ccms_id.eql?(result.firm_id),
firm.name.eql?(result.firm_name),
@provider.contact_id.eql?(result.contact_id),
offices_match?,
].all?
end

def firm
@firm ||= @provider.firm
end

def provider_offices
@provider_offices ||= @provider.offices
end

def offices_match?
return false if provider_offices.count != result.offices.count

result.offices.each do |office|
provider_office = provider_offices.find_by!(ccms_id: office.id)
return false if provider_office.code != office.code
end
true
rescue ActiveRecord::RecordNotFound
false
end
end
end
91 changes: 91 additions & 0 deletions spec/services/pda/compare_provider_details_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
require "rails_helper"

FirmStruct = Struct.new(:id, :name)
OfficeStruct = Struct.new(:id, :code)
ProviderDetailsStruct = Struct.new(:firm_id, :contact_id, :firm_name, :offices)

RSpec.describe PDA::CompareProviderDetails do
describe ".call" do
subject(:call) { described_class.call(provider.id) }

before do
firm.providers << provider
provider.offices << office
stub_provider_details_retriever(
provider:,
contact_id:,
firm: ccms_firm,
offices: [ccms_office],
)
end

let(:provider) { create(:provider, username:, contact_id:) }
let(:firm) { create(:firm, ccms_id: ccms_firm_id, name: firm_name) }
let(:office) { create(:office, ccms_id: ccms_office_id, code: office_code) }
let(:firm_name) { "Test Firm" }
let(:office_code) { "6D456C" }
let(:username) { "test-user" }
let(:ccms_firm_id) { "1" }
let(:ccms_office_id) { "2" }
let(:contact_id) { 105 }
let(:ccms_firm) { FirmStruct.new(ccms_firm_id, firm_name) }
let(:ccms_office) { OfficeStruct.new(ccms_office_id, office_code) }

context "when PDA returns details that match the provider's details" do
it "logs that the details match" do
expect(Rails.logger).to receive(:info).with("Compare provider details for Provider #{provider.id}. Provider details API results match.")
call
end
end

context "when PDA returns details that do not match the provider's details" do
let(:ccms_firm) { FirmStruct.new(ccms_firm_id, "Wrong firm name") }

it "logs that the details do not match" do
expect(Rails.logger).to receive(:info).with("Compare provider details for Provider #{provider.id}. Provider details API results do not match.")
call
end

context "when the office details do not match" do
context "when the office codes do not match" do
let(:ccms_office) { OfficeStruct.new(ccms_office_id, "9R678Y") }

it "logs that the details do not match" do
expect(Rails.logger).to receive(:info).with("Compare provider details for Provider #{provider.id}. Provider details API results do not match.")
call
end
end

context "when the office returned by PDA does not exist in apply" do
let(:ccms_office) { OfficeStruct.new(3, office_code) }

it "logs that the details do not match" do
expect(Rails.logger).to receive(:info).with("Compare provider details for Provider #{provider.id}. Provider details API results do not match.")
call
end
end

context "when an office is missing from the PDA response" do
let(:additional_office) { create(:office, ccms_id: "3", code: "4C880Q") }

it "logs that the details do not match" do
provider.offices << additional_office
expect(Rails.logger).to receive(:info).with("Compare provider details for Provider #{provider.id}. Provider details API results do not match.")
call
end
end
end
end
end

def stub_provider_details_retriever(provider:, firm:, offices:, contact_id: 104)
allow(PDA::ProviderDetailsRetriever)
.to receive(:call)
.with(provider.username)
.and_return(api_response(firm:, offices:, contact_id:))
end

def api_response(firm:, offices:, contact_id:)
ProviderDetailsStruct.new(firm_id: firm.id, contact_id:, firm_name: firm.name, offices:)
end
end

0 comments on commit 45fbae1

Please sign in to comment.