From f2f33038f2d730f535504f5bd19545192fd9f5be Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Thu, 18 May 2023 18:42:28 +0530 Subject: [PATCH] fix: Make category non-mandatory (#7117) Creating articles in a new help center without categories throws an error. This PR fixes that. --- .../accounts/articles/_article.json.jbuilder | 7 +++--- .../v1/accounts/articles_controller_spec.rb | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/views/api/v1/accounts/articles/_article.json.jbuilder b/app/views/api/v1/accounts/articles/_article.json.jbuilder index 0de240a1a..8c03f8f7b 100644 --- a/app/views/api/v1/accounts/articles/_article.json.jbuilder +++ b/app/views/api/v1/accounts/articles/_article.json.jbuilder @@ -8,11 +8,12 @@ json.position article.position json.account_id article.account_id json.updated_at article.updated_at.to_i json.meta article.meta + json.category do json.id article.category_id - json.name article.category.name - json.slug article.category.slug - json.locale article.category.locale + json.name article.category&.name + json.slug article.category&.slug + json.locale article.category&.locale end json.views article.views diff --git a/spec/controllers/api/v1/accounts/articles_controller_spec.rb b/spec/controllers/api/v1/accounts/articles_controller_spec.rb index e204a346a..703542bb2 100644 --- a/spec/controllers/api/v1/accounts/articles_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/articles_controller_spec.rb @@ -41,6 +41,29 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do expect(json_response['payload']['position']).to be(3) end + it 'creates article even if category is not provided' do + article_params = { + article: { + category_id: nil, + description: 'test description', + title: 'MyTitle', + slug: 'my-title', + content: 'This is my content.', + status: :published, + author_id: agent.id, + position: 3 + } + } + 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') + expect(json_response['payload']['status']).to eql('draft') + expect(json_response['payload']['position']).to be(3) + end + it 'associate to the root article' do root_article = create(:article, category: category, slug: 'root-article', portal: portal, account_id: account.id, author_id: agent.id, associated_article_id: nil)