parent
2198930185
commit
ae72757d23
23 changed files with 511 additions and 42 deletions
3
Gemfile
3
Gemfile
|
@ -128,6 +128,9 @@ gem 'html2text'
|
||||||
# to calculate working hours
|
# to calculate working hours
|
||||||
gem 'working_hours'
|
gem 'working_hours'
|
||||||
|
|
||||||
|
# full text search for articles
|
||||||
|
gem 'pg_search'
|
||||||
|
|
||||||
group :production, :staging do
|
group :production, :staging do
|
||||||
# we dont want request timing out in development while using byebug
|
# we dont want request timing out in development while using byebug
|
||||||
gem 'rack-timeout'
|
gem 'rack-timeout'
|
||||||
|
|
|
@ -388,6 +388,9 @@ GEM
|
||||||
parser (3.1.1.0)
|
parser (3.1.1.0)
|
||||||
ast (~> 2.4.1)
|
ast (~> 2.4.1)
|
||||||
pg (1.3.2)
|
pg (1.3.2)
|
||||||
|
pg_search (2.3.6)
|
||||||
|
activerecord (>= 5.2)
|
||||||
|
activesupport (>= 5.2)
|
||||||
procore-sift (0.16.0)
|
procore-sift (0.16.0)
|
||||||
rails (> 4.2.0)
|
rails (> 4.2.0)
|
||||||
pry (0.14.1)
|
pry (0.14.1)
|
||||||
|
@ -694,6 +697,7 @@ DEPENDENCIES
|
||||||
mock_redis
|
mock_redis
|
||||||
newrelic_rpm
|
newrelic_rpm
|
||||||
pg
|
pg
|
||||||
|
pg_search
|
||||||
procore-sift
|
procore-sift
|
||||||
pry-rails
|
pry-rails
|
||||||
puma
|
puma
|
||||||
|
@ -742,4 +746,4 @@ RUBY VERSION
|
||||||
ruby 3.0.4p208
|
ruby 3.0.4p208
|
||||||
|
|
||||||
BUNDLED WITH
|
BUNDLED WITH
|
||||||
2.3.14
|
2.3.15
|
||||||
|
|
48
app/controllers/api/v1/accounts/articles_controller.rb
Normal file
48
app/controllers/api/v1/accounts/articles_controller.rb
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController
|
||||||
|
before_action :portal
|
||||||
|
before_action :fetch_article, except: [:index, :create]
|
||||||
|
|
||||||
|
def index
|
||||||
|
@articles = @portal.articles
|
||||||
|
@articles.search(list_params) if params[:payload].present?
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
@article = @portal.articles.create!(article_params)
|
||||||
|
end
|
||||||
|
|
||||||
|
def edit; end
|
||||||
|
|
||||||
|
def show; end
|
||||||
|
|
||||||
|
def update
|
||||||
|
@article.update!(article_params)
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
@article.destroy!
|
||||||
|
head :ok
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def fetch_article
|
||||||
|
@article = @portal.articles.find(params[:id])
|
||||||
|
end
|
||||||
|
|
||||||
|
def portal
|
||||||
|
@portal ||= Current.account.portals.find_by(slug: params[:portal_id])
|
||||||
|
end
|
||||||
|
|
||||||
|
def article_params
|
||||||
|
params.require(:article).permit(
|
||||||
|
:title, :content, :description, :position, :category_id, :author_id
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def list_params
|
||||||
|
params.require(:payload).permit(
|
||||||
|
:category_slug, :locale, :query
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
|
@ -3,7 +3,7 @@ class Api::V1::Accounts::CategoriesController < Api::V1::Accounts::BaseControlle
|
||||||
before_action :fetch_category, except: [:index, :create]
|
before_action :fetch_category, except: [:index, :create]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@categories = @portal.categories
|
@categories = @portal.categories.search(params)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
@ -31,7 +31,7 @@ class Api::V1::Accounts::CategoriesController < Api::V1::Accounts::BaseControlle
|
||||||
|
|
||||||
def category_params
|
def category_params
|
||||||
params.require(:category).permit(
|
params.require(:category).permit(
|
||||||
:name, :description, :position
|
:name, :description, :position, :slug, :locale
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,25 +2,34 @@
|
||||||
#
|
#
|
||||||
# Table name: articles
|
# Table name: articles
|
||||||
#
|
#
|
||||||
# id :bigint not null, primary key
|
# id :bigint not null, primary key
|
||||||
# content :text
|
# content :text
|
||||||
# description :text
|
# description :text
|
||||||
# status :integer
|
# status :integer
|
||||||
# title :string
|
# title :string
|
||||||
# views :integer
|
# views :integer
|
||||||
# created_at :datetime not null
|
# created_at :datetime not null
|
||||||
# updated_at :datetime not null
|
# updated_at :datetime not null
|
||||||
# account_id :integer not null
|
# account_id :integer not null
|
||||||
# author_id :integer
|
# author_id :bigint
|
||||||
# category_id :integer
|
# category_id :integer
|
||||||
# folder_id :integer
|
# folder_id :integer
|
||||||
# portal_id :integer not null
|
# portal_id :integer not null
|
||||||
|
#
|
||||||
|
# Indexes
|
||||||
|
#
|
||||||
|
# index_articles_on_author_id (author_id)
|
||||||
|
#
|
||||||
|
# Foreign Keys
|
||||||
|
#
|
||||||
|
# fk_rails_... (author_id => users.id)
|
||||||
#
|
#
|
||||||
class Article < ApplicationRecord
|
class Article < ApplicationRecord
|
||||||
|
include PgSearch::Model
|
||||||
|
|
||||||
belongs_to :account
|
belongs_to :account
|
||||||
belongs_to :category
|
belongs_to :category
|
||||||
belongs_to :portal
|
belongs_to :portal
|
||||||
belongs_to :folder
|
|
||||||
belongs_to :author, class_name: 'User'
|
belongs_to :author, class_name: 'User'
|
||||||
|
|
||||||
before_validation :ensure_account_id
|
before_validation :ensure_account_id
|
||||||
|
@ -32,6 +41,36 @@ class Article < ApplicationRecord
|
||||||
|
|
||||||
enum status: { draft: 0, published: 1 }
|
enum status: { draft: 0, published: 1 }
|
||||||
|
|
||||||
|
scope :search_by_category_slug, ->(category_slug) { where(categories: { slug: category_slug }) if category_slug.present? }
|
||||||
|
scope :search_by_category_locale, ->(locale) { where(categories: { locale: locale }) if locale.present? }
|
||||||
|
|
||||||
|
# TODO: if text search slows down https://www.postgresql.org/docs/current/textsearch-features.html#TEXTSEARCH-UPDATE-TRIGGERS
|
||||||
|
pg_search_scope(
|
||||||
|
:text_search,
|
||||||
|
against: %i[
|
||||||
|
title
|
||||||
|
description
|
||||||
|
content
|
||||||
|
],
|
||||||
|
using: {
|
||||||
|
tsearch: {
|
||||||
|
prefix: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
def self.search(params)
|
||||||
|
records = joins(
|
||||||
|
:category
|
||||||
|
).search_by_category_slug(params[:category_slug]).search_by_category_locale(params[:locale])
|
||||||
|
records.text_search(params[:query]) if params[:query].present?
|
||||||
|
records.page(current_page(params))
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.current_page(params)
|
||||||
|
params[:page] || 1
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def ensure_account_id
|
def ensure_account_id
|
||||||
|
|
|
@ -2,20 +2,22 @@
|
||||||
#
|
#
|
||||||
# Table name: categories
|
# Table name: categories
|
||||||
#
|
#
|
||||||
# id :bigint not null, primary key
|
# id :bigint not null, primary key
|
||||||
# description :text
|
# description :text
|
||||||
# locale :string default("en")
|
# locale :string default("en")
|
||||||
# name :string
|
# name :string
|
||||||
# position :integer
|
# position :integer
|
||||||
# created_at :datetime not null
|
# slug :string not null
|
||||||
# updated_at :datetime not null
|
# created_at :datetime not null
|
||||||
# account_id :integer not null
|
# updated_at :datetime not null
|
||||||
# portal_id :integer not null
|
# account_id :integer not null
|
||||||
|
# portal_id :integer not null
|
||||||
#
|
#
|
||||||
# Indexes
|
# Indexes
|
||||||
#
|
#
|
||||||
# index_categories_on_locale (locale)
|
# index_categories_on_locale (locale)
|
||||||
# index_categories_on_locale_and_account_id (locale,account_id)
|
# index_categories_on_locale_and_account_id (locale,account_id)
|
||||||
|
# index_categories_on_slug_and_locale_and_portal_id (slug,locale,portal_id) UNIQUE
|
||||||
#
|
#
|
||||||
class Category < ApplicationRecord
|
class Category < ApplicationRecord
|
||||||
belongs_to :account
|
belongs_to :account
|
||||||
|
@ -25,7 +27,20 @@ class Category < ApplicationRecord
|
||||||
|
|
||||||
before_validation :ensure_account_id
|
before_validation :ensure_account_id
|
||||||
validates :account_id, presence: true
|
validates :account_id, presence: true
|
||||||
|
validates :slug, presence: true
|
||||||
validates :name, presence: true
|
validates :name, presence: true
|
||||||
|
validates :locale, uniqueness: { scope: %i[slug portal_id],
|
||||||
|
message: 'should be unique in the category and portal' }
|
||||||
|
|
||||||
|
scope :search_by_locale, ->(locale) { where(locale: locale) if locale.present? }
|
||||||
|
|
||||||
|
def self.search(params)
|
||||||
|
search_by_locale(params[:locale]).page(current_page(params)).order(position: :asc)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.current_page(params)
|
||||||
|
params[:page] || 1
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
|
|
@ -92,6 +92,7 @@ class User < ApplicationRecord
|
||||||
has_many :portals, through: :portals_members
|
has_many :portals, through: :portals_members
|
||||||
has_many :team_members, dependent: :destroy_async
|
has_many :team_members, dependent: :destroy_async
|
||||||
has_many :teams, through: :team_members
|
has_many :teams, through: :team_members
|
||||||
|
has_many :articles, foreign_key: 'author_id', dependent: :nullify
|
||||||
|
|
||||||
before_validation :set_password_and_uid, on: :create
|
before_validation :set_password_and_uid, on: :create
|
||||||
|
|
||||||
|
|
20
app/views/api/v1/accounts/articles/_article.json.jbuilder
Normal file
20
app/views/api/v1/accounts/articles/_article.json.jbuilder
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
json.id article.id
|
||||||
|
json.category_id article.category_id
|
||||||
|
json.title article.title
|
||||||
|
json.content article.content
|
||||||
|
json.description article.description
|
||||||
|
json.status article.status
|
||||||
|
json.account_id article.account_id
|
||||||
|
|
||||||
|
if article.portal.present?
|
||||||
|
json.portal do
|
||||||
|
json.partial! 'api/v1/accounts/portals/portal.json.jbuilder', portal: article.portal
|
||||||
|
end
|
||||||
|
end
|
||||||
|
json.views article.views
|
||||||
|
|
||||||
|
if article.author.present?
|
||||||
|
json.author do
|
||||||
|
json.partial! 'api/v1/models/agent.json.jbuilder', resource: article.author
|
||||||
|
end
|
||||||
|
end
|
3
app/views/api/v1/accounts/articles/create.json.jbuilder
Normal file
3
app/views/api/v1/accounts/articles/create.json.jbuilder
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
json.payload do
|
||||||
|
json.partial! 'article', article: @article
|
||||||
|
end
|
3
app/views/api/v1/accounts/articles/edit.json.jbuilder
Normal file
3
app/views/api/v1/accounts/articles/edit.json.jbuilder
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
json.payload do
|
||||||
|
json.partial! 'article', article: @article
|
||||||
|
end
|
3
app/views/api/v1/accounts/articles/index.json.jbuilder
Normal file
3
app/views/api/v1/accounts/articles/index.json.jbuilder
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
json.payload do
|
||||||
|
json.array! @articles, partial: 'article', as: :article
|
||||||
|
end
|
3
app/views/api/v1/accounts/articles/show.json.jbuilder
Normal file
3
app/views/api/v1/accounts/articles/show.json.jbuilder
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
json.payload do
|
||||||
|
json.partial! 'article', article: @article
|
||||||
|
end
|
3
app/views/api/v1/accounts/articles/update.json.jbuilder
Normal file
3
app/views/api/v1/accounts/articles/update.json.jbuilder
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
json.payload do
|
||||||
|
json.partial! 'article', article: @article
|
||||||
|
end
|
|
@ -1,5 +1,7 @@
|
||||||
json.id category.id
|
json.id category.id
|
||||||
json.name category.name
|
json.name category.name
|
||||||
|
json.slug category.slug
|
||||||
|
json.locale category.locale
|
||||||
json.description category.description
|
json.description category.description
|
||||||
json.position category.position
|
json.position category.position
|
||||||
json.account_id category.account_id
|
json.account_id category.account_id
|
||||||
|
|
|
@ -162,8 +162,8 @@ Rails.application.routes.draw do
|
||||||
resources :categories do
|
resources :categories do
|
||||||
resources :folders
|
resources :folders
|
||||||
end
|
end
|
||||||
|
resources :articles
|
||||||
end
|
end
|
||||||
resources :articles
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
# end of account scoped api routes
|
# end of account scoped api routes
|
||||||
|
|
6
db/migrate/20220527080906_add_reference_for_author_id.rb
Normal file
6
db/migrate/20220527080906_add_reference_for_author_id.rb
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
class AddReferenceForAuthorId < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
remove_column :articles, :author_id, :integer
|
||||||
|
add_reference :articles, :author, foreign_key: { to_table: :users }
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,7 @@
|
||||||
|
class AddIndexOnCategorySlugAndLocale < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
add_column :categories, :slug, :string, null: false, default: ''
|
||||||
|
add_index :categories, [:slug, :locale, :portal_id], unique: true
|
||||||
|
change_column_default :categories, :slug, from: '', to: nil
|
||||||
|
end
|
||||||
|
end
|
|
@ -116,7 +116,6 @@ ActiveRecord::Schema.define(version: 2022_06_10_091206) do
|
||||||
t.integer "portal_id", null: false
|
t.integer "portal_id", null: false
|
||||||
t.integer "category_id"
|
t.integer "category_id"
|
||||||
t.integer "folder_id"
|
t.integer "folder_id"
|
||||||
t.integer "author_id"
|
|
||||||
t.string "title"
|
t.string "title"
|
||||||
t.text "description"
|
t.text "description"
|
||||||
t.text "content"
|
t.text "content"
|
||||||
|
@ -124,6 +123,8 @@ ActiveRecord::Schema.define(version: 2022_06_10_091206) do
|
||||||
t.integer "views"
|
t.integer "views"
|
||||||
t.datetime "created_at", precision: 6, null: false
|
t.datetime "created_at", precision: 6, null: false
|
||||||
t.datetime "updated_at", precision: 6, null: false
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
|
t.bigint "author_id"
|
||||||
|
t.index ["author_id"], name: "index_articles_on_author_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "attachments", id: :serial, force: :cascade do |t|
|
create_table "attachments", id: :serial, force: :cascade do |t|
|
||||||
|
@ -193,8 +194,10 @@ ActiveRecord::Schema.define(version: 2022_06_10_091206) do
|
||||||
t.datetime "created_at", precision: 6, null: false
|
t.datetime "created_at", precision: 6, null: false
|
||||||
t.datetime "updated_at", precision: 6, null: false
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
t.string "locale", default: "en"
|
t.string "locale", default: "en"
|
||||||
|
t.string "slug", null: false
|
||||||
t.index ["locale", "account_id"], name: "index_categories_on_locale_and_account_id"
|
t.index ["locale", "account_id"], name: "index_categories_on_locale_and_account_id"
|
||||||
t.index ["locale"], name: "index_categories_on_locale"
|
t.index ["locale"], name: "index_categories_on_locale"
|
||||||
|
t.index ["slug", "locale", "portal_id"], name: "index_categories_on_slug_and_locale_and_portal_id", unique: true
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "channel_api", force: :cascade do |t|
|
create_table "channel_api", force: :cascade do |t|
|
||||||
|
@ -817,6 +820,7 @@ ActiveRecord::Schema.define(version: 2022_06_10_091206) do
|
||||||
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
|
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
|
||||||
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
|
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
|
||||||
add_foreign_key "agent_bots", "accounts", on_delete: :cascade
|
add_foreign_key "agent_bots", "accounts", on_delete: :cascade
|
||||||
|
add_foreign_key "articles", "users", column: "author_id"
|
||||||
add_foreign_key "campaigns", "accounts", on_delete: :cascade
|
add_foreign_key "campaigns", "accounts", on_delete: :cascade
|
||||||
add_foreign_key "campaigns", "inboxes", on_delete: :cascade
|
add_foreign_key "campaigns", "inboxes", on_delete: :cascade
|
||||||
add_foreign_key "contact_inboxes", "contacts", on_delete: :cascade
|
add_foreign_key "contact_inboxes", "contacts", on_delete: :cascade
|
||||||
|
|
137
spec/controllers/api/v1/accounts/articles_controller_spec.rb
Normal file
137
spec/controllers/api/v1/accounts/articles_controller_spec.rb
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe 'Api::V1::Accounts::Articles', type: :request do
|
||||||
|
let(:account) { create(:account) }
|
||||||
|
let(:agent) { create(:user, account: account, role: :agent) }
|
||||||
|
let!(:portal) { create(:portal, name: 'test_portal', account_id: account.id) }
|
||||||
|
let!(:category) { create(:category, name: 'category', portal: portal, account_id: account.id, locale: 'en', slug: 'category_slug') }
|
||||||
|
let!(:article) { create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id) }
|
||||||
|
|
||||||
|
describe 'POST /api/v1/accounts/{account.id}/portals/{portal.slug}/articles' do
|
||||||
|
context 'when it is an unauthenticated user' do
|
||||||
|
it 'returns unauthorized' do
|
||||||
|
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles", params: {}
|
||||||
|
expect(response).to have_http_status(:unauthorized)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when it is an authenticated user' do
|
||||||
|
it 'creates article' do
|
||||||
|
article_params = {
|
||||||
|
article: {
|
||||||
|
category_id: category.id,
|
||||||
|
description: 'test description',
|
||||||
|
title: 'MyTitle',
|
||||||
|
content: 'This is my content.',
|
||||||
|
status: :published,
|
||||||
|
author_id: agent.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles",
|
||||||
|
params: article_params,
|
||||||
|
headers: agent.create_new_auth_token
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
json_response = JSON.parse(response.body)
|
||||||
|
expect(json_response['payload']['title']).to eql('MyTitle')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'PUT /api/v1/accounts/{account.id}/portals/{portal.slug}/articles/{article.id}' do
|
||||||
|
context 'when it is an unauthenticated user' do
|
||||||
|
it 'returns unauthorized' do
|
||||||
|
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{article.id}", params: {}
|
||||||
|
expect(response).to have_http_status(:unauthorized)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when it is an authenticated user' do
|
||||||
|
it 'updates category' do
|
||||||
|
article_params = {
|
||||||
|
article: {
|
||||||
|
title: 'MyTitle2',
|
||||||
|
description: 'test_description'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(article.title).not_to eql(article_params[:article][:title])
|
||||||
|
|
||||||
|
put "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{article.id}",
|
||||||
|
params: article_params,
|
||||||
|
headers: agent.create_new_auth_token
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
json_response = JSON.parse(response.body)
|
||||||
|
expect(json_response['payload']['title']).to eql(article_params[:article][:title])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'DELETE /api/v1/accounts/{account.id}/portals/{portal.slug}/articles/{article.id}' do
|
||||||
|
context 'when it is an unauthenticated user' do
|
||||||
|
it 'returns unauthorized' do
|
||||||
|
delete "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{article.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}/portals/#{portal.slug}/articles/#{article.id}",
|
||||||
|
headers: agent.create_new_auth_token
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
deleted_article = Article.find_by(id: article.id)
|
||||||
|
expect(deleted_article).to be nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'GET /api/v1/accounts/{account.id}/portals/{portal.slug}/articles' do
|
||||||
|
context 'when it is an unauthenticated user' do
|
||||||
|
it 'returns unauthorized' do
|
||||||
|
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles"
|
||||||
|
expect(response).to have_http_status(:unauthorized)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when it is an authenticated user' do
|
||||||
|
it 'get all articles' do
|
||||||
|
article2 = create(:article, account_id: account.id, portal: portal, category: category, author_id: agent.id)
|
||||||
|
expect(article2.id).not_to be nil
|
||||||
|
|
||||||
|
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles",
|
||||||
|
headers: agent.create_new_auth_token,
|
||||||
|
params: { payload: {} }
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
json_response = JSON.parse(response.body)
|
||||||
|
expect(json_response['payload'].count).to be 2
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'get all articles with searched params' do
|
||||||
|
article2 = create(:article, account_id: account.id, portal: portal, category: category, author_id: agent.id)
|
||||||
|
expect(article2.id).not_to be nil
|
||||||
|
|
||||||
|
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles",
|
||||||
|
headers: agent.create_new_auth_token,
|
||||||
|
params: { payload: { category_slug: category.slug } }
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
json_response = JSON.parse(response.body)
|
||||||
|
expect(json_response['payload'].count).to be 2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'GET /api/v1/accounts/{account.id}/portals/{portal.slug}/articles/{article.id}' do
|
||||||
|
it 'get article' do
|
||||||
|
article2 = create(:article, account_id: account.id, portal: portal, category: category, author_id: agent.id)
|
||||||
|
expect(article2.id).not_to be nil
|
||||||
|
|
||||||
|
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles/#{article2.id}",
|
||||||
|
headers: agent.create_new_auth_token
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
json_response = JSON.parse(response.body)
|
||||||
|
|
||||||
|
expect(json_response['payload']['title']).to eq(article2.title)
|
||||||
|
expect(json_response['payload']['id']).to eq(article2.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -4,7 +4,7 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||||
let(:account) { create(:account) }
|
let(:account) { create(:account) }
|
||||||
let(:agent) { create(:user, account: account, role: :agent) }
|
let(:agent) { create(:user, account: account, role: :agent) }
|
||||||
let!(:portal) { create(:portal, name: 'test_portal', account_id: account.id) }
|
let!(:portal) { create(:portal, name: 'test_portal', account_id: account.id) }
|
||||||
let!(:category) { create(:category, name: 'category', portal: portal, account_id: account.id) }
|
let!(:category) { create(:category, name: 'category', portal: portal, account_id: account.id, slug: 'category_slug') }
|
||||||
|
|
||||||
describe 'POST /api/v1/accounts/{account.id}/portals/{portal.slug}/categories' do
|
describe 'POST /api/v1/accounts/{account.id}/portals/{portal.slug}/categories' do
|
||||||
context 'when it is an unauthenticated user' do
|
context 'when it is an unauthenticated user' do
|
||||||
|
@ -15,14 +15,17 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when it is an authenticated user' do
|
context 'when it is an authenticated user' do
|
||||||
it 'creates category' do
|
category_params = {
|
||||||
category_params = {
|
category: {
|
||||||
category: {
|
name: 'test_category',
|
||||||
name: 'test_category',
|
description: 'test_description',
|
||||||
description: 'test_description',
|
position: 1,
|
||||||
position: 1
|
locale: 'es',
|
||||||
}
|
slug: 'test_category_1'
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
it 'creates category' do
|
||||||
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories",
|
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories",
|
||||||
params: category_params,
|
params: category_params,
|
||||||
headers: agent.create_new_auth_token
|
headers: agent.create_new_auth_token
|
||||||
|
@ -30,6 +33,37 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||||
json_response = JSON.parse(response.body)
|
json_response = JSON.parse(response.body)
|
||||||
expect(json_response['payload']['name']).to eql('test_category')
|
expect(json_response['payload']['name']).to eql('test_category')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'will throw an error on locale, category_id uniqueness' do
|
||||||
|
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories",
|
||||||
|
params: category_params,
|
||||||
|
headers: agent.create_new_auth_token
|
||||||
|
|
||||||
|
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories",
|
||||||
|
params: category_params,
|
||||||
|
headers: agent.create_new_auth_token
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
|
json_response = JSON.parse(response.body)
|
||||||
|
expect(json_response['message']).to eql('Locale should be unique in the category and portal')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'will throw an error slug presence' do
|
||||||
|
category_params = {
|
||||||
|
category: {
|
||||||
|
name: 'test_category',
|
||||||
|
description: 'test_description',
|
||||||
|
position: 1,
|
||||||
|
locale: 'es'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories",
|
||||||
|
params: category_params,
|
||||||
|
headers: agent.create_new_auth_token
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
|
json_response = JSON.parse(response.body)
|
||||||
|
expect(json_response['message']).to eql("Slug can't be blank")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -92,7 +126,7 @@ RSpec.describe 'Api::V1::Accounts::Categories', type: :request do
|
||||||
|
|
||||||
context 'when it is an authenticated user' do
|
context 'when it is an authenticated user' do
|
||||||
it 'get all portals' do
|
it 'get all portals' do
|
||||||
category2 = create(:category, name: 'test_category_2', portal: portal)
|
category2 = create(:category, name: 'test_category_2', portal: portal, locale: 'es', slug: 'category_slug_2')
|
||||||
expect(category2.id).not_to be nil
|
expect(category2.id).not_to be nil
|
||||||
|
|
||||||
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories",
|
get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/categories",
|
||||||
|
|
|
@ -2,13 +2,11 @@ FactoryBot.define do
|
||||||
factory :article, class: 'Article' do
|
factory :article, class: 'Article' do
|
||||||
account_id { 1 }
|
account_id { 1 }
|
||||||
category_id { 1 }
|
category_id { 1 }
|
||||||
folder_id { 1 }
|
|
||||||
author_id { 1 }
|
author_id { 1 }
|
||||||
title { 'MyString' }
|
title { 'MyString' }
|
||||||
content { 'MyText' }
|
content { 'MyText' }
|
||||||
|
description { 'MyDescrption' }
|
||||||
status { 1 }
|
status { 1 }
|
||||||
views { 1 }
|
views { 1 }
|
||||||
seo_title { 'MyString' }
|
|
||||||
seo { '' }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,4 +14,105 @@ RSpec.describe Article, type: :model do
|
||||||
it { is_expected.to belong_to(:category) }
|
it { is_expected.to belong_to(:category) }
|
||||||
it { is_expected.to belong_to(:author) }
|
it { is_expected.to belong_to(:author) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'search' do
|
||||||
|
let!(:account) { create(:account) }
|
||||||
|
let(:user) { create(:user, account_ids: [account.id], role: :agent) }
|
||||||
|
let!(:portal_1) { create(:portal, account_id: account.id) }
|
||||||
|
let!(:portal_2) { create(:portal, account_id: account.id) }
|
||||||
|
let!(:category_1) { create(:category, slug: 'category_1', locale: 'en', portal_id: portal_1.id) }
|
||||||
|
let!(:category_2) { create(:category, slug: 'category_2', locale: 'es', portal_id: portal_1.id) }
|
||||||
|
let!(:category_3) { create(:category, slug: 'category_3', locale: 'es', portal_id: portal_2.id) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
create(:article, category_id: category_1.id, content: 'This is the content', description: 'this is the description', title: 'this is title',
|
||||||
|
portal_id: portal_1.id, author_id: user.id)
|
||||||
|
create(:article, category_id: category_1.id, title: 'title 1', portal_id: portal_1.id, author_id: user.id)
|
||||||
|
create(:article, category_id: category_2.id, title: 'title 2', portal_id: portal_2.id, author_id: user.id)
|
||||||
|
create(:article, category_id: category_2.id, title: 'title 3', portal_id: portal_1.id, author_id: user.id)
|
||||||
|
create(:article, category_id: category_3.id, title: 'title 6', portal_id: portal_2.id, author_id: user.id)
|
||||||
|
create(:article, category_id: category_2.id, title: 'title 7', portal_id: portal_1.id, author_id: user.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when no parameters passed' do
|
||||||
|
it 'returns all the articles in portal' do
|
||||||
|
records = portal_1.articles.search({})
|
||||||
|
expect(records.count).to eq(portal_1.articles.count)
|
||||||
|
|
||||||
|
records = portal_2.articles.search({})
|
||||||
|
expect(records.count).to eq(portal_2.articles.count)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when params passed' do
|
||||||
|
it 'returns all the articles with all the params filters' do
|
||||||
|
params = { query: 'title', locale: 'es', category_slug: 'category_3' }
|
||||||
|
records = portal_2.articles.search(params)
|
||||||
|
expect(records.count).to eq(1)
|
||||||
|
|
||||||
|
params = { query: 'this', locale: 'en', category_slug: 'category_1' }
|
||||||
|
records = portal_1.articles.search(params)
|
||||||
|
expect(records.count).to eq(2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when some params missing' do
|
||||||
|
it 'returns data with category slug' do
|
||||||
|
params = { category_slug: 'category_2' }
|
||||||
|
records = portal_1.articles.search(params)
|
||||||
|
expect(records.count).to eq(2)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns data with locale' do
|
||||||
|
params = { locale: 'es' }
|
||||||
|
records = portal_2.articles.search(params)
|
||||||
|
expect(records.count).to eq(2)
|
||||||
|
|
||||||
|
params = { locale: 'en' }
|
||||||
|
records = portal_1.articles.search(params)
|
||||||
|
expect(records.count).to eq(2)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns data with text_search query' do
|
||||||
|
params = { query: 'title' }
|
||||||
|
records = portal_2.articles.search(params)
|
||||||
|
expect(records.count).to eq(2)
|
||||||
|
|
||||||
|
params = { query: 'title' }
|
||||||
|
records = portal_1.articles.search(params)
|
||||||
|
expect(records.count).to eq(4)
|
||||||
|
|
||||||
|
params = { query: 'the content' }
|
||||||
|
records = portal_2.articles.search(params)
|
||||||
|
expect(records.count).to eq(2)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns data with text_search query and locale' do
|
||||||
|
params = { query: 'the title', locale: 'es' }
|
||||||
|
records = portal_2.articles.search(params)
|
||||||
|
expect(records.count).to eq(2)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns records with locale and category_slug' do
|
||||||
|
params = { category_slug: 'category_2', locale: 'es' }
|
||||||
|
records = portal_1.articles.search(params)
|
||||||
|
expect(records.count).to eq(2)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'return records with category_slug and text_search query' do
|
||||||
|
params = { category_slug: 'category_2', query: 'the title' }
|
||||||
|
records = portal_1.articles.search(params)
|
||||||
|
expect(records.count).to eq(2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with pagination' do
|
||||||
|
it 'returns paginated articles' do
|
||||||
|
create_list(:article, 30, category_id: category_2.id, title: 'title 1', portal_id: portal_2.id, author_id: user.id)
|
||||||
|
params = { category_slug: 'category_2' }
|
||||||
|
records = portal_2.articles.search(params)
|
||||||
|
expect(records.count).to eq(25)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,4 +12,39 @@ RSpec.describe Category, type: :model do
|
||||||
it { is_expected.to have_many(:folders) }
|
it { is_expected.to have_many(:folders) }
|
||||||
it { is_expected.to have_many(:articles) }
|
it { is_expected.to have_many(:articles) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'search' do
|
||||||
|
let!(:account) { create(:account) }
|
||||||
|
let(:user) { create(:user, account_ids: [account.id], role: :agent) }
|
||||||
|
let!(:portal_1) { create(:portal, account_id: account.id) }
|
||||||
|
let!(:portal_2) { create(:portal, account_id: account.id) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
create(:category, slug: 'category_1', locale: 'en', portal_id: portal_1.id)
|
||||||
|
create(:category, slug: 'category_2', locale: 'es', portal_id: portal_1.id)
|
||||||
|
create(:category, slug: 'category_3', locale: 'es', portal_id: portal_2.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when no parameters passed' do
|
||||||
|
it 'returns all the articles in portal' do
|
||||||
|
records = portal_1.categories.search({})
|
||||||
|
expect(records.count).to eq(portal_1.categories.count)
|
||||||
|
|
||||||
|
records = portal_2.categories.search({})
|
||||||
|
expect(records.count).to eq(portal_2.categories.count)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when params passed' do
|
||||||
|
it 'returns all the categories with all the params filters' do
|
||||||
|
params = { locale: 'es' }
|
||||||
|
records = portal_2.categories.search(params)
|
||||||
|
expect(records.count).to eq(1)
|
||||||
|
|
||||||
|
params = { locale: 'en' }
|
||||||
|
records = portal_1.categories.search(params)
|
||||||
|
expect(records.count).to eq(1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue