Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix user archival #434

Merged
merged 7 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading