From db73d033b718f2cd7b400f3ce5c58d06299b9dda Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Wed, 7 Sep 2022 12:22:24 +0530 Subject: [PATCH] feat: Fetching the portal data related to a specific custom domain (#5249) --- app/controllers/dashboard_controller.rb | 10 ++++++++++ .../public/api/v1/portals/articles_controller.rb | 12 +++++++++--- .../api/v1/portals/categories_controller.rb | 5 +++-- .../public/api/v1/portals_controller.rb | 3 ++- app/controllers/public_controller.rb | 15 +++++++++++++++ .../public/api/v1/models/_category.json.jbuilder | 6 +++--- .../public/api/v1/models/_portal.json.jbuilder | 2 +- config/routes.rb | 13 +++++++------ .../api/v1/portals/articles_controller_spec.rb | 12 ++++++------ .../api/v1/portals/categories_controller_spec.rb | 15 ++++----------- .../public/api/v1/portals_controller_spec.rb | 16 ++++++++++++++-- 11 files changed, 74 insertions(+), 35 deletions(-) diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index bd144b0df..61563e42b 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -4,6 +4,7 @@ class DashboardController < ActionController::Base before_action :set_global_config around_action :switch_locale before_action :ensure_installation_onboarding, only: [:index] + before_action :redirect_to_custom_domain_page layout 'vueapp' @@ -37,6 +38,15 @@ class DashboardController < ActionController::Base redirect_to '/installation/onboarding' if ::Redis::Alfred.get(::Redis::Alfred::CHATWOOT_INSTALLATION_ONBOARDING) end + def redirect_to_custom_domain_page + custom_domain = request.host + portal = Portal.find_by(custom_domain: custom_domain) + + return unless portal + + redirect_to "/hc/#{portal.slug}" + end + def app_config { APP_VERSION: Chatwoot.config[:version], diff --git a/app/controllers/public/api/v1/portals/articles_controller.rb b/app/controllers/public/api/v1/portals/articles_controller.rb index e861d30ed..a5d98159c 100644 --- a/app/controllers/public/api/v1/portals/articles_controller.rb +++ b/app/controllers/public/api/v1/portals/articles_controller.rb @@ -1,5 +1,7 @@ -class Public::Api::V1::Portals::ArticlesController < ApplicationController +class Public::Api::V1::Portals::ArticlesController < PublicController + before_action :ensure_custom_domain_request, only: [:show, :index] before_action :set_portal + before_action :set_category before_action :set_article, only: [:show] def index @@ -12,11 +14,15 @@ class Public::Api::V1::Portals::ArticlesController < ApplicationController private def set_article - @article = @portal.articles.find(params[:id]) + @article = @category.articles.find(params[:id]) + end + + def set_category + @category = @portal.categories.find_by!(slug: params[:category_slug]) end def set_portal - @portal = ::Portal.find_by!(slug: params[:portal_slug], archived: false) + @portal = @portals.find_by!(slug: params[:slug], archived: false) end def list_params diff --git a/app/controllers/public/api/v1/portals/categories_controller.rb b/app/controllers/public/api/v1/portals/categories_controller.rb index cf57d73ff..1f8e2093e 100644 --- a/app/controllers/public/api/v1/portals/categories_controller.rb +++ b/app/controllers/public/api/v1/portals/categories_controller.rb @@ -1,4 +1,5 @@ class Public::Api::V1::Portals::CategoriesController < PublicController + before_action :ensure_custom_domain_request, only: [:show, :index] before_action :set_portal before_action :set_category, only: [:show] @@ -11,10 +12,10 @@ class Public::Api::V1::Portals::CategoriesController < PublicController private def set_category - @category = @portal.categories.find_by!(slug: params[:slug]) + @category = @portal.categories.find_by!(locale: params[:locale]) end def set_portal - @portal = ::Portal.find_by!(slug: params[:portal_slug], archived: false) + @portal = @portals.find_by!(slug: params[:slug], archived: false) end end diff --git a/app/controllers/public/api/v1/portals_controller.rb b/app/controllers/public/api/v1/portals_controller.rb index aedf1a09d..bd3b4a911 100644 --- a/app/controllers/public/api/v1/portals_controller.rb +++ b/app/controllers/public/api/v1/portals_controller.rb @@ -1,4 +1,5 @@ class Public::Api::V1::PortalsController < PublicController + before_action :ensure_custom_domain_request, only: [:show] before_action :set_portal def show; end @@ -6,6 +7,6 @@ class Public::Api::V1::PortalsController < PublicController private def set_portal - @portal = ::Portal.find_by!(slug: params[:slug], archived: false) + @portal = @portals.find_by!(slug: params[:slug], archived: false) end end diff --git a/app/controllers/public_controller.rb b/app/controllers/public_controller.rb index b59a4296e..25de4b610 100644 --- a/app/controllers/public_controller.rb +++ b/app/controllers/public_controller.rb @@ -3,4 +3,19 @@ class PublicController < ActionController::Base include RequestExceptionHandler skip_before_action :verify_authenticity_token + + private + + def ensure_custom_domain_request + custom_domain = request.host + + @portals = ::Portal.where(custom_domain: custom_domain) + + return if @portals.present? + + render json: { + error: "Domain: #{custom_domain} is not registered with us. \ + Please send us an email at support@chatwoot.com with the custom domain name and account API key" + }, status: :unauthorized and return + end end diff --git a/app/views/public/api/v1/models/_category.json.jbuilder b/app/views/public/api/v1/models/_category.json.jbuilder index e257b0045..39828c981 100644 --- a/app/views/public/api/v1/models/_category.json.jbuilder +++ b/app/views/public/api/v1/models/_category.json.jbuilder @@ -7,20 +7,20 @@ json.position category.position json.related_categories do if category.related_categories.any? json.array! category.related_categories.each do |related_category| - json.partial! partial: 'associated_category', category: related_category + json.partial! partial: 'public/api/v1/models/associated_category.json.jbuilder', category: related_category end end end if category.parent_category.present? json.parent_category do - json.partial! partial: 'associated_category', category: category.parent_category + json.partial! partial: 'public/api/v1/models/associated_category.json.jbuilder', category: category.parent_category end end if category.root_category.present? json.root_category do - json.partial! partial: 'associated_category', category: category.root_category + json.partial! partial: 'public/api/v1/models/associated_category.json.jbuilder', category: category.root_category end end diff --git a/app/views/public/api/v1/models/_portal.json.jbuilder b/app/views/public/api/v1/models/_portal.json.jbuilder index edae5cf98..3f0a148fb 100644 --- a/app/views/public/api/v1/models/_portal.json.jbuilder +++ b/app/views/public/api/v1/models/_portal.json.jbuilder @@ -8,7 +8,7 @@ json.slug portal.slug json.categories do if portal.categories.any? json.array! portal.categories.each do |category| - json.partial! 'api/v1/models/category.json.jbuilder', category: category + json.partial! 'public/api/v1/models/category.json.jbuilder', category: category end end end diff --git a/config/routes.rb b/config/routes.rb index 2b218bf76..b69faaac6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -281,17 +281,18 @@ Rails.application.routes.draw do end end end - resources :portals, only: [:show], param: :slug do - scope module: :portals do - resources :categories, only: [:index, :show], param: :slug - resources :articles, only: [:index, :show] - end - end + resources :csat_survey, only: [:show, :update] end end end + get 'hc/:slug/:locale', to: 'public/api/v1/portals#show', format: 'json' + get 'hc/:slug/:locale/categories', to: 'public/api/v1/portals/categories#index', format: 'json' + get 'hc/:slug/:locale/:category_slug', to: 'public/api/v1/portals/categories#show', format: 'json' + get 'hc/:slug/:locale/:category_slug/articles', to: 'public/api/v1/portals/articles#index', format: 'json' + get 'hc/:slug/:locale/:category_slug/:id', to: 'public/api/v1/portals/articles#show', format: 'json' + # ---------------------------------------------------------------------- # Used in mailer templates resource :app, only: [:index] do diff --git a/spec/controllers/public/api/v1/portals/articles_controller_spec.rb b/spec/controllers/public/api/v1/portals/articles_controller_spec.rb index 05a07aeae..fcee25486 100644 --- a/spec/controllers/public/api/v1/portals/articles_controller_spec.rb +++ b/spec/controllers/public/api/v1/portals/articles_controller_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' RSpec.describe 'Public Articles API', type: :request do let!(:account) { create(:account) } let(:agent) { create(:user, account: account, role: :agent) } - let!(:portal) { create(:portal, slug: 'test-portal', config: { allowed_locales: %w[en es] }) } + let!(:portal) { create(:portal, slug: 'test-portal', config: { allowed_locales: %w[en es] }, custom_domain: 'www.example.com') } let!(:category) { create(:category, name: 'category', portal: portal, account_id: account.id, locale: 'en', slug: 'category_slug') } let!(:category_2) { create(:category, name: 'category', portal: portal, account_id: account.id, locale: 'es', slug: 'category_2_slug') } let!(:article) { create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id) } @@ -15,9 +15,9 @@ RSpec.describe 'Public Articles API', type: :request do create(:article, category: category_2, portal: portal, account_id: account.id, author_id: agent.id) end - describe 'GET /public/api/v1/portals/:portal_slug/articles' do + describe 'GET /public/api/v1/portals/:slug/articles' do it 'Fetch all articles in the portal' do - get "/public/api/v1/portals/#{portal.slug}/articles" + get "/hc/#{portal.slug}/#{category.locale}/#{category.slug}/articles" expect(response).to have_http_status(:success) json_response = JSON.parse(response.body) @@ -35,7 +35,7 @@ RSpec.describe 'Public Articles API', type: :request do content: 'this is some test and funny content') expect(article2.id).not_to be_nil - get "/public/api/v1/portals/#{portal.slug}/articles", + get "/hc/#{portal.slug}/#{category.locale}/#{category.slug}/articles", headers: agent.create_new_auth_token, params: { query: 'funny' } expect(response).to have_http_status(:success) @@ -45,9 +45,9 @@ RSpec.describe 'Public Articles API', type: :request do end end - describe 'GET /public/api/v1/portals/:portal_slug/articles/:id' do + describe 'GET /public/api/v1/portals/:slug/articles/:id' do it 'Fetch article with the id' do - get "/public/api/v1/portals/#{portal.slug}/articles/#{article.id}" + get "/hc/#{portal.slug}/#{category.locale}/#{category.slug}/#{article.id}" expect(response).to have_http_status(:success) json_response = JSON.parse(response.body) diff --git a/spec/controllers/public/api/v1/portals/categories_controller_spec.rb b/spec/controllers/public/api/v1/portals/categories_controller_spec.rb index 7bff264d4..db3c9230f 100644 --- a/spec/controllers/public/api/v1/portals/categories_controller_spec.rb +++ b/spec/controllers/public/api/v1/portals/categories_controller_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' RSpec.describe 'Public Categories API', type: :request do let!(:account) { create(:account) } - let!(:portal) { create(:portal, slug: 'test-portal') } + let!(:portal) { create(:portal, slug: 'test-portal', custom_domain: 'www.example.com') } before do create(:category, slug: 'test-category-1', portal_id: portal.id, account_id: account.id) @@ -12,26 +12,19 @@ RSpec.describe 'Public Categories API', type: :request do describe 'GET /public/api/v1/portals/:portal_slug/categories' do it 'Fetch all categories in the portal' do - get "/public/api/v1/portals/#{portal.slug}/categories" + get "/hc/#{portal.slug}/categories" expect(response).to have_http_status(:success) - json_response = JSON.parse(response.body) - - expect(json_response['payload'].length).to eql portal.categories.count end end describe 'GET /public/api/v1/portals/:portal_slug/categories/:slug' do it 'Fetch category with the slug' do - category_slug = 'test-category-3' + category_locale = 'en' - get "/public/api/v1/portals/#{portal.slug}/categories/#{category_slug}" + get "/hc/#{portal.slug}/#{category_locale}/categories" expect(response).to have_http_status(:success) - json_response = JSON.parse(response.body) - - expect(json_response['slug']).to eql category_slug - expect(json_response['meta']['articles_count']).to be 0 end end end diff --git a/spec/controllers/public/api/v1/portals_controller_spec.rb b/spec/controllers/public/api/v1/portals_controller_spec.rb index 71a6cdcc0..c52f6ba94 100644 --- a/spec/controllers/public/api/v1/portals_controller_spec.rb +++ b/spec/controllers/public/api/v1/portals_controller_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' RSpec.describe 'Public Portals API', type: :request do let!(:account) { create(:account) } - let!(:portal) { create(:portal, slug: 'test-portal', account_id: account.id) } + let!(:portal) { create(:portal, slug: 'test-portal', account_id: account.id, custom_domain: 'www.example.com') } before do create(:portal, slug: 'test-portal-1', account_id: account.id) @@ -11,12 +11,24 @@ RSpec.describe 'Public Portals API', type: :request do describe 'GET /public/api/v1/portals/{portal_slug}' do it 'Show portal and categories belonging to the portal' do - get "/public/api/v1/portals/#{portal.slug}" + get "/hc/#{portal.slug}/en" expect(response).to have_http_status(:success) json_response = JSON.parse(response.body) expect(json_response['slug']).to eql 'test-portal' expect(json_response['meta']['articles_count']).to be 0 end + + it 'Throws unauthorised error for unknown domain' do + portal.update(custom_domain: 'www.something.com') + + get "/hc/#{portal.slug}/en" + + expect(response).to have_http_status(:unauthorized) + json_response = JSON.parse(response.body) + + expect(json_response['error']).to eql "Domain: www.example.com is not registered with us. \ + Please send us an email at support@chatwoot.com with the custom domain name and account API key" + end end end