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/config/routes.rb b/config/routes.rb index 0593b9e16..65b58f94a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,6 +20,7 @@ Rails.application.routes.draw do get '/app/accounts/:account_id/settings/inboxes/new/microsoft', to: 'dashboard#index', as: 'app_new_microsoft_inbox' get '/app/accounts/:account_id/settings/inboxes/new/:inbox_id/agents', to: 'dashboard#index', as: 'app_twitter_inbox_agents' get '/app/accounts/:account_id/settings/inboxes/new/:inbox_id/agents', to: 'dashboard#index', as: 'app_microsoft_inbox_agents' + get '/app/accounts/:account_id/settings/inboxes/:inbox_id', to: 'dashboard#index', as: 'app_microsoft_inbox_settings' resource :widget, only: [:show] namespace :survey do