chore: Update Help Center APIs to accomodate locale (#4515)

This commit is contained in:
Pranav Raj S 2022-04-20 16:00:37 +05:30 committed by GitHub
parent 4f3a271355
commit f2f0d466f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 114 additions and 57 deletions

View file

@ -4,6 +4,6 @@ class Api::V1::Accounts::Kbase::BaseController < Api::V1::Accounts::BaseControll
private
def portal
@portal ||= Current.account.kbase_portals.find_by(id: params[:portal_id])
@portal ||= Current.account.kbase_portals.find_by(slug: params[:portal_id])
end
end

View file

@ -1,10 +1,12 @@
class Api::V1::Accounts::Kbase::PortalsController < Api::V1::Accounts::Kbase::BaseController
class Api::V1::Accounts::Kbase::PortalsController < Api::V1::Accounts::BaseController
before_action :fetch_portal, except: [:index, :create]
def index
@portals = Current.account.kbase_portals
end
def show; end
def create
@portal = Current.account.kbase_portals.create!(portal_params)
end
@ -21,7 +23,11 @@ class Api::V1::Accounts::Kbase::PortalsController < Api::V1::Accounts::Kbase::Ba
private
def fetch_portal
@portal = current_account.kbase_portals.find(params[:id])
@portal = Current.account.kbase_portals.find_by(slug: permitted_params[:id])
end
def permitted_params
params.permit(:id)
end
def portal_params

View file

@ -4,6 +4,7 @@
#
# id :bigint not null, primary key
# description :text
# locale :string default("en")
# name :string
# position :integer
# created_at :datetime not null
@ -11,6 +12,10 @@
# account_id :integer not null
# portal_id :integer not null
#
# Indexes
#
# index_kbase_categories_on_locale_and_account_id (locale,account_id)
#
class Kbase::Category < ApplicationRecord
belongs_to :account
belongs_to :portal

View file

@ -3,8 +3,8 @@
# Table name: kbase_portals
#
# id :bigint not null, primary key
# account_id :integer not null
# color :string
# config :jsonb
# custom_domain :string
# header_text :text
# homepage_link :string
@ -13,6 +13,7 @@
# slug :string not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
#
# Indexes
#
@ -26,5 +27,5 @@ class Kbase::Portal < ApplicationRecord
validates :account_id, presence: true
validates :name, presence: true
validates :slug, presence: true
validates :slug, presence: true, uniqueness: true
end

View file

@ -6,3 +6,4 @@ json.homepage_link portal.homepage_link
json.name portal.name
json.page_title portal.page_title
json.slug portal.slug
json.config portal.config

View file

@ -1,3 +1 @@
json.payload do
json.partial! 'portal', portal: @portal
end
json.partial! 'portal', portal: @portal

View file

@ -1,3 +1 @@
json.payload do
json.array! @portals, partial: 'portal', as: :portal
end
json.array! @portals, partial: 'portal', as: :portal

View file

@ -0,0 +1 @@
json.partial! 'portal', portal: @portal

View file

@ -1,3 +1 @@
json.payload do
json.partial! 'portal', portal: @portal
end
json.partial! 'portal', portal: @portal

View file

@ -0,0 +1 @@
json.partial! 'portal', portal: @portal

View file

@ -0,0 +1,5 @@
json.id category.id
json.name category.name
json.locale category.locale
json.description category.description
json.position category.position

View file

@ -0,0 +1,9 @@
json.id portal.id
json.color portal.color
json.custom_domain portal.custom_domain
json.header_text portal.header_text
json.homepage_link portal.homepage_link
json.name portal.name
json.page_title portal.page_title
json.slug portal.slug
json.config portal.config

View file

@ -0,0 +1,5 @@
class AddConfigToPortal < ActiveRecord::Migration[6.1]
def change
add_column :kbase_portals, :config, :jsonb, default: { allowed_locales: ['en'] }
end
end

View file

@ -0,0 +1,7 @@
class AddLocaleToKbaseCategory < ActiveRecord::Migration[6.1]
def change
add_column :kbase_categories, :locale, :string, default: 'en'
add_index :kbase_categories, :locale
add_index :kbase_categories, [:locale, :account_id]
end
end

View file

@ -507,6 +507,8 @@ ActiveRecord::Schema.define(version: 2022_04_18_094715) do
t.integer "position"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "locale", default: "en"
t.index ["locale", "account_id"], name: "index_kbase_categories_on_locale_and_account_id"
end
create_table "kbase_folders", force: :cascade do |t|
@ -528,6 +530,7 @@ ActiveRecord::Schema.define(version: 2022_04_18_094715) do
t.text "header_text"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.jsonb "config", default: {"allowed_locales"=>["en"]}
t.index ["slug"], name: "index_kbase_portals_on_slug", unique: true
end

View file

@ -6,10 +6,10 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
let!(:portal) { create(:kbase_portal, name: 'test_portal', account_id: account.id) }
let!(:category) { create(:kbase_category, name: 'category', portal: portal, account_id: account.id) }
describe 'POST /api/v1/accounts/{account.id}/kbase/portals/{portal.id}/categories' do
describe 'POST /api/v1/accounts/{account.id}/kbase/portals/{portal.slug}/categories' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}/categories", params: {}
post "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}/categories", params: {}
expect(response).to have_http_status(:unauthorized)
end
end
@ -23,7 +23,7 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
position: 1
}
}
post "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}/categories",
post "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}/categories",
params: category_params,
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
@ -33,10 +33,10 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
end
end
describe 'PUT /api/v1/accounts/{account.id}/kbase/portals/{portal.id}/categories/{category.id}' do
describe 'PUT /api/v1/accounts/{account.id}/kbase/portals/{portal.slug}/categories/{category.id}' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
put "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}/categories/#{category.id}", params: {}
put "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}/categories/#{category.id}", params: {}
expect(response).to have_http_status(:unauthorized)
end
end
@ -53,7 +53,7 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
expect(category.name).not_to eql(category_params[:category][:name])
put "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}/categories/#{category.id}",
put "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}/categories/#{category.id}",
params: category_params,
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
@ -63,17 +63,17 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
end
end
describe 'DELETE /api/v1/accounts/{account.id}/kbase/portals/{portal.id}/categories/{category.id}' do
describe 'DELETE /api/v1/accounts/{account.id}/kbase/portals/{portal.slug}/categories/{category.id}' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
delete "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}/categories/#{category.id}", params: {}
delete "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}/categories/#{category.id}", params: {}
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
it 'deletes category' do
delete "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}/categories/#{category.id}",
delete "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}/categories/#{category.id}",
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
deleted_category = Kbase::Category.find_by(id: category.id)
@ -82,10 +82,10 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
end
end
describe 'GET /api/v1/accounts/{account.id}/kbase/portals/{portal.id}/categories' do
describe 'GET /api/v1/accounts/{account.id}/kbase/portals/{portal.slug}/categories' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}/categories"
get "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}/categories"
expect(response).to have_http_status(:unauthorized)
end
end
@ -95,7 +95,7 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Categories', type: :request do
category2 = create(:kbase_category, name: 'test_category_2', portal: portal)
expect(category2.id).not_to be nil
get "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}/categories",
get "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}/categories",
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)

View file

@ -3,7 +3,47 @@ require 'rails_helper'
RSpec.describe 'Api::V1::Accounts::Kbase::Portals', type: :request do
let(:account) { create(:account) }
let(:agent) { create(:user, account: account, role: :agent) }
let!(:portal) { create(:kbase_portal, name: 'test_portal', account_id: account.id) }
let!(:portal) { create(:kbase_portal, slug: 'portal-1', name: 'test_portal', account_id: account.id) }
describe 'GET /api/v1/accounts/{account.id}/kbase/portals' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/kbase/portals"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
it 'get all portals' do
portal2 = create(:kbase_portal, name: 'test_portal_2', account_id: account.id, slug: 'portal-2')
expect(portal2.id).not_to be nil
get "/api/v1/accounts/#{account.id}/kbase/portals",
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response.count).to be 2
end
end
end
describe 'GET /api/v1/accounts/{account.id}/kbase/portals/{portal.slug}' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/kbase/portals"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
it 'get one portals' do
get "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}",
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['name']).to eq portal.name
end
end
end
describe 'POST /api/v1/accounts/{account.id}/kbase/portals' do
context 'when it is an unauthenticated user' do
@ -26,15 +66,15 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Portals', type: :request do
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['payload']['name']).to eql('test_portal')
expect(json_response['name']).to eql('test_portal')
end
end
end
describe 'PUT /api/v1/accounts/{account.id}/kbase/portals/{portal.id}' do
describe 'PUT /api/v1/accounts/{account.id}/kbase/portals/{portal.slug}' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
put "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}", params: {}
put "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}", params: {}
expect(response).to have_http_status(:unauthorized)
end
end
@ -49,53 +89,32 @@ RSpec.describe 'Api::V1::Accounts::Kbase::Portals', type: :request do
expect(portal.name).to eql('test_portal')
put "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}",
put "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}",
params: portal_params,
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['payload']['name']).to eql(portal_params[:portal][:name])
expect(json_response['name']).to eql(portal_params[:portal][:name])
end
end
end
describe 'DELETE /api/v1/accounts/{account.id}/kbase/portals/{portal.id}' do
describe 'DELETE /api/v1/accounts/{account.id}/kbase/portals/{portal.slug}' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
delete "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}", params: {}
delete "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}", params: {}
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
it 'deletes portal' do
delete "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.id}",
delete "/api/v1/accounts/#{account.id}/kbase/portals/#{portal.slug}",
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
deleted_portal = Kbase::Portal.find_by(id: portal.id)
deleted_portal = Kbase::Portal.find_by(id: portal.slug)
expect(deleted_portal).to be nil
end
end
end
describe 'GET /api/v1/accounts/{account.id}/kbase/portals' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/kbase/portals"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
it 'get all portals' do
portal2 = create(:kbase_portal, name: 'test_portal_2', account_id: account.id)
expect(portal2.id).not_to be nil
get "/api/v1/accounts/#{account.id}/kbase/portals",
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['payload'].count).to be 2
end
end
end
end