Skip to content

Commit

Permalink
Simplify the Funding Decision data model
Browse files Browse the repository at this point in the history
  • Loading branch information
timpeat committed Dec 17, 2024
1 parent 856c894 commit d07f41e
Show file tree
Hide file tree
Showing 45 changed files with 806 additions and 314 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ gem 'laa-criminal-applications-datastore-api-client',

gem 'laa-criminal-legal-aid-schemas',
github: 'ministryofjustice/laa-criminal-legal-aid-schemas',
tag: 'v1.4.3'
tag: 'v1.5.0'

# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem 'sprockets-rails'
Expand Down
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ GIT

GIT
remote: https://github.com/ministryofjustice/laa-criminal-legal-aid-schemas.git
revision: 861869efc25d2804f2b66d62df50d1330f28243f
tag: v1.4.3
revision: dfe3d23ab4cc93ea3659db5d20950e14ce37a6a1
tag: v1.5.0
specs:
laa-criminal-legal-aid-schemas (1.4.3)
laa-criminal-legal-aid-schemas (1.5.0)
dry-schema (~> 1.13)
dry-struct (~> 1.6.0)
json-schema (~> 4.0.0)
Expand Down
2 changes: 1 addition & 1 deletion app/aggregates/deciding/commands/create_draft_from_maat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Deciding
class CreateDraftFromMaat < Command
attribute :application_id, Types::Uuid
attribute :application_type, Types::ApplicationType
attribute :maat_decision, Maat::Decision
attribute :maat_decision, Decisions::Draft
attribute :user_id, Types::Uuid

def call
Expand Down
2 changes: 1 addition & 1 deletion app/aggregates/deciding/commands/set_funding_decision.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Deciding
class SetFundingDecision < Command
attribute :user_id, Types::Uuid
attribute :funding_decision, Types::FundingDecisionResult
attribute :funding_decision, Types::FundingDecision

def call
with_decision do |decision|
Expand Down
6 changes: 4 additions & 2 deletions app/aggregates/deciding/commands/update_from_maat_decision.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module Deciding
class UpdateFromMaatDecision < Command
attribute :maat_decision, Maat::Decision
attribute :maat_decision, Decisions::Draft
attribute :user_id, Types::Uuid

def call
with_decision do |decision|
decision.sync_with_maat(maat_decision:, user_id:)
raise MaatRecordNotChanged unless maat_decision.checksum != decision.checksum

decision.sync_with_maat(maat_decision: maat_decision.to_h, user_id: user_id)
end
end
end
Expand Down
7 changes: 3 additions & 4 deletions app/aggregates/deciding/decision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def create_draft(application_id:, user_id:, reference:)
def create_draft_from_maat(application_id:, maat_decision:, user_id:, application_type:)
raise AlreadyCreated unless @state.nil?

maat_decision = maat_decision.to_h
apply DraftCreatedFromMaat.new(
data: { decision_id:, application_id:, maat_decision:, user_id:, application_type: }
)
Expand All @@ -42,9 +43,7 @@ def link_to_cifc(application_id:, user_id:)
end

def sync_with_maat(maat_decision:, user_id:)
raise MaatRecordNotChanged unless maat_decision&.checksum != checksum

apply SynchedWithMaat.build(self, maat_decision:, user_id:)
apply SynchedWithMaat.build(self, maat_decision: maat_decision.to_h, user_id: user_id)
end

def set_interests_of_justice(user_id:, interests_of_justice:)
Expand Down Expand Up @@ -128,7 +127,7 @@ def send_to_provider(user_id:, application_id:)
end

def update_from_maat(maat_attributes)
decision = Maat::Decision.new(maat_attributes)
decision = Decisions::Draft.new(maat_attributes)

@maat_id = decision.maat_id
@reference = decision.reference
Expand Down
7 changes: 1 addition & 6 deletions app/components/decision_result_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ def call
attr_reader :result

def colour
case result
when /fail|inel/
'red'
else
'green'
end
{ 'grant' => 'green', 'refuse' => 'red' }.fetch(result, nil)
end
end
1 change: 0 additions & 1 deletion app/controllers/casework/maat_decisions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def new
# import failed due to a technical issue).
def create
@form_object = ::Decisions::MaatIdForm.new(application: @crime_application)

@form_object.create_with_user!(permitted_params, current_user_id)

set_flash(:maat_decision_linked, maat_id: @form_object.maat_id)
Expand Down
65 changes: 19 additions & 46 deletions app/lib/maat/decision.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,28 @@
module Maat
class Decision < LaaCrimeSchemas::Structs::Decision
attribute :maat_id, Types::Integer
attribute? :case_id, Types::String.optional
attribute :funding_decision, Types::FundingDecisionResult.optional
class Decision < Dry::Struct
transform_keys(&:to_sym)

def checksum
Digest::MD5.hexdigest(to_json)
end
attribute? :maat_ref, Types::MaatId
attribute? :usn, Types::ApplicationReference.optional
attribute? :case_id, Types::String
attribute? :case_type, Types::String
attribute? :app_created_date, Types::DateTime.optional

alias decision_id maat_id
attribute? :ioj_result, Types::String.optional
attribute? :ioj_reason, Types::String.optional
attribute? :ioj_assessor_name, Types::String.optional

class << self
def build(response)
new(
reference: response['usn'],
maat_id: response['maat_ref'],
case_id: response['case_id'],
funding_decision: funding_decision(response['funding_decision']),
interests_of_justice: interests_of_justice(response),
means: means(response)
)
end
attribute? :ioj_appeal_result, Types::String.optional

def funding_decision(maat_value)
return nil if maat_value.blank?
attribute? :means_result, Types::String.optional
attribute? :means_assessor_name, Types::String.optional
attribute? :date_means_created, Types::DateTime.optional

maat_value.downcase
end
attribute? :passport_result, Types::String.optional
attribute? :passport_assessor_name, Types::String.optional
attribute? :date_passport_created, Types::DateTime.optional

def interests_of_justice(response)
return nil if response['ioj_result'].blank?

{
result: response['ioj_result'].downcase,
details: response['ioj_reason'],
assessed_by: response['ioj_assessor_name'],
assessed_on: response['app_created_date']
}
end

def result(maat_result); end

def means(response)
return nil if response['means_result'].blank?

{
result: response['means_result'].downcase,
assessed_by: response['means_assessor_name'],
assessed_on: response['date_means_created']
}
end
end
attribute? :funding_decision, Types::String.optional
attribute? :cc_rep_decision, Types::String.optional
end
end
2 changes: 1 addition & 1 deletion app/lib/maat/get_decision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get(path)
# the response body includes a MAAT ID (maat_ref).
return nil unless response.body.present? && response.body['maat_ref'].present?

Decision.build(response.body)
Decision.new(response.body)
end

attr_reader :http_client
Expand Down
7 changes: 3 additions & 4 deletions app/models/decisions/draft.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module Decisions
class Draft < LaaCrimeSchemas::Structs::Decision
attribute? :funding_decision, Types::Nil | Types::FundingDecisionResult
attribute? :funding_decision, Types::Nil | Types::FundingDecision
attribute? :reference, Types::Nil | Types::Integer
attribute? :decision_id, Types::Nil | Types::Integer | Types::Uuid
attribute? :application_id, Types::Uuid
attribute? :maat_id, Types::Integer.optional
attribute? :case_id, Types::String.optional
attribute? :checksum, Types::String.optional

def to_param
{ crime_application_id: application_id, decision_id: decision_id }
Expand All @@ -16,8 +15,8 @@ def complete?
funding_decision.present?
end

def as_json
LaaCrimeSchemas::Structs::Decision.new(self)
def checksum
Digest::MD5.hexdigest(to_json)
end

class << self
Expand Down
2 changes: 1 addition & 1 deletion app/models/decisions/overall_result_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class OverallResultForm
validates :funding_decision, inclusion: { in: :possible_decisions }

def possible_decisions
[Types::FundingDecisionResult['granted_on_ioj'], Types::FundingDecisionResult['fail_on_ioj']]
[Types::FundingDecision['grant'], Types::FundingDecision['refuse']]
end

class << self
Expand Down
24 changes: 24 additions & 0 deletions app/services/maat/base_translator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Maat
class BaseTranslator
def initialize(original:)
@original = original
end

class << self
def translate(original)
new(original:).translate
end
end

# :nocov:
def translate
raise 'implement in subclasses'
end
# :nocov:
#

private

attr_reader :original
end
end
6 changes: 5 additions & 1 deletion app/services/maat/create_draft_decision_from_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ def application_id
delegate :decision_id, to: :maat_decision

def maat_decision
@maat_decision ||= Maat::GetDecision.new.by_usn!(reference)
return @maat_decision if @maat_decision

maat_decision = Maat::GetDecision.new.by_usn!(reference)

Maat::DecisionTranslator.translate(maat_decision) if maat_decision.present?
end
end
end
20 changes: 20 additions & 0 deletions app/services/maat/crown_court_decision_translator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Maat
class CrownCourtDecisionTranslator < BaseTranslator
def translate
return nil if funding_decision.blank?

Types::FundingDecision[funding_decision]
end

private

def funding_decision
case original
when /Granted/
'grant'
when /Refused|Failed/
'refuse'
end
end
end
end
54 changes: 54 additions & 0 deletions app/services/maat/decision_translator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module Maat
class DecisionTranslator
def initialize(maat_decision:)
@maat_decision = maat_decision
end

class << self
def translate(maat_decision)
new(maat_decision:).translate
end
end

def translate
Decisions::Draft.new(
maat_id:, case_id:, reference:, interests_of_justice:,
means:, funding_decision:, decision_id:
)
end

private

def maat_id
maat_decision.maat_ref
end
alias decision_id maat_id

def reference
maat_decision.usn
end

def interests_of_justice
InterestsOfJusticeTranslator.translate(maat_decision)
end

def means
MeansTranslator.translate(maat_decision)
end

def funding_decision
return crown_court_decision if crown_court_decision
return nil unless maat_decision.funding_decision

FundingDecisionTranslator.translate(maat_decision.funding_decision)
end

delegate :case_id, to: :maat_decision

def crown_court_decision
CrownCourtDecisionTranslator.translate(maat_decision.cc_rep_decision)
end

attr_reader :maat_decision
end
end
20 changes: 20 additions & 0 deletions app/services/maat/funding_decision_translator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Maat
class FundingDecisionTranslator < BaseTranslator
def translate
return nil if funding_decision.blank?

Types::FundingDecision[funding_decision]
end

private

def funding_decision
case original
when /PASS|GRANTED|FULL/
'grant'
when /INEL|FAIL/
'refuse'
end
end
end
end
Loading

0 comments on commit d07f41e

Please sign in to comment.