From a4111f851dbdb6c766d356385e13b7e83e230771 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Thu, 7 Sep 2023 12:52:38 +0100 Subject: [PATCH] Stop duplicate log entries when closing requests. The been_published check joins to the event table, so this call was e.g. returning one row per event. Fixes #4776. --- app/models/info_request.rb | 26 ++++++++++++++------------ spec/models/info_request_spec.rb | 5 ++++- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 73b3b1039e..29fae8c88a 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -542,12 +542,13 @@ def self.requests_very_old_after_months def self.stop_new_responses_on_old_requests # 'old' months since last change to request, only allow new incoming # messages from authority domains - InfoRequest - .been_published - .where(allow_new_responses_from: 'anybody') - .where.not(url_title: 'holding_pen') - .updated_before(requests_old_after_months.months.ago.to_date) - .find_in_batches do |batch| + InfoRequest. + been_published. + where(allow_new_responses_from: 'anybody'). + where.not(url_title: 'holding_pen'). + updated_before(requests_old_after_months.months.ago.to_date). + distinct. + find_in_batches do |batch| batch.each do |info_request| old_allow_new_responses_from = info_request.allow_new_responses_from @@ -565,12 +566,13 @@ def self.stop_new_responses_on_old_requests # 'very_old' months since last change to request, don't allow any new # incoming messages - InfoRequest - .been_published - .where(allow_new_responses_from: %w[anybody authority_only]) - .where.not(url_title: 'holding_pen') - .updated_before(requests_very_old_after_months.months.ago.to_date) - .find_in_batches do |batch| + InfoRequest. + been_published. + where(allow_new_responses_from: %w[anybody authority_only]). + where.not(url_title: 'holding_pen'). + updated_before(requests_very_old_after_months.months.ago.to_date). + distinct. + find_in_batches do |batch| batch.each do |info_request| old_allow_new_responses_from = info_request.allow_new_responses_from diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index 5ae655fafd..9a0eba95b3 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -431,8 +431,11 @@ it 'logs an event after changing new responses to authority_only' do request.update(updated_at: 6.months.ago - 1.day) + request.log_event('edit', {}) subject - last_event = request.reload.last_event + request.reload + last_event = request.last_event + expect(request.info_request_events.size).to eq(3) expect(last_event.event_type).to eq('edit') expect(last_event.params). to match(old_allow_new_responses_from: 'anybody',