Handle authentication errors when appending data

This catches exceptions that can happen when an authentication is expired
or removed by the user on google's side.
This commit is contained in:
bumi 2020-04-09 11:51:54 +02:00
parent 10d80c6548
commit 94966a9933
3 changed files with 31 additions and 2 deletions

View File

@ -1,8 +1,20 @@
class SubmissionAppendJob < ApplicationJob
queue_as :default
def perform(submission_id)
rescue_from(Signet::AuthorizationError, Google::Apis::AuthorizationError) do |exception|
submission_id = self.arguments.first
Rails.logger.error("AuthorizationError during SubmissionAppend: submission_id=#{submission_id}")
submission = Submission.find(submission_id)
submission.form.deactivate!('AuthorizationError')
end
def perform(*args)
submission_id = args.first
submission = Submission.find(submission_id)
if submission.form.active?
submission.append_to_spreadsheet
else
Rails.logger.error("Inactive form: submission_id=#{submission_id} form_id=#{submission.form_id}")
end
end
end

View File

@ -11,6 +11,14 @@ class Form < ApplicationRecord
validates_presence_of :title
def deactivate!(reason = nil)
self.user.deactivate!(reason)
end
def active?
self.user.active?
end
def google_spreadsheet_url
"https://docs.google.com/spreadsheets/d/#{google_spreadsheet_id}/edit" if google_spreadsheet_id.present?
end

View File

@ -25,6 +25,15 @@ class User < ApplicationRecord
end
end
def deactivate!(reason = nil)
# currently we only use deactivate if we get an authentication exception appending data to a spreadsheet
authentications.last&.update(expires_at: Time.current)
end
def active?
authentications.last.present? && !authentications.last.expired?
end
def google_authorization
authentications.last.google_authorization
end