Skip to content

Commit

Permalink
Update submit flow (move its part to controller)
Browse files Browse the repository at this point in the history
  • Loading branch information
luciajanikova committed Sep 27, 2023
1 parent f5db640 commit e40e697
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
16 changes: 14 additions & 2 deletions app/controllers/message_drafts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def update
def submit
authorize @message_draft

if @message_draft.submit
if @message_draft.submittable?
Govbox::SubmitMessageDraftJob.perform_later(@message_draft)
@message_draft.being_submitted!

redirect_path = @message_draft.original_message.present? ? message_path(@message_draft.original_message) : message_drafts_path
redirect_to redirect_path, notice: "Správa bola zaradená na odoslanie."
else
Expand All @@ -41,7 +44,16 @@ def submit
end

def submit_all
Govbox::SubmitMultipleMessageDraftsJob.perform_later(@message_drafts.to_a)
jobs_batch = GoodJob::Batch.new

@message_drafts.each do |message_draft|
next unless message_draft.submittable?

jobs_batch.add { Govbox::SubmitMessageDraftJob.perform_later(message_draft, schedule_sync: false) }
message_draft.being_submitted!
end

jobs_batch.enqueue(on_finish: Govbox::FinishMessageDraftsSubmitJob, box: @message_drafts.first.thread.folder.box)
end

def destroy
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/govbox/submit_message_draft_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def perform(message_draft, schedule_sync: true, upvs_client: UpvsEnvironment.upv
message_draft.metadata["status"] = "submitted"
message_draft.save!

Govbox::SyncBoxJob.set(wait: 3.minutes).perform_later(message_draft.thread.folder.box)
Govbox::SyncBoxJob.set(wait: 3.minutes).perform_later(message_draft.thread.folder.box) if schedule_sync
else
handle_submit_fail(message_draft, response_status, response_body.dig("message"))
end
Expand Down
18 changes: 5 additions & 13 deletions app/models/message_draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,6 @@ def self.create_message_reply(original_message: , author:)
)
end

def submit(jobs_batch: nil)
return false unless submittable?

if jobs_batch
jobs_batch.add { Govbox::SubmitMessageDraftJob.perform_later(self, schedule_sync: false) }
else
Govbox::SubmitMessageDraftJob.perform_later(self)
end

metadata["status"] = "being_submitted"
save!
end

def update_content(title:, body:)
self.title = title
metadata["message_body"] = body
Expand Down Expand Up @@ -125,6 +112,11 @@ def submitted?
metadata["status"] == "submitted"
end

def being_submitted!
metadata["status"] = "being_submitted"
save!
end

def original_message
Message.find(metadata["original_message_id"]) if metadata["original_message_id"]
end
Expand Down

0 comments on commit e40e697

Please sign in to comment.