From df373bec622f3e8b042adce2c24dfbf77e146468 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Sun, 2 Jun 2024 19:27:26 +0530 Subject: [PATCH] refactor: move update_channel --- .../microsoft/callbacks_controller.rb | 23 ++-------------- app/controllers/oauth_callback_controller.rb | 27 ++++++++++++++++--- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/app/controllers/microsoft/callbacks_controller.rb b/app/controllers/microsoft/callbacks_controller.rb index 3b4ff7d9f..a409096de 100644 --- a/app/controllers/microsoft/callbacks_controller.rb +++ b/app/controllers/microsoft/callbacks_controller.rb @@ -5,14 +5,8 @@ class Microsoft::CallbacksController < OauthCallbackController 'microsoft' end - def handle_response - inbox, already_exists = find_or_create_inbox - - if already_exists - redirect_to app_email_inbox_settings_url(account_id: account.id, inbox_id: inbox.id) - else - redirect_to app_email_inbox_agents_url(account_id: account.id, inbox_id: inbox.id) - end + def imap_address + 'outlook.office365.com' end def oauth_client @@ -26,17 +20,4 @@ class Microsoft::CallbacksController < OauthCallbackController token_url: 'https://login.microsoftonline.com/common/oauth2/v2.0/token' }) end - - def update_channel(channel_email) - channel_email.update!({ - imap_login: users_data['email'], imap_address: 'outlook.office365.com', - imap_port: '993', imap_enabled: true, - provider: 'microsoft', - provider_config: { - access_token: parsed_body['access_token'], - refresh_token: parsed_body['refresh_token'], - expires_on: (Time.current.utc + 1.hour).to_s - } - }) - end end diff --git a/app/controllers/oauth_callback_controller.rb b/app/controllers/oauth_callback_controller.rb index 45ed68350..fe5782f93 100644 --- a/app/controllers/oauth_callback_controller.rb +++ b/app/controllers/oauth_callback_controller.rb @@ -14,6 +14,16 @@ class OauthCallbackController < ApplicationController private + def handle_response + inbox, already_exists = find_or_create_inbox + + if already_exists + redirect_to app_email_inbox_settings_url(account_id: account.id, inbox_id: inbox.id) + else + redirect_to app_email_inbox_agents_url(account_id: account.id, inbox_id: inbox.id) + end + end + 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 @@ -29,6 +39,19 @@ class OauthCallbackController < ApplicationController [channel_email.inbox, channel_exists] end + def update_channel(channel_email) + channel_email.update!({ + imap_login: users_data['email'], imap_address: imap_address, + imap_port: '993', imap_enabled: true, + provider: provider_name, + provider_config: { + access_token: parsed_body['access_token'], + refresh_token: parsed_body['refresh_token'], + expires_on: (Time.current.utc + 1.hour).to_s + } + }) + end + def provider_name raise NotImplementedError end @@ -37,10 +60,6 @@ class OauthCallbackController < ApplicationController raise NotImplementedError end - def update_channel(channel_email) - raise NotImplementedError - end - def create_channel_with_inbox ActiveRecord::Base.transaction do channel_email = Channel::Email.create!(email: users_data['email'], account: account)