From 41a68eee0ab28baad95b8c4b70129c303f860433 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 25 Mar 2024 11:39:38 +0000 Subject: [PATCH 1/2] CLDC-3341 Update nationality all csv labels (#2328) * Update nationality all csv labels * Update csv tests * Return prefers not to say labels --- .../lettings/questions/nationality_all.rb | 7 ++++ .../form/sales/questions/nationality_all.rb | 7 ++++ ..._csv_export_labels_23_during_24_period.csv | 2 ++ .../lettings_log_csv_export_labels_24.csv | 2 +- ..._csv_export_labels_23_during_24_period.csv | 2 ++ .../files/sales_logs_csv_export_labels_24.csv | 2 +- .../csv/lettings_log_csv_service_spec.rb | 35 +++++++++++++++---- .../csv/sales_log_csv_service_spec.rb | 35 +++++++++++++++---- 8 files changed, 76 insertions(+), 16 deletions(-) create mode 100644 spec/fixtures/files/lettings_log_csv_export_labels_23_during_24_period.csv create mode 100644 spec/fixtures/files/sales_logs_csv_export_labels_23_during_24_period.csv diff --git a/app/models/form/lettings/questions/nationality_all.rb b/app/models/form/lettings/questions/nationality_all.rb index 8d59a37fb..a8324595f 100644 --- a/app/models/form/lettings/questions/nationality_all.rb +++ b/app/models/form/lettings/questions/nationality_all.rb @@ -15,4 +15,11 @@ class Form::Lettings::Questions::NationalityAll < ::Form::Question end QUESTION_NUMBER_FROM_YEAR = { 2023 => 36, 2024 => 35 }.freeze + + def label_from_value(value) + return unless value + return "Tenant prefers not to say" if value.to_i.zero? + + answer_options[value.to_s]["name"] + end end diff --git a/app/models/form/sales/questions/nationality_all.rb b/app/models/form/sales/questions/nationality_all.rb index dd8377830..68ef122bf 100644 --- a/app/models/form/sales/questions/nationality_all.rb +++ b/app/models/form/sales/questions/nationality_all.rb @@ -21,4 +21,11 @@ class Form::Sales::Questions::NationalityAll < ::Form::Question @buyer_index == 1 ? 26 : 34 end end + + def label_from_value(value) + return unless value + return "Buyer prefers not to say" if value.to_i.zero? + + answer_options[value.to_s]["name"] + end end diff --git a/spec/fixtures/files/lettings_log_csv_export_labels_23_during_24_period.csv b/spec/fixtures/files/lettings_log_csv_export_labels_23_during_24_period.csv new file mode 100644 index 000000000..e05d7c0f0 --- /dev/null +++ b/spec/fixtures/files/lettings_log_csv_export_labels_23_during_24_period.csv @@ -0,0 +1,2 @@ +id,status,duplicate_set_id,created_by,is_dpo,created_at,updated_by,updated_at,creation_method,old_id,old_form_id,collection_start_year,address_line1_as_entered,address_line2_as_entered,town_or_city_as_entered,county_as_entered,postcode_full_as_entered,la_as_entered,assigned_to,owning_organisation_name,managing_organisation_name,needstype,lettype,renewal,startdate,renttype,renttype_detail,irproduct,irproduct_other,lar,tenancycode,propcode,declaration,postcode_known,uprn_known,uprn,uprn_confirmed,address_line1_input,postcode_full_input,address_search_value_check,uprn_selection,address_line1,address_line2,town_or_city,county,postcode_full,is_la_inferred,la_label,la,first_time_property_let_as_social_housing,unitletas,rsnvac,newprop,offered,unittype_gn,builtype,wchair,beds,voiddate,vacdays,void_date_value_check,majorrepairs,mrcdate,major_repairs_date_value_check,joint,startertenancy,tenancy,tenancyother,tenancylength,sheltered,hhmemb,pregnancy_value_check,refused,hhtype,totchild,totelder,totadult,age1,retirement_value_check,sex1,ethnic_group,ethnic,national,nationality_all,ecstat1,details_known_2,relat2,partner_under_16_value_check,multiple_partners_value_check,age2,sex2,ecstat2,details_known_3,relat3,age3,sex3,ecstat3,details_known_4,relat4,age4,sex4,ecstat4,details_known_5,relat5,age5,sex5,ecstat5,details_known_6,relat6,age6,sex6,ecstat6,details_known_7,relat7,age7,sex7,ecstat7,details_known_8,relat8,age8,sex8,ecstat8,armedforces,leftreg,reservist,preg_occ,housingneeds,housingneeds_type,housingneeds_a,housingneeds_b,housingneeds_c,housingneeds_f,housingneeds_g,housingneeds_h,housingneeds_other,illness,illness_type_4,illness_type_5,illness_type_2,illness_type_6,illness_type_7,illness_type_3,illness_type_9,illness_type_8,illness_type_1,illness_type_10,layear,waityear,reason,reasonother,reasonother_value_check,prevten,new_old,homeless,ppcodenk,ppostcode_full,previous_la_known,is_previous_la_inferred,prevloc_label,prevloc,reasonpref,rp_homeless,rp_insan_unsat,rp_medwel,rp_hardship,rp_dontknow,cbl,cap,chr,accessible_register,letting_allocation_none,referral,referral_value_check,net_income_known,incref,incfreq,earnings,net_income_value_check,hb,has_benefits,benefits,household_charge,nocharge,period,is_carehome,chcharge,wchchrg,carehome_charges_value_check,brent,wrent,rent_value_check,scharge,wscharge,pscharge,wpschrge,supcharg,wsupchrg,tcharge,wtcharge,scharge_value_check,pscharge_value_check,supcharg_value_check,hbrentshortfall,tshortfall_known,tshortfall,wtshortfall,scheme_code,scheme_service_name,scheme_sensitive,SCHTYPE,scheme_registered_under_care_act,scheme_owning_organisation_name,scheme_primary_client_group,scheme_has_other_client_group,scheme_secondary_client_group,scheme_support_type,scheme_intended_stay,scheme_created_at,location_code,location_postcode,location_name,location_units,location_type_of_unit,location_mobility_type,location_local_authority,location_startdate +,completed,,s.port@jeemayle.com,false,2023-11-26T00:00:00+00:00,,2024-04-01T00:00:00+01:00,single log,,,2023,address line 1 as entered,address line 2 as entered,town or city as entered,county as entered,AB1 2CD,la as entered,,DLUHC,DLUHC,General needs,Affordable rent general needs local authority,No,2023-11-26,Affordable Rent,Affordable Rent,Rent to Buy,,No,HIJKLMN,ABCDEFG,Yes,Yes,No,,,,,,,fake address,,London,,NW9 5LL,No,Barnet,E09000003,No,Affordable rent basis,Tenant abandoned property,No,2,House,Purpose built,Yes,3,2023-11-24,,,Yes,2023-11-25,,Don’t know,Yes,Assured Shorthold Tenancy (AST) – Fixed term,,2,,4,,Yes,4,0,0,2,35,,Female,White,Irish,Tenant prefers not to say,,Other,Yes,Partner,,,32,Male,Not seeking work,No,Prefers not to say,Not known,Prefers not to say,Prefers not to say,Yes,Person prefers not to say,Not known,Person prefers not to say,Person prefers not to say,,,,,,,,,,,,,,,,,,,,,Yes – the person is a current or former regular,No – they left up to and including 5 years ago,Yes,No,Yes,Fully wheelchair accessible housing,Yes,No,No,No,No,No,No,Yes,No,No,Yes,No,No,No,No,No,No,No,Less than 1 year,1 year but under 2 years,Loss of tied accommodation,,,Other supported housing,2,No,Yes,TN23 6LZ,Yes,No,Ashford,E07000105,Yes,,Yes,,,,No,No,Yes,No,,Tenant applied directly (no referral or nomination),,Yes,No,Weekly,268,,Universal Credit housing element,Yes,All,,No,Every 2 weeks,,,,,200.0,100.0,,50.0,25.0,40.0,20.0,35.0,17.5,325.0,162.5,,,,Yes,Yes,12.0,6.0,,,,,,,,,,,,,,,,,,,, diff --git a/spec/fixtures/files/lettings_log_csv_export_labels_24.csv b/spec/fixtures/files/lettings_log_csv_export_labels_24.csv index e05d7c0f0..a4674d290 100644 --- a/spec/fixtures/files/lettings_log_csv_export_labels_24.csv +++ b/spec/fixtures/files/lettings_log_csv_export_labels_24.csv @@ -1,2 +1,2 @@ id,status,duplicate_set_id,created_by,is_dpo,created_at,updated_by,updated_at,creation_method,old_id,old_form_id,collection_start_year,address_line1_as_entered,address_line2_as_entered,town_or_city_as_entered,county_as_entered,postcode_full_as_entered,la_as_entered,assigned_to,owning_organisation_name,managing_organisation_name,needstype,lettype,renewal,startdate,renttype,renttype_detail,irproduct,irproduct_other,lar,tenancycode,propcode,declaration,postcode_known,uprn_known,uprn,uprn_confirmed,address_line1_input,postcode_full_input,address_search_value_check,uprn_selection,address_line1,address_line2,town_or_city,county,postcode_full,is_la_inferred,la_label,la,first_time_property_let_as_social_housing,unitletas,rsnvac,newprop,offered,unittype_gn,builtype,wchair,beds,voiddate,vacdays,void_date_value_check,majorrepairs,mrcdate,major_repairs_date_value_check,joint,startertenancy,tenancy,tenancyother,tenancylength,sheltered,hhmemb,pregnancy_value_check,refused,hhtype,totchild,totelder,totadult,age1,retirement_value_check,sex1,ethnic_group,ethnic,national,nationality_all,ecstat1,details_known_2,relat2,partner_under_16_value_check,multiple_partners_value_check,age2,sex2,ecstat2,details_known_3,relat3,age3,sex3,ecstat3,details_known_4,relat4,age4,sex4,ecstat4,details_known_5,relat5,age5,sex5,ecstat5,details_known_6,relat6,age6,sex6,ecstat6,details_known_7,relat7,age7,sex7,ecstat7,details_known_8,relat8,age8,sex8,ecstat8,armedforces,leftreg,reservist,preg_occ,housingneeds,housingneeds_type,housingneeds_a,housingneeds_b,housingneeds_c,housingneeds_f,housingneeds_g,housingneeds_h,housingneeds_other,illness,illness_type_4,illness_type_5,illness_type_2,illness_type_6,illness_type_7,illness_type_3,illness_type_9,illness_type_8,illness_type_1,illness_type_10,layear,waityear,reason,reasonother,reasonother_value_check,prevten,new_old,homeless,ppcodenk,ppostcode_full,previous_la_known,is_previous_la_inferred,prevloc_label,prevloc,reasonpref,rp_homeless,rp_insan_unsat,rp_medwel,rp_hardship,rp_dontknow,cbl,cap,chr,accessible_register,letting_allocation_none,referral,referral_value_check,net_income_known,incref,incfreq,earnings,net_income_value_check,hb,has_benefits,benefits,household_charge,nocharge,period,is_carehome,chcharge,wchchrg,carehome_charges_value_check,brent,wrent,rent_value_check,scharge,wscharge,pscharge,wpschrge,supcharg,wsupchrg,tcharge,wtcharge,scharge_value_check,pscharge_value_check,supcharg_value_check,hbrentshortfall,tshortfall_known,tshortfall,wtshortfall,scheme_code,scheme_service_name,scheme_sensitive,SCHTYPE,scheme_registered_under_care_act,scheme_owning_organisation_name,scheme_primary_client_group,scheme_has_other_client_group,scheme_secondary_client_group,scheme_support_type,scheme_intended_stay,scheme_created_at,location_code,location_postcode,location_name,location_units,location_type_of_unit,location_mobility_type,location_local_authority,location_startdate -,completed,,s.port@jeemayle.com,false,2023-11-26T00:00:00+00:00,,2024-04-01T00:00:00+01:00,single log,,,2023,address line 1 as entered,address line 2 as entered,town or city as entered,county as entered,AB1 2CD,la as entered,,DLUHC,DLUHC,General needs,Affordable rent general needs local authority,No,2023-11-26,Affordable Rent,Affordable Rent,Rent to Buy,,No,HIJKLMN,ABCDEFG,Yes,Yes,No,,,,,,,fake address,,London,,NW9 5LL,No,Barnet,E09000003,No,Affordable rent basis,Tenant abandoned property,No,2,House,Purpose built,Yes,3,2023-11-24,,,Yes,2023-11-25,,Don’t know,Yes,Assured Shorthold Tenancy (AST) – Fixed term,,2,,4,,Yes,4,0,0,2,35,,Female,White,Irish,Tenant prefers not to say,,Other,Yes,Partner,,,32,Male,Not seeking work,No,Prefers not to say,Not known,Prefers not to say,Prefers not to say,Yes,Person prefers not to say,Not known,Person prefers not to say,Person prefers not to say,,,,,,,,,,,,,,,,,,,,,Yes – the person is a current or former regular,No – they left up to and including 5 years ago,Yes,No,Yes,Fully wheelchair accessible housing,Yes,No,No,No,No,No,No,Yes,No,No,Yes,No,No,No,No,No,No,No,Less than 1 year,1 year but under 2 years,Loss of tied accommodation,,,Other supported housing,2,No,Yes,TN23 6LZ,Yes,No,Ashford,E07000105,Yes,,Yes,,,,No,No,Yes,No,,Tenant applied directly (no referral or nomination),,Yes,No,Weekly,268,,Universal Credit housing element,Yes,All,,No,Every 2 weeks,,,,,200.0,100.0,,50.0,25.0,40.0,20.0,35.0,17.5,325.0,162.5,,,,Yes,Yes,12.0,6.0,,,,,,,,,,,,,,,,,,,, +,in_progress,,s.port@jeemayle.com,false,2024-04-01T00:00:00+01:00,,2024-04-01T00:00:00+01:00,single log,,,2024,address line 1 as entered,address line 2 as entered,town or city as entered,county as entered,AB1 2CD,la as entered,,DLUHC,DLUHC,General needs,Affordable rent general needs local authority,No,2024-04-01,Affordable Rent,Affordable Rent,,,No,HIJKLMN,ABCDEFG,Yes,Yes,No,,,,,,,fake address,,London,,NW9 5LL,No,Barnet,E09000003,No,Affordable rent basis,Tenant abandoned property,No,2,House,Purpose built,Yes,3,2024-03-30,,,Yes,2024-03-31,,Don’t know,Yes,Assured Shorthold Tenancy (AST) – Fixed term,,2,,4,,Yes,4,0,0,2,35,,Female,White,Irish,,Australia,Other,Yes,Partner,,,32,Male,Not seeking work,No,Prefers not to say,Not known,Prefers not to say,Prefers not to say,Yes,Person prefers not to say,Not known,Person prefers not to say,Person prefers not to say,,,,,,,,,,,,,,,,,,,,,Yes – the person is a current or former regular,No – they left up to and including 5 years ago,Yes,No,Yes,Fully wheelchair accessible housing,Yes,No,No,No,No,No,No,Yes,No,No,Yes,No,No,No,No,No,No,No,Less than 1 year,1 year but under 2 years,Loss of tied accommodation,,,Other supported housing,2,No,Yes,TN23 6LZ,Yes,No,Ashford,E07000105,Yes,,Yes,,,,No,No,Yes,No,,Tenant applied directly (no referral or nomination),,Yes,No,Weekly,268,,Universal Credit housing element,Yes,All,,No,Every 2 weeks,,,,,200.0,100.0,,50.0,25.0,40.0,20.0,35.0,17.5,325.0,162.5,,,,Yes,Yes,12.0,6.0,,,,,,,,,,,,,,,,,,,, diff --git a/spec/fixtures/files/sales_logs_csv_export_labels_23_during_24_period.csv b/spec/fixtures/files/sales_logs_csv_export_labels_23_during_24_period.csv new file mode 100644 index 000000000..360e2a49d --- /dev/null +++ b/spec/fixtures/files/sales_logs_csv_export_labels_23_during_24_period.csv @@ -0,0 +1,2 @@ +id,status,duplicate_set_id,created_at,updated_at,old_form_id,collection_start_year,creation_method,is_dpo,address_line1_as_entered,address_line2_as_entered,town_or_city_as_entered,county_as_entered,postcode_full_as_entered,la_as_entered,assigned_to,owning_organisation_name,managing_organisation_name,created_by,day,month,year,purchid,ownershipsch,type,othtype,companybuy,buylivein,jointpur,jointmore,noint,privacynotice,uprn,uprn_confirmed,address_line1_input,postcode_full_input,uprn_selection,address_line1,address_line2,town_or_city,county,pcode1,pcode2,la_known,la,la_label,beds,proptype,builtype,pcodenk,wchair,age1,sex1,ethnic_group,ethnic,national,nationality_all,ecstat1,buy1livein,relat2,age2,sex2,ethnic_group2,ethnicbuy2,nationalbuy2,nationality_all_buyer2,ecstat2,buy2livein,hholdcount,relat3,age3,sex3,ecstat3,relat4,age4,sex4,ecstat4,relat5,age5,sex5,ecstat5,relat6,age6,sex6,ecstat6,prevten,ppcodenk,ppostc1,ppostc2,previous_la_known,prevloc,prevloc_label,pregyrha,pregother,pregla,pregghb,pregblank,buy2living,prevtenbuy2,hhregres,hhregresstill,armedforcesspouse,disabled,wheel,income1nk,income1,inc1mort,income2nk,income2,inc2mort,hb,savingsnk,savings,prevown,prevshared,proplen,staircase,stairbought,stairowned,staircasesale,resale,exday,exmonth,exyear,hoday,homonth,hoyear,lanomagr,soctenant,frombeds,fromprop,socprevten,value,equity,mortgageused,mortgage,mortgagelender,mortgagelenderother,mortlen,extrabor,deposit,cashdis,mrent,has_mscharge,mscharge,discount,grant +,completed,,2023-12-08T00:00:00+00:00,2024-05-01T00:00:00+01:00,,2023,single log,false,address line 1 as entered,address line 2 as entered,town or city as entered,county as entered,AB1 2CD,la as entered,,DLUHC,DLUHC,billyboy@eyeklaud.com,8,12,2023,,Yes - a discounted ownership scheme,Right to Acquire (RTA),,,,Yes,Yes,Yes,1,,,,,,Address line 1,,Town or city,,SW1A,1AA,1,E09000003,Barnet,2,Flat or maisonette,Purpose built,0,Yes,30,Non-binary,Buyer prefers not to say,17,United Kingdom,,Full-time - 30 hours or more,Yes,Partner,35,Non-binary,Buyer prefers not to say,,Buyer prefers not to say,,Full-time - 30 hours or more,Yes,3,Child,14,Non-binary,,Other,Not known,Non-binary,"In government training into work, such as New Deal",Prefers not to say,Not known,Prefers not to say,Prefers not to say,,,,,Local authority tenant,No,,,No,,,1,1,1,1,,Don't know,,Yes,Yes,No,Yes,Yes,Yes,10000,Yes,Yes,10000,Yes,"Don’t know ",No,,Yes,No,10,,,,,,,,,,,,,,,,,110000.0,,Yes,20000.0,Cambridge Building Society,,10,Yes,80000.0,,,Yes,100.0,,10000.0 diff --git a/spec/fixtures/files/sales_logs_csv_export_labels_24.csv b/spec/fixtures/files/sales_logs_csv_export_labels_24.csv index 360e2a49d..5e9659290 100644 --- a/spec/fixtures/files/sales_logs_csv_export_labels_24.csv +++ b/spec/fixtures/files/sales_logs_csv_export_labels_24.csv @@ -1,2 +1,2 @@ id,status,duplicate_set_id,created_at,updated_at,old_form_id,collection_start_year,creation_method,is_dpo,address_line1_as_entered,address_line2_as_entered,town_or_city_as_entered,county_as_entered,postcode_full_as_entered,la_as_entered,assigned_to,owning_organisation_name,managing_organisation_name,created_by,day,month,year,purchid,ownershipsch,type,othtype,companybuy,buylivein,jointpur,jointmore,noint,privacynotice,uprn,uprn_confirmed,address_line1_input,postcode_full_input,uprn_selection,address_line1,address_line2,town_or_city,county,pcode1,pcode2,la_known,la,la_label,beds,proptype,builtype,pcodenk,wchair,age1,sex1,ethnic_group,ethnic,national,nationality_all,ecstat1,buy1livein,relat2,age2,sex2,ethnic_group2,ethnicbuy2,nationalbuy2,nationality_all_buyer2,ecstat2,buy2livein,hholdcount,relat3,age3,sex3,ecstat3,relat4,age4,sex4,ecstat4,relat5,age5,sex5,ecstat5,relat6,age6,sex6,ecstat6,prevten,ppcodenk,ppostc1,ppostc2,previous_la_known,prevloc,prevloc_label,pregyrha,pregother,pregla,pregghb,pregblank,buy2living,prevtenbuy2,hhregres,hhregresstill,armedforcesspouse,disabled,wheel,income1nk,income1,inc1mort,income2nk,income2,inc2mort,hb,savingsnk,savings,prevown,prevshared,proplen,staircase,stairbought,stairowned,staircasesale,resale,exday,exmonth,exyear,hoday,homonth,hoyear,lanomagr,soctenant,frombeds,fromprop,socprevten,value,equity,mortgageused,mortgage,mortgagelender,mortgagelenderother,mortlen,extrabor,deposit,cashdis,mrent,has_mscharge,mscharge,discount,grant -,completed,,2023-12-08T00:00:00+00:00,2024-05-01T00:00:00+01:00,,2023,single log,false,address line 1 as entered,address line 2 as entered,town or city as entered,county as entered,AB1 2CD,la as entered,,DLUHC,DLUHC,billyboy@eyeklaud.com,8,12,2023,,Yes - a discounted ownership scheme,Right to Acquire (RTA),,,,Yes,Yes,Yes,1,,,,,,Address line 1,,Town or city,,SW1A,1AA,1,E09000003,Barnet,2,Flat or maisonette,Purpose built,0,Yes,30,Non-binary,Buyer prefers not to say,17,United Kingdom,,Full-time - 30 hours or more,Yes,Partner,35,Non-binary,Buyer prefers not to say,,Buyer prefers not to say,,Full-time - 30 hours or more,Yes,3,Child,14,Non-binary,,Other,Not known,Non-binary,"In government training into work, such as New Deal",Prefers not to say,Not known,Prefers not to say,Prefers not to say,,,,,Local authority tenant,No,,,No,,,1,1,1,1,,Don't know,,Yes,Yes,No,Yes,Yes,Yes,10000,Yes,Yes,10000,Yes,"Don’t know ",No,,Yes,No,10,,,,,,,,,,,,,,,,,110000.0,,Yes,20000.0,Cambridge Building Society,,10,Yes,80000.0,,,Yes,100.0,,10000.0 +,in_progress,,2024-05-01T00:00:00+01:00,2024-05-01T00:00:00+01:00,,2024,single log,false,address line 1 as entered,address line 2 as entered,town or city as entered,county as entered,AB1 2CD,la as entered,,DLUHC,DLUHC,billyboy@eyeklaud.com,1,5,2024,,Yes - a discounted ownership scheme,Right to Acquire (RTA),,,,Yes,Yes,Yes,1,,,,,,Address line 1,,Town or city,,SW1A,1AA,1,E09000003,Barnet,2,Flat or maisonette,Purpose built,0,Yes,30,Non-binary,Buyer prefers not to say,17,,Australia,Full-time - 30 hours or more,Yes,Partner,35,Non-binary,Buyer prefers not to say,,13,,Full-time - 30 hours or more,Yes,3,Child,14,Non-binary,Child under 16,Other,Not known,Non-binary,"In government training into work, such as New Deal",Prefers not to say,Not known,Prefers not to say,Prefers not to say,,,,,Local authority tenant,Yes,SW1A,1AA,Yes,E09000003,Barnet,1,1,1,1,,Don't know,,Yes,Yes,No,Yes,Yes,Yes,10000,Yes,Yes,10000,Yes,"Don’t know ",No,,Yes,No,10,,,,,,,,,,,,,,,,,110000.0,,Yes,20000.0,Cambridge Building Society,,10,Yes,80000.0,,,Yes,100.0,,10000.0 diff --git a/spec/services/csv/lettings_log_csv_service_spec.rb b/spec/services/csv/lettings_log_csv_service_spec.rb index c0232ef5d..fdd3b5b2c 100644 --- a/spec/services/csv/lettings_log_csv_service_spec.rb +++ b/spec/services/csv/lettings_log_csv_service_spec.rb @@ -172,14 +172,35 @@ RSpec.describe Csv::LettingsLogCsvService do context "when the current form is 2024" do let(:now) { Time.zone.local(2024, 4, 1) } - it "exports the CSV with 2024 ordering and all values correct" do - expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_labels_24.csv") - values_to_delete = %w[id vacdays] - values_to_delete.each do |attribute| - index = csv.first.index(attribute) - csv.second[index] = nil + context "and the log is for 2024 collection period" do + let(:fixed_time) { Time.zone.local(2024, 4, 1) } + + before do + log.update!(national: nil) + log.update!(nationality_all: 36) + end + + it "exports the CSV with 2024 ordering and all values correct" do + expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_labels_24.csv") + values_to_delete = %w[id vacdays] + values_to_delete.each do |attribute| + index = csv.first.index(attribute) + csv.second[index] = nil + end + expect(csv).to eq expected_content + end + end + + context "and the log is for 2023 collection period" do + it "exports the CSV with 2024 ordering and all values correct" do + expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_labels_23_during_24_period.csv") + values_to_delete = %w[id vacdays] + values_to_delete.each do |attribute| + index = csv.first.index(attribute) + csv.second[index] = nil + end + expect(csv).to eq expected_content end - expect(csv).to eq expected_content end end diff --git a/spec/services/csv/sales_log_csv_service_spec.rb b/spec/services/csv/sales_log_csv_service_spec.rb index e34595942..205f81416 100644 --- a/spec/services/csv/sales_log_csv_service_spec.rb +++ b/spec/services/csv/sales_log_csv_service_spec.rb @@ -168,14 +168,35 @@ RSpec.describe Csv::SalesLogCsvService do context "when the current form is 2024" do let(:now) { Time.zone.local(2024, 5, 1) } - it "exports the CSV with the 2024 ordering and all values correct" do - expected_content = CSV.read("spec/fixtures/files/sales_logs_csv_export_labels_24.csv") - values_to_delete = %w[id] - values_to_delete.each do |attribute| - index = csv.first.index(attribute) - csv.second[index] = nil + context "and the log is for 2024 collection period" do + let(:fixed_time) { Time.zone.local(2024, 5, 1) } + + before do + log.update!(national: nil) + log.update!(nationality_all: 36) + end + + it "exports the CSV with the 2024 ordering and all values correct" do + expected_content = CSV.read("spec/fixtures/files/sales_logs_csv_export_labels_24.csv") + values_to_delete = %w[id] + values_to_delete.each do |attribute| + index = csv.first.index(attribute) + csv.second[index] = nil + end + expect(csv).to eq expected_content + end + end + + context "and the log is for 2023 collection period" do + it "exports the CSV with the 2024 ordering and all values correct" do + expected_content = CSV.read("spec/fixtures/files/sales_logs_csv_export_labels_23_during_24_period.csv") + values_to_delete = %w[id] + values_to_delete.each do |attribute| + index = csv.first.index(attribute) + csv.second[index] = nil + end + expect(csv).to eq expected_content end - expect(csv).to eq expected_content end end From 5bbf0148b5a54f04e21e0763e68633362c436c37 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 25 Mar 2024 12:11:34 +0000 Subject: [PATCH 2/2] CLDC-2478 Allow deleting locations (#2285) * Add delete confirmation page * Allow deleting location * Add delete location button and update policy * Add delete button to CYA * Update location policy * Do not display deleted locationa as an option * Move delete button into the button group * Add informative text for locations that have logs * Refactor query * CLDC-2478 Allow deleting scheme (#2286) * Add delete confirmation page * Allow deleting scheme * Add delete scheme button and update policy * Add delete button to CYA * Update scheme policy * Do not display deleted schemes as an option * Add informative text for schemesrbe that have logs * Refactor query * Update tests * Add feature toggle --- app/controllers/locations_controller.rb | 9 +- app/controllers/organisations_controller.rb | 2 +- app/controllers/schemes_controller.rb | 11 +- app/helpers/locations_helper.rb | 4 + app/helpers/schemes_helper.rb | 4 + .../form/lettings/questions/location_id.rb | 4 +- .../form/lettings/questions/scheme_id.rb | 10 +- app/models/location.rb | 7 + app/models/scheme.rb | 10 +- app/policies/location_policy.rb | 17 ++ app/policies/scheme_policy.rb | 17 ++ app/services/feature_toggle.rb | 8 + app/views/locations/check_answers.html.erb | 3 + .../locations/delete_confirmation.html.erb | 24 ++ app/views/locations/show.html.erb | 11 +- app/views/schemes/check_answers.html.erb | 4 + .../schemes/delete_confirmation.html.erb | 24 ++ app/views/schemes/show.html.erb | 7 + config/locales/en.yml | 2 + config/routes.rb | 4 + .../20240301125651_add_discarded_at_column.rb | 5 + ...0017_add_discarded_at_column_to_schemes.rb | 5 + db/schema.rb | 2 + .../lettings/questions/location_id_spec.rb | 12 + .../form/lettings/questions/scheme_id_spec.rb | 13 + spec/models/scheme_spec.rb | 8 + spec/policies/location_policy_spec.rb | 92 ++++++ spec/policies/scheme_policy_spec.rb | 94 ++++++ spec/requests/locations_controller_spec.rb | 270 +++++++++++++++++ .../requests/organisations_controller_spec.rb | 5 + spec/requests/schemes_controller_spec.rb | 286 ++++++++++++++++++ spec/views/locations/show.html.erb_spec.rb | 2 + 32 files changed, 961 insertions(+), 15 deletions(-) create mode 100644 app/views/locations/delete_confirmation.html.erb create mode 100644 app/views/schemes/delete_confirmation.html.erb create mode 100644 db/migrate/20240301125651_add_discarded_at_column.rb create mode 100644 db/migrate/20240304100017_add_discarded_at_column_to_schemes.rb create mode 100644 spec/policies/location_policy_spec.rb create mode 100644 spec/policies/scheme_policy_spec.rb diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index b84185b51..2b75da1a0 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -14,8 +14,8 @@ class LocationsController < ApplicationController def index authorize @scheme - @pagy, @locations = pagy(filter_manager.filtered_locations(@scheme.locations, search_term, session_filters)) - @total_count = @scheme.locations.size + @pagy, @locations = pagy(filter_manager.filtered_locations(@scheme.locations.visible, search_term, session_filters)) + @total_count = @scheme.locations.visible.size @searched = search_term.presence @filter_type = "scheme_locations" end @@ -230,6 +230,11 @@ class LocationsController < ApplicationController end end + def delete + @location.discard! + redirect_to scheme_locations_path(@scheme), notice: I18n.t("notification.location_deleted", postcode: @location.postcode) + end + private def authorize_user diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index 4612e6bc9..dc5a36c9b 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -21,7 +21,7 @@ class OrganisationsController < ApplicationController end def schemes - organisation_schemes = Scheme.where(owning_organisation: [@organisation] + @organisation.parent_organisations) + organisation_schemes = Scheme.visible.where(owning_organisation: [@organisation] + @organisation.parent_organisations) @pagy, @schemes = pagy(filter_manager.filtered_schemes(organisation_schemes, search_term, session_filters)) @searched = search_term.presence diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 5eec2a294..cfc6fe8a0 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -13,11 +13,11 @@ class SchemesController < ApplicationController def index redirect_to schemes_organisation_path(current_user.organisation) unless current_user.support? - all_schemes = Scheme.all + all_visible_schemes = Scheme.visible - @pagy, @schemes = pagy(filter_manager.filtered_schemes(all_schemes, search_term, session_filters)) + @pagy, @schemes = pagy(filter_manager.filtered_schemes(all_visible_schemes, search_term, session_filters)) @searched = search_term.presence - @total_count = all_schemes.size + @total_count = all_visible_schemes.size @filter_type = "schemes" end @@ -223,6 +223,11 @@ class SchemesController < ApplicationController def csv_confirmation; end + def delete + @scheme.discard! + redirect_to schemes_organisation_path(@scheme.owning_organisation), notice: I18n.t("notification.scheme_deleted", service_name: @scheme.service_name) + end + private def authorize_user diff --git a/app/helpers/locations_helper.rb b/app/helpers/locations_helper.rb index f718e9576..f963c7040 100644 --- a/app/helpers/locations_helper.rb +++ b/app/helpers/locations_helper.rb @@ -73,6 +73,10 @@ module LocationsHelper return govuk_button_link_to "Reactivate this location", scheme_location_new_reactivation_path(location.scheme, location) if location.deactivated? end + def delete_location_link(location) + govuk_button_link_to "Delete this location", scheme_location_delete_confirmation_path(location.scheme, location), warning: true + end + def location_creation_success_notice(location) if location.confirmed "#{location.postcode} #{location.startdate.blank? || location.startdate.before?(Time.zone.now) ? 'has been' : 'will be'} added to this scheme" diff --git a/app/helpers/schemes_helper.rb b/app/helpers/schemes_helper.rb index 078419619..1cc89d3d5 100644 --- a/app/helpers/schemes_helper.rb +++ b/app/helpers/schemes_helper.rb @@ -15,6 +15,10 @@ module SchemesHelper return govuk_button_link_to "Reactivate this scheme", scheme_new_reactivation_path(scheme) if scheme.deactivated? end + def delete_scheme_link(scheme) + govuk_button_link_to "Delete this scheme", scheme_delete_confirmation_path(scheme), warning: true + end + def owning_organisation_options(current_user) all_orgs = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } user_org = [OpenStruct.new(id: current_user.organisation_id, name: current_user.organisation.name)] diff --git a/app/models/form/lettings/questions/location_id.rb b/app/models/form/lettings/questions/location_id.rb index cbc9b840a..d79bcab76 100644 --- a/app/models/form/lettings/questions/location_id.rb +++ b/app/models/form/lettings/questions/location_id.rb @@ -20,7 +20,7 @@ class Form::Lettings::Questions::LocationId < ::Form::Question answer_opts = {} return answer_opts unless ActiveRecord::Base.connected? - Location.started_in_2_weeks.select(:id, :postcode, :name).each_with_object(answer_opts) do |location, hsh| + Location.visible.started_in_2_weeks.select(:id, :postcode, :name).each_with_object(answer_opts) do |location, hsh| hsh[location.id.to_s] = { "value" => location.postcode, "hint" => location.name } hsh end @@ -29,7 +29,7 @@ class Form::Lettings::Questions::LocationId < ::Form::Question def displayed_answer_options(lettings_log, _user = nil) return {} unless lettings_log.scheme - scheme_location_ids = lettings_log.scheme.locations.confirmed.pluck(:id) + scheme_location_ids = lettings_log.scheme.locations.visible.confirmed.pluck(:id) answer_options.select { |k, _v| scheme_location_ids.include?(k.to_i) } end diff --git a/app/models/form/lettings/questions/scheme_id.rb b/app/models/form/lettings/questions/scheme_id.rb index 4c533f43a..ef7904cc8 100644 --- a/app/models/form/lettings/questions/scheme_id.rb +++ b/app/models/form/lettings/questions/scheme_id.rb @@ -19,8 +19,8 @@ class Form::Lettings::Questions::SchemeId < ::Form::Question answer_opts = { "" => "Select an option" } return answer_opts unless ActiveRecord::Base.connected? - Scheme.select(:id, :service_name, :primary_client_group, - :secondary_client_group).each_with_object(answer_opts) do |scheme, hsh| + Scheme.visible.select(:id, :service_name, :primary_client_group, + :secondary_client_group).each_with_object(answer_opts) do |scheme, hsh| hsh[scheme.id.to_s] = scheme hsh end @@ -29,10 +29,10 @@ class Form::Lettings::Questions::SchemeId < ::Form::Question def displayed_answer_options(lettings_log, _user = nil) organisation = lettings_log.owning_organisation || lettings_log.created_by&.organisation schemes = if organisation - Scheme.includes(:locations).select(:id).where(owning_organisation_id: organisation.id, - confirmed: true) + Scheme.visible.includes(:locations).select(:id).where(owning_organisation_id: organisation.id, + confirmed: true) else - Scheme.includes(:locations).select(:id).where(confirmed: true) + Scheme.visible.includes(:locations).select(:id).where(confirmed: true) end filtered_scheme_ids = schemes.joins(:locations).merge(Location.started_in_2_weeks).map(&:id) answer_options.select do |k, _v| diff --git a/app/models/location.rb b/app/models/location.rb index bfe9f77a4..43285bfbb 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -79,6 +79,8 @@ class Location < ApplicationRecord .where.not(id: activating_soon.pluck(:id)) } + scope :visible, -> { where(discarded_at: nil) } + LOCAL_AUTHORITIES = LocalAuthority.all.map { |la| [la.name, la.code] }.to_h enum local_authorities: LOCAL_AUTHORITIES @@ -138,6 +140,7 @@ class Location < ApplicationRecord end def status_at(date) + return :deleted if discarded_at.present? return :incomplete unless confirmed return :deactivated if open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date @@ -190,6 +193,10 @@ class Location < ApplicationRecord LocalAuthority.where(id: [la.id] + la.linked_local_authority_ids) end + def discard! + update!(discarded_at: Time.zone.now) + end + private PIO = PostcodeService.new diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 0bfd56373..2518293a0 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -77,6 +77,8 @@ class Scheme < ApplicationRecord .where.not(id: activating_soon.pluck(:id)) } + scope :visible, -> { where(discarded_at: nil) } + validate :validate_confirmed validate :validate_owning_organisation @@ -229,7 +231,7 @@ class Scheme < ApplicationRecord end def validate_confirmed - required_attributes = attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units deactivation_date deactivation_date_type startdate] + required_attributes = attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units deactivation_date deactivation_date_type startdate discarded_at] if confirmed == true required_attributes.any? do |attribute| @@ -264,6 +266,7 @@ class Scheme < ApplicationRecord end def status_at(date) + return :deleted if discarded_at.present? return :incomplete unless confirmed && locations.confirmed.any? return :deactivated if open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date @@ -288,4 +291,9 @@ class Scheme < ApplicationRecord def deactivates_in_a_long_time? status_at(6.months.from_now) == :deactivating_soon end + + def discard! + update!(discarded_at: Time.zone.now) + locations.each(&:discard!) + end end diff --git a/app/policies/location_policy.rb b/app/policies/location_policy.rb index 5d6d6d467..436b961c6 100644 --- a/app/policies/location_policy.rb +++ b/app/policies/location_policy.rb @@ -26,6 +26,17 @@ class LocationPolicy user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner end + def delete_confirmation? + delete? + end + + def delete? + return false unless user.support? + return false unless location.status == :incomplete || location.status == :deactivated + + !has_any_logs_in_editable_collection_period + end + %w[ update_postcode? update_local_authority? @@ -75,4 +86,10 @@ private def scheme_owned_by_user_org_or_stock_owner scheme&.owning_organisation == user.organisation || user.organisation.stock_owners.exists?(scheme&.owning_organisation_id) end + + def has_any_logs_in_editable_collection_period + editable_from_date = FormHandler.instance.earliest_open_for_editing_collection_start_date + + LettingsLog.where(location_id: location.id).after_date(editable_from_date).or(LettingsLog.where(startdate: nil, location_id: location.id)).any? + end end diff --git a/app/policies/scheme_policy.rb b/app/policies/scheme_policy.rb index f2710eb06..6b97a46de 100644 --- a/app/policies/scheme_policy.rb +++ b/app/policies/scheme_policy.rb @@ -65,9 +65,26 @@ class SchemePolicy end end + def delete_confirmation? + delete? + end + + def delete? + return false unless user.support? + return false unless scheme.status == :incomplete || scheme.status == :deactivated + + !has_any_logs_in_editable_collection_period + end + private def scheme_owned_by_user_org_or_stock_owner scheme&.owning_organisation == user.organisation || user.organisation.stock_owners.exists?(scheme&.owning_organisation_id) end + + def has_any_logs_in_editable_collection_period + editable_from_date = FormHandler.instance.earliest_open_for_editing_collection_start_date + + LettingsLog.where(scheme_id: scheme.id).after_date(editable_from_date).or(LettingsLog.where(startdate: nil, scheme_id: scheme.id)).any? + end end diff --git a/app/services/feature_toggle.rb b/app/services/feature_toggle.rb index 22d43f513..f145b7132 100644 --- a/app/services/feature_toggle.rb +++ b/app/services/feature_toggle.rb @@ -26,4 +26,12 @@ class FeatureToggle def self.service_moved? false end + + def self.delete_scheme_enabled? + !Rails.env.production? + end + + def self.delete_location_enabled? + !Rails.env.production? + end end diff --git a/app/views/locations/check_answers.html.erb b/app/views/locations/check_answers.html.erb index 63a48d4d5..8cd8bde2a 100644 --- a/app/views/locations/check_answers.html.erb +++ b/app/views/locations/check_answers.html.erb @@ -42,6 +42,9 @@ <% if LocationPolicy.new(current_user, @location).create? %>
<% end %> diff --git a/app/views/locations/delete_confirmation.html.erb b/app/views/locations/delete_confirmation.html.erb new file mode 100644 index 000000000..0eea3adc8 --- /dev/null +++ b/app/views/locations/delete_confirmation.html.erb @@ -0,0 +1,24 @@ +<% content_for :before_content do %> + <% content_for :title, "Are you sure you want to delete this location?" %> + <%= govuk_back_link(href: :back) %> +<% end %> + +