diff --git a/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder b/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder index e7cb2f0ee..8bc43014a 100644 --- a/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder +++ b/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder @@ -23,8 +23,7 @@ end # json.connector_version app_instance.oauth_keys[:version] # end -app_instance.without_tenant = params[:unscoped].present? -app_instance.app.tap do |a| +@apps[app_instance.app_id].first.tap do |a| json.app_id a.id json.app_name a.name json.app_nid a.nid diff --git a/api/lib/mno_enterprise/concerns/controllers/jpi/v1/app_instances_controller.rb b/api/lib/mno_enterprise/concerns/controllers/jpi/v1/app_instances_controller.rb index cbae629a9..c8c2bb957 100644 --- a/api/lib/mno_enterprise/concerns/controllers/jpi/v1/app_instances_controller.rb +++ b/api/lib/mno_enterprise/concerns/controllers/jpi/v1/app_instances_controller.rb @@ -20,6 +20,8 @@ def index i.owner = parent_organization can?(:access,i) end + ids = @app_instances.collect{ |i| i.app_id } + @apps = MnoEnterprise::App.where('id.in' => ids, unscoped: params[:unscoped].present?).group_by(&:id) end # POST /mnoe/jpi/v1/organization/1/app_instances diff --git a/api/spec/controllers/mno_enterprise/jpi/v1/admin/organizations_controller_spec.rb b/api/spec/controllers/mno_enterprise/jpi/v1/admin/organizations_controller_spec.rb index 1d474cb59..795044e44 100644 --- a/api/spec/controllers/mno_enterprise/jpi/v1/admin/organizations_controller_spec.rb +++ b/api/spec/controllers/mno_enterprise/jpi/v1/admin/organizations_controller_spec.rb @@ -43,13 +43,11 @@ def partial_hash_for_arrears(arrear) let(:org_invite) { build(:org_invite, organization: organization) } let(:app_instance) { build(:app_instance, organization: organization) } let(:credit_card) { build(:credit_card, organization: organization) } - let(:app) { build(:app)} before do organizations = [organization] allow(organizations).to receive(:loaded?).and_return(true) allow_any_instance_of(MnoEnterprise::User).to receive(:organizations).and_return(organizations) - allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:app).and_return(app) api_stub_for(get: "/organizations/#{organization.id}/invoices", response: from_api([invoice])) api_stub_for(get: "/organizations", response: from_api([organization])) api_stub_for(get: "/organizations/#{organization.id}", response: from_api(organization)) diff --git a/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb b/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb index 0c25a2f7e..30c2eeb78 100644 --- a/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb +++ b/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb @@ -17,11 +17,9 @@ module MnoEnterprise before { api_stub_for(get: "/users/#{user.id}", response: from_api(user)) } # Stub organization + associations let(:organization) { build(:organization) } - let(:app) { build(:app)} before { allow_any_instance_of(MnoEnterprise::User).to receive(:organizations).and_return([organization]) } - before { allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:without_tenant).and_return(false) } - before { allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:app).and_return(app) } + let(:app) { build(:app) } describe 'GET #index' do let(:app_instance) { build(:app_instance, status: "running") } @@ -34,6 +32,11 @@ module MnoEnterprise allow(app_instances).to receive(:active).and_return(app_instances) # Updated since last tick allow(app_instances).to receive(:where).and_return([app_instance]) + + allow(app_instance).to receive(:app_id).and_return(app.id) + + api_stub_for(get: "/apps?filter[id.in][]=#{app.id}&filter[unscoped]=false", response: from_api([app])) + api_stub_for(get: "/apps?filter[id.in]&filter[unscoped]=false", response: from_api(nil)) end before { sign_in user } @@ -52,11 +55,11 @@ module MnoEnterprise end context 'with unscoped data' do - before { allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:without_tenant).and_return(true) } + before { api_stub_for(get: "/apps?filter[id.in][]=#{app.id}&filter[unscoped]=true", response: from_api([app])) } + subject { get :index, organization_id: organization.id, timestamp: timestamp, unscoped: true } it 'retrieved the app instance with the app' do subject - expect(app_instance.app).to eq(app) end end diff --git a/api/spec/lib/mno_enterprise/intercom_events_listener_spec.rb b/api/spec/lib/mno_enterprise/intercom_events_listener_spec.rb index 55e9895e6..723974d19 100644 --- a/api/spec/lib/mno_enterprise/intercom_events_listener_spec.rb +++ b/api/spec/lib/mno_enterprise/intercom_events_listener_spec.rb @@ -8,7 +8,7 @@ module MnoEnterprise let(:credit_card) { build(:credit_card) } let(:organization) { build(:organization) } before do - allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:app).and_return(app) + # allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:app).and_return(app) api_stub_for(get: "/users/#{user.id}", response: from_api(user)) api_stub_for(get: "/users/#{user.id}/organizations", response: from_api([organization])) api_stub_for(get: "/organizations/#{organization.id}/app_instances", response: from_api([app_instance])) diff --git a/core/lib/mno_enterprise/concerns/models/app_instance.rb b/core/lib/mno_enterprise/concerns/models/app_instance.rb index 2b1e2ceac..ecf688df1 100644 --- a/core/lib/mno_enterprise/concerns/models/app_instance.rb +++ b/core/lib/mno_enterprise/concerns/models/app_instance.rb @@ -54,6 +54,7 @@ module MnoEnterprise::Concerns::Models::AppInstance # Associations #============================================================== belongs_to :owner, class_name: 'MnoEnterprise::Organization' + belongs_to :app, class_name: 'MnoEnterprise::App' # Define connector_stack?, cloud_stack? etc. methods [:cube, :cloud, :connector].each do |stackname| @@ -64,9 +65,9 @@ module MnoEnterprise::Concerns::Models::AppInstance scope :active, -> { where('status.in' => ACTIVE_STATUSES) } - def app - MnoEnterprise::App.find(self.app_id, { unscoped: self.without_tenant }) - end + # def app + # MnoEnterprise::App.find(self.app_id, { unscoped: self.without_tenant }) + # end end #==================================================================