Module: MnoEnterprise::Concerns::Controllers::Jpi::V1::Impac::KpisController

Extended by:
ActiveSupport::Concern
Included in:
Jpi::V1::Impac::KpisController
Defined in:
lib/mno_enterprise/concerns/controllers/jpi/v1/impac/kpis_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /mnoe/jpi/v1/impac/dashboards/:dashboard_id/kpis

-> POST /api/mnoe/v1/dashboards/:id/kpis
-> POST /api/mnoe/v1/users/:id/alerts


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mno_enterprise/concerns/controllers/jpi/v1/impac/kpis_controller.rb', line 52

def create
  # TODO: ability for managing widget.
  authorize! :manage_dashboard, dashboard
  # TODO: attach widget onto KPI capability.
  # authorize! :manage_widget, widget if widget

  # TODO: nest alert in as a param, with the current user as a recipient.
  if @kpi = kpi_parent.kpis.create(kpi_create_params)
    # Creates a default alert for kpis created with targets defined.
    if kpi.targets.present?
      current_user.alerts.create({service: 'inapp', impac_kpi_id: kpi.id})
      # TODO: reload is adding the recipients to the kpi alerts (making another request).
      kpi.reload
    end
    render 'show'
  else
    msg = kpi.errors.full_messages.join(', ') || 'unable to create KPI.'
    render_bad_request("create kpi (id=#{kpi.id})", msg)
  end
end

#destroyObject

DELETE /mnoe/jpi/v1/impac/kpis/:id

-> DELETE /api/mnoe/v1/kpis/:id


106
107
108
109
110
111
112
113
114
# File 'lib/mno_enterprise/concerns/controllers/jpi/v1/impac/kpis_controller.rb', line 106

def destroy
  authorize! :manage_kpi, kpi

  if kpi.destroy
    head status: :ok
  else
    render_bad_request('destroy kpi', "impossible to destroy kpi (id=#{kpi.id})")
  end
end

#indexObject

Instance methods

GET /mnoe/jpi/v1/impac/kpis This action is used as a sort of ‘proxy’ for retrieving KPI templates which are usually retrieved from Impac! API, and customising the attributes.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mno_enterprise/concerns/controllers/jpi/v1/impac/kpis_controller.rb', line 21

def index
  # Retrieve kpis templates from impac api.
  # TODO: improve request params to work for strong parameters
  attrs = params.slice('metadata', 'opts')
  auth = { username: MnoEnterprise.tenant_id, password: MnoEnterprise.tenant_key }

  response = begin
    MnoEnterprise::ImpacClient.send_get('/api/v2/kpis', attrs, basic_auth: auth)
  rescue => e
    return render json: { message: "Unable to retrieve kpis from Impac API | Error #{e}" }
  end

  # customise available kpis
  kpis = (response['kpis'] || []).map do |kpi|
    kpi = kpi.with_indifferent_access
    kpi[:watchables].map do |watchable|
      kpi.merge(
        name: "#{kpi[:name]} #{watchable.capitalize unless kpi[:name].downcase.index(watchable)}".strip,
        watchables: [watchable],
        target_placeholders: {watchable => kpi[:target_placeholders][watchable]},
      )
    end
  end
  .flatten

  render json: { kpis: kpis }
end

#updateObject

PUT /mnoe/jpi/v1/impac/kpis/:id

-> PUT /api/mnoe/v1/kpis/:id


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mno_enterprise/concerns/controllers/jpi/v1/impac/kpis_controller.rb', line 75

def update
  authorize! :manage_kpi, kpi

  params = kpi_update_params

  # TODO: refactor into models
  # --
  # Creates an in-app alert if target is set for the first time (in-app alerts should be activated by default)
  if kpi.targets.blank? && params[:targets].present?
    current_user.alerts.create({service: 'inapp', impac_kpi_id: kpi.id})

  # If targets have changed, reset all the alerts 'sent' status to false.
  elsif kpi.targets && params[:targets].present? && params[:targets] != kpi.targets
    kpi.alerts.each { |alert| alert.update(sent: false) }

  # Removes all the alerts if the targets are removed (kpi has no targets set,
  # and params contains no targets to be set)
  elsif params[:targets].blank? && kpi.targets.blank?
    kpi.alerts.each(&:destroy)
  end

  if kpi.update(kpi_update_params)
    render 'show'
  else
    msg = @kpi.errors.full_messages.join(', ') || 'unable to update KPI.'
    render_bad_request("update kpi (id=#{kpi.id})", msg)
  end
end