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

AP-3805: HRMC request for partner #5459

Merged
merged 10 commits into from
Jul 12, 2023
9 changes: 9 additions & 0 deletions app/models/partner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ class Partner < ApplicationRecord
belongs_to :legal_aid_application, dependent: :destroy
has_many :hmrc_responses, class_name: "HMRC::Response", as: :owner
has_many :employments, as: :owner

def json_for_hmrc
{
first_name:,
last_name:,
dob: date_of_birth,
nino: national_insurance_number,
}
end
end
19 changes: 13 additions & 6 deletions app/services/hmrc/create_responses_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ def self.call(legal_aid_application)
def call
return unless @legal_aid_application.hmrc_responses.empty?

applicant = @legal_aid_application.applicant
individuals = [@legal_aid_application.applicant]
individuals << @legal_aid_application.partner if check_partner?

USE_CASES.each do |use_case|
hmrc_response = @legal_aid_application.hmrc_responses.create(use_case:, owner_id: applicant.id, owner_type: applicant.class)
if use_mock?
MockInterfaceResponseService.call(hmrc_response)
else
HMRC::SubmissionWorker.perform_async(hmrc_response.id)
individuals.each do |person|
hmrc_response = person.hmrc_responses.create(use_case:, legal_aid_application: @legal_aid_application)
if use_mock?
MockInterfaceResponseService.call(hmrc_response)
else
HMRC::SubmissionWorker.perform_async(hmrc_response.id)
end
end
end
end
Expand All @@ -35,5 +38,9 @@ def use_mock?
def not_production_environment?
!HostEnv.production?
end

def check_partner?
@legal_aid_application.applicant.has_partner? && @legal_aid_application.partner.has_national_insurance_number?
end
end
end
6 changes: 3 additions & 3 deletions app/services/hmrc/interface/submission_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def hmrc_interface_url
end

def request_body
@request_body ||= { filter: applicant_values.merge(date_values) }.to_json
@request_body ||= { filter: owner_values.merge(date_values) }.to_json
end

private
Expand All @@ -30,8 +30,8 @@ def use_case
@use_case ||= @hmrc_response.use_case
end

def applicant_values
@applicant_values ||= application.applicant.json_for_hmrc
def owner_values
@owner_values ||= @hmrc_response.owner.json_for_hmrc
end

def date_values
Expand Down
5 changes: 3 additions & 2 deletions app/services/hmrc/mock_interface_response_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ def self.call(*args)
new(*args).call
end

attr_reader :application
attr_reader :application, :owner

delegate :applicant, to: :application, allow_nil: true
delegate :first_name, :last_name, :national_insurance_number, :date_of_birth, to: :applicant, allow_nil: true
delegate :first_name, :last_name, :national_insurance_number, :date_of_birth, to: :owner, allow_nil: true

def initialize(hmrc_response)
@hmrc_response = hmrc_response
@application = @hmrc_response.legal_aid_application
@owner = @hmrc_response.owner
@submission_id = SecureRandom.uuid
@reference_date = @application.calculation_date || Time.zone.today
@use_case_name = "use_case_#{@hmrc_response.use_case}"
Expand Down
14 changes: 7 additions & 7 deletions app/services/hmrc/parsed_response/persistor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ def self.call(hmrc_response)
def initialize(hmrc_response)
@hmrc_response = hmrc_response
@application = hmrc_response.legal_aid_application
@person = @hmrc_response.owner
end

def call
return unless persistable?

destroy_existing_employments if @application.employments.any?
destroy_existing_employments if @person.employments.any?
persist_response
end

Expand All @@ -22,12 +23,12 @@ def call
attr_reader :hmrc_response

def persistable?
Validator.call(hmrc_response, applicant: @application.applicant)
Validator.call(hmrc_response, person: @hmrc_response.owner)
end

def destroy_existing_employments
@application.employments.map(&:destroy!)
@application.reload
@person.employments.map(&:destroy!)
@person.reload
end

def persist_response
Expand All @@ -43,14 +44,13 @@ def no_employments_in_response?
end

def create_employments
applicant = @application.applicant
employments_array.each_with_index do |_emp, i|
@application.employments << ::Employment.new(name: "Job #{i + 1}", owner_id: applicant.id, owner_type: applicant.class)
@application.employments << ::Employment.new(name: "Job #{i + 1}", owner_id: @person.id, owner_type: @person.class)
end
end

