From eafd3ae44df856141088efb4c543040325c3fe55 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Thu, 23 May 2024 16:03:40 +0530 Subject: [PATCH] feat: new re-authorization flow for Microsoft (#9510) This PR adds a cleaner re-authorization flow to Microsoft. This PR has the following changes 1. Use `reauthorization_required` value for Microsoft Channel 2. Refactor `InboxReconnectionRequired` to reuse the `banner` component 3. Refactor `microsoft/Reauthorize.vue` to reuse `InboxReconnectionRequired` component 4. Update `reauthorizable.rb` to update cache keys if the model has an inbox 5. Update `microsoft/callbacks_controller.rb` to handle the reauthorization case with a redirect to the inbox settings page if the inbox already exists at the time of authorization. ## How Has This Been Tested? - [x] Local Instance - [ ] Staging Instance - [x] Unit tests ## Pending Tasks - [ ] ~Success Toast~ will do this in a follow-up PR with the screen ## Demo The following video shows the whole process of creation and re-authorization of the Microsoft channel https://www.loom.com/share/e5cd9bd4439c4741b0dcfe66d67f88b3?sid=100f3642-43e4-46b3-8123-88a5dd9d8509 --------- Co-authored-by: Muhsin Keloth --- .../microsoft/callbacks_controller.rb | 19 +++- .../dashboard/i18n/locale/en/inboxMgmt.json | 3 +- .../dashboard/settings/inbox/Settings.vue | 13 +-- .../inbox/channels/microsoft/Reauthorize.vue | 101 +++++++----------- .../components/InboxReconnectionRequired.vue | 23 ++-- .../inbox/settingsPage/ConfigurationPage.vue | 8 +- app/models/concerns/reauthorizable.rb | 8 ++ app/views/api/v1/models/_inbox.json.jbuilder | 5 +- config/routes.rb | 1 + .../microsoft/callbacks_controller_spec.rb | 2 +- 10 files changed, 88 insertions(+), 95 deletions(-) diff --git a/app/controllers/microsoft/callbacks_controller.rb b/app/controllers/microsoft/callbacks_controller.rb index 215103bd4..aa1c22d91 100644 --- a/app/controllers/microsoft/callbacks_controller.rb +++ b/app/controllers/microsoft/callbacks_controller.rb @@ -7,9 +7,14 @@ class Microsoft::CallbacksController < ApplicationController redirect_uri: "#{base_url}/microsoft/callback" ) - inbox = find_or_create_inbox + inbox, already_exists = find_or_create_inbox ::Redis::Alfred.delete(users_data['email'].downcase) - redirect_to app_microsoft_inbox_agents_url(account_id: account.id, inbox_id: inbox.id) + + if already_exists + redirect_to app_microsoft_inbox_settings_url(account_id: account.id, inbox_id: inbox.id) + else + redirect_to app_microsoft_inbox_agents_url(account_id: account.id, inbox_id: inbox.id) + end rescue StandardError => e ChatwootExceptionTracker.new(e).capture_exception redirect_to '/' @@ -40,9 +45,17 @@ class Microsoft::CallbacksController < ApplicationController def find_or_create_inbox channel_email = Channel::Email.find_by(email: users_data['email'], account: account) + # we need this value to know where to redirect on sucessful processing of the callback + channel_exists = channel_email.present? + channel_email ||= create_microsoft_channel_with_inbox update_microsoft_channel(channel_email) - channel_email.inbox + + # reauthorize channel, this code path only triggers when microsoft auth is successful + # reauthorized will also update cache keys for the associated inbox + channel_email.reauthorized! + + [channel_email.inbox, channel_exists] end # Fallback name, for when name field is missing from users_data diff --git a/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json b/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json index f887ecb87..95821069c 100644 --- a/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json +++ b/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json @@ -2,7 +2,8 @@ "INBOX_MGMT": { "HEADER": "Inboxes", "SIDEBAR_TXT": "

Inbox

When you connect a website or a facebook Page to Chatwoot, it is called an Inbox. You can have unlimited inboxes in your Chatwoot account.

Click on Add Inbox to connect a website or a Facebook Page.

In the Dashboard, you can see all the conversations from all your inboxes in a single place and respond to them under the `Conversations` tab.

You can also see conversations specific to an inbox by clicking on the inbox name on the left pane of the dashboard.

", - "RECONNECTION_REQUIRED": "Your inbox is disconnected, you will not receive any new messages. Click here to reconnect.", + "RECONNECTION_REQUIRED": "Your inbox is disconnected. You won't receive new messages until you reauthorize it.", + "CLICK_TO_RECONNECT": "Click here to reconnect.", "LIST": { "404": "There are no inboxes attached to this account." }, diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue index eb9108232..be4d94cb5 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue @@ -21,10 +21,7 @@ - +
-
- -
-
- - -
-
-
- - - + + + diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/components/InboxReconnectionRequired.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/components/InboxReconnectionRequired.vue index 3847ae0a7..fe6a80586 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/inbox/components/InboxReconnectionRequired.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/components/InboxReconnectionRequired.vue @@ -1,19 +1,14 @@ diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/settingsPage/ConfigurationPage.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/settingsPage/ConfigurationPage.vue index 0aa3b1c09..c10e5994e 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/inbox/settingsPage/ConfigurationPage.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/settingsPage/ConfigurationPage.vue @@ -104,10 +104,6 @@
-
@@ -130,7 +126,7 @@ " >