Browse Source

Fixes #5463 - delete reporting profile with id=1.

Co-authored-by: Martin Gruner <mg@zammad.com>
Rolf Schmidt 1 month ago
parent
commit
62ba8df31e

+ 29 - 13
app/assets/javascripts/app/controllers/report.coffee

@@ -18,7 +18,10 @@ class Reporting extends App.ControllerAppContent
             objectName: 'Report'
           )
           return
-        @config = data.config
+        @config   = data.config
+        @profiles = data.profiles
+        @checkStoreParams()
+
         App.Collection.load(type: 'ReportProfile', data: data.profiles)
         @render()
     )
@@ -27,7 +30,7 @@ class Reporting extends App.ControllerAppContent
     return @params if @params
 
     @params = {}
-    paramsRaw = App.SessionStorage.get('report::params')
+    paramsRaw = @getStoreParams()
     if paramsRaw
       @params = paramsRaw
       return @params
@@ -60,7 +63,20 @@ class Reporting extends App.ControllerAppContent
 
     @params
 
-  storeParams: =>
+  checkStoreParams: =>
+    params = @getStoreParams()
+    return if _.isEmpty(params)
+
+    configProfileIDs = @profiles.map((profile) -> profile.id)
+    storeProfileIDs  = Object.keys(params.profileSelected).map((id) -> parseInt(id))
+    return if _.intersection(configProfileIDs, storeProfileIDs).length is storeProfileIDs.length
+
+    App.SessionStorage.delete('report::params')
+
+  getStoreParams: ->
+    App.SessionStorage.get('report::params')
+
+  setStoreParams: =>
     # store latest params
     App.SessionStorage.set('report::params', @params)
 
@@ -116,7 +132,7 @@ class Graph extends App.Controller
     for key, value of data.data
       if @params.backendSelected[key] is true
         dataNew[key] = value
-      @ui.storeParams()
+      @ui.setStoreParams()
 
     if !@lastParams
       @lastParams = {}
@@ -252,7 +268,7 @@ class Download extends App.Controller
     $(e.target).parent().addClass('active')
     @profileSelectedId = $(e.target).data('profile-id')
     @params.downloadBackendSelected = $(e.target).data('backend')
-    @ui.storeParams()
+    @ui.setStoreParams()
     @table = false
     @render()
 
@@ -272,7 +288,7 @@ class Download extends App.Controller
       for backend in @config.metric[@params.metric].backend
         if backend.dataDownload && !@params.downloadBackendSelected
           @params.downloadBackendSelected = backend.name
-      @ui.storeParams()
+      @ui.setStoreParams()
 
     # get used profiles
     profiles = []
@@ -458,7 +474,7 @@ class TimePicker extends App.Controller
     $(e.target).parent().parent().find('li').removeClass('active')
     $(e.target).parent().addClass('active')
     App.Event.trigger('ui:report:rerender')
-    @ui.storeParams()
+    @ui.setStoreParams()
 
   selectTimeMonth: (e) =>
     e.preventDefault()
@@ -466,7 +482,7 @@ class TimePicker extends App.Controller
     $(e.target).parent().parent().find('li').removeClass('active')
     $(e.target).parent().addClass('active')
     App.Event.trigger('ui:report:rerender')
-    @ui.storeParams()
+    @ui.setStoreParams()
 
   selectTimeWeek: (e) =>
     e.preventDefault()
@@ -474,7 +490,7 @@ class TimePicker extends App.Controller
     $(e.target).parent().parent().find('li').removeClass('active')
     $(e.target).parent().addClass('active')
     App.Event.trigger('ui:report:rerender')
-    @ui.storeParams()
+    @ui.setStoreParams()
 
   selectTimeYear: (e) =>
     e.preventDefault()
@@ -483,7 +499,7 @@ class TimePicker extends App.Controller
     $(e.target).parent().parent().find('li').removeClass('active')
     $(e.target).parent().addClass('active')
     App.Event.trigger('ui:report:rerender')
-    @ui.storeParams()
+    @ui.setStoreParams()
 
   _timeSlotPicker: ->
     @timeRangeYear = []
@@ -592,7 +608,7 @@ class Sidebar extends App.Controller
     @params.metric                  = metric
     @params.downloadBackendSelected = undefined
     App.Event.trigger('ui:report:rerender')
-    @ui.storeParams()
+    @ui.setStoreParams()
 
   selectProfile: (e) =>
     profile_id = $(e.target).val()
@@ -600,7 +616,7 @@ class Sidebar extends App.Controller
       delete @params.profileSelected[key]
     @params.profileSelected[profile_id] = true
     App.Event.trigger('ui:report:rerender')
-    @ui.storeParams()
+    @ui.setStoreParams()
 
   selectBackend: (e) =>
     backend = $(e.target).val()
@@ -610,7 +626,7 @@ class Sidebar extends App.Controller
     else
       delete @params.backendSelected[backend]
     App.Event.trigger('ui:report:rerender')
-    @ui.storeParams()
+    @ui.setStoreParams()
 
 App.Config.set('report', Reporting, 'Routes')
 App.Config.set('Reporting', { prio: 8000, parent: '', name: __('Reporting'), translate: true, target: '#report', icon: 'report', permission: ['report'] }, 'NavBarRight')

+ 1 - 0
app/assets/javascripts/app/models/_application_model.coffee

@@ -742,6 +742,7 @@ set new attributes of model (remove already available attributes)
   @_fillUp: (data, classNames = []) ->
 
     # fill up via relations
+    return data if !data
     return data if !App[@className].configure_attributes
     for attribute in App[@className].configure_attributes
 

+ 10 - 1
app/controllers/reports_controller.rb

@@ -11,9 +11,18 @@ class ReportsController < ApplicationController
       }
       return
     end
+
+    profiles = Report::Profile.list
+    if profiles.blank?
+      render json: {
+        error: __('There are currently no report profiles configured.'),
+      }
+      return
+    end
+
     render json: {
       config:   Report.config,
-      profiles: Report::Profile.list,
+      profiles: profiles,
     }
   end
 

+ 1 - 0
app/models/report/profile.rb

@@ -3,6 +3,7 @@
 class Report::Profile < ApplicationModel
   self.table_name = 'report_profiles'
   include ChecksConditionValidation
+  include ChecksClientNotification
   include HasSearchIndexBackend
   include CanSelector
   include CanSearch

+ 7 - 3
i18n/zammad.pot

@@ -12084,11 +12084,11 @@ msgstr ""
 msgid "Requesting the Grant"
 msgstr ""
 
-#: app/controllers/reports_controller.rb:57
+#: app/controllers/reports_controller.rb:66
 msgid "Required parameter 'downloadBackendSelected' is missing."
 msgstr ""
 
-#: app/controllers/reports_controller.rb:116
+#: app/controllers/reports_controller.rb:125
 msgid "Required parameter 'profile' is missing."
 msgstr ""
 
@@ -14341,7 +14341,7 @@ msgstr ""
 msgid "The account link was successfully removed!"
 msgstr ""
 
-#: app/controllers/reports_controller.rb:129
+#: app/controllers/reports_controller.rb:138
 msgid "The active reporting profile could not be found."
 msgstr ""
 
@@ -15356,6 +15356,10 @@ msgstr ""
 msgid "Theme"
 msgstr ""
 
+#: app/controllers/reports_controller.rb:17
+msgid "There are currently no report profiles configured."
+msgstr ""
+
 #: app/assets/javascripts/app/views/import/zendesk.jst.eco:64
 msgid "There are more than 1000 tickets in the Zendesk system. Due to API rate limit restrictions, Zammad can't get the exact number of tickets yet and has to fetch them in batches of 1000. This might take some time, better grab a cup of coffee. The total number of tickets gets updated as soon as the currently known number is surpassed."
 msgstr ""

+ 27 - 0
spec/system/report_spec.rb

@@ -59,4 +59,31 @@ RSpec.describe 'Report', searchindex: true, type: :system do
       expect(page).to have_no_css('.modal')
     end
   end
+
+  describe 'delete reporting profile with id=1 #5463' do
+    context 'when report profiles are not present', authenticated_as: :authenticate do
+      def authenticate
+        Report::Profile.destroy_all
+        true
+      end
+
+      it 'does show a message that no report profiles are given' do
+        visit 'report'
+        expect(page).to have_text('There are currently no report profiles configured.')
+      end
+    end
+
+    context 'when selected report profile is replaced with a new one' do
+      it 'does clear the session storage cache and selects the new report profile' do
+        visit 'report'
+        expect(page).to have_text(Report::Profile.first.name)
+        click ".js-dataDownloadBackendSelector[data-backend='count::closed']"
+        Report::Profile.first.destroy
+        new_report_profile = create(:report_profile)
+        refresh
+        expect(page).to have_text(new_report_profile.name)
+        expect(page).to have_no_text('The report could not be generated')
+      end
+    end
+  end
 end