-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
494 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
class InsightsController < Api::V1Controller | ||
before_action :find_insightable | ||
|
||
def index | ||
render json: { | ||
insights: InsightSerializer.new( | ||
actual_insights, | ||
{ | ||
params: { | ||
previous_insights: previous_insights, | ||
insight_fields: insight_fields, | ||
ratio_enabled: ratio_enabled?, | ||
ratio_type: ratio_type | ||
} | ||
} | ||
).serializable_hash, | ||
ratio_type: ratio_enabled? ? ratio_type : nil | ||
}.compact, status: :ok | ||
end | ||
|
||
private | ||
|
||
def find_insightable | ||
find_company if params[:company_id] | ||
find_repository if params[:repository_id] | ||
|
||
page_not_found unless @insightable | ||
end | ||
|
||
def find_company | ||
@insightable = authorized_scope(Company.order(id: :desc)).find_by(uuid: params[:company_id]) | ||
end | ||
|
||
def find_repository | ||
@insightable = authorized_scope(Repository.order(id: :desc)).find_by(uuid: params[:repository_id]) | ||
end | ||
|
||
def visible_insights | ||
@visible_insights ||= | ||
Insights::VisibleQuery | ||
.new(relation: @insightable.insights) | ||
.resolve(insightable: @insightable) | ||
.load | ||
end | ||
|
||
def actual_insights | ||
visible_insights.select(&:actual?) | ||
end | ||
|
||
def previous_insights | ||
visible_insights.reject(&:actual?) | ||
end | ||
|
||
def insight_fields | ||
if @insightable.premium? && @insightable.configuration.insight_fields.present? | ||
@insightable.selected_insight_fields | ||
else | ||
Insight::DEFAULT_ATTRIBUTES | ||
end | ||
end | ||
|
||
def ratio_enabled? | ||
@insightable.premium? && @insightable.configuration.insight_ratio | ||
end | ||
|
||
def ratio_type | ||
@insightable.configuration.insight_ratio_type | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Api::V1::InsightsController do | ||
describe 'GET#index' do | ||
it_behaves_like 'required api auth' | ||
it_behaves_like 'required valid api auth' | ||
|
||
context 'for logged users' do | ||
let!(:company) { create :company, configuration: { insight_ratio: true } } | ||
let!(:user) { create :user } | ||
let(:api_access_token) { create :api_access_token, user: user } | ||
|
||
before { create :subscription, user: user } | ||
|
||
context 'for companies' do | ||
before do | ||
insight = create :insight, insightable: company, comments_count: 1 | ||
create :insight, insightable: company, comments_count: 2, previous_date: 1.week.ago, entity: insight.entity | ||
end | ||
|
||
context 'for user company' do | ||
before { company.update!(user: user) } | ||
|
||
it 'returns data', :aggregate_failures do | ||
get :index, params: { company_id: company.uuid, api_access_token: api_access_token.value } | ||
|
||
response_values = response.parsed_body.dig('insights', 'data', 0, 'attributes', 'values') | ||
|
||
expect(response).to have_http_status :ok | ||
expect(response_values.keys).to eq Insight::DEFAULT_ATTRIBUTES.map(&:to_s) | ||
expect(response_values.dig('comments_count', 'value')).to eq 1 | ||
end | ||
|
||
context 'for selected insight fields' do | ||
before do | ||
company.configuration.insight_fields = { comments_count: true, reviews_count: true } | ||
company.save! | ||
end | ||
|
||
it 'returns data', :aggregate_failures do | ||
get :index, params: { company_id: company.uuid, api_access_token: api_access_token.value } | ||
|
||
response_values = response.parsed_body.dig('insights', 'data', 0, 'attributes', 'values') | ||
|
||
expect(response).to have_http_status :ok | ||
expect(response_values.keys).to contain_exactly('comments_count', 'reviews_count') | ||
expect(response_values.dig('comments_count', 'value')).to eq 1 | ||
end | ||
end | ||
|
||
context 'for change ratio' do | ||
before do | ||
company.configuration.insight_ratio_type = 'change' | ||
company.save! | ||
end | ||
|
||
it 'returns data', :aggregate_failures do | ||
get :index, params: { company_id: company.uuid, api_access_token: api_access_token.value } | ||
|
||
response_values = response.parsed_body.dig('insights', 'data', 0, 'attributes', 'values') | ||
|
||
expect(response).to have_http_status :ok | ||
expect(response_values.keys).to eq Insight::DEFAULT_ATTRIBUTES.map(&:to_s) | ||
expect(response_values.dig('comments_count', 'value')).to eq 1 | ||
end | ||
end | ||
end | ||
end | ||
|
||
context 'for repositories' do | ||
let!(:repository) { create :repository, company: company } | ||
|
||
before { create :insight, insightable: repository, comments_count: 2 } | ||
|
||
context 'for user company' do | ||
before { company.update!(user: user) } | ||
|
||
it 'returns data', :aggregate_failures do | ||
get :index, params: { repository_id: repository.uuid, api_access_token: api_access_token.value } | ||
|
||
response_values = response.parsed_body.dig('insights', 'data', 0, 'attributes', 'values') | ||
|
||
expect(response).to have_http_status :ok | ||
expect(response_values.keys).to eq Insight::DEFAULT_ATTRIBUTES.map(&:to_s) | ||
expect(response_values.dig('comments_count', 'value')).to eq 2 | ||
end | ||
end | ||
end | ||
end | ||
|
||
def do_request(api_access_token=nil) | ||
get :index, params: { company_id: 'unexisting', api_access_token: api_access_token }.compact | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.