Merge pull request #6573 from tchak/fix-purge-stale-transfers
fix(transfer): manually nullify staled transfers references
This commit is contained in:
commit
422265886a
4 changed files with 46 additions and 10 deletions
|
@ -16,7 +16,7 @@ module Users
|
|||
.joins(:dossiers)
|
||||
.find_by!(id: params[:id], dossiers: { user: current_user })
|
||||
|
||||
transfer.destroy
|
||||
transfer.destroy_and_nullify
|
||||
redirect_to dossiers_path
|
||||
end
|
||||
|
||||
|
|
|
@ -2,6 +2,6 @@ class Cron::PurgeStaleTransfersJob < Cron::CronJob
|
|||
self.schedule_expression = "every day at midnight"
|
||||
|
||||
def perform
|
||||
DossierTransfer.stale.destroy_all
|
||||
DossierTransfer.destroy_stale
|
||||
end
|
||||
end
|
||||
|
|
|
@ -45,6 +45,22 @@ class DossierTransfer < ApplicationRecord
|
|||
User.find_by(email: email)&.locale || I18n.default_locale
|
||||
end
|
||||
|
||||
def destroy_and_nullify
|
||||
transaction do
|
||||
# Rails cascading is not working with default scopes. Doing nullify cascade manually.
|
||||
Dossier.with_discarded.where(transfer: self).update_all(dossier_transfer_id: nil)
|
||||
destroy
|
||||
end
|
||||
end
|
||||
|
||||
def self.destroy_stale
|
||||
transaction do
|
||||
# Rails cascading is not working with default scopes. Doing nullify cascade manually.
|
||||
Dossier.with_discarded.where(transfer: stale).update_all(dossier_transfer_id: nil)
|
||||
stale.destroy_all
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def send_notification
|
||||
|
|
|
@ -46,16 +46,36 @@ RSpec.describe DossierTransfer, type: :model do
|
|||
it { expect(DossierTransfer.with_dossiers.count).to eq(0) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'dossier relationship' do
|
||||
let(:transfer) { create(:dossier_transfer) }
|
||||
let(:dossier) { create(:dossier, user: user, transfer: transfer) }
|
||||
describe '#destroy_and_nullify' do
|
||||
let(:transfer) { create(:dossier_transfer) }
|
||||
let(:dossier) { create(:dossier, user: user, transfer: transfer) }
|
||||
let(:discarded_dossier) { create(:dossier, user: user, transfer: dossier.transfer) }
|
||||
|
||||
it 'nullify transfer relationship on dossier' do
|
||||
expect(dossier.transfer).to eq(transfer)
|
||||
transfer.destroy
|
||||
expect(dossier.reload.transfer).to be_nil
|
||||
end
|
||||
before do
|
||||
discarded_dossier.discard!
|
||||
end
|
||||
|
||||
it 'nullify transfer relationship on dossier' do
|
||||
expect(dossier.transfer).to eq(transfer)
|
||||
transfer.destroy_and_nullify
|
||||
expect(dossier.reload.transfer).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe '#destroy_stale' do
|
||||
let(:transfer) { create(:dossier_transfer, created_at: 1.month.ago) }
|
||||
let(:dossier) { create(:dossier, user: user, transfer: transfer) }
|
||||
let(:discarded_dossier) { create(:dossier, user: user, transfer: dossier.transfer) }
|
||||
|
||||
before do
|
||||
discarded_dossier.discard!
|
||||
end
|
||||
|
||||
it 'nullify the transfer on discarded dossier' do
|
||||
DossierTransfer.destroy_stale
|
||||
expect(DossierTransfer.count).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue