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

TODO: nest alert in as a param, with the current user as a recipient.



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

def create
  if params[:kpi][:widget_id].present?
    return render_not_found('widget') if widget.blank?
  else
    return render_not_found('dashboard') if dashboard.blank?
  end  
  authorize! :create_impac_kpis, kpi_parent.kpis.build(kpi_create_params)

  @kpi = kpi_parent.kpis.create(kpi_create_params)
  unless kpi.errors?
    # 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: should widget KPIs create an email alert automatically?
      current_user.alerts.create({service: 'email', impac_kpi_id: kpi.id}) if widget.present?
      # 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


112
113
114
115
116
117
118
119
120
121
# File 'lib/mno_enterprise/concerns/controllers/jpi/v1/impac/kpis_controller.rb', line 112

def destroy
  render_not_found('kpi') unless kpi.present?
  authorize! :destroy_impac_kpis, 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


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mno_enterprise/concerns/controllers/jpi/v1/impac/kpis_controller.rb', line 80

def update
  render_not_found('kpi') unless kpi.present?
  authorize! :update_impac_kpis, 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