From 5142d07e6c9e0d285b8164608ac8132d185b4359 Mon Sep 17 00:00:00 2001 From: Gaurav Gupta Date: Wed, 17 Apr 2024 16:57:45 -0700 Subject: [PATCH] 80854 Add fetch_attibutes to Lorota Redis Client (#16370) --- .../app/services/v2/lorota/redis_client.rb | 24 ++++++++ .../services/v2/lorota/redis_client_spec.rb | 58 +++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/modules/check_in/app/services/v2/lorota/redis_client.rb b/modules/check_in/app/services/v2/lorota/redis_client.rb index 7e3de3c7a26..3d1c1209e75 100644 --- a/modules/check_in/app/services/v2/lorota/redis_client.rb +++ b/modules/check_in/app/services/v2/lorota/redis_client.rb @@ -58,6 +58,30 @@ def session_id_prefix(uuid:) def retry_attempt_prefix(uuid:) "authentication_retry_limit_#{uuid}" end + + def icn(uuid:) + fetch_attribute(uuid:, attribute: :icn) + end + + def fetch_attribute(uuid:, attribute:) + identifiers = appointment_identifiers(uuid:) + return nil if identifiers.nil? + + parsed_identifiers = Oj.load(identifiers).with_indifferent_access + parsed_identifiers.dig(:data, :attributes, attribute) + end + + private + + def appointment_identifiers(uuid:) + @appointment_identifiers ||= Hash.new do |h, key| + h[key] = Rails.cache.read( + "#{redis_session_prefix}_appointment_identifiers_#{key}", + namespace: 'check-in-lorota-v2-cache' + ) + end + @appointment_identifiers[uuid] + end end end end diff --git a/modules/check_in/spec/services/v2/lorota/redis_client_spec.rb b/modules/check_in/spec/services/v2/lorota/redis_client_spec.rb index 19609c0df41..f696210070f 100644 --- a/modules/check_in/spec/services/v2/lorota/redis_client_spec.rb +++ b/modules/check_in/spec/services/v2/lorota/redis_client_spec.rb @@ -21,6 +21,24 @@ let(:redis_expiry_time) { 12.hours } let(:retry_attempt_expiry) { 7.days } + let(:uuid) { '755f64db-336f-4614-a3eb-15f732d48de1' } + let(:patient_icn) { '2113957154V785237' } + let(:mobile_phone) { '7141234567' } + let(:station_number) { '500' } + let(:patient_cell_phone) { '1234567890' } + let(:facility_type) { 'abc' } + + let(:appointment_identifiers) do + { + data: { + id: uuid, + type: :appointment_identifier, + attributes: { patientDFN: '123', stationNo: station_number, icn: patient_icn, mobilePhone: mobile_phone, + patientCellPhone: patient_cell_phone, facilityType: facility_type } + } + } + end + before do allow(Rails).to receive(:cache).and_return(memory_store) @@ -172,4 +190,44 @@ expect(val).to eq(retry_count) end end + + describe '#icn' do + context 'when cache does not exist' do + it 'returns nil' do + expect(redis_client.icn(uuid:)).to eq(nil) + end + end + + context 'when cache exists' do + before do + Rails.cache.write( + "check_in_lorota_v2_appointment_identifiers_#{uuid}", + appointment_identifiers.to_json, + namespace: 'check-in-lorota-v2-cache', + expires_in: redis_expiry_time + ) + end + + it 'returns the cached value' do + expect(redis_client.icn(uuid:)).to eq(patient_icn) + end + end + + context 'when cache has expired' do + before do + Rails.cache.write( + "check_in_lorota_v2_appointment_identifiers_#{uuid}", + appointment_identifiers.to_json, + namespace: 'check-in-lorota-v2-cache', + expires_in: redis_expiry_time + ) + end + + it 'returns nil' do + Timecop.travel(redis_expiry_time.from_now) do + expect(redis_client.icn(uuid:)).to eq(nil) + end + end + end + end end