@ -247,12 +247,12 @@ module Imports
apply_date_consistency! ( attributes )
apply_date_consistency! ( attributes )
apply_household_consistency! ( attributes )
apply_household_consistency! ( attributes )
lettings_log = save_lettings_log ( attributes )
lettings_log = save_lettings_log ( attributes , previous_status )
compute_differences ( lettings_log , attributes )
compute_differences ( lettings_log , attributes )
check_status_completed ( lettings_log , previous_status ) unless @logs_overridden . include? ( lettings_log . old_id )
check_status_completed ( lettings_log , previous_status ) unless @logs_overridden . include? ( lettings_log . old_id )
end
end
def save_lettings_log ( attributes )
def save_lettings_log ( attributes , previous_status )
lettings_log = LettingsLog . new ( attributes )
lettings_log = LettingsLog . new ( attributes )
begin
begin
lettings_log . save!
lettings_log . save!
@ -264,21 +264,29 @@ module Imports
record . update! ( attributes )
record . update! ( attributes )
record
record
rescue ActiveRecord :: RecordInvalid = > e
rescue ActiveRecord :: RecordInvalid = > e
rescue_validation_or_raise ( lettings_log , attributes , e )
rescue_validation_or_raise ( lettings_log , attributes , previous_status , e )
end
end
end
end
def rescue_validation_or_raise ( lettings_log , attributes , exception )
def rescue_validation_or_raise ( lettings_log , attributes , previous_status , exception )
if lettings_log . errors . of_kind? ( :referral , :internal_transfer_non_social_housing )
# Blank out all invalid fields for in-progress logs
if %w[ saved submitted-invalid ] . include? ( previous_status )
lettings_log . errors . each do | error |
@logger . warn ( " Log #{ lettings_log . old_id } : Removing field #{ error . attribute } from log triggering validation: #{ error . type } " )
attributes . delete ( error . attribute . to_s )
end
@logs_overridden << lettings_log . old_id
save_lettings_log ( attributes , previous_status )
elsif lettings_log . errors . of_kind? ( :referral , :internal_transfer_non_social_housing )
@logger . warn ( " Log #{ lettings_log . old_id } : Removing internal transfer referral since previous tenancy is a non social housing " )
@logger . warn ( " Log #{ lettings_log . old_id } : Removing internal transfer referral since previous tenancy is a non social housing " )
@logs_overridden << lettings_log . old_id
@logs_overridden << lettings_log . old_id
attributes . delete ( " referral " )
attributes . delete ( " referral " )
save_lettings_log ( attributes )
save_lettings_log ( attributes , previous_status )
elsif lettings_log . errors . of_kind? ( :referral , :internal_transfer_fixed_or_lifetime )
elsif lettings_log . errors . of_kind? ( :referral , :internal_transfer_fixed_or_lifetime )
@logger . warn ( " Log #{ lettings_log . old_id } : Removing internal transfer referral since previous tenancy is fixed terms or lifetime " )
@logger . warn ( " Log #{ lettings_log . old_id } : Removing internal transfer referral since previous tenancy is fixed terms or lifetime " )
@logs_overridden << lettings_log . old_id
@logs_overridden << lettings_log . old_id
attributes . delete ( " referral " )
attributes . delete ( " referral " )
save_lettings_log ( attributes )
save_lettings_log ( attributes , previous_status )
else
else
raise exception
raise exception
end
end