Skip to content
Draft
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
15 changes: 9 additions & 6 deletions app/controllers/forms/batch_submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def create
@batch_submissions_input = Forms::BatchSubmissionsInput.new(batch_submissions_input_params)

if @batch_submissions_input.submit
redirect_to form_path(current_form.id), success: success_message(current_form)
success_message = success_message(@batch_submissions_input.batch_frequencies, @batch_submissions_input.delivery_configurations_changed?)
redirect_to form_path(current_form.id), success: success_message
else
render :new, status: :unprocessable_content
end
Expand All @@ -22,14 +23,16 @@ def batch_submissions_input_params
params.require(:forms_batch_submissions_input).permit(batch_frequencies: []).merge(form: current_form)
end

def success_message(form)
return nil unless form.send_daily_submission_batch_previously_changed? || form.send_weekly_submission_batch_previously_changed?
def success_message(batch_frequencies, delivery_configurations_changed)
return nil unless delivery_configurations_changed

if form.send_daily_submission_batch && form.send_weekly_submission_batch
batch_frequencies = Array(batch_frequencies)

if batch_frequencies.include?("daily") && batch_frequencies.include?("weekly")
t("banner.success.form.batch_submissions.daily_and_weekly_enabled")
elsif form.send_daily_submission_batch
elsif batch_frequencies.include?("daily")
t("banner.success.form.batch_submissions.daily_enabled")
elsif form.send_weekly_submission_batch
elsif batch_frequencies.include?("weekly")
t("banner.success.form.batch_submissions.weekly_enabled")
else
t("banner.success.form.batch_submissions.disabled")
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/forms/submission_attachments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Forms
class SubmissionAttachmentsController < FormsController
before_action :submission_type_email?
before_action :has_email_delivery_configuration?

def new
authorize current_form, :can_view_form?
Expand All @@ -24,14 +24,14 @@ def submission_attachments_input_params
params.require(:forms_submission_attachments_input).permit(submission_format: []).merge(form: current_form)
end

def submission_type_email?
redirect_to error_404_path unless current_form.email?
def has_email_delivery_configuration?
redirect_to error_404_path if current_form.immediate_email_delivery_configuration.blank?
end

def success_message(form)
return nil unless form.submission_format_previously_changed?
return nil unless form.immediate_email_delivery_configuration.formats_previously_changed?

case form.submission_format
case form.immediate_email_delivery_configuration.formats
when []
t("banner.success.form.receive_no_attachments")
when %w[csv]
Expand Down
25 changes: 19 additions & 6 deletions app/input_objects/forms/batch_submissions_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,40 @@ class Forms::BatchSubmissionsInput < BaseInput
attr_accessor :form, :batch_frequencies

def submit
@delivery_configurations_before = batch_delivery_configuration_snapshot
selected_frequencies = Array(batch_frequencies)

form.send_daily_submission_batch = selected_frequencies.include?("daily")
form.send_weekly_submission_batch = selected_frequencies.include?("weekly")

%w[daily weekly].each do |frequency|
matching_delivery_configurations = form.delivery_configurations.where(delivery_method: "email", delivery_schedule: frequency)

if selected_frequencies.include?(frequency)
form.delivery_configurations.find_or_create_by!(delivery_method: "email", delivery_schedule: frequency, formats: %w[csv])
else
form.delivery_configurations.where(delivery_method: "email", delivery_schedule: frequency).destroy_all
matching_delivery_configurations.destroy_all
end
end

form.delivery_configurations.reload
form.save_draft!
end

def delivery_configurations_changed?
@delivery_configurations_before != batch_delivery_configuration_snapshot
end

def assign_form_values
self.batch_frequencies ||= []
self.batch_frequencies << "daily" if form.send_daily_submission_batch
self.batch_frequencies << "weekly" if form.send_weekly_submission_batch
self.batch_frequencies << "daily" if form.delivery_configurations.daily.any?
self.batch_frequencies << "weekly" if form.delivery_configurations.weekly.any?
self
end

private

def batch_delivery_configuration_snapshot
form.delivery_configurations
.where(delivery_method: "email", delivery_schedule: %w[daily weekly])
.pluck(:delivery_method, :delivery_schedule, :formats)
.sort
end
end
9 changes: 3 additions & 6 deletions app/input_objects/forms/submission_attachments_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ class Forms::SubmissionAttachmentsInput < BaseInput
def submit
return false if invalid?

formats = submission_format.compact_blank
form.submission_format = formats

delivery_configuration = form.delivery_configurations.where(delivery_method: "email", delivery_schedule: "immediate").first_or_initialize
delivery_configuration.formats = formats
delivery_configuration = form.immediate_email_delivery_configuration || form.build_immediate_email_delivery_configuration
delivery_configuration.formats = submission_format.compact_blank
delivery_configuration.save!
form.delivery_configurations.reload

form.save_draft!
end

def assign_form_values
self.submission_format = form.submission_format
self.submission_format = form.immediate_email_delivery_configuration&.formats || []
self
end

Expand Down
15 changes: 2 additions & 13 deletions app/models/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Form < ApplicationRecord
include FormStateMachine
extend Mobility

self.ignored_columns += [:language]
self.ignored_columns += %i[language submission_type submission_format send_daily_submission_batch send_weekly_submission_batch]

SUPPORTED_LANGUAGES = %w[en cy].freeze

Expand All @@ -18,6 +18,7 @@ class Form < ApplicationRecord
has_one :draft_form_document, -> { where tag: "draft", language: :en }, class_name: "FormDocument"
has_many :conditions, through: :pages, source: :routing_conditions
has_many :delivery_configurations, dependent: :destroy
has_one :immediate_email_delivery_configuration, -> { where delivery_method: "email", delivery_schedule: "immediate" }, class_name: "DeliveryConfiguration"

translates :name,
:privacy_policy_url,
Expand All @@ -30,26 +31,14 @@ class Form < ApplicationRecord
:what_happens_next_markdown,
:payment_url

enum :submission_type, {
email: "email",
s3: "s3",
}

enum :send_copy_of_answers, {
disabled: "disabled",
enabled: "enabled",
}, prefix: :send_copy_of_answers

# ActiveRecord doesn't support enums with arrays
# enum :submission_format, {
# csv: "csv",
# json: "json",
# }

validates :name, presence: true
validates :payment_url, url: true, allow_blank: true
validate :marking_complete_with_errors
validates :submission_type, presence: true
validates :send_copy_of_answers, presence: true
validates :available_languages, presence: true, inclusion: { in: SUPPORTED_LANGUAGES }
validates :submission_email, email_address: { message: :invalid_email }, allow_blank: true
Expand Down
27 changes: 23 additions & 4 deletions app/models/form_document/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class FormDocument::Content
attribute :support_email, :string
attribute :support_phone, :string
attribute :s3_bucket_name, :string
attribute :submission_type, :string
attribute :submission_format, array: true
attribute :declaration_text, :string
attribute :declaration_markdown, :string
attribute :s3_bucket_region, :string
Expand All @@ -31,9 +29,8 @@ class FormDocument::Content
attribute :privacy_policy_url, :string
attribute :s3_bucket_aws_account_id, :string
attribute :what_happens_next_markdown, :string
attribute :send_daily_submission_batch, :boolean
attribute :send_weekly_submission_batch, :boolean
attribute :send_copy_of_answers, :string
attribute :delivery_configurations, array: true

alias_attribute :id, :form_id

Expand All @@ -54,4 +51,26 @@ def self.from_form_document(form_document)
def has_welsh_translation?
available_languages.present? && available_languages.include?("cy")
end

def has_email_delivery?
email_delivery_configuration.present?
end

def email_delivery_configuration
delivery_configurations.find do |delivery_configuration|
delivery_configuration["delivery_method"] == "email" && delivery_configuration["delivery_schedule"] == "immediate"
end
end

def daily_submission_batch_enabled?
delivery_configurations.any? do |delivery_configuration|
delivery_configuration["delivery_schedule"] == "daily"
end
end

def weekly_submission_batch_enabled?
delivery_configurations.any? do |delivery_configuration|
delivery_configuration["delivery_schedule"] == "weekly"
end
end
end
2 changes: 1 addition & 1 deletion app/services/form_task_list_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def how_you_get_completed_forms_section_tasks

def how_you_get_completed_forms_optional_subsection
rows = []
rows << submission_attachments_task if @form.email?
rows << submission_attachments_task if @form.immediate_email_delivery_configuration.present?
rows << batch_submissions_task

return nil if rows.empty?
Expand Down
18 changes: 13 additions & 5 deletions app/services/reports/form_documents_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,31 @@ def has_payments?(form_document)
end

def has_csv_submission_email_attachments(form_document)
form_document["content"]["submission_type"] == "email" && form_document["content"]["submission_format"].include?("csv")
form_document["content"]["delivery_configurations"].any? do |delivery_configuration|
delivery_configuration["delivery_method"] == "email" &&
delivery_configuration["delivery_schedule"] == "immediate" &&
delivery_configuration["formats"].include?("csv")
end
end

def has_json_submission_email_attachments(form_document)
form_document["content"]["submission_type"] == "email" && form_document["content"]["submission_format"].include?("json")
form_document["content"]["delivery_configurations"].any? do |delivery_configuration|
delivery_configuration["delivery_method"] == "email" &&
delivery_configuration["delivery_schedule"] == "immediate" &&
delivery_configuration["formats"].include?("json")
end
end

def has_daily_submission_csv(form_document)
form_document["content"]["send_daily_submission_batch"]
form_document["content"]["delivery_configurations"].any? { |c| c["delivery_schedule"] == "daily" }
end

def has_weekly_submission_csv(form_document)
form_document["content"]["send_weekly_submission_batch"]
form_document["content"]["delivery_configurations"].any? { |c| c["delivery_schedule"] == "weekly" }
end

def has_s3_submissions(form_document)
form_document["content"]["submission_type"] == "s3"
form_document["content"]["delivery_configurations"].any? { |c| c["delivery_method"] == "s3" }
end

def has_exit_pages?(form_document)
Expand Down
18 changes: 12 additions & 6 deletions app/services/reports/forms_csv_report_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class Reports::FormsCsvReportService
"Support phone",
"Privacy policy URL",
"What happens next markdown",
"Submission type",
"Submission formats",
"Delivery methods",
"Daily submissions CSV enabled",
"Weekly submissions CSV enabled",
"Has Welsh translation",
Expand Down Expand Up @@ -77,12 +76,19 @@ def form_row(form)
form["content"]["support_phone"],
form["content"]["privacy_policy_url"],
form["content"]["what_happens_next_markdown"],
form["content"]["submission_type"],
form["content"]["submission_format"]&.sort&.join(" "),
form["content"]["send_daily_submission_batch"],
form["content"]["send_weekly_submission_batch"],
format_delivery_methods(form),
Reports::FormDocumentsService.has_daily_submission_csv(form),
Reports::FormDocumentsService.has_weekly_submission_csv(form),
Reports::FormDocumentsService.has_welsh_translation(form),
Reports::FormDocumentsService.copy_of_answers_enabled?(form),
]
end

def format_delivery_methods(form)
form["content"]["delivery_configurations"].map { |delivery_configuration|
next unless delivery_configuration["delivery_schedule"] == "immediate"

"#{delivery_configuration['delivery_method']}: #{delivery_configuration['formats'].join(', ')}"
}.compact.join("\n")
end
end
5 changes: 3 additions & 2 deletions app/services/task_status_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,14 @@ def welsh_language_status
end

def submission_attachments_status
return :completed if @form.email? && @form.submission_format.any?
email_delivery_configuration = @form.immediate_email_delivery_configuration
return :completed if email_delivery_configuration.present? && email_delivery_configuration.formats.any?

:optional
end

def batch_submissions_status
return :completed if @form.send_daily_submission_batch || @form.send_weekly_submission_batch
return :completed if @form.delivery_configurations.daily.any? || @form.delivery_configurations.weekly.any?

:optional
end
Expand Down
12 changes: 6 additions & 6 deletions app/views/forms/_made_live_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,18 @@
<h4 class="govuk-heading-s"><%= t('.how_you_get_completed_forms.submission_email') %></h4>
<p class="govuk-!-text-break-word"><%= form_document.submission_email %></p>

<% if form_document.submission_type == "email" %>
<% submission_format = ["email", *form_document.submission_format].join("_") %>
<% if form_document.has_email_delivery? %>
<% formats = ["email", *form_document.email_delivery_configuration["formats"]].join("_") %>
<h4 class="govuk-heading-s"><%= t(".how_you_get_completed_forms.csv_and_json") %></h4>
<p><%= t(".how_you_get_completed_forms.submission_format.email.#{submission_format}_html") %></p>
<p><%= t(".how_you_get_completed_forms.submission_format.email.#{formats}_html") %></p>
<% end %>

<h4 class="govuk-heading-s"><%= t(".how_you_get_completed_forms.batch_submissions.title") %></h4>
<% if form_document.send_daily_submission_batch && form_document.send_weekly_submission_batch %>
<% if form_document.daily_submission_batch_enabled? && form_document.weekly_submission_batch_enabled? %>
<p><%= t(".how_you_get_completed_forms.batch_submissions.daily_and_weekly_enabled") %></p>
<% elsif form_document.send_daily_submission_batch %>
<% elsif form_document.daily_submission_batch_enabled? %>
<p><%= t(".how_you_get_completed_forms.batch_submissions.daily_enabled") %></p>
<% elsif form_document.send_weekly_submission_batch %>
<% elsif form_document.weekly_submission_batch_enabled? %>
<p><%= t(".how_you_get_completed_forms.batch_submissions.weekly_enabled") %></p>
<% else %>
<p><%= t(".how_you_get_completed_forms.batch_submissions.disabled") %></p>
Expand Down
9 changes: 0 additions & 9 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ en:
submission_format:
blank: Sorry, there was a problem. Please try again.
invalid_submission_format: Sorry, there was a problem. Please try again.
forms/submission_type_input:
attributes:
submission_type:
blank: Choose if you want to get completed forms as CSV files
group_member_input:
attributes:
member_email_address:
Expand Down Expand Up @@ -1055,9 +1051,6 @@ en:
submission_format_options:
csv: Get a CSV file of each completed form
json: Get a JSON file of each completed form
forms_submission_type_input:
submission_type_options:
email_with_csv: Get CSV files
group:
name: Enter a name for your new group
group_member_input:
Expand Down Expand Up @@ -1214,8 +1207,6 @@ en:
send_copy_of_answers: Do you want to give people the option to get a copy of their answers by email?
forms_submission_attachments_input:
submission_format: Do you want to get a CSV or JSON file of each completed form?
forms_submission_type_input:
submission_type: Do you want to get completed forms as CSV files?
mou_signature:
crown_agreed: Do you agree to the MOU?
non_crown_agreed: Do you agree to the GOV.UK Forms agreement?
Expand Down
2 changes: 0 additions & 2 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,6 @@
support_phone: "08000800",
what_happens_next_markdown: "Test",
share_preview_completed: true,
submission_type: "s3",
submission_format: %w[csv],
s3_bucket_region: "eu-west-2",
s3_bucket_name: "govuk-forms-submissions-to-s3-test",
s3_bucket_aws_account_id: "711966560482",
Expand Down
Loading
Loading