Skip to content

Commit

Permalink
Fixes to lower tier registration (#769)
Browse files Browse the repository at this point in the history
From: https://eaflood.atlassian.net/browse/RUBY-838

This fixes various issue on the lower tier registration created during the new journey.
 - The route was not set when completing the registration, fixed in the state machine
 - The registration finance details were not set. Since the old system generates a fake worldpay payment for an order of 0 balance and no items, we reproduced the same here in the completion job
 - Make sure we don't set an expire date on LT registration
  • Loading branch information
cintamani committed Mar 31, 2020
1 parent 7e200c9 commit cb17e60
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ module CanChangeRegistrationStatus

# Transition effects
def set_expiry_date
return if registration.lower_tier?

registration.update_attributes(expires_on: Rails.configuration.expires_after.years.from_now)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ module CanUseNewRegistrationWorkflow

transitions from: :declaration_form,
to: :registration_completed_form,
if: :lower_tier?
if: :lower_tier?,
# TODO: This don't get triggered if in the `success`
# callback block, hence we went for `after`
after: :set_metadata_route

transitions from: :declaration_form,
to: :cards_form
Expand Down
2 changes: 1 addition & 1 deletion app/models/waste_carriers_engine/new_registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class NewRegistration < TransientRegistration

after_initialize :build_meta_data

def prepare_for_payment(mode, _user)
def prepare_for_payment(mode, _user = nil)
BuildNewRegistrationFinanceDetailsService.run(
transient_registration: self,
payment_method: mode
Expand Down
2 changes: 2 additions & 0 deletions app/models/waste_carriers_engine/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def update_after_worldpay(status)
private

def generate_description
return unless order_items.any?

description = order_items.map(&:description)
.join(", plus ")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def new_registration_order(payment_method)

order.order_items = []

order.order_items << OrderItem.new_registration_item
order.order_items << OrderItem.new_registration_item if transient_registration.upper_tier?

if transient_registration.temp_cards.positive?
if transient_registration.temp_cards&.positive?
order.order_items << OrderItem.new_copy_cards_item(transient_registration.temp_cards)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ def run(transient_registration)
@transient_registration = transient_registration

transient_registration.with_lock do
prepare_finance_details_for_lower_tier

copy_names_to_contact_address
copy_data_from_transient_registration

set_reg_identifier
set_expiry_date
set_expiry_date if registration.upper_tier?

update_meta_data

Expand Down Expand Up @@ -43,6 +45,13 @@ def copy_names_to_contact_address
transient_registration.contact_address.last_name = transient_registration.last_name
end

def prepare_finance_details_for_lower_tier
return if transient_registration.upper_tier?

transient_registration.prepare_for_payment(:worldpay)
transient_registration.reload
end

def update_meta_data
registration.metaData.route = transient_registration.metaData.route
registration.metaData.date_registered = Time.current
Expand Down Expand Up @@ -70,6 +79,9 @@ def set_reg_identifier
end

def copy_data_from_transient_registration
# Make sure data are loaded into attributes if setted on this instance
transient_registration.reload

new_attributes = transient_registration.attributes.except(
"_id",
"reg_identifier",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
module WasteCarriersEngine
RSpec.describe BuildNewRegistrationFinanceDetailsService do
describe ".run" do
let(:transient_registration) { double(:transient_registration, contact_email: "user@example.com", temp_cards: 2) }
let(:transient_registration) do
double(
:transient_registration,
contact_email: "user@example.com",
temp_cards: 2,
upper_tier?: true
)
end
let(:order) { double(:order, order_items: []) }

before do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ module WasteCarriersEngine
expect(new_registration_scope.any?).to be_falsey
end

context "when the registration is a lower tier registration" do
let(:transient_registration) do
create(
:new_registration,
:has_required_lower_tier_data
)
end

it "activates the registration, set up finance details and does not set an expire date" do
registration = described_class.run(transient_registration)

expect(registration.expires_on).to be_nil
expect(registration).to be_active
expect(registration.finance_details).to be_present
expect(registration.finance_details.orders.count).to eq(1)
expect(registration.finance_details.balance).to eq(0)
end
end

context "when the balance have been cleared and there are no pending convictions checks" do
let(:finance_details) { build(:finance_details, :has_paid_order_and_payment) }

Expand Down

0 comments on commit cb17e60

Please sign in to comment.