def create_employment_payments
employment = @application.employments.order(:name).first
employment = @person.employments.order(:name).first
income_array.each do |income_hash|
employment.employment_payments << new_employment_payment(income_hash)
end
Expand Down
19 changes: 10 additions & 9 deletions app/services/hmrc/parsed_response/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ class Validator

Error = Struct.new(:attribute, :message)

def self.call(hmrc_response, applicant:)
new(hmrc_response, applicant:).call
# person can be Applicant or Partner
def self.call(hmrc_response, person:)
new(hmrc_response, person:).call
end

def initialize(hmrc_response, applicant:)
def initialize(hmrc_response, person:)
@hmrc_response = hmrc_response
@response = hmrc_response.response
@applicant = applicant
@person = person
@errors = []
end

Expand All @@ -30,7 +31,7 @@ def call

private

attr_reader :hmrc_response, :response, :applicant
attr_reader :hmrc_response, :response, :person

def validate_use_case
errors << error(:use_case, "use_case must be \"one\", but was \"#{hmrc_response.use_case}\"") if hmrc_response.use_case != "one"
Expand Down Expand Up @@ -59,10 +60,10 @@ def validate_response_income
end

def validate_response_individual
errors << error(:individual, "individual must match applicant") unless individual &&
applicant &&
applicant.national_insurance_number.casecmp?(individual["nino"]) &&
applicant.date_of_birth.iso8601 == individual["dateOfBirth"]
errors << error(:individual, "individual must match person") unless individual &&
person &&
person.national_insurance_number.casecmp?(individual["nino"]) &&
person.date_of_birth.iso8601 == individual["dateOfBirth"]
end

def validate_response_employments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
<% @legal_aid_application.hmrc_responses.each do |hmrc_response| %>
<details>
<summary class="govuk-heading-m govuk-details__summary"
title="HMRC use case <%= hmrc_response.use_case %>"
aria-label="HMRC use case <%= hmrc_response.use_case %> result">
HMRC use case <%= hmrc_response.use_case %>
title="HMRC use case <%= hmrc_response.use_case %> <%= hmrc_response.owner_type %>"
aria-label="HMRC use case <%= hmrc_response.use_case %> <%= hmrc_response.owner_type %> result">
HMRC response - use case <%= hmrc_response.use_case %> for the <%= hmrc_response.owner_type.downcase %>
</summary>
<section>
<% if hmrc_response.response.present? %>
Expand Down
1 change: 1 addition & 0 deletions features/step_definitions/partner_means_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
:provider_assessing_means,
:with_proceedings,
applicant: @applicant,
partner: create(:partner),
provider_step: "client_completed_means",
provider: @registered_provider
bank_account = @applicant.bank_accounts.first
Expand Down
8 changes: 4 additions & 4 deletions spec/services/cfe_civil/components/employments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
end

context "when the applicant is employed but has no PAYE data" do
let(:applicant) { create(:applicant, :employed) }

before do
create(:employment, legal_aid_application:, owner_id: applicant.id, owner_type: "Applicant")
applicant = create(:applicant, :employed)
create(:employment, legal_aid_application:, owner_id: applicant.id, owner_type: applicant.class)
end

it "renders the expected, empty hash" do
Expand All @@ -29,7 +28,8 @@
let(:applicant) { create(:applicant, :employed) }

before do
create(:employment, :example1_usecase1, legal_aid_application:, owner_id: applicant.id, owner_type: "Applicant")
create(:applicant, :employed)
create(:employment, :example1_usecase1, legal_aid_application:, owner_id: applicant.id, owner_type: applicant.class)
end

it "renders the expected JSON" do
Expand Down
26 changes: 25 additions & 1 deletion spec/services/hmrc/create_responses_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
expect { call }.to change { legal_aid_application.hmrc_responses.count }.by(2)
end

it "adds te applicant as owner to each response record created" do
it "adds the applicant as owner to each response record created" do
legal_aid_application.reload.hmrc_responses.each do |response|
expect(response.owner_id).to eq(legal_aid_application.applicant.id)
expect(response.owner_type).to eq(legal_aid_application.applicant.class)
Expand Down Expand Up @@ -69,6 +69,30 @@
end
end

context "when successful and applicant has a partner with an NI number" do
let(:legal_aid_application) { create(:legal_aid_application, :with_applicant_and_partner, :with_transaction_period) }

it "creates four hmrc_response records, one for each use case for each individual" do
expect { call }.to change { legal_aid_application.hmrc_responses.count }.by(4)
expect(legal_aid_application.applicant.hmrc_responses.count).to eq 2
expect(legal_aid_application.partner.hmrc_responses.count).to eq 2
end

it "adds the applicant as owner to each response record created" do
legal_aid_application.applicant.reload.hmrc_responses.each do |response|
expect(response.owner_id).to eq(legal_aid_application.applicant.id)
expect(response.owner_type).to eq(legal_aid_application.applicant.class)
end
end

it "adds the partner as owner to each response record created" do
legal_aid_application.partner.reload.hmrc_responses.each do |response|
expect(response.owner_id).to eq(legal_aid_application.partner.id)
expect(response.owner_type).to eq(legal_aid_application.partner.class)
end
end
end

context "when requests already exist" do
let(:applicant) { legal_aid_application.applicant }
let!(:hmrc_response) { create(:hmrc_response, legal_aid_application:, owner_id: applicant.id, owner_type: applicant.class) }
Expand Down
46 changes: 43 additions & 3 deletions spec/services/hmrc/interface/submission_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
)
end

let(:application) { create(:legal_aid_application, :with_applicant, :with_transaction_period) }
let(:applicant) { application.applicant }
let(:application) { create(:legal_aid_application, :with_applicant_and_partner, :with_transaction_period) }
let(:owner) { application.applicant }
let(:use_case) { "one" }
let(:hmrc_response) { create(:hmrc_response, :use_case_one, legal_aid_application: application, owner_id: applicant.id, owner_type: applicant.class) }
let(:hmrc_response) { create(:hmrc_response, :use_case_one, legal_aid_application: application, owner_id: owner.id, owner_type: owner.class) }

describe ".call" do
subject(:call) { interface.call }
Expand Down Expand Up @@ -92,4 +92,44 @@ def error_response
expect(call.keys).to match_array %i[id _links]
end
end

describe ".request_body" do
subject(:request_body) { interface.request_body }

context "when the owner is an applicant" do
let(:expected_data) do
colinbruce marked this conversation as resolved.
Show resolved Hide resolved
{
filter: {
first_name: owner.first_name,
last_name: owner.last_name,
dob: owner.date_of_birth,
nino: owner.national_insurance_number,
start_date: Time.zone.today - 3.months,
end_date: Time.zone.today,
},
}.to_json
end

it { expect(request_body).to eq(expected_data) }
end

context "when the owner is a partner" do
let(:owner) { application.partner }

let(:expected_data) do
{
filter: {
first_name: owner.first_name,
last_name: owner.last_name,
dob: owner.date_of_birth,
nino: owner.national_insurance_number,
start_date: Time.zone.today - 3.months,
end_date: Time.zone.today,
},
}.to_json
end

it { expect(request_body).to eq(expected_data) }
end
end
end
14 changes: 13 additions & 1 deletion spec/services/hmrc/mock_interface_response_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

let(:applicant) { create(:applicant) }
let(:application) { create(:legal_aid_application, applicant:) }
let(:hmrc_response) { create(:hmrc_response, :use_case_one, legal_aid_application: application, submission_id: guid, owner_id: applicant.id, owner_type: applicant.class) }
let(:owner) { applicant }
let(:hmrc_response) { create(:hmrc_response, :use_case_one, legal_aid_application: application, submission_id: guid, owner_id: owner.id, owner_type: owner.class) }
let(:guid) { SecureRandom.uuid }
let(:hmrc_data) { hmrc_response.response["data"] }
let(:not_found_response) do
Expand Down Expand Up @@ -376,4 +377,15 @@
end
end
end

context "when the mock response owner is set to a partner that is available to the mock response service" do
let(:applicant) { create(:applicant, first_name: "Langley", last_name: "Yorke", national_insurance_number: "MN212451D", date_of_birth: "1992-07-22") }
let(:partner) { create(:partner, first_name: "Ida", last_name: "Paisley", national_insurance_number: "OE726113A", date_of_birth: "1987-11-24") }
let(:application) { create(:legal_aid_application, applicant:, partner:) }
let(:owner) { partner }

it "returns data for the partner, _not_ the applicant" do
expect(hmrc_data[1]["individuals/matching/individual"]["firstName"]).to eq "Ida"
end
end
end
Loading