Browse Source
* Update nocharge assignment logic for clarity in lettings_log_variables * Add Rake tasks to update nocharge with household_charge for current and older logs * Update nocharge field to be empty in general needs log XML files * Update nocharge assignment to skip validation during save and add RSpec tests for bulk update task * Remove nocharges from general needs testsmain
19 changed files with 126 additions and 17 deletions
@ -0,0 +1,56 @@
|
||||
namespace :bulk_update do |
||||
desc "Update nocharge with household_charge for current logs" |
||||
task update_current_logs_nocharges: :environment do |
||||
updated_logs_count = 0 |
||||
status_changed_logs = [] |
||||
|
||||
logs = LettingsLog.where(startdate: Time.zone.local(2024, 4, 1)...Time.zone.local(2026, 4, 1)).where.not("nocharge = household_charge") |
||||
puts "Updating logs with startdate between 2024-04-01 and 2026-04-01" |
||||
puts "Total logs to update: #{logs.count}" |
||||
|
||||
logs.find_each do |log| |
||||
status_pre_change = log.status |
||||
|
||||
log.nocharge = log.household_charge |
||||
if log.save(validate: false) |
||||
updated_logs_count += 1 |
||||
Rails.logger.info "Updated nocharge for log #{log.id}" |
||||
else |
||||
Rails.logger.error "Failed to update log #{log.id}: #{log.errors.full_messages.join(', ')}" |
||||
end |
||||
|
||||
status_post_change = log.status |
||||
status_changed_logs << log.id if status_pre_change != status_post_change |
||||
end |
||||
|
||||
puts "#{updated_logs_count} logs were updated." |
||||
puts "Logs with changed status: [#{status_changed_logs.join(', ')}]" |
||||
end |
||||
|
||||
desc "Update nocharge with household_charge for older logs" |
||||
task update_older_logs_nocharges: :environment do |
||||
updated_logs_count = 0 |
||||
status_changed_logs = [] |
||||
|
||||
logs = LettingsLog.where("startdate < ?", Time.zone.local(2024, 4, 1)).where.not("nocharge = household_charge") |
||||
|
||||
logs.find_each do |log| |
||||
status_pre_change = log.status |
||||
|
||||
log.skip_update_status = true |
||||
log.nocharge = log.household_charge |
||||
if log.save |
||||
updated_logs_count += 1 |
||||
Rails.logger.info "Updated nocharge for log #{log.id}" |
||||
else |
||||
Rails.logger.error "Failed to update log #{log.id}: #{log.errors.full_messages.join(', ')}" |
||||
end |
||||
|
||||
status_post_change = log.status |
||||
status_changed_logs << log.id if status_pre_change != status_post_change |
||||
end |
||||
|
||||
puts "#{updated_logs_count} logs were updated." |
||||
puts "Logs with changed status: [#{status_changed_logs.join(', ')}]" |
||||
end |
||||
end |
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,53 @@
|
||||
require "rails_helper" |
||||
require "rake" |
||||
|
||||
RSpec.describe "recalculate_nocharges", type: :task do |
||||
before do |
||||
Rake.application.rake_require("tasks/recalculate_nocharges") |
||||
Rake::Task.define_task(:environment) |
||||
task.reenable |
||||
end |
||||
|
||||
describe "bulk_update:update_current_logs_nocharges" do |
||||
let(:task) { Rake::Task["bulk_update:update_current_logs_nocharges"] } |
||||
let(:organisation) { FactoryBot.create(:organisation, name: "MHCLG", provider_type: "LA", housing_registration_no: 1234) } |
||||
let(:user) { FactoryBot.create(:user, organisation:, email: "fake@email.com") } |
||||
let(:scheme) { FactoryBot.create(:scheme, :export, owning_organisation: organisation) } |
||||
let(:location) { FactoryBot.create(:location, :export, scheme:, startdate: Time.zone.local(2021, 4, 1), old_id: "1a") } |
||||
|
||||
let(:log_to_update) do |
||||
build(:lettings_log, :completed, :sh, needstype: 2, scheme:, location:, assigned_to: user, startdate: Time.zone.local(2024, 6, 1), age1: 35, sex1: "F", age2: 32, sex2: "M", underoccupation_benefitcap: 4, sheltered: 1, household_charge: 0, nocharge: 1) |
||||
end |
||||
|
||||
let(:log_not_to_update) do |
||||
build(:lettings_log, :completed, needstype: 2, household_charge: 1, nocharge: 1) |
||||
end |
||||
|
||||
context "when running the task" do |
||||
before do |
||||
log_to_update.save!(validate: false) |
||||
log_not_to_update.save!(validate: false) |
||||
end |
||||
|
||||
it "updates logs where household_charge and nocharge are different" do |
||||
expect(log_to_update.nocharge).to eq(1) |
||||
expect(log_to_update.household_charge).to eq(0) |
||||
|
||||
task.invoke |
||||
|
||||
log_to_update.reload |
||||
expect(log_to_update.nocharge).to eq(0) |
||||
end |
||||
|
||||
it "does not update logs where household_charge and nocharge are the same" do |
||||
expect(log_not_to_update.nocharge).to eq(1) |
||||
expect(log_not_to_update.household_charge).to eq(1) |
||||
|
||||
task.invoke |
||||
|
||||
log_not_to_update.reload |
||||
expect(log_not_to_update.nocharge).to eq(1) |
||||
end |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue