Skip to content

Commit

Permalink
Update handling message drafts submissions erorrs
Browse files Browse the repository at this point in the history
  • Loading branch information
luciajanikova committed Sep 19, 2023
1 parent db27e7b commit 9ed6cf2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
33 changes: 21 additions & 12 deletions app/jobs/govbox/submit_message_draft_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,28 @@ def perform(message_draft, upvs_client: UpvsEnvironment.upvs_client)
sktalk_api = upvs_client.api(message_draft.thread.folder.box).sktalk

begin
success, response_status = sktalk_api.receive_and_save_to_outbox(message_draft_data)
success, response_status, response_body = sktalk_api.receive_and_save_to_outbox(message_draft_data)
if success
message_draft.metadata["status"] = "submitted"
message_draft.save!

Govbox::SyncBoxJob.set(wait: 3.minutes).perform_later(message_draft.thread.folder.box)
else
handle_submit_fail(message_draft, response_status)
handle_submit_fail(message_draft, response_status, response_body.dig("message"))
end

message_draft.save!
rescue Error => error
message_draft.metadata["status"] = "submit_failed_temporary"
message_draft.save!

rescue SubmissionError => error
raise error
end
end

class SubmissionError < StandardError
end

class TemporarySubmissionError < SubmissionError
end

private

def build_objects(message_draft)
Expand All @@ -52,14 +57,18 @@ def build_objects(message_draft)
objects
end

def handle_submit_fail(message_draft, response_status)
def handle_submit_fail(message_draft, response_status, response_message)
case response_status
when 408
# TODO
when 422
message_draft.metadata["status"] = "submit_failed_unprocessable"
when 408, 503
message_draft.metadata["status"] = "temporary_submit_fail"
message_draft.save

raise TemporarySubmissionError, "#{response_status}, #{response_message}"
else
message_draft.metadata["status"] = "submit_failed_temporary"
message_draft.metadata["status"] = "submit_fail"
message_draft.save

raise SubmissionError, "#{response_status}, #{response_message}"
end
end
end
2 changes: 1 addition & 1 deletion app/lib/upvs/govbox_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def authorization_successful?(response_status, authorization_code)
class SkTalk < Namespace
def receive_and_save_to_outbox(data)
response_status, response_body = @api.request(:post, "#{@api.url}/api/sktalk/receive_and_save_to_outbox", data.to_json, header)
[submit_successful?(response_status, response_body['receive_result'], response_body['save_to_outbox_result']), response_status]
[submit_successful?(response_status, response_body['receive_result'], response_body['save_to_outbox_result']), response_status, response_body]
end

private
Expand Down

0 comments on commit 9ed6cf2

Please sign in to comment.