mno-enterprise-api 3.3.3 → 3.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +5 -5
  2. data/app/controllers/mno_enterprise/jpi/v1/admin/impac/dashboards_controller.rb +105 -0
  3. data/app/controllers/mno_enterprise/jpi/v1/admin/impac/widgets_controller.rb +20 -8
  4. data/app/controllers/mno_enterprise/jpi/v1/admin/invoices_controller.rb +1 -1
  5. data/app/controllers/mno_enterprise/jpi/v1/admin/organizations_controller.rb +1 -140
  6. data/app/controllers/mno_enterprise/jpi/v1/admin/sub_tenants_controller.rb +64 -0
  7. data/app/controllers/mno_enterprise/jpi/v1/admin/users_controller.rb +30 -20
  8. data/app/views/mno_enterprise/jpi/v1/admin/impac/dashboards/_dashboard.json.jbuilder +12 -0
  9. data/app/views/mno_enterprise/jpi/v1/admin/impac/dashboards/index.json.jbuilder +1 -0
  10. data/app/views/mno_enterprise/jpi/v1/admin/impac/dashboards/show.json.jbuilder +1 -0
  11. data/app/views/mno_enterprise/jpi/v1/admin/impac/widgets/_widget.json.jbuilder +1 -0
  12. data/app/views/mno_enterprise/jpi/v1/admin/organizations/_organization.json.jbuilder +1 -1
  13. data/app/views/mno_enterprise/jpi/v1/admin/sub_tenants/_sub_tenant.json.jbuilder +1 -0
  14. data/app/views/mno_enterprise/jpi/v1/admin/sub_tenants/index.json.jbuilder +2 -0
  15. data/app/views/mno_enterprise/jpi/v1/admin/sub_tenants/show.json.jbuilder +12 -0
  16. data/app/views/mno_enterprise/jpi/v1/admin/users/_user.json.jbuilder +1 -1
  17. data/app/views/mno_enterprise/jpi/v1/admin/users/show.json.jbuilder +5 -5
  18. data/app/views/mno_enterprise/jpi/v1/current_users/show.json.jbuilder +4 -2
  19. data/app/views/mno_enterprise/jpi/v1/impac/widgets/_widget.json.jbuilder +1 -0
  20. data/app/views/mno_enterprise/jpi/v1/organizations/_organization.json.jbuilder +1 -1
  21. data/config/routes.rb +9 -1
  22. data/lib/mno_enterprise/concerns/controllers/jpi/v1/admin/organizations_controller.rb +159 -0
  23. data/lib/mno_enterprise/concerns/controllers/jpi/v1/impac/alerts_controller.rb +3 -6
  24. data/lib/mno_enterprise/concerns/controllers/jpi/v1/impac/dashboards_controller.rb +29 -38
  25. data/lib/mno_enterprise/concerns/controllers/jpi/v1/impac/kpis_controller.rb +7 -8
  26. data/lib/mno_enterprise/concerns/controllers/jpi/v1/impac/widgets_controller.rb +14 -2
  27. data/lib/mno_enterprise/concerns/controllers/jpi/v1/organizations_controller.rb +1 -1
  28. data/spec/controllers/mno_enterprise/jpi/v1/admin/impac/dashboard_controller_spec.rb +149 -0
  29. data/spec/controllers/mno_enterprise/jpi/v1/admin/impac/dashboard_templates_controller_spec.rb +151 -139
  30. data/spec/controllers/mno_enterprise/jpi/v1/admin/impac/kpis_controller_spec.rb +95 -69
  31. data/spec/controllers/mno_enterprise/jpi/v1/admin/impac/widgets_controller_spec.rb +169 -81
  32. data/spec/controllers/mno_enterprise/jpi/v1/admin/invoices_controller_spec.rb +1 -1
  33. data/spec/controllers/mno_enterprise/jpi/v1/admin/organizations_controller_spec.rb +2 -1
  34. data/spec/controllers/mno_enterprise/jpi/v1/admin/sub_tenants_controller_spec.rb +172 -0
  35. data/spec/controllers/mno_enterprise/jpi/v1/admin/users_controller_spec.rb +29 -12
  36. data/spec/controllers/mno_enterprise/jpi/v1/current_users_controller_spec.rb +4 -2
  37. data/spec/controllers/mno_enterprise/jpi/v1/impac/dashboards_controller_spec.rb +26 -15
  38. data/spec/controllers/mno_enterprise/jpi/v1/impac/kpis_controller_spec.rb +11 -7
  39. data/spec/routing/mno_enterprise/jpi/v1/admin/impac/dashboards_controller_routing_spec.rb +28 -0
  40. data/spec/routing/mno_enterprise/jpi/v1/impac/dashboards_controller_routing_spec.rb +4 -0
  41. metadata +85 -70
@@ -0,0 +1,12 @@
1
+ json.extract! dashboard, :id, :name, :full_name, :currency
2
+
3
+ json.metadata dashboard.settings
4
+
5
+ json.data_sources dashboard.organizations.map do |org|
6
+ json.id org.id
7
+ json.uid org.uid
8
+ json.label org.name
9
+ end
10
+
11
+ json.kpis dashboard.kpis, partial: 'mno_enterprise/jpi/v1/admin/impac/kpis/kpi', as: :kpi
12
+ json.widgets dashboard.widgets, partial: 'mno_enterprise/jpi/v1/admin/impac/widgets/widget', as: :widget
@@ -0,0 +1 @@
1
+ json.array! @dashboards, partial: 'dashboard', as: :dashboard
@@ -0,0 +1 @@
1
+ json.partial! 'dashboard', dashboard: @dashboard
@@ -3,5 +3,6 @@ json.name widget.name
3
3
  json.endpoint (widget.endpoint || widget.widget_category)
4
4
  json.width widget.width
5
5
  json.metadata widget.settings
6
+ json.layouts widget.layouts
6
7
 
7
8
  json.kpis widget.kpis, partial: 'mno_enterprise/jpi/v1/admin/impac/kpis/kpi', as: :kpi
@@ -1 +1 @@
1
- json.extract! organization, :id, :name, :uid, :soa_enabled, :created_at, :account_frozen
1
+ json.extract! organization, :id, :name, :uid, :soa_enabled, :created_at, :account_frozen, :financial_year_end_month
@@ -0,0 +1 @@
1
+ json.extract! sub_tenant, :id, :name, :created_at, :updated_at, :client_ids, :account_manager_ids
@@ -0,0 +1,2 @@
1
+ json.sub_tenants @sub_tenants, partial: 'sub_tenant', as: :sub_tenant
2
+ json.metadata @sub_tenants.metadata if @sub_tenants.respond_to?(:metadata)
@@ -0,0 +1,12 @@
1
+ json.sub_tenant do
2
+ json.partial! 'sub_tenant', sub_tenant: @sub_tenant
3
+
4
+ json.clients @sub_tenant_clients do |org|
5
+ json.extract! org, :id, :uid, :name, :created_at
6
+ end
7
+
8
+ json.account_managers @sub_tenant_account_managers do |user|
9
+ json.extract! user, :id, :uid, :name, :surname, :email, :created_at, :admin_role
10
+ end
11
+
12
+ end
@@ -1 +1 @@
1
- json.extract! user, :id, :uid, :email, :phone, :name, :surname, :admin_role, :created_at, :confirmed_at, :last_sign_in_at, :sign_in_count
1
+ json.extract! user, :id, :uid, :email, :phone, :name, :surname, :admin_role, :created_at, :updated_at, :confirmed_at, :last_sign_in_at, :sign_in_count, :mnoe_sub_tenant_id, :client_ids
@@ -2,10 +2,10 @@ json.user do
2
2
  json.partial! 'user', user: @user
3
3
 
4
4
  json.organizations @user_organizations do |org|
5
- json.id org.id
6
- json.uid org.uid
7
- json.name org.name
8
- json.account_frozen org.account_frozen
9
- json.created_at org.created_at
5
+ json.extract! org, :id, :uid, :name, :account_frozen, :created_at
6
+ end
7
+
8
+ json.clients @user_clients do |org|
9
+ json.extract! org, :id, :uid, :name, :account_frozen, :created_at
10
10
  end
11
11
  end
@@ -1,4 +1,4 @@
1
- json.cache! ['v1', @user.cache_key, session[:impersonator_user_id]] do
1
+ json.cache! ['v2', @user.cache_key, session[:impersonator_user_id]] do
2
2
  json.current_user do
3
3
  json.id @user.id
4
4
  json.name @user.name
@@ -17,6 +17,7 @@ json.cache! ['v1', @user.cache_key, session[:impersonator_user_id]] do
17
17
  json.admin_role @user.admin_role
18
18
  json.avatar_url avatar_url(@user)
19
19
  json.tos_accepted_at @user.meta_data[:tos_accepted_at] || false
20
+ json.mnoe_sub_tenant_id @user.mnoe_sub_tenant_id
20
21
  if current_impersonator
21
22
  json.current_impersonator true
22
23
  json.current_impersonator_role current_impersonator.admin_role
@@ -29,7 +30,7 @@ json.cache! ['v1', @user.cache_key, session[:impersonator_user_id]] do
29
30
  # Embed association if user is persisted
30
31
  if @user.id
31
32
  json.organizations do
32
- json.array! (@user.organizations.active || []) do |o|
33
+ json.array! (@user.organizations.active.include_acl(session[:impersonator_user_id]) || []) do |o|
33
34
  json.id o.id
34
35
  json.uid o.uid
35
36
  json.name o.name
@@ -37,6 +38,7 @@ json.cache! ['v1', @user.cache_key, session[:impersonator_user_id]] do
37
38
  json.current_user_role o.role
38
39
  json.has_myob_essentials_only o.has_myob_essentials_only?
39
40
  json.financial_year_end_month o.financial_year_end_month
41
+ json.acl o.acl
40
42
  end
41
43
  end
42
44
 
@@ -4,5 +4,6 @@ json.endpoint (widget.endpoint || widget.widget_category)
4
4
  json.width widget.width
5
5
  json.metadata widget.settings
6
6
  json.owner widget.owner
7
+ json.layouts widget.layouts
7
8
 
8
9
  json.kpis widget.kpis, partial: 'mno_enterprise/jpi/v1/impac/kpis/kpi', as: :kpi
@@ -1,4 +1,4 @@
1
- json.extract! organization, :id, :name, :soa_enabled, :payment_restriction, :account_frozen #, :current_support_plan
1
+ json.extract! organization, :id, :name, :soa_enabled, :payment_restriction, :account_frozen, :financial_year_end_month #, :current_support_plan
2
2
  # json.show_new_db_features !!organization.get_meta_data(:show_new_db_features)
3
3
  # if organization.support_plan
4
4
  # json.custom_training_credits organization.support_plan.custom_training_credits
@@ -95,7 +95,7 @@ MnoEnterprise::Engine.routes.draw do
95
95
  end
96
96
  end
97
97
  # Maestrano-hub events
98
- resources :events, only: [:create]
98
+ resources :events, only: [:create]
99
99
  end
100
100
 
101
101
  #============================================================
@@ -198,6 +198,7 @@ MnoEnterprise::Engine.routes.draw do
198
198
  resource :invites, only: [:create]
199
199
  end
200
200
  end
201
+ resources :sub_tenants, only: [:index, :show, :destroy, :update, :create]
201
202
  resources :tenant_invoices, only: [:index, :show]
202
203
  resources :invoices, only: [:index, :show] do
203
204
  collection do
@@ -222,6 +223,13 @@ MnoEnterprise::Engine.routes.draw do
222
223
 
223
224
  # Dashboard templates designer
224
225
  namespace :impac do
226
+ post 'dashboards/:id/copy', to: 'dashboards#copy'
227
+
228
+ # TODO: DRY between both?
229
+ resources :dashboards, only: [:index, :create, :update, :destroy] do
230
+ resources :widgets, shallow: true, only: [:create, :update, :destroy]
231
+ resources :kpis, shallow: true, only: [:create, :update, :destroy]
232
+ end
225
233
  resources :dashboard_templates, only: [:index, :show, :destroy, :update, :create] do
226
234
  resources :widgets, shallow: true, only: [:create, :update, :destroy]
227
235
  resources :kpis, shallow: true, only: [:create, :update, :destroy]
@@ -0,0 +1,159 @@
1
+ module MnoEnterprise::Concerns::Controllers::Jpi::V1::Admin::OrganizationsController
2
+ extend ActiveSupport::Concern
3
+
4
+ #==================================================================
5
+ # Included methods
6
+ #==================================================================
7
+ # 'included do' causes the included code to be evaluated in the
8
+ # context where it is included rather than being executed in the module's context
9
+ included do
10
+ end
11
+
12
+ #==================================================================
13
+ # Instance methods
14
+ #==================================================================
15
+ # GET /mnoe/jpi/v1/admin/organizations
16
+ def index
17
+ if params[:terms]
18
+ # Search mode
19
+ @organizations = []
20
+ JSON.parse(params[:terms]).map { |t| @organizations = @organizations | MnoEnterprise::Organization.where(Hash[*t]).fetch }
21
+ response.headers['X-Total-Count'] = @organizations.count
22
+ else
23
+ # Index mode
24
+ query = MnoEnterprise::Organization
25
+ query = query.limit(params[:limit]) if params[:limit]
26
+ query = query.skip(params[:offset]) if params[:offset]
27
+ query = query.order_by(params[:order_by]) if params[:order_by]
28
+ query = query.where(params[:where]) if params[:where]
29
+ all = query.all
30
+
31
+ all.params[:sub_tenant_id] = params[:sub_tenant_id]
32
+ all.params[:account_manager_id] = params[:account_manager_id]
33
+
34
+ @organizations = all.fetch
35
+
36
+ response.headers['X-Total-Count'] = @organizations.metadata[:pagination][:count]
37
+ end
38
+ end
39
+
40
+ # GET /mnoe/jpi/v1/admin/organizations/1
41
+ def show
42
+ @organization = MnoEnterprise::Organization.find(params[:id])
43
+ @organization_active_apps = @organization.app_instances.active.to_a
44
+ end
45
+
46
+ # GET /mnoe/jpi/v1/admin/organizations/in_arrears
47
+ def in_arrears
48
+ @arrears = MnoEnterprise::ArrearsSituation.all.fetch
49
+ end
50
+
51
+ # GET /mnoe/jpi/v1/admin/organizations/count
52
+ def count
53
+ organizations_count = MnoEnterprise::Tenant.get('tenant').organizations_count
54
+ render json: {count: organizations_count }
55
+ end
56
+
57
+ # POST /mnoe/jpi/v1/admin/organizations
58
+ def create
59
+ # Create new organization
60
+ @organization = MnoEnterprise::Organization.create(organization_update_params)
61
+
62
+ # OPTIMIZE: move this into a delayed job?
63
+ update_app_list
64
+
65
+ @organization_active_apps = @organization.app_instances
66
+
67
+ render 'show'
68
+ end
69
+
70
+ # PATCH /mnoe/jpi/v1/admin/organizations/1
71
+ def update
72
+ # get organization
73
+ @organization = MnoEnterprise::Organization.find(params[:id])
74
+
75
+ update_app_list
76
+
77
+ @organization_active_apps = @organization.app_instances.active
78
+
79
+ render 'show'
80
+ end
81
+
82
+ # POST /mnoe/jpi/v1/admin/organizations/1/users
83
+ # Invite a user to the organization (and create it if needed)
84
+ # This does not send any emails (emails are manually triggered later)
85
+ def invite_member
86
+ @organization = MnoEnterprise::Organization.find(params[:id])
87
+
88
+ # Find or create a new user - We create it in the frontend as MnoHub will send confirmation instructions for newly
89
+ # created users
90
+ user = MnoEnterprise::User.find_by(email: user_params[:email]) || create_unconfirmed_user(user_params)
91
+
92
+ # Create the invitation
93
+ invite = @organization.org_invites.create(
94
+ user_email: user.email,
95
+ user_role: params[:user][:role],
96
+ referrer_id: current_user.id,
97
+ status: 'staged' # Will be updated to 'accepted' for unconfirmed users
98
+ )
99
+
100
+ @user = if user.confirmed?
101
+ invite.accept!(user)
102
+ invite.reload
103
+ else
104
+ user.reload
105
+ end
106
+ end
107
+
108
+ protected
109
+
110
+ def organization_permitted_update_params
111
+ [:name]
112
+ end
113
+
114
+ def organization_update_params
115
+ params.fetch(:organization, {}).permit(*organization_permitted_update_params)
116
+ end
117
+
118
+ def user_params
119
+ params.require(:user).permit(:email, :name, :surname, :phone)
120
+ end
121
+
122
+ # Create an unconfirmed user and skip the confirmation notification
123
+ # TODO: monkey patch User#confirmation_required? to simplify this? Use refinements?
124
+ def create_unconfirmed_user(user_params)
125
+ user = MnoEnterprise::User.new(user_params)
126
+ user.skip_confirmation_notification!
127
+ user.save
128
+
129
+ # Reset the confirmation field so we can track when the invite is send - #confirmation_sent_at is when the confirmation_token was generated (not sent)
130
+ # Not ideal as we do 2 saves, and the previous save trigger a call to the backend to validate the token uniqueness
131
+ user.assign_attributes(confirmation_sent_at: nil, confirmation_token: nil)
132
+ user.save
133
+ user
134
+ end
135
+
136
+ # Update App List to match the list passed in params
137
+ def update_app_list
138
+ # Differentiate between a null app_nids params and no app_nids params
139
+ return unless params[:organization].key?(:app_nids) && (desired_nids = Array(params[:organization][:app_nids]))
140
+
141
+ existing_apps = @organization.app_instances.active
142
+
143
+ existing_apps.each do |app_instance|
144
+ desired_nids.delete(app_instance.app.nid) || app_instance.terminate
145
+ end
146
+
147
+ desired_nids.each do |nid|
148
+ begin
149
+ @organization.app_instances.create(product: nid)
150
+ rescue => e
151
+ Rails.logger.error { "#{e.message} #{e.backtrace.join("\n")}" }
152
+ end
153
+
154
+ end
155
+
156
+ # Force reload
157
+ existing_apps.reload
158
+ end
159
+ end
@@ -19,8 +19,7 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::Impac::AlertsController
19
19
  def create
20
20
  return render_bad_request('attach alert to kpi', 'no alert specified') unless params.require(:alert)
21
21
  return render_not_found('kpi') unless kpi_alert.kpi
22
-
23
- authorize! :manage_alert, kpi_alert
22
+ authorize! :update_impac_kpis, kpi_alert.kpi
24
23
 
25
24
  if (@alert = current_user.alerts.create(kpi_alert.attributes))
26
25
  render 'show'
@@ -33,11 +32,10 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::Impac::AlertsController
33
32
  def update
34
33
  return render_bad_request('update alert attributes', 'no alert hash specified') unless params.require(:alert)
35
34
  return render_not_found('alert') unless alert
35
+ authorize! :update_impac_kpis, alert.kpi
36
36
 
37
37
  attributes = params.require(:alert).permit(:title, :webhook, :sent)
38
38
 
39
- authorize! :manage_alert, alert
40
-
41
39
  if alert.update(attributes)
42
40
  render 'show'
43
41
  else
@@ -48,8 +46,7 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::Impac::AlertsController
48
46
  # DELETE /jpi/v1/impac/alerts/:id
49
47
  def destroy
50
48
  return render_not_found('alert') unless alert
51
-
52
- authorize! :manage_alert, alert
49
+ authorize! :update_impac_kpis, alert.kpi
53
50
 
54
51
  service = alert.service
55
52
  if alert.destroy
@@ -28,15 +28,10 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::Impac::DashboardsControlle
28
28
  # POST /mnoe/jpi/v1/impac/dashboards
29
29
  # -> POST /api/mnoe/v1/users/1/dashboards
30
30
  def create
31
- # TODO: dashboards.build breaks as dashboard.organization_ids returns nil, instead of an
32
- # empty array. (see MnoEnterprise::Impac::Dashboard #organizations)
33
- # @dashboard = dashboards.build(dashboard_create_params)
34
- # TODO: enable authorization
35
- # authorize! :manage_dashboard, @dashboard
36
- # if @dashboard.save
31
+ authorize! :create_impac_dashboards, dashboards.build(dashboard_create_params)
32
+
37
33
  if @dashboard = dashboards.create(dashboard_create_params)
38
34
  MnoEnterprise::EventLogger.info('dashboard_create', current_user.id, 'Dashboard Creation', @dashboard)
39
-
40
35
  render 'show'
41
36
  else
42
37
  render_bad_request('create dashboard', @dashboard.errors)
@@ -47,9 +42,7 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::Impac::DashboardsControlle
47
42
  # -> PUT /api/mnoe/v1/dashboards/1
48
43
  def update
49
44
  return render_not_found('dashboard') unless dashboard
50
-
51
- # TODO: enable authorization
52
- # authorize! :manage_dashboard, dashboard
45
+ authorize! :update_impac_dashboards, dashboard
53
46
 
54
47
  if dashboard.update(dashboard_update_params)
55
48
  render 'show'
@@ -62,9 +55,7 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::Impac::DashboardsControlle
62
55
  # -> DELETE /api/mnoe/v1/dashboards/1
63
56
  def destroy
64
57
  return render_not_found('dashboard') unless dashboard
65
-
66
- # TODO: enable authorization
67
- # authorize! :manage_dashboard, dashboard
58
+ authorize! :destroy_impac_dashboards, dashboard
68
59
 
69
60
  if dashboard.destroy
70
61
  MnoEnterprise::EventLogger.info('dashboard_delete', current_user.id, 'Dashboard Deletion', dashboard)
@@ -81,6 +72,7 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::Impac::DashboardsControlle
81
72
  # POST mnoe/jpi/v1/impac/dashboards/1/copy
82
73
  def copy
83
74
  return render_not_found('template') unless template
75
+ authorize! :create_impac_dashboards, template
84
76
 
85
77
  # Owner is the current user by default, can be overriden to something else (eg: current organization)
86
78
  @dashboard = template.copy(current_user, dashboard_params[:name], dashboard_params[:organization_ids])
@@ -91,35 +83,34 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::Impac::DashboardsControlle
91
83
 
92
84
  private
93
85
 
94
- def dashboards
95
- @dashboards ||= current_user.dashboards
96
- end
86
+ def dashboards
87
+ @dashboards ||= current_user.dashboards
88
+ end
97
89
 
98
- def dashboard
99
- @dashboard ||= current_user.dashboards.find(params[:id].to_i)
100
- end
90
+ def dashboard
91
+ @dashboard ||= current_user.dashboards.find(params[:id].to_i)
92
+ end
101
93
 
102
- def templates
103
- @templates ||= MnoEnterprise::Impac::Dashboard.templates
104
- end
94
+ def templates
95
+ @templates ||= MnoEnterprise::Impac::Dashboard.templates
96
+ end
105
97
 
106
- def template
107
- @template ||= templates.find(params[:id].to_i)
108
- end
98
+ def template
99
+ @template ||= templates.find(params[:id].to_i)
100
+ end
109
101
 
110
- def whitelisted_params
111
- [:name, :currency, {widgets_order: []}, {organization_ids: []}]
112
- end
102
+ def whitelisted_params
103
+ [:name, :currency, { widgets_order: [] }, { organization_ids: [] }]
104
+ end
113
105
 
114
- # Allows all metadata attrs to be permitted, and maps it to :settings
115
- # for the Her "meta_data" issue.
116
- def dashboard_params
117
- params.require(:dashboard).permit(*whitelisted_params).tap do |whitelisted|
118
- whitelisted[:settings] = params[:dashboard][:metadata] || {}
119
- end
120
- .except(:metadata)
106
+ # Allows all metadata attrs to be permitted, and maps it to :settings
107
+ # for the Her "meta_data" issue.
108
+ def dashboard_params
109
+ params.require(:dashboard).permit(*whitelisted_params).tap do |whitelisted|
110
+ whitelisted[:settings] = params[:dashboard][:metadata] || {}
121
111
  end
122
- alias :dashboard_update_params :dashboard_params
123
- alias :dashboard_create_params :dashboard_params
124
-
112
+ .except(:metadata)
113
+ end
114
+ alias :dashboard_update_params :dashboard_params
115
+ alias :dashboard_create_params :dashboard_params
125
116
  end
@@ -49,16 +49,15 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::Impac::KpisController
49
49
  # POST /mnoe/jpi/v1/impac/dashboards/:dashboard_id/kpis
50
50
  # -> POST /api/mnoe/v1/dashboards/:id/kpis
51
51
  # -> POST /api/mnoe/v1/users/:id/alerts
52
+ # TODO: nest alert in as a param, with the current user as a recipient.
52
53
  def create
53
54
  if params[:kpi][:widget_id].present?
54
55
  return render_not_found('widget') if widget.blank?
55
- authorize! :manage_widget, widget
56
56
  else
57
57
  return render_not_found('dashboard') if dashboard.blank?
58
- authorize! :manage_dashboard, dashboard
59
58
  end
59
+ authorize! :create_impac_kpis, kpi_parent.kpis.build(kpi_create_params)
60
60
 
61
- # TODO: nest alert in as a param, with the current user as a recipient.
62
61
  @kpi = kpi_parent.kpis.create(kpi_create_params)
63
62
  unless kpi.errors?
64
63
  # Creates a default alert for kpis created with targets defined.
@@ -80,8 +79,7 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::Impac::KpisController
80
79
  # -> PUT /api/mnoe/v1/kpis/:id
81
80
  def update
82
81
  render_not_found('kpi') unless kpi.present?
83
-
84
- authorize! :manage_kpi, kpi
82
+ authorize! :update_impac_kpis, kpi
85
83
 
86
84
  params = kpi_update_params
87
85
 
@@ -113,8 +111,7 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::Impac::KpisController
113
111
  # -> DELETE /api/mnoe/v1/kpis/:id
114
112
  def destroy
115
113
  render_not_found('kpi') unless kpi.present?
116
-
117
- authorize! :manage_kpi, kpi
114
+ authorize! :destroy_impac_kpis, kpi
118
115
 
119
116
  if kpi.destroy
120
117
  head status: :ok
@@ -147,7 +144,9 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::Impac::KpisController
147
144
 
148
145
  def kpi_create_params
149
146
  whitelist = [:dashboard_id, :widget_id, :endpoint, :source, :element_watched, {extra_watchables: []}]
150
- extract_params(whitelist)
147
+ create_params = extract_params(whitelist)
148
+ create_params[:settings][:organization_ids] ||= HashWithIndifferentAccess.new(kpi_parent.settings)[:organization_ids]
149
+ create_params
151
150
  end
152
151
 
153
152
  def kpi_update_params