Skip to content

Instantly share code, notes, and snippets.

@cored
Created January 26, 2021 23:41
Show Gist options
  • Save cored/f1ed5515efc98838b83a506f4e432716 to your computer and use it in GitHub Desktop.
Save cored/f1ed5515efc98838b83a506f4e432716 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
module Billing
module Models
class BillableEvent < ActiveRecord::Base
DrugHealthScreeningDataNotPresent = Class.new(StandardError)
acts_as_paranoid
belongs_to :event
before_validation :source_from_event
validates_uniqueness_of :event_id
validates :event_id, :source_id, :source_object, :fee_category, :fee_name, presence: true
validates :source_object,
inclusion: {
in: Models::Event::BILLABLE_OBJECTS,
message: "%{value} IN #{Models::Event::BILLABLE_OBJECTS.join(',')}" # rubocop:disable Style/FormatStringToken
}
validates :fee_category,
inclusion: {
in: Models::InvoiceItem::FEE_TYPES,
message: "%{value} IN #{Models::InvoiceItem::FEE_TYPES.join(',')}" # rubocop:disable Style/FormatStringToken
}
def fee_category
super&.to_sym
end
private
def source_from_event
self.source_id ||= event&.data&.[]('id')
self.source_object ||= event&.data&.[]('object')
self.fee_category ||= source_fee_category
self.fee_name ||= source_fee_name
end
# rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
def source_fee_category
Models::Event::SURCHARGES_MAP[event&.data&.[]('object')] ||
Models::Event::SURCHARGES_MAP[event&.data&.[]('full_object_name')] ||
Models::Event::SERVICE_FEES_MAP["#{event&.type}.#{event&.data&.[]('object')}"] ||
Models::Event::SERVICE_FEES_MAP[event&.type] ||
Models::Event::PASSTHROUGH_FEES_MAP[event&.request_type || event&.type] ||
:service_fees
end
def source_fee_name
if source_fee_category == :service_fees
service_fee_name
elsif Models::Event::SURCHARGES_MAP.key?(event&.data&.[]('object')) ||
Models::Event::SURCHARGES_MAP.key?(event&.data&.[]('full_object_name'))
surcharge_name
elsif Models::Event::PASSTHROUGH_FEES_MAP.value?(source_fee_category)
passthrough_fee_name
elsif source_fee_category == :abuse_registry_fees
additional_verification_surcharge_name
elsif source_fee_category == :state_service_fees
state_service_fee_name
else
Models::Event::CATEGORY_NAME_MAP[source_fee_category]
end
end
# rubocop:enable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
def service_fee_name
case event&.type
when 'report.county_search_service_fee'
'County Level Verification'
when 'report.education_verification_service_fee'
'International Education Verification'
when 'report.employment_verification_service_fee'
'International Employment Verification'
else
event&.package_title
end
end
def surcharge_name
service_id = event.data['service_id']
network = event.data['network']
raise DrugHealthScreeningDataNotPresent unless service_id.present? && network.present?
[
service_id.underscore.humanize.titleize,
network.underscore.humanize.titleize
].join(' - ')
end
def passthrough_fee_name
county_state = [event&.court, event&.county, event&.state].compact.join(' - ')
pointer_prefix = source_object == 'pointer_state_criminal_search_request' ? ' Pointer' : ''
county_state.concat(
pointer_prefix,
" #{Models::Event::CATEGORY_NAME_MAP[source_fee_category]}"
).strip
end
def additional_verification_surcharge_name
"#{event.data['state']} #{event.data['object'].titleize} Fee"
end
def state_service_fee_name
pointer_prefix = event.type == 'pointer_state_criminal_search.completed' ? 'Pointer ' : ''
pointer_prefix + 'State Service Fee'
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment