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

CRIMAPP 1320 add non means funding decision #699

Closed
wants to merge 3 commits into from
Closed
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
26 changes: 26 additions & 0 deletions app/aggregates/deciding.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Deciding
class DecisionNotFound < StandardError; end
class ApplicationNotAssignedToUser < StandardError; end

class DraftCreated < RailsEventStore::Event; end
class InterestsOfJusticeSet < RailsEventStore::Event; end
class FundingDecisionSet < RailsEventStore::Event; end

class << self
def stream_name(decision_id)
"Deciding$#{decision_id}"
end
end

class Configuration
def call(event_store)
event_store.subscribe(
DecisionHandler, to: [Reviewing::AddDecision]
)
end
end

class DecisionHandler
def call(event); end
end
end
31 changes: 31 additions & 0 deletions app/aggregates/deciding/command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Deciding
class Command < Dry::Struct
attribute :decision_id, Types::Uuid

def with_decision(&block)
repository.with_aggregate(
Decision.new(decision_id),
stream_name,
&block
)
end

private

def repository
@repository ||= AggregateRoot::Repository.new(
Rails.configuration.event_store
)
end

def stream_name
Deciding.stream_name(decision_id)
end

class << self
def call(args)
new(args).call
end
end
end
end
12 changes: 12 additions & 0 deletions app/aggregates/deciding/commands/create_draft.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Deciding
class CreateDraft < Command
attribute :application_id, Types::Uuid
attribute :user_id, Types::Uuid

def call
with_decision do |decision|
decision.create_draft(user_id:, application_id:)
end
end
end
end
7 changes: 7 additions & 0 deletions app/aggregates/deciding/commands/load_decision.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Deciding
class LoadDecision < Command
def call
repository.load(Decision.new(decision_id), stream_name)
end
end
end
13 changes: 13 additions & 0 deletions app/aggregates/deciding/commands/set_funding_decision.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Deciding
class SetFundingDecision < Command
attribute :user_id, Types::Uuid
attribute :result, Types::FundingDecisionResult
attribute :details, Types::String

def call
with_decision do |decision|
decision.set_funding_decision(user_id:, result:, details:)
end
end
end
end
12 changes: 12 additions & 0 deletions app/aggregates/deciding/commands/set_interests_of_justice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Deciding
class SetInterestsOfJustice < Command
attribute :user_id, Types::Uuid
attribute :interests_of_justice, Types::InterestsOfJusticeDecision

def call
with_decision do |decision|
decision.set_interests_of_justice(user_id:, interests_of_justice:)
end
end
end
end
54 changes: 54 additions & 0 deletions app/aggregates/deciding/decision.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module Deciding
class Decision
include AggregateRoot

def initialize(decision_id)
@decision_id = decision_id
@interests_of_justice = nil
end

attr_accessor :application_id, :decision_id, :result, :details, :state

def create_draft(user_id:, application_id:)
apply DraftCreated.new(
data: { decision_id:, application_id:, user_id: }
)
end

def set_interests_of_justice(user_id:, interests_of_justice:)
apply InterestsOfJusticeSet.new(
data: { decision_id:, application_id:, user_id:, interests_of_justice: }
)
end

def set_funding_decision(user_id:, result:, details:)
apply FundingDecisionSet.new(
data: { decision_id:, application_id:, user_id:, result:, details: }
)
end

on DraftCreated do |event|
@application_id = event.data.fetch(:application_id)
@state = Types::DecisionState[:draft]
end

on InterestsOfJusticeSet do |event|
@interests_of_justice = event.data.fetch(:interests_of_justice)
end

on FundingDecisionSet do |event|
@result = event.data.fetch(:result)
@details = event.data.fetch(:details)
end

def interests_of_justice
return if @interests_of_justice.nil?

Types::InterestsOfJusticeDecision[@interests_of_justice]
end

def to_param
decision_id
end
end
end
3 changes: 3 additions & 0 deletions app/aggregates/reviewing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class CannotMarkAsReadyWhenSentBack < Error; end
class CannotSendBackWhenCompleted < Error; end
class NotReceived < Error; end

class DecisionAdded < Event; end
class DecisionRemoved < Event; end

class << self
def stream_name(application_id)
"Reviewing$#{application_id}"
Expand Down
3 changes: 1 addition & 2 deletions app/aggregates/reviewing/available_reviewer_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ class AvailableReviewerActions
marked_as_ready: [:complete, :send_back],
},
non_means: {
open: [:complete, :send_back],
marked_as_ready: [:complete, :send_back] # TODO: remove once all non-means in this state processed
open: FeatureFlags.adding_decisions.enabled? ? [:add_funding_decision, :send_back] : [:complete, :send_back],
},
pse: {
open: [:complete]
Expand Down
2 changes: 1 addition & 1 deletion app/aggregates/reviewing/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def repository
end

def stream_name
Reviewing.stream_name(application_id)
"Reviewing$#{application_id}"
end

class << self
Expand Down
13 changes: 13 additions & 0 deletions app/aggregates/reviewing/commands/add_decision.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Reviewing
class AddDecision < Command
attribute :application_id, Types::Uuid
attribute :user_id, Types::Uuid
attribute :decision_id, Types::Uuid

def call
with_review do |review|
review.add_decision(user_id:, decision_id:)
end
end
end
end
13 changes: 13 additions & 0 deletions app/aggregates/reviewing/commands/remove_decision.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Reviewing
class RemoveDecision < Command
attribute :application_id, Types::Uuid
attribute :user_id, Types::Uuid
attribute :decision_id, Types::Uuid

def call
with_review do |review|
review.remove_decision(user_id:, decision_id:)
end
end
end
end
51 changes: 29 additions & 22 deletions app/aggregates/reviewing/review.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,13 @@ class Review

def initialize(id)
@id = id
@application_type = nil
@state = nil
@return_reason = nil
@reviewer_id = nil
@reviewed_at = nil
@received_at = nil
@submitted_at = nil
@superseded_at = nil
@superseded_by = nil
@parent_id = nil
@work_stream = nil
end

attr_reader :id, :state, :return_reason, :reviewed_at, :reviewer_id,
:submitted_at, :superseded_by, :superseded_at, :parent_id,
:work_stream, :application_type
@decision_ids = []
end

attr_accessor :state, :return_reason, :reviewed_at, :reviewer_id, :submitted_at, :superseded_by,
:superseded_at, :parent_id, :work_stream, :application_type

attr_reader :id, :decision_ids

alias application_id id

Expand Down Expand Up @@ -68,6 +59,18 @@ def mark_as_ready(user_id:)
)
end

def add_decision(user_id:, decision_id:)
apply DecisionAdded.new(
data: { application_id:, user_id:, decision_id: }
)
end

def remove_decision(user_id:, decision_id:)
apply DecisionRemoved.new(
data: { application_id:, user_id:, decision_id: }
)
end

on ApplicationReceived do |event|
@state = Types::ReviewState[:open]
@received_at = event.timestamp
Expand All @@ -77,6 +80,14 @@ def mark_as_ready(user_id:)
@work_stream = event.data.fetch(:work_stream, Types::WorkStreamType['criminal_applications_team'])
end

on DecisionAdded do |event|
@decision_ids << event.data.fetch(:decision_id)
end

on DecisionRemoved do |event|
@decision_ids -= [event.data.fetch(:decision_id)]
end

on SentBack do |event|
@state = Types::ReviewState[:sent_back]
@return_reason = event.data.fetch(:reason, nil)
Expand Down Expand Up @@ -108,15 +119,11 @@ def received?
end

def business_day
return nil unless @submitted_at

BusinessDay.new(day_zero: @submitted_at)
BusinessDay.new(day_zero: @submitted_at) if @submitted_at.present?
end

def reviewed_on
return nil unless @reviewed_at

@reviewed_at.in_time_zone('London').to_date
@reviewed_at.in_time_zone('London').to_date if @reviewed_at.present?
end

def available_reviewer_actions
Expand Down
82 changes: 82 additions & 0 deletions app/components/decision_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
class DecisionComponent < ViewComponent::Base
include ActionView::Helpers
include AppTextHelper

def initialize(decision:, decision_iteration:)
@decision = decision
@decision_iteration = decision_iteration

super
end

def call
govuk_summary_card(title:, actions:) do |_card|
govuk_summary_list do |list|
if interests_of_justice.present?
list.with_row do |row|
row.with_key { label_text(:result, scope: [:decision, :interests_of_justice]) }
row.with_value { ioj_result }
end

list.with_row do |row|
row.with_key { label_text(:details, scope: [:decision, :interests_of_justice]) }
row.with_value { simple_format(interests_of_justice[:details]) }
end

list.with_row do |row|
row.with_key { label_text(:assessed_by, scope: [:decision, :interests_of_justice]) }
row.with_value { interests_of_justice[:assessed_by] }
end

list.with_row do |row|
row.with_key { label_text(:assessed_on, scope: [:decision, :interests_of_justice]) }
row.with_value { l interests_of_justice[:assessed_on], format: :compact }
end
end

list.with_row do |row|
row.with_key { label_text(:result, scope: [:decision]) }
row.with_value { render DecisionResultComponent.new(result: decision.result) }
end

list.with_row do |row|
row.with_key { label_text(:details, scope: [:decision]) }
row.with_value { decision.details }
end
end
end
end

private

def ioj_result
t(interests_of_justice[:result], scope: [:values, :decision_result])
end

attr_reader :decision, :decision_iteration

delegate :means, :interests_of_justice, to: :decision

def actions
[change_link, remove_link].compact
end

def remove_link
button_to('Remove', { action: :destroy, id: decision.decision_id }, method: :delete)
end

def change_link
govuk_link_to('Change', { action: :edit, id: decision.decision_id })
end

def title
safe_join(['Case', count].compact, ' ')
end

def count
return unless decision_iteration
return unless decision_iteration.size > 1

decision_iteration.index + 1
end
end
Loading
Loading