Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip - automate H41 transmissions creation for Dry Run #166

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions app/event_source/subscribers/system_date/changed_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
62 changes: 62 additions & 0 deletions app/operations/fdsh/h41/open_transmissions/find_or_create.rb
Original file line number Diff line number Diff line change
@@ -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
112 changes: 112 additions & 0 deletions spec/operations/fdsh/h41/open_transmissions/find_or_create_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading