Skip to content

Commit

Permalink
Merge pull request #5871 from ministryofjustice/ap-4359/cfe-partner-s…
Browse files Browse the repository at this point in the history
…ubmissions

AP-4359: Add cfe_civil partner submission
  • Loading branch information
colinbruce authored Oct 26, 2023
2 parents 94847a0 + 5a6b2f8 commit d939662
Show file tree
Hide file tree
Showing 19 changed files with 772 additions and 190 deletions.
3 changes: 3 additions & 0 deletions app/services/cfe_civil/component_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ComponentList
Components::Vehicles,
Components::Properties,
Components::ExplicitRemarks,
Components::Partner,
].freeze

NON_PASSPORTED_WITH_BANK_TRANSACTIONS_SERVICES = [
Expand All @@ -27,6 +28,7 @@ class ComponentList
Components::IrregularIncomes,
Components::Employments,
Components::CashTransactions,
Components::Partner,
].freeze

NON_PASSPORTED_WITH_REGULAR_TRANSACTIONS_SERVICES = [
Expand All @@ -42,6 +44,7 @@ class ComponentList
Components::Employments,
Components::RegularTransactions,
Components::CashTransactions,
Components::Partner,
].freeze

def self.call(object)
Expand Down
9 changes: 5 additions & 4 deletions app/services/cfe_civil/components/base_data_block.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module CFECivil
module Components
class BaseDataBlock
attr_reader :legal_aid_application
attr_reader :legal_aid_application, :owner_type

def self.call(legal_aid_application)
new(legal_aid_application).call
def self.call(legal_aid_application, owner_type = "Applicant")
new(legal_aid_application, owner_type).call
end

def initialize(legal_aid_application)
def initialize(legal_aid_application, owner_type = "Applicant")
@legal_aid_application = legal_aid_application
@owner_type = owner_type
end
end
end
Expand Down
14 changes: 13 additions & 1 deletion app/services/cfe_civil/components/capitals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class Capitals < BaseDataBlock
life_assurance_endowment_policy: "Life assurance and endowment policies not linked to a mortgage",
}.freeze

PARTNER_SAVINGS_AMOUNT_FIELDS = {
partner_offline_current_accounts: "Partner current accounts",
partner_offline_savings_accounts: "Partner savings accounts",
joint_offline_current_accounts: "Joint current accounts",
joint_offline_savings_accounts: "Joint savings accounts",
}.freeze

def call
{
capitals: {
Expand All @@ -37,6 +44,8 @@ def other_assets_declaration
end

def itemised_other_assets
return [] if owner_type == "Partner"

array_of_hashes_for(other_assets_declaration, OTHER_ASSET_FIELDS)
end

Expand All @@ -45,6 +54,8 @@ def bank_account_assets
end

def bank_accounts
return [] if owner_type == "Partner"

[current_account_balance, savings_account_balance].compact
end

Expand All @@ -67,7 +78,8 @@ def savings_account_balance
end

def savings_amounts
array_of_hashes_for(savings_amount, SAVINGS_AMOUNT_FIELDS)
fields = owner_type == "Partner" ? PARTNER_SAVINGS_AMOUNT_FIELDS : SAVINGS_AMOUNT_FIELDS
array_of_hashes_for(savings_amount, fields)
end

def array_of_hashes_for(model, field_names_and_descriptions)
Expand Down
3 changes: 2 additions & 1 deletion app/services/cfe_civil/components/cash_transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def call
private

def cash_transactions_for(operation)
cash_transactions.joins(:transaction_type).where(transaction_type: { operation: })
cash_transactions.joins(:transaction_type)
.where(transaction_type: { operation: }).where(owner_type:)
.order("transaction_type.name", :transaction_date)
.group_by(&:transaction_type_id)
end
Expand Down
18 changes: 14 additions & 4 deletions app/services/cfe_civil/components/employments.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
module CFECivil
module Components
class Employments < BaseDataBlock
delegate :employments, to: :legal_aid_application

def call
if employment_and_payments?
{ employment_income: employment_income_payload }.to_json
if owner_type.eql?("Partner")
{ employments: employment_income_payload }.to_json
else
{ employment_income: employment_income_payload }.to_json
end
else
{}.to_json
end
end

private

def party
@party ||= legal_aid_application.send(owner_type.downcase)
end

def employment_and_payments?
legal_aid_application.hmrc_employment_income? && legal_aid_application.employment_payments.present?
party.hmrc_employment_income? && party.employment_payments.present?
end

def employments
party.employments
end

def employment_income_payload
Expand Down
52 changes: 37 additions & 15 deletions app/services/cfe_civil/components/irregular_incomes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,57 @@ module CFECivil
module Components
class IrregularIncomes < BaseDataBlock
def call
{
irregular_incomes: {
payments: payment_data,
},
}.to_json
payment_data.to_json
end

private

def payment_data
if student_finance_data?
[
{
income_type: "student_loan",
frequency: "annual",
amount: student_finance_amount.to_f,
if student_finance_data? && owner_type.eql?("Partner")
{
irregular_incomes: [
{
income_type: "student_loan",
frequency: "annual",
amount: student_finance_amount.to_f,
},
],
}
elsif student_finance_data?
{
irregular_incomes: {
payments: [
{
income_type: "student_loan",
frequency: "annual",
amount: student_finance_amount.to_f,
},
],
},
]
}
elsif owner_type.eql?("Partner")
{
irregular_incomes: [],
}
else
[]
{
irregular_incomes: {
payments: [],
},
}
end
end

def party
@party ||= legal_aid_application.send(owner_type.downcase)
end

def student_finance_data?
legal_aid_application.applicant.student_finance? && student_finance_amount.positive?
party.student_finance? && student_finance_amount.positive?
end

def student_finance_amount
legal_aid_application.applicant.student_finance_amount
party.student_finance_amount
end
end
end
Expand Down
41 changes: 41 additions & 0 deletions app/services/cfe_civil/components/partner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module CFECivil
module Components
class Partner < BaseDataBlock
delegate :partner, to: :legal_aid_application

def call
return {}.to_json if partner.blank?

result = base_partner
# Most records are stored with an Owner field that is tied to Applicant or Partner models, using their IDs
# Vehicles are stored with the, optional, output of a radio button as client or partner (lower cased)
# For this reason the call to Components::Vehicles.new is slightly different from the rest of the generators
result.merge! JSON.parse(Components::CashTransactions.new(@legal_aid_application, "Partner").call)
result.merge! JSON.parse(Components::IrregularIncomes.new(@legal_aid_application, "Partner").call)
result.merge! JSON.parse(Components::Vehicles.new(@legal_aid_application, "partner").call)
result.merge! JSON.parse(Components::Employments.new(@legal_aid_application, "Partner").call)
result.merge! JSON.parse(Components::RegularTransactions.new(@legal_aid_application, "Partner").call)
result.merge! JSON.parse(Components::Capitals.new(@legal_aid_application, "Partner").call)
# Outgoings # skipped, these are generated from Truelayer and that is not currently supported by the partner flow
# state_benefits # skipped, these are generated from Truelayer and that is not currently supported by the partner flow
# other_incomes # skipped, these are generated from Truelayer and that is not currently supported by the partner flow
# capitals # skipped, these are generated from Truelayer and that is not currently supported by the partner flow
# additional_properties # skipping as this is submitted via client
# dependants # skipping as this is submitted via client
{
partner: result,
}.to_json
end

private

def base_partner
{
partner: {
date_of_birth: partner.date_of_birth.strftime("%Y-%m-%d"),
},
}
end
end
end
end
8 changes: 5 additions & 3 deletions app/services/cfe_civil/components/regular_transactions.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module CFECivil
module Components
class RegularTransactions < BaseDataBlock
delegate :regular_transactions, to: :legal_aid_application

def call
{
regular_transactions: build_regular_transactions,
Expand All @@ -11,8 +9,12 @@ def call

private

def party
@party ||= legal_aid_application.send(owner_type.downcase)
end

def build_regular_transactions
regular_transactions.each_with_object([]) do |regular_transaction, payload|
party.regular_transactions.each_with_object([]) do |regular_transaction, payload|
payload << {
category: regular_transaction.transaction_type.name,
operation: regular_transaction.transaction_type.operation,
Expand Down
38 changes: 18 additions & 20 deletions app/services/cfe_civil/components/vehicles.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
module CFECivil
module Components
class Vehicles < BaseDataBlock
delegate :vehicles, to: :legal_aid_application
def self.call(legal_aid_application, owner_type = :client)
new(legal_aid_application, owner_type).call
end

def call
vehicle_request_body.to_json
{
vehicles: vehicles_request_body,
}.to_json
end

private

def vehicle_request_body
vehicles.any? ? vehicle_present_request : vehicle_absent_request
def vehicles
legal_aid_application.vehicles.where(owner: owner_type)
end

def vehicle_absent_request
{
vehicles: [],
}
end
def vehicles_request_body
return [] unless vehicles.any?

def vehicle_present_request
{
vehicles: vehicles.map do |vehicle|
{
value: vehicle.estimated_value.to_f,
loan_amount_outstanding: vehicle.payment_remaining.to_f,
date_of_purchase: vehicle.purchased_on&.strftime("%Y-%m-%d"),
in_regular_use: vehicle.used_regularly,
}
end,
}
vehicles.map do |vehicle|
{
value: vehicle.estimated_value.to_f,
loan_amount_outstanding: vehicle.payment_remaining.to_f,
date_of_purchase: vehicle.purchased_on&.strftime("%Y-%m-%d"),
in_regular_use: vehicle.used_regularly,
}
end
end
end
end
Expand Down
46 changes: 46 additions & 0 deletions spec/factories/employments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,52 @@
net_employment_income: 1902.3)
end
end
trait :example2_usecase1 do
id { "87654321-1234-1234-1234-123456789abc" }
name { "Job 877" }

after(:create) do |employment|
create(:employment_payment,
id: "20231024-0000-0000-0000-123456789abc",
employment:,
date: Date.new(2021, 11, 28),
gross: 1868.98,
benefits_in_kind: 0.0,
tax: -161.8,
national_insurance: -128.64,
net_employment_income: 1578.54)

create(:employment_payment,
id: "20230924-0000-0000-0000-123456789abc",
employment:,
date: Date.new(2021, 10, 28),
gross: 1868.98,
benefits_in_kind: 0.0,
tax: -111,
national_insurance: -128.64,
net_employment_income: 1629.34)

create(:employment_payment,
id: "20230824-0000-0000-0000-123456789abc",
employment:,
date: Date.new(2021, 9, 28),
gross: 2492.61,
benefits_in_kind: 0.0,
tax: -286.6,
national_insurance: -203.47,
net_employment_income: 2002.54)

create(:employment_payment,
id: "20230724-0000-0000-0000-123456789abc",
employment:,
date: Date.new(2021, 8, 28),
gross: 2345.29,
benefits_in_kind: 0.0,
tax: -257.2,
national_insurance: -185.79,
net_employment_income: 1902.3)
end
end

trait :with_irregularities do
after(:create) do |employment|
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/regular_transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@
trait :rent_or_mortgage do
transaction_type { TransactionType.find_by(name: "rent_or_mortgage") || create(:transaction_type, :rent_or_mortgage) }
end

trait :friends_or_family do
transaction_type { TransactionType.find_by(name: "friends_or_family") || create(:transaction_type, :friends_or_family) }
end
end
end
Loading

0 comments on commit d939662

Please sign in to comment.