Skip to content
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
117 changes: 0 additions & 117 deletions lib/tasks/forms.rake
Original file line number Diff line number Diff line change
Expand Up @@ -128,123 +128,6 @@ namespace :forms do
end
end

desc "Updates form documents to add value to all selection options"
task add_value_to_selection_options: :environment do
# find all form documents where any of the steps have an answer_type of selection
form_documents_with_selection_steps = FormDocument.where("jsonb_path_exists(content, '$.steps[*] ? (@.data.answer_type == \"selection\")')")
Rails.logger.info "data_migrations:add_value_to_selection_options will update #{form_documents_with_selection_steps.count} form_documents"

form_documents_with_selection_steps.find_each do |form_document|
form_document.content["steps"].each do |step|
next unless step["data"]["answer_type"] == "selection"

step["data"]["answer_settings"]["selection_options"].each do |option|
option["value"] = option["name"]
end
end

begin
form_document.save!
rescue StandardError => e
Rails.logger.info "data_migrations:add_value_to_selection_options Failed to update form #{form_document.id}: #{e.message}"
end
end

Page.where(answer_type: "selection").find_each do |page|
page.answer_settings["selection_options"].each do |option|
option["value"] = option["name"]
end

begin
page.save!
rescue StandardError => e
Rails.logger.info "data_migrations:add_value_to_selection_options Failed to update page #{page.id}: #{e.message}"
end
end

DraftQuestion.where(answer_type: "selection").find_each do |question|
new_selection_options = question.answer_settings[:selection_options].each do |option|
option[:value] = option[:name]
end

question.answer_settings = question.answer_settings.merge(selection_options: new_selection_options)

begin
question.save!
rescue StandardError => e
Rails.logger.info "data_migrations:add_value_to_selection_options Failed to update question #{question.id}: #{e.message}"
end
end

Rails.logger.info "data_migrations:add_value_to_selection_options finished"
end

desc "Check selection options in Page DraftQuestion and FormDocument"
task check_selection_options: :environment do
pages_with_selection_options_with_no_value = Page.where(answer_type: "selection")
.where("jsonb_path_exists(answer_settings, '$.selection_options[*] ? (!exists(@.value))')")
.count

draft_questions_with_selection_options_with_no_value = DraftQuestion.where(answer_type: "selection")
.where("jsonb_path_exists(answer_settings, '$.selection_options[*] ? (!exists(@.value))')")
.count

Rails.logger.info "Pages with selection options with no value: #{pages_with_selection_options_with_no_value}"
Rails.logger.info "DraftQuestions with selection options with no value: #{draft_questions_with_selection_options_with_no_value}"
Rails.logger.info "data_migrations:check_selection_options finished"
end

desc "Updates form documents to add send_weekly_submission_batch attribute"
task add_send_weekly_submission_batch_to_form_documents: :environment do
# find all form documents that don't have a batch_submissions attribute
form_documents_without_send_weekly_submission_batch = FormDocument.where("NOT jsonb_path_exists(content, '$.send_weekly_submission_batch')")
Rails.logger.info "data_migrations:add_send_weekly_submission_batch_to_form_documents will update #{form_documents_without_send_weekly_submission_batch.count} form documents"

form_documents_without_send_weekly_submission_batch.find_each do |form_document|
form_document.content["send_weekly_submission_batch"] = false

begin
form_document.save!
rescue StandardError => e
Rails.logger.info "data_migrations:add_send_weekly_submission_batch_to_form_documents Failed to update form #{form_document.id}: #{e.message}"
end
end

Rails.logger.info "data_migrations:add_send_weekly_submission_batch_to_form_documents finished"
end

desc "Add declation_markdown based on declation_text"
task convert_declaration_text_to_markdown: :environment do
Rails.logger.info "convert_declaration_text_to_markdown: started"

Form.find_each do |form|
next if form.declaration_text.blank?

atttibutes_to_update = {}

markdown = MarkdownConversionService.new(form.declaration_text).to_markdown

atttibutes_to_update[:declaration_markdown] = markdown

if form.declaration_text_cy.present?
markdown_cy = MarkdownConversionService.new(form.declaration_text_cy).to_markdown
atttibutes_to_update[:declaration_markdown_cy] = markdown_cy
end

form.update!(atttibutes_to_update)
end

FormDocument.find_each do |form_document|
next if form_document.content["declaration_markdown"].present?

markdown = MarkdownConversionService.new(form_document.content["declaration_text"]).to_markdown

form_document.update!(content: form_document.content.merge({ "declaration_markdown": markdown }))
end

Rails.logger.info "convert_declaration_text_to_markdown: finished"
end

desc "List all forms that are not in a group"
task list_forms_without_group: :environment do
forms = Form.where.missing(:group_form)
Expand Down
128 changes: 0 additions & 128 deletions spec/lib/tasks/forms.rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -658,132 +658,4 @@
end
end
end

describe "add_value_to_selection_options" do
subject(:task) do
Rake::Task["forms:add_value_to_selection_options"]
end

let(:form) { create :form, pages: }
let(:form_without_selection_options) { create :form, :with_pages }
let(:pages) do
[
create(:page,
:with_selection_settings,
answer_settings: DataStruct.new(
only_one_option: "true",
selection_options: [{ name: "option 1" }, { name: "option 2" }],
)),
create(:page, :with_single_line_text_settings),
]
end
let(:draft_question) { create :selection_draft_question, selection_options: [{ name: "option 1" }, { name: "option 2" }] }

it "adds value to the step with selection options" do
expect {
task.invoke
}.to change { form.draft_form_document.reload.content.dig("steps", 0, "data", "answer_settings") }.to(
{ "only_one_option" => "true", # only_one_option doesn't change
"selection_options" => [{ "name" => "option 1", "value" => "option 1" },
{ "name" => "option 2", "value" => "option 2" }] },
)
end

it "does not add value to the step without selection options" do
expect {
task.invoke
}.not_to(change { form.draft_form_document.reload.content.dig("steps", 1, "data", "answer_settings") })
end

it "does not change form documents without selection options" do
expect {
task.invoke
}.not_to(change { form_without_selection_options.draft_form_document.reload.content["steps"] })
end

it "updates the pages with selection options" do
expect {
task.invoke
}.to change { pages.first.reload.answer_settings.as_json }.to(
{ "only_one_option" => "true",
"selection_options" => [{ "name" => "option 1", "value" => "option 1" },
{ "name" => "option 2", "value" => "option 2" }] },
)
end

it "updates the draft questions with selection options" do
expect {
task.invoke
}.to change { draft_question.reload.answer_settings.as_json }.to(
{ "only_one_option" => "true",
"selection_options" => [{ "name" => "option 1", "value" => "option 1" },
{ "name" => "option 2", "value" => "option 2" }] },
)
end
end

describe "add_send_weekly_submission_batch_to_form_documents" do
subject(:task) do
Rake::Task["forms:add_send_weekly_submission_batch_to_form_documents"]
end

let!(:form_with_send_weekly_submission_batch) { create :form, send_weekly_submission_batch: true }
let(:form_without_send_weekly_submission_batch) { create :form }

before do
form_without_send_weekly_submission_batch.draft_form_document.update(content: form_without_send_weekly_submission_batch.draft_form_document.content.except("send_weekly_submission_batch"))
end

it "does not change forms that already have send_weekly_submission_batch set" do
expect {
task.invoke
}.not_to(change { form_with_send_weekly_submission_batch.reload.draft_form_document.content["send_weekly_submission_batch"] })
end

it "sets send_weekly_submission_batch to false for forms that do not have it set" do
expect {
task.invoke
}.to change { form_without_send_weekly_submission_batch.reload.draft_form_document.content["send_weekly_submission_batch"] }.from(nil).to(false)
end
end

describe "convert_declaration_text_to_markdown" do
subject(:task) do
Rake::Task["forms:convert_declaration_text_to_markdown"]
end

let(:form) { create :form, declaration_text: "<p>This is a declaration</p>" }
let(:form_document) { form.draft_form_document }

it "converts declaration_text to markdown" do
expect {
task.invoke
}.to change { form.reload.declaration_markdown }.from(nil).to("This is a declaration\n\n")
end

it "does not set declaration_markdown if markdown_text is empty" do
form.update!(declaration_text: "")

expect {
task.invoke
}.not_to(change { form.reload.declaration_markdown })
end

it "converts form documents" do
expect {
task.invoke
}.to change { form_document.reload.content["declaration_markdown"] }.from(nil).to("This is a declaration\n\n")
end

context "when a form has a Welsh version" do
let(:form) { create :form, :with_welsh_translation, declaration_text: "<p>This is a declaration</p>", declaration_text_cy: "<p>This is the Welsh declaration</p>" }
let(:form_document) { form.draft_form_document }

it "converts declaration_text to markdown" do
expect {
task.invoke
}.to change { form.reload.declaration_markdown_cy }.from(nil).to("This is the Welsh declaration\n\n")
end
end
end
end