Refactor for reauthorize_page method (#110)

* Refactor for reauthorize_page method

* Fix the rubocop issues
This commit is contained in:
Ender Ahmet Yurt
2019-12-13 00:12:16 +03:00
committed by Sojan Jose
parent febc4bef83
commit cef1200351

View File

@@ -22,30 +22,43 @@ class Api::V1::CallbacksController < ApplicationController
@page_details = mark_already_existing_facebook_pages(fb_object.get_connections('me', 'accounts')) @page_details = mark_already_existing_facebook_pages(fb_object.get_connections('me', 'accounts'))
end end
def reauthorize_page # get params[:inbox_id], current_account, params[:omniauth_token] # get params[:inbox_id], current_account, params[:omniauth_token]
inbox = current_account.inboxes.find_by(id: params[:inbox_id]) def reauthorize_page
if inbox if @inbox&.first&.facebook?
fb_page_id = inbox.channel.page_id fb_page_id = @inbox.channel.page_id
page_details = fb_object.get_connections('me', 'accounts') page_details = fb_object.get_connections('me', 'accounts')
(page_details || []).each do |page_detail|
next unless fb_page_id == page_detail['id'] # found the page which has to be reauthorised
fb_page = current_account.facebook_pages.find_by(page_id: fb_page_id) (page_details || []).each do |page_detail|
if fb_page if fb_page_id == page_detail['id'] # found the page which has to be reauthorised
fb_page.update!( update_fb_page(fb_page_id, page_detail['access_token'])
user_access_token: @user_access_token, head :ok
page_access_token: page_detail['access_token'] end
end
end
head :unprocessable_entity
end
private
def inbox
@inbox = current_account.inboxes.find_by(id: params[:inbox_id])
end
def update_fb_page
if fb_page(fb_page_id)
fb_page.update_attributes!(
user_access_token: @user_access_token, page_access_token: access_token
) )
head :ok head :ok
else else
head :unprocessable_entity head :unprocessable_entity
end end
end end
end
head :unprocessable_entity
end
private def fb_page(fb_page_id)
current_account.facebook_pages.find_by(page_id: fb_page_id)
end
def fb_object def fb_object
@user_access_token = long_lived_token(params[:omniauth_token]) @user_access_token = long_lived_token(params[:omniauth_token])