Skip to content

Commit

Permalink
Fix user archival (#434)
Browse files Browse the repository at this point in the history
* Fix user archival

* Fix method call on nil

* Fix null error

* Fix validation

* Fix user nil

* Fix lint again

* Disable lint on line
  • Loading branch information
wilco375 authored Oct 11, 2024
1 parent 8664c66 commit 239a947
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
13 changes: 12 additions & 1 deletion app/jobs/user_archive_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ def migrate_keep_entities(user)
key = entity_key(entity)
records = entity.where({ key => user })

records.each { |r| r.update({ key => global_archive_user }) && r.versions.destroy_all }
migrate_keep_entity_records(key, records)
end
end

def migrate_keep_entity_records(key, records)
records.each do |r|
unless r.update({ key => global_archive_user })
raise ActiveRecord::RecordInvalid.new(r),
"Failed to update #{r.class} record (ID: #{r.id})"
end

r.versions.destroy_all
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/form/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Response < ApplicationRecord
has_many :closed_question_answers, dependent: :destroy
has_many :closed_questions, through: :closed_question_answers, source: :question

validates :user, uniqueness: { scope: :form }
validates :user, uniqueness: { scope: :form, unless: -> { user_id == 0 } } # rubocop:disable Style/NumericPredicate
validate :form_allows_responses?

after_create :update_completed_status!
Expand Down
16 changes: 16 additions & 0 deletions spec/models/form/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@
it { expect(another_response).not_to be_valid }
end

context 'when the user is archived' do
let(:archived_user) { create(:user, id: 0) }
let(:another_response) do
build(:response, form: form, user: archived_user)
end
let(:form) { create(:form) }

before do
create(:response, form: form, user: archived_user)
end

it 'allows multiple responses' do
expect(another_response).to be_valid
end
end

context 'when form is not published' do
before do
response.form.update(respond_from: 2.days.ago, respond_until: Date.yesterday)
Expand Down

0 comments on commit 239a947

Please sign in to comment.