diff --git a/app/controllers/api/submissions_controller.rb b/app/controllers/api/submissions_controller.rb index 1ae634e67..bda564c0c 100644 --- a/app/controllers/api/submissions_controller.rb +++ b/app/controllers/api/submissions_controller.rb @@ -74,8 +74,8 @@ def create Submissions.send_signature_requests(submissions) submissions.each do |submission| - if submission.submitters.all?(&:completed_at?) && submission.submitters.last - ProcessSubmitterCompletionJob.perform_async({ 'submitter_id' => submission.submitters.last.id }) + submission.submitters.each do |submitter| + ProcessSubmitterCompletionJob.perform_async({ 'submitter_id' => submitter.id }) if submitter.completed_at? end end diff --git a/app/jobs/process_submitter_completion_job.rb b/app/jobs/process_submitter_completion_job.rb index 56ed4c5df..19c278d64 100644 --- a/app/jobs/process_submitter_completion_job.rb +++ b/app/jobs/process_submitter_completion_job.rb @@ -6,6 +6,8 @@ class ProcessSubmitterCompletionJob def perform(params = {}) submitter = Submitter.find(params['submitter_id']) + create_completed_submitter!(submitter) + is_all_completed = !submitter.submission.submitters.exists?(completed_at: nil) if !is_all_completed && submitter.submission.submitters_order_preserved? @@ -24,7 +26,7 @@ def perform(params = {}) enqueue_completed_emails(submitter) end - create_completed_submitter!(submitter) + create_completed_documents!(submitter) enqueue_completed_webhooks(submitter, is_all_completed:) end @@ -45,15 +47,19 @@ def create_completed_submitter!(submitter) completed_at: submitter.completed_at ) - submitter.documents.each do |attachment| - next if attachment.metadata['sha256'].blank? - - completed_submitter.completed_documents << CompletedDocument.new(sha256: attachment.metadata['sha256']) - end - completed_submitter.save! completed_submitter + rescue ActiveRecord::RecordNotUnique + retry + end + + def create_completed_documents!(submitter) + submitter.documents.filter_map do |attachment| + next if attachment.metadata['sha256'].blank? + + CompletedDocument.find_or_create_by!(sha256: attachment.metadata['sha256'], submitter_id: submitter.id) + end end def enqueue_completed_webhooks(submitter, is_all_completed: false) diff --git a/app/models/completed_document.rb b/app/models/completed_document.rb index 2c842e291..5ec53ca44 100644 --- a/app/models/completed_document.rb +++ b/app/models/completed_document.rb @@ -16,7 +16,7 @@ # index_completed_documents_on_submitter_id (submitter_id) # class CompletedDocument < ApplicationRecord - belongs_to :submitter + belongs_to :submitter, optional: true has_one :completed_submitter, primary_key: :submitter_id, inverse_of: :completed_documents, dependent: :destroy end diff --git a/app/models/completed_submitter.rb b/app/models/completed_submitter.rb index 181f2241f..e34f65d2e 100644 --- a/app/models/completed_submitter.rb +++ b/app/models/completed_submitter.rb @@ -18,7 +18,7 @@ # Indexes # # index_completed_submitters_on_account_id (account_id) -# index_completed_submitters_on_submitter_id (submitter_id) +# index_completed_submitters_on_submitter_id (submitter_id) UNIQUE # class CompletedSubmitter < ApplicationRecord belongs_to :submitter diff --git a/db/migrate/20241026161207_add_unique_index_on_completed_submitters.rb b/db/migrate/20241026161207_add_unique_index_on_completed_submitters.rb new file mode 100644 index 000000000..30b47c986 --- /dev/null +++ b/db/migrate/20241026161207_add_unique_index_on_completed_submitters.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class AddUniqueIndexOnCompletedSubmitters < ActiveRecord::Migration[7.2] + def change + remove_index :completed_submitters, :submitter_id + add_index :completed_submitters, :submitter_id, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 556514761..3fd0ab117 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_10_22_125135) do +ActiveRecord::Schema[7.2].define(version: 2024_10_26_161207) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -109,7 +109,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["account_id"], name: "index_completed_submitters_on_account_id" - t.index ["submitter_id"], name: "index_completed_submitters_on_submitter_id" + t.index ["submitter_id"], name: "index_completed_submitters_on_submitter_id", unique: true end create_table "document_generation_events", force: :cascade do |t|