From 4cfc301bfac24c784e65472eaa7a6d4523f97afa Mon Sep 17 00:00:00 2001 From: Sai Kumar Kotagiri Date: Mon, 9 Oct 2023 09:17:59 -0400 Subject: [PATCH] wip - automate H41 transmissions creation for Dry Run --- .../system_date/changed_subscriber.rb | 37 +++++- .../h41/open_transmissions/find_or_create.rb | 62 ++++++++++ .../open_transmissions/find_or_create_spec.rb | 112 ++++++++++++++++++ 3 files changed, 207 insertions(+), 4 deletions(-) create mode 100644 app/operations/fdsh/h41/open_transmissions/find_or_create.rb create mode 100644 spec/operations/fdsh/h41/open_transmissions/find_or_create_spec.rb diff --git a/app/event_source/subscribers/system_date/changed_subscriber.rb b/app/event_source/subscribers/system_date/changed_subscriber.rb index 36906046..38f8f912 100644 --- a/app/event_source/subscribers/system_date/changed_subscriber.rb +++ b/app/event_source/subscribers/system_date/changed_subscriber.rb @@ -20,6 +20,39 @@ class ChangedSubscriber def process_date_change_event(subscriber_logger, system_date) subscriber_logger.info "system_date: #{system_date}" + process_date_advanced_for_h41(subscriber_logger, system_date) + process_date_advanced_for_h36(subscriber_logger, system_date) + end + + def subscriber_logger_for(event) + Logger.new("#{Rails.root}/log/#{event}_#{Date.today.in_time_zone('Eastern Time (US & Canada)').strftime('%Y_%m_%d')}.log") + end + + # TODO: Eligible Date for Renewal H41 Transmissions is hard coded to September 15th. + # This should be changed to a configurable value using Resource Registry. + def date_eligible_for_renewal_h41_transmissions?(system_date) + Date.new(system_date.year, 9, 15) == system_date + end + + def process_date_advanced_for_h41(subscriber_logger, system_date) + date_eligible = date_eligible_for_renewal_h41_transmissions?(system_date) + subscriber_logger.info( + "System Date: #{system_date} is #{date_eligible ? 'ELIGIBLE' : 'NOT ELIGIBLE'} for creation of H41 Renewal Transmissions." + ) + + return unless date_eligible + + result = ::Fdsh::H41::OpenTransmissions::FindOrCreate.new.call({ reporting_year: system_date.year }) + if result.success? + subscriber_logger.info "H41 Transmission create: Successfully created H41 transmissions for the year: #{system_date.year}" + else + subscriber_logger.info "H41 Transmission create: Failed to created H41 transmissions for the year: #{result.failure}" + end + rescue StandardError => e + subscriber_logger.error "H41 Transmission create: for system_date: #{system_date} error: #{e} backtrace: #{e.backtrace}" + end + + def process_date_advanced_for_h36(subscriber_logger, system_date) return if system_date != Date.today.beginning_of_month year = system_date.year @@ -37,10 +70,6 @@ def process_date_change_event(subscriber_logger, system_date) subscriber_logger.error "on_system_date_changed H36 Transmission create: error: #{e} backtrace: #{e.backtrace}" subscriber_logger.error "on_system_date_changed H36 Transmission create: ------- end" end - - def subscriber_logger_for(event) - Logger.new("#{Rails.root}/log/#{event}_#{Date.today.in_time_zone('Eastern Time (US & Canada)').strftime('%Y_%m_%d')}.log") - end end end end diff --git a/app/operations/fdsh/h41/open_transmissions/find_or_create.rb b/app/operations/fdsh/h41/open_transmissions/find_or_create.rb new file mode 100644 index 00000000..f1f3f48b --- /dev/null +++ b/app/operations/fdsh/h41/open_transmissions/find_or_create.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +module Fdsh + module H41 + module OpenTransmissions + # Operation's job is to find or create 'open' H41 Transmissions for the given reporting_year. + # Example: + # During renewals, the system date is advanced to September 15th. + # This operation creates H41 OpenTransmissions for the reporting_year to handle incoming transactions. + class FindOrCreate + include Dry::Monads[:result, :do] + + H41_TRANSMISSION_TYPES = [:corrected, :original, :void].freeze + + # @param [Hash] opts The options to create Open H41 Transmissions + # @option opts [Integer] :reporting_year required + # @return [Dry::Monads::Result] + def call(params) + reporting_year = yield validate(params) + result = yield find_or_create_open_transmission(reporting_year) + + Success(result) + end + + private + + def validate(params) + if params[:reporting_year].is_a?(Integer) + Success(params[:reporting_year]) + else + Failure("Invalid reporting_year: #{params[:reporting_year]}. Must be an integer.") + end + end + + def find_or_create_open_transmission(reporting_year) + all_results = H41_TRANSMISSION_TYPES.inject({}) do |results, transmission_type| + result = find_or_create(reporting_year, transmission_type) + results[transmission_type] = result.failure? ? result.failure : result.success + results + end + + # Returns a hash with keys as transmission_type and values as result(a Failure string or a Success transmission) + if all_results.values.all? { |result| result.is_a?(::Transmittable::Transmission) } + Success(all_results) + else + Failure(all_results) + end + end + + def find_or_create(reporting_year, transmission_type) + ::Fdsh::H41::Transmissions::FindOrCreate.new.call( + { + reporting_year: reporting_year, + status: :open, + transmission_type: transmission_type + } + ) + end + end + end + end +end diff --git a/spec/operations/fdsh/h41/open_transmissions/find_or_create_spec.rb b/spec/operations/fdsh/h41/open_transmissions/find_or_create_spec.rb new file mode 100644 index 00000000..fd8cc489 --- /dev/null +++ b/spec/operations/fdsh/h41/open_transmissions/find_or_create_spec.rb @@ -0,0 +1,112 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Fdsh::H41::OpenTransmissions::FindOrCreate do + include Dry::Monads[:result, :do] + + let(:result) { subject.call(input_params) } + + before :all do + DatabaseCleaner.clean + end + + after :each do + DatabaseCleaner.clean + end + + let(:corrected_transmission) { FactoryBot.create(:h41_corrected_transmission, reporting_year: reporting_year) } + let(:original_transmission) { FactoryBot.create(:h41_original_transmission, reporting_year: reporting_year) } + let(:void_transmission) { FactoryBot.create(:h41_void_transmission, reporting_year: reporting_year) } + let(:input_params) { { reporting_year: reporting_year } } + + let(:failure_message) { 'Failed to create transmission.' } + + describe '#call' do + context 'with invalid reporting_year' do + let(:reporting_year) { 'test' } + + it 'returns failure with errors' do + expect(result.failure).to eq( + 'Invalid reporting_year: test. Must be an integer.' + ) + end + end + + context 'with exisiting open transmissions' do + let(:reporting_year) { Date.today.year.next } + + before do + corrected_transmission + original_transmission + void_transmission + end + + it 'returns success' do + expect(result.success?).to be_truthy + end + + it 'returns success with existing open transmissions' do + operation_result = result.success + expect(operation_result[:corrected]).to eq(corrected_transmission) + expect(operation_result[:original]).to eq(original_transmission) + expect(operation_result[:void]).to eq(void_transmission) + end + end + + context 'with no existing open transmissions' do + let(:reporting_year) { Date.today.year.next } + + it 'returns success' do + expect(result.success?).to be_truthy + end + + it 'returns success with newly created open transmissions' do + operation_result = result.success + expect(operation_result[:corrected]).to be_a(::H41::Transmissions::Outbound::CorrectedTransmission) + expect(operation_result[:original]).to be_a(::H41::Transmissions::Outbound::OriginalTransmission) + expect(operation_result[:void]).to be_a(::H41::Transmissions::Outbound::VoidTransmission) + end + end + + context 'with all failed transmission generation' do + let(:reporting_year) { Date.today.year.next } + + before do + allow(subject).to receive(:find_or_create).and_return(Failure(failure_message)) + end + + it 'returns failure with errors' do + expect(result.failure?).to be_truthy + end + + it 'returns failure with errors for Original Transmission' do + operation_result = result.failure + expect(operation_result[:corrected]).to eq(failure_message) + expect(operation_result[:original]).to eq(failure_message) + expect(operation_result[:void]).to eq(failure_message) + end + end + + context 'with all failed transmission generation' do + let(:reporting_year) { Date.today.year.next } + + before do + corrected_transmission + allow(subject).to receive(:find_or_create).and_call_original + allow(subject).to receive(:find_or_create).with(reporting_year, :original).and_return(Failure(failure_message)) + end + + it 'returns failure with errors' do + expect(result.failure?).to be_truthy + end + + it 'returns failure with errors for Original Transmission' do + operation_result = result.failure + expect(operation_result[:corrected]).to eq(corrected_transmission) + expect(operation_result[:original]).to eq(failure_message) + expect(operation_result[:void]).to be_a(::H41::Transmissions::Outbound::VoidTransmission) + end + end + end +end