Skip to content

Commit

Permalink
Merge pull request #564 from BrunoChauvet/feature/tenant-dashboards
Browse files Browse the repository at this point in the history
Feature/tenant dashboards
  • Loading branch information
ouranos authored Nov 3, 2017
2 parents b70c9c2 + 824d407 commit b693bcd
Show file tree
Hide file tree
Showing 37 changed files with 378 additions and 25 deletions.
3 changes: 3 additions & 0 deletions api/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--color
--backtrace
--require spec_helper
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Jpi::V1::Admin::OrganizationsController < Jpi::V1::Admin::BaseResourceCont
:geo_country_code, :geo_state_code, :geo_city,
:geo_tz, :geo_currency, :metadata, :industry, :size,
:financial_year_end_month, :credit_card,
:financial_metrics, :created_at]
:financial_metrics, :created_at, :external_id]
# GET /mnoe/jpi/v1/admin/organizations
def index
if params[:terms]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module MnoEnterprise
class Jpi::V1::Admin::SubscriptionEventsController < Jpi::V1::Admin::BaseResourceController
include MnoEnterprise::Concerns::Controllers::Jpi::V1::Admin::SubscriptionEventsController
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module MnoEnterprise
class Jpi::V1::SubscriptionEventsController < Jpi::V1::BaseResourceController
include MnoEnterprise::Concerns::Controllers::Jpi::V1::SubscriptionEventsController
end
end
Original file line number Diff line number Diff line change
@@ -1 +1 @@
json.extract! organization, :id, :name, :uid, :soa_enabled, :created_at, :account_frozen, :financial_metrics, :billing_currency
json.extract! organization, :id, :name, :uid, :soa_enabled, :created_at, :account_frozen, :financial_metrics, :billing_currency, :external_id
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
json.id subscription_event.id
json.event_type subscription_event.event_type
json.status subscription_event.status
json.message subscription_event.message
json.provisioning_data subscription_event.provisioning_data
json.created_at subscription_event.created_at
json.updated_at subscription_event.updated_at
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.subscription_events @subscription_events, partial: 'subscription_event', as: :subscription_event
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json.subscription_event do
json.partial! 'subscription_event', subscription_event: @subscription_event
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ json.status subscription.status
json.subscription_type subscription.subscription_type
json.start_date subscription.start_date
json.end_date subscription.end_date
json.currency subscription.currency
json.max_licenses subscription.max_licenses
json.available_licenses subscription.available_licenses
json.external_id subscription.external_id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
json.id subscription_event.id
json.event_type subscription_event.event_type
json.status subscription_event.status
json.message subscription_event.message
json.provisioning_data subscription_event.provisioning_data
json.created_at subscription_event.created_at
json.updated_at subscription_event.updated_at
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.subscription_events @subscription_events, partial: 'subscription_event', as: :subscription_event
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json.subscription_event do
json.partial! 'subscription_event', subscription_event: @subscription_event
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ json.status subscription.status
json.subscription_type subscription.subscription_type
json.start_date subscription.start_date
json.end_date subscription.end_date
json.currency subscription.currency
json.max_licenses subscription.max_licenses
json.available_licenses subscription.available_licenses
json.external_id subscription.external_id
Expand Down
4 changes: 4 additions & 0 deletions api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@
member do
post :cancel
end

resources :subscription_events, only: [:index, :show]
end
end
end
Expand Down Expand Up @@ -258,6 +260,8 @@
post :approve
post :fulfill
end

resources :subscription_events, only: [:index, :show]
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module MnoEnterprise::Concerns::Controllers::Jpi::V1::Admin::SubscriptionEventsController
extend ActiveSupport::Concern

SUBSCRIPTION_EVENT_INCLUDES ||= [:'subscription']

#==================================================================
# Instance methods
#==================================================================
# GET /mnoe/jpi/v1/admin/organizations/1/subscriptions/xyz/subscription_events
def index
query = fetch_subscription_events(params[:organization_id], params[:subscription_id])
@subscription_events = query.to_a
response.headers['X-Total-Count'] = query.meta.record_count
end

# GET /mnoe/jpi/v1/admin/organizations/1/subscriptions/xyz/subscription_events/id
def show
@subscription_event = fetch_subscription_event(params[:organization_id], params[:subscription_id], params[:id], SUBSCRIPTION_EVENT_INCLUDES)
return render_not_found('SubscriptionEvent') unless @subscription_event
end

protected

def fetch_subscription_events(organization_id, subscription_id)
MnoEnterprise::SubscriptionEvent
.apply_query_params(params)
.with_params(_metadata: { act_as_manager: current_user.id, organization_id: organization_id })
.includes(SUBSCRIPTION_EVENT_INCLUDES)
.where('subscription.id' => subscription_id)
end

def fetch_subscription_event(organization_id, subscription_id, id, includes = nil)
rel = MnoEnterprise::SubscriptionEvent
.with_params(_metadata: { act_as_manager: current_user.id, organization_id: organization_id })
.where('subscription.id' => subscription_id, id: id)
rel = rel.includes(*includes) if includes.present?
rel.first
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module MnoEnterprise::Concerns::Controllers::Jpi::V1::Admin::SubscriptionsController
extend ActiveSupport::Concern

SUBSCRIPTION_INCLUDES ||= [:product_instance, :'product_pricing.product', :product_contract, :organization, :user, :'license_assignments.user', :'product_instance.product']
SUBSCRIPTION_INCLUDES ||= [:'product_pricing.product', :product_contract, :organization, :user, :'license_assignments.user', :'product_instance.product']

#==================================================================
# Instance methods
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module MnoEnterprise::Concerns::Controllers::Jpi::V1::SubscriptionEventsController
extend ActiveSupport::Concern

SUBSCRIPTION_EVENT_INCLUDES ||= [:'subscription']

#==================================================================
# Instance methods
#==================================================================
# GET /mnoe/jpi/v1/organizations/1/subscriptions/xyz/subscription_events
def index
authorize! :manage_app_instances, parent_organization
@subscription_events = fetch_subscription_events(parent_organization.id, params[:subscription_id])
end

# GET /mnoe/jpi/v1/organizations/1/subscriptions/xyz/subscription_events/id
def show
authorize! :manage_app_instances, parent_organization
@subscription_event = fetch_subscription_event(parent_organization.id, params[:subscription_id], params[:id])
end

protected

def fetch_subscription_events(organization_id, subscription_id)
query = MnoEnterprise::SubscriptionEvent.with_params(_metadata: { organization_id: organization_id })
MnoEnterprise::SubscriptionEvent.fetch_all(query.includes(*SUBSCRIPTION_EVENT_INCLUDES).where('subscription.id' => subscription_id))
end

def fetch_subscription_event(organization_id, subscription_id, id)
query = MnoEnterprise::SubscriptionEvent.with_params(_metadata: { organization_id: organization_id })
query.includes(*SUBSCRIPTION_EVENT_INCLUDES).where('subscription.id' => subscription_id, id: id).first
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module MnoEnterprise::Concerns::Controllers::Jpi::V1::SubscriptionsController
extend ActiveSupport::Concern

SUBSCRIPTION_INCLUDES ||= [:product_instance, :'product_pricing.product', :product_contract, :organization, :user, :'license_assignments.user', :'product_instance.product']
SUBSCRIPTION_INCLUDES ||= [:'product_pricing.product', :product_contract, :organization, :user, :'license_assignments.user', :'product_instance.product']

#==================================================================
# Instance methods
Expand Down
2 changes: 1 addition & 1 deletion api/lib/mno_enterprise/csv_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def self.process(file_path)
address = MnoEnterprise::Address.new(
city: row['city'],
country_code: row['country'],
street: [row['address1'], row['address2']].reject(&:empty?).join(' '),
street: [row['address1'], row['address2']].reject(&:blank?).join(' '),
state_code: row['state_province'],
postal_code: row['postal_code']
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module MnoEnterprise
{
organizations: [
:uid, :name, :account_frozen, :soa_enabled, :mails, :logo, :latitude, :longitude, :geo_country_code, :geo_state_code,
:geo_city, :geo_tz, :geo_currency, :metadata, :industry, :size, :financial_year_end_month, :credit_card, :financial_metrics, :created_at
:geo_city, :geo_tz, :geo_currency, :metadata, :industry, :size, :financial_year_end_month, :credit_card, :financial_metrics, :created_at, :external_id
].join(',')
}
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require 'rails_helper'

module MnoEnterprise
describe Jpi::V1::Admin::SubscriptionEventsController, type: :controller do
include MnoEnterprise::TestingSupport::SharedExamples::JpiV1Admin

render_views
routes { MnoEnterprise::Engine.routes }
before { request.env["HTTP_ACCEPT"] = 'application/json' }

before(:each) do
Settings.merge!(dashboard: { marketplace: {provisioning: true } })
Rails.application.reload_routes!
end

#===============================================
# Assignments
#===============================================
let(:user) { build(:user, :admin) }
let!(:current_user_stub) { stub_user(user) }

let(:organization) { build(:organization) }
let(:subscription) { build(:subscription) }
let(:subscription_event) { build(:subscription_event, subscription: subscription) }

#===============================================
# Specs
#===============================================
before { sign_in user }

describe 'GET #index' do
subject { get :index, organization_id: organization.id, subscription_id: subscription.id }

let(:data) { JSON.parse(response.body) }
let(:includes) { [:'subscription'] }
let(:expected_params) { { filter: { 'subscription.id': subscription.id }, _metadata: { act_as_manager: user.id, organization_id: organization.id } } }

before { stub_api_v2(:get, "/subscription_events", [subscription_event], includes, expected_params) }

it_behaves_like 'a jpi v1 admin action'

it 'returns the subscription events' do
subject
expect(data['subscription_events'].first['id']).to eq(subscription_event.id)
end
end

describe 'GET #show' do
subject { get :show, id: subscription_event.id, organization_id: organization.id, subscription_id: subscription.id }

let(:data) { JSON.parse(response.body) }
let(:includes) { [:'subscription'] }
let(:expected_params) do
{
filter: { id: subscription_event.id, 'subscription.id': subscription.id },
_metadata: { act_as_manager: user.id, organization_id: organization.id },
page: { number: 1, size: 1 } }
end

before { allow(subscription_event).to receive(:license_assignments).and_return([]) }
before { allow(subscription_event).to receive(:organization).and_return(organization) }
before { stub_api_v2(:get, "/subscription_events", subscription_event, includes, expected_params) }

it_behaves_like 'a jpi v1 admin action'

it 'returns the subscription event' do
subject
expect(data['subscription_event']['id']).to eq(subscription_event.id)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module MnoEnterprise
subject { get :index }

let(:data) { JSON.parse(response.body) }
let(:includes) { [:product_instance, :'product_pricing.product', :product_contract, :organization, :user, :'license_assignments.user', :'product_instance.product'] }
let(:includes) { [:'product_pricing.product', :product_contract, :organization, :user, :'license_assignments.user', :'product_instance.product'] }
let(:expected_params) { { _metadata: { act_as_manager: user.id } } }

before { allow(subscription).to receive(:license_assignments).and_return([]) }
Expand All @@ -45,7 +45,7 @@ module MnoEnterprise
subject { get :show, id: subscription.id, organization_id: organization.id }

let(:data) { JSON.parse(response.body) }
let(:includes) { [:product_instance, :'product_pricing.product', :product_contract, :organization, :user, :'license_assignments.user', :'product_instance.product'] }
let(:includes) { [:'product_pricing.product', :product_contract, :organization, :user, :'license_assignments.user', :'product_instance.product'] }
let(:expected_params) do
{
filter: { id: subscription.id, organization_id: organization.id },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'rails_helper'

module MnoEnterprise
describe Jpi::V1::SubscriptionEventsController, type: :controller do
include MnoEnterprise::TestingSupport::JpiV1TestHelper
render_views
routes { MnoEnterprise::Engine.routes }
before { request.env['HTTP_ACCEPT'] = 'application/json' }

before(:all) do
Settings.merge!(dashboard: {marketplace: {provisioning: true}})
Rails.application.reload_routes!
end

# Stub controller ability
let!(:ability) { stub_ability }
before { allow(ability).to receive(:can?).with(any_args).and_return(true) }

# Stub user and user call
let!(:user) { build(:user) }
let!(:current_user_stub) { stub_user(user) }

# Stub organization and association
let!(:organization) { build(:organization, orga_relations: []) }
let!(:orga_relation) { organization.orga_relations << build(:orga_relation, user_id: user.id, organization_id: organization.id, role: 'Super Admin') }
let!(:organization_stub) { stub_api_v2(:get, "/organizations/#{organization.id}", organization, %i(orga_relations users)) }

describe 'GET #index' do
let(:subscription) { build(:subscription) }
let(:subscription_event) { build(:subscription_event, subscription: subscription) }

let(:expected_params) do
{
filter: { 'subscription.id': subscription.id },
_metadata: { organization_id: organization.id }
}
end
before { stub_api_v2(:get, "/subscription_events", [subscription_event], [:subscription], expected_params) }
before { sign_in user }

subject { get :index, organization_id: organization.id, subscription_id: subscription.id }

it_behaves_like 'jpi v1 protected action'
end

describe 'GET #show' do
let(:subscription) { build(:subscription) }
let(:subscription_event) { build(:subscription_event, subscription: subscription) }

let(:expected_params) do
{
filter: { id: subscription_event.id, 'subscription.id': subscription.id },
_metadata: { organization_id: organization.id },
page: { number: 1, size: 1 }
}
end
before { stub_api_v2(:get, "/subscription_events", subscription_event, [:subscription], expected_params) }
before { sign_in user }

subject { get :show, organization_id: organization.id, subscription_id: subscription.id, id: subscription_event.id }

it_behaves_like 'jpi v1 protected action'
end
end
end
Loading

0 comments on commit b693bcd

Please sign in to comment.