diff --git a/app/controllers/admin/reporting_controller.rb b/app/controllers/admin/reporting_controller.rb index 245040166..edd9d885c 100644 --- a/app/controllers/admin/reporting_controller.rb +++ b/app/controllers/admin/reporting_controller.rb @@ -10,7 +10,7 @@ def hisps def lifespan @form_lifespans = Submission.select('form_id, count(*) as num_submissions, (max(submissions.created_at) - min(submissions.created_at)) as lifespan').group(:form_id) - @forms = Form.select(:id, :name, :organization_id, :uuid).where('exists (select id, uuid from submissions where submissions.form_id = forms.id)') + @forms = Form.select(:id, :name, :organization_id, :uuid, :short_uuid).where('exists (select id from submissions where submissions.form_id = forms.id)') @orgs = Organization.order(:name) @org_summary = [] @orgs.each do |org| @@ -36,7 +36,7 @@ def lifespan end def no_submissions - @forms = Form.published.select(:id, :name, :organization_id, :uuid).where("not exists (select id, uuid from submissions where submissions.form_id = forms.id and submissions.created_at > current_date - interval '30' day)").order(:organization_id) + @forms = Form.published.select(:id, :name, :organization_id, :uuid, :short_uuid).where("not exists (select id, uuid from submissions where submissions.form_id = forms.id and submissions.created_at > current_date - interval '30' day)").order(:organization_id) @orgs = Organization.order(:name) @org_summary = [] @orgs.each do |org| diff --git a/app/models/form.rb b/app/models/form.rb index 212c9fc35..bcada6323 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -18,15 +18,17 @@ class Form < ApplicationRecord acts_as_taggable_on :tags validates :name, presence: true - validates :disclaimer_text, length: { in: 0..1000, allow_blank: true } + validates :uuid, presence: true + validates :short_uuid, presence: true validates :delivery_method, presence: true + validates :disclaimer_text, length: { in: 0..1000, allow_blank: true } validates :anticipated_delivery_count, numericality: true, allow_nil: true validate :omb_number_with_expiration_date validate :valid_form_kinds validate :target_for_delivery_method validate :ensure_modal_text - before_create :set_uuid + before_validation :set_uuid, on: :create after_create :create_first_form_section before_destroy :ensure_no_responses @@ -124,12 +126,6 @@ def suppress_submit_button (questions.size == 1 && questions.collect(&:question_type).include?('custom_text_display')) end - def self.find_by_short_uuid(short_uuid) - return nil unless short_uuid && short_uuid.length == 8 - - where('uuid LIKE ?', "#{short_uuid}%").first - end - def self.find_by_legacy_touchpoints_id(id) return nil unless id && id.length < 4 @@ -146,10 +142,6 @@ def to_param short_uuid end - def short_uuid - uuid[0..7] - end - def send_notifications? notification_emails.present? end @@ -158,14 +150,6 @@ def create_first_form_section form_sections.create(title: (I18n.t 'form.page_1'), position: 1) end - # def to_param - # short_uuid - # end - - def short_uuid - uuid[0..7] - end - # used to initially set tags (or reset them, if necessary) def set_submission_tags! submission_tags = submissions.collect(&:tags).uniq.sort_by { |i| i.name } @@ -228,19 +212,21 @@ def events def duplicate!(new_user:) new_form = dup + new_form.aasm_state = :created new_form.name = "Copy of #{name}" new_form.title = new_form.name new_form.survey_form_activations = 0 new_form.response_count = 0 new_form.questions_count = 0 new_form.last_response_created_at = nil - new_form.aasm_state = :created new_form.uuid = nil + new_form.short_uuid = nil new_form.legacy_touchpoint_id = nil new_form.legacy_touchpoint_uuid = nil - new_form.template = false + new_form.notification_emails = nil new_form.organization = new_user.organization new_form.legacy_form_embed = false + new_form.template = false new_form.save! # Manually remove the Form Section created with create_first_form_section @@ -286,10 +272,6 @@ def self.archive_expired! end end - def set_uuid - self.uuid = SecureRandom.uuid if uuid.blank? - end - def self.send_inactive_form_emails_since(days_ago) inactive_published_forms = find_inactive_forms_since(days_ago) @@ -764,6 +746,11 @@ def self.forms_whose_retention_period_has_passed private + def set_uuid + self.uuid ||= SecureRandom.uuid + self.short_uuid ||= self.uuid[0..7] + end + def set_submitted_at self.update(submitted_at: Time.current) end diff --git a/db/migrate/20241126184348_form_short_uuid.rb b/db/migrate/20241126184348_form_short_uuid.rb new file mode 100644 index 000000000..53ad2ef44 --- /dev/null +++ b/db/migrate/20241126184348_form_short_uuid.rb @@ -0,0 +1,18 @@ +class FormShortUuid < ActiveRecord::Migration[7.2] + def change + add_column :forms, :short_uuid, :string, limit: 8 + add_index :forms, :short_uuid, unique: true + + remove_index :forms, :uuid + add_index :forms, :uuid, unique: true + + remove_index :submissions, :uuid + add_index :submissions, :uuid, unique: true + + Form.all.each do |form| + form.update({ + short_uuid: form.uuid[0..7] + }) + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 5d3f31495..770d5877f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_11_12_222412) do +ActiveRecord::Schema[7.2].define(version: 2024_11_26_184348) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -309,12 +309,14 @@ t.boolean "legacy_form_embed", default: false t.datetime "archived_at" t.string "audience", default: "public", comment: "indicates whether a form is intended for a public or internal audience" + t.string "short_uuid", limit: 8 t.index ["legacy_touchpoint_id"], name: "index_forms_on_legacy_touchpoint_id" t.index ["legacy_touchpoint_uuid"], name: "index_forms_on_legacy_touchpoint_uuid" t.index ["organization_id"], name: "index_forms_on_organization_id" t.index ["service_id"], name: "index_forms_on_service_id" + t.index ["short_uuid"], name: "index_forms_on_short_uuid", unique: true t.index ["user_id"], name: "index_forms_on_user_id" - t.index ["uuid"], name: "index_forms_on_uuid" + t.index ["uuid"], name: "index_forms_on_uuid", unique: true end create_table "ivn_component_links", force: :cascade do |t| diff --git a/db/seeds.rb b/db/seeds.rb index 43b2759fd..f373094e5 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -613,7 +613,6 @@ def production_suitable_seeds fiscal_year: 2021, quarter: 2, rating: 'TRUE', - likert_or_thumb_question: "", number_of_interactions: 123_456, number_of_people_offered_the_survey: 9_876, likert_or_thumb_question: "thumbs_up_down",