Skip to content

Commit

Permalink
fix completed submitters
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj committed Oct 26, 2024
1 parent 46e948a commit 1b636c5
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/controllers/api/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 13 additions & 7 deletions app/jobs/process_submitter_completion_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/models/completed_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/models/completed_submitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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|
Expand Down

0 comments on commit 1b636c5

Please sign in to comment.