Files
leadchat/app/models/concerns/reauthorizable.rb
Shivam Mishra 9a1c54a82d feat: disable automation rules if condition fails multiple times (#9017)
* feat: add email for disabling automation rule

* feat: disable automation rules and notify admin

* feat: reset error count after update

* feat: trigger invalid_condition_error if rule is invalid

* feat: setup error trackable concern

* refactor: use ErrorTrackable in Reauthorizable

* fix: optional argument

* feat: separate reauthorization_required_key

* test: update case to use ERROR_TRACKABLE_COUNT

* Revert "test: update case to use ERROR_TRACKABLE_COUNT"

This reverts commit f439847147556a02759a7597a7fcf1d66091cafc.

* Revert "feat: separate reauthorization_required_key"

This reverts commit f4514fce217b0a2f2c2bf701a15de0a8b47acbc4.

* Revert "fix: optional argument"

This reverts commit 93b4194ec3f10f67e2402388c966c071c4d3b4fd.

* Revert "refactor: use ErrorTrackable in Reauthorizable"

This reverts commit 513c2a522bc782e73ea4b0f5ae34ce01e70e042c.

* Revert "feat: setup error trackable concern"

This reverts commit 278683060cf422f60af5d5c77100aa5272141141.

* feat: use reauthorizable for automation rule

* feat: remove redis key

* test: fix method names

* chore: refactor

---------

Co-authored-by: Vishnu Narayanan <iamwishnu@gmail.com>
Co-authored-by: Sojan <sojan@pepalo.com>
2024-03-28 13:14:16 +05:30

83 lines
2.9 KiB
Ruby

# This concern is primarily targeted for business models dependent on external services
# The auth tokens we obtained on their behalf could expire or becomes invalid.
# We would be aware of it until we make the API call to the service and it throws error
# Example:
# when a user changes his/her password, the auth token they provided to chatwoot becomes invalid
# This module helps to capture the errors into a counter and when threshold is passed would mark
# the object to be reauthorized. We will also send an email to the owners alerting them of the error.
# In the UI, we will check for the reauthorization_required? status and prompt the reauthorization flow
module Reauthorizable
extend ActiveSupport::Concern
AUTHORIZATION_ERROR_THRESHOLD = 2
# model attribute
def reauthorization_required?
::Redis::Alfred.get(reauthorization_required_key).present?
end
# model attribute
def authorization_error_count
::Redis::Alfred.get(authorization_error_count_key).to_i
end
# action to be performed when we receive authorization errors
# Implement in your exception handling logic for authorization errors
def authorization_error!
::Redis::Alfred.incr(authorization_error_count_key)
# we are giving precendence to the authorization error threshhold defined in the class
# so that channels can override the default value
prompt_reauthorization! if authorization_error_count >= self.class::AUTHORIZATION_ERROR_THRESHOLD
end
# Performed automatically if error threshold is breached
# could used to manually prompt reauthorization if auth scope changes
def prompt_reauthorization!
::Redis::Alfred.set(reauthorization_required_key, true)
mailer = AdministratorNotifications::ChannelNotificationsMailer.with(account: account)
case self.class.name
when 'Integrations::Hook'
process_integration_hook_reauthorization_emails(mailer)
when 'Channel::FacebookPage'
mailer.facebook_disconnect(inbox).deliver_later
when 'Channel::Whatsapp'
mailer.whatsapp_disconnect(inbox).deliver_later
when 'Channel::Email'
mailer.email_disconnect(inbox).deliver_later
when 'AutomationRule'
update!(active: false)
mailer.automation_rule_disabled(self).deliver_later
end
end
def process_integration_hook_reauthorization_emails(mailer)
if slack?
mailer.slack_disconnect.deliver_later
elsif dialogflow?
mailer.dialogflow_disconnect.deliver_later
end
end
# call this after you successfully Reauthorized the object in UI
def reauthorized!
::Redis::Alfred.delete(authorization_error_count_key)
::Redis::Alfred.delete(reauthorization_required_key)
end
private
def authorization_error_count_key
format(::Redis::Alfred::AUTHORIZATION_ERROR_COUNT, obj_type: self.class.table_name.singularize, obj_id: id)
end
def reauthorization_required_key
format(::Redis::Alfred::REAUTHORIZATION_REQUIRED, obj_type: self.class.table_name.singularize, obj_id: id)
end
end