diff --git a/app/controllers/submissions_controller.rb b/app/controllers/submissions_controller.rb index c39e99e5..96c7c43a 100644 --- a/app/controllers/submissions_controller.rb +++ b/app/controllers/submissions_controller.rb @@ -79,6 +79,8 @@ class SubmissionsController < ApplicationController end def destroy + template = @submission.template + notice = if params[:permanently].in?(['true', true]) @submission.destroy! @@ -92,7 +94,11 @@ class SubmissionsController < ApplicationController I18n.t('submission_has_been_archived') end - redirect_back(fallback_location: @submission.template_id ? template_path(@submission.template) : root_path, notice:) + if params[:permanently].in?(['true', true]) + redirect_to(template ? template_path(template) : root_path, notice:) + else + redirect_back(fallback_location: template ? template_path(template) : root_path, notice:) + end end private diff --git a/app/controllers/submitter_edit_form_controller.rb b/app/controllers/submitter_edit_form_controller.rb new file mode 100644 index 00000000..c961b326 --- /dev/null +++ b/app/controllers/submitter_edit_form_controller.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class SubmitterEditFormController < ApplicationController + skip_before_action :authenticate_user! + skip_authorization_check + + def update + @submitter = Submitter.find_by!(slug: params[:submitter_slug]) + + if @submitter.submission.archived_at? || @submitter.submission.expired? || @submitter.submission.template&.archived_at? + return redirect_to submit_form_completed_path(@submitter.slug), + alert: I18n.t('form_cannot_be_edited') + end + + unless @submitter.completed_at? + return redirect_to submit_form_path(@submitter.slug) + end + + ActiveRecord::Base.transaction do + @submitter.update!(completed_at: nil, opened_at: nil) + @submitter.submission_events.where(event_type: 'complete_form').destroy_all + @submitter.documents.each(&:purge) + end + + redirect_to submit_form_path(@submitter.slug) + end +end diff --git a/app/controllers/submitter_edit_values_controller.rb b/app/controllers/submitter_edit_values_controller.rb new file mode 100644 index 00000000..d025befc --- /dev/null +++ b/app/controllers/submitter_edit_values_controller.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +class SubmitterEditValuesController < ApplicationController + NON_EDITABLE_TYPES = %w[signature initials image stamp file payment verification kba heading strikethrough].freeze + + before_action :load_and_authorize_submitter + + def edit + all_fields = @submitter.submission.template_fields || @submitter.submission.template.fields + @fields = all_fields.select { |f| f['submitter_uuid'] == @submitter.uuid } + .reject { |f| NON_EDITABLE_TYPES.include?(f['type']) } + end + + def update + all_fields = @submitter.submission.template_fields || @submitter.submission.template.fields + editable_fields = all_fields.select { |f| f['submitter_uuid'] == @submitter.uuid } + .reject { |f| NON_EDITABLE_TYPES.include?(f['type']) } + editable_uuids = editable_fields.map { |f| f['uuid'] } + + submitted_values = params[:values].to_h.slice(*editable_uuids) + + ActiveRecord::Base.transaction do + @submitter.update!(values: @submitter.values.merge(submitted_values)) + + @submitter.documents.each(&:purge) + + SubmissionEvent.create!( + submitter: @submitter, + event_type: :admin_edit_values, + data: { user_id: current_user.id, user_email: current_user.email, updated_uuids: editable_uuids } + ) + end + + Submissions::GenerateResultAttachments.call(@submitter) + + redirect_to submission_path(@submitter.submission), + notice: I18n.t('submission_values_have_been_updated') + end + + private + + def load_and_authorize_submitter + @submitter = Submitter.find(params[:id]) + authorize! :update, @submitter.submission + end +end diff --git a/app/controllers/submitters_reopen_controller.rb b/app/controllers/submitters_reopen_controller.rb new file mode 100644 index 00000000..e7b0edf5 --- /dev/null +++ b/app/controllers/submitters_reopen_controller.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +class SubmittersReopenController < ApplicationController + before_action :load_and_authorize_submitter + + def update + ActiveRecord::Base.transaction do + @submitter.update!(completed_at: nil, opened_at: nil) + + @submitter.submission_events.where(event_type: 'complete_form').destroy_all + + @submitter.documents.each(&:purge) + + SubmissionEvent.create!( + submitter: @submitter, + event_type: :admin_reopen_form, + data: { user_id: current_user.id, user_email: current_user.email } + ) + end + + if @submitter.email.present? + SendSubmitterInvitationEmailJob.perform_async('submitter_id' => @submitter.id) + end + + redirect_to submission_path(@submitter.submission), + notice: I18n.t('submission_has_been_reopened') + end + + private + + def load_and_authorize_submitter + @submitter = Submitter.find(params[:id]) + authorize! :update, @submitter.submission + end +end diff --git a/app/models/submission_event.rb b/app/models/submission_event.rb index c5746204..79750320 100644 --- a/app/models/submission_event.rb +++ b/app/models/submission_event.rb @@ -64,7 +64,9 @@ class SubmissionEvent < ApplicationRecord invite_party: 'invite_party', complete_form: 'complete_form', decline_form: 'decline_form', - api_complete_form: 'api_complete_form' + api_complete_form: 'api_complete_form', + admin_reopen_form: 'admin_reopen_form', + admin_edit_values: 'admin_edit_values' }, scope: false private diff --git a/app/views/submissions/show.html.erb b/app/views/submissions/show.html.erb index f6ee7282..c564d9b5 100644 --- a/app/views/submissions/show.html.erb +++ b/app/views/submissions/show.html.erb @@ -19,6 +19,16 @@ <% if signed_in? && can?(:create, @submission) && @submission.archived_at? && !is_all_completed %> <%= button_to button_title(title: t('unarchive'), disabled_with: t('unarchive')[0..-2], icon: svg_icon('rotate', class: 'w-6 h-6')), submission_unarchive_index_path(@submission), class: 'btn btn-primary btn-ghost text-base hidden md:flex' %> <% end %> + <% if signed_in? && can?(:destroy, @submission) && !@submission.archived_at? %> + + <%= button_to button_title(title: t('archive'), disabled_with: t('archive')[0..-2], icon: svg_icon('archive', class: 'w-6 h-6')), submission_path(@submission), class: 'btn btn-ghost text-base hidden md:flex', method: :delete %> + + <% end %> + <% if signed_in? && can?(:destroy, @submission) && @submission.archived_at? %> + + <%= button_to button_title(title: t('remove'), disabled_with: t('remove')[0..-2], icon: svg_icon('trash', class: 'w-6 h-6')), submission_path(@submission, permanently: true), class: 'btn btn-ghost btn-error text-base hidden md:flex', method: :delete, data: { turbo_confirm: t('submission_deletion_is_irreversible_and_will_permanently_remove_all_associated_signed_documents_with_it_are_you_sure_') } %> + + <% end %> <% if @submission.audit_trail.present? %> <% elsif signed_in? %> @@ -242,6 +252,14 @@ <%= button_to t('resubmit'), submitters_resubmit_path(submitter), method: :put, class: 'btn btn-sm btn-primary w-full', form: { target: '_blank' }, data: { turbo: false } %> <% end %> + <% if signed_in? && submitter && submitter.completed_at? && can?(:update, @submission) && !@submission.archived_at? %> +
No editable text fields found for this submitter.
+ <% else %> + <% field_counters = Hash.new { 0 } %> + <% @fields.each do |field| %> + <% field_counters[field['type']] += 1 %> + <% current_value = @submitter.values[field['uuid']] %> +