From ade53ec169cc0d12af521af0abe85ecd9ceb8273 Mon Sep 17 00:00:00 2001 From: martintomas Date: Tue, 7 Nov 2023 11:33:23 +0100 Subject: [PATCH] refactoring: Hide V4.0 MQ assessments if V5.0 BETA data exists with same assessment date --- app/helpers/mq_assessment_helper.rb | 7 +++++++ app/models/mq/assessment.rb | 2 +- app/services/api/charts/mq_assessment.rb | 8 +++++++- app/views/tpi/companies/_mq_assessment.html.erb | 4 ++-- app/views/tpi/companies/show.html.erb | 4 ++-- spec/services/api/charts/mq_assessment_spec.rb | 6 ++++-- 6 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 app/helpers/mq_assessment_helper.rb diff --git a/app/helpers/mq_assessment_helper.rb b/app/helpers/mq_assessment_helper.rb new file mode 100644 index 000000000..17345bdd1 --- /dev/null +++ b/app/helpers/mq_assessment_helper.rb @@ -0,0 +1,7 @@ +module MQAssessmentHelper + def hide_mq_assessments_with_same_date(assessments) + result = [] + assessments.group_by(&:assessment_date).each { |_date, a| result << a.max_by(&:methodology_version) } + result + end +end diff --git a/app/models/mq/assessment.rb b/app/models/mq/assessment.rb index 598e971b5..2e3b46082 100644 --- a/app/models/mq/assessment.rb +++ b/app/models/mq/assessment.rb @@ -49,7 +49,7 @@ def previous_assessments # keep select and sort by, company - mq assessments will be cached once company .mq_assessments - .currently_published + .select { |a| a.publication_date <= DateTime.now } .select { |a| a.assessment_date < assessment_date } .sort_by(&:assessment_date) end diff --git a/app/services/api/charts/mq_assessment.rb b/app/services/api/charts/mq_assessment.rb index c129710c8..5573ec921 100644 --- a/app/services/api/charts/mq_assessment.rb +++ b/app/services/api/charts/mq_assessment.rb @@ -47,7 +47,7 @@ def company_mq_assessments @company_mq_assessments ||= begin query = company.mq_assessments.currently_published.order(:assessment_date) query = query.without_beta_methodologies unless @enable_beta_mq_assessments - query + hide_mq_assessments_with_same_date query end end @@ -57,6 +57,12 @@ def max_level beta_assessment.beta_levels.last.to_i end + + def hide_mq_assessments_with_same_date(assessments) + result = [] + assessments.group_by(&:assessment_date).each { |_date, a| result << a.max_by(&:methodology_version) } + result + end end end end diff --git a/app/views/tpi/companies/_mq_assessment.html.erb b/app/views/tpi/companies/_mq_assessment.html.erb index 1c70d9d08..11ef814f4 100644 --- a/app/views/tpi/companies/_mq_assessment.html.erb +++ b/app/views/tpi/companies/_mq_assessment.html.erb @@ -15,9 +15,9 @@
-
Nr of assessments: <%= assessment.previous_assessments.size + 1 %>
+
Nr of assessments: <%= hide_mq_assessments_with_same_date(assessment.previous_assessments).size + 1 %>
- Number of assessments: <%= assessment.previous_assessments.size + 1 %> + Number of assessments: <%= hide_mq_assessments_with_same_date(assessment.previous_assessments).size + 1 %>
<%= react_component('charts/mq-level', { diff --git a/app/views/tpi/companies/show.html.erb b/app/views/tpi/companies/show.html.erb index 3ad25bccf..a9bf42014 100644 --- a/app/views/tpi/companies/show.html.erb +++ b/app/views/tpi/companies/show.html.erb @@ -74,7 +74,7 @@

Management Quality

- Number of assessments: <%= @company_presenter.mq_assessments.size %> + Number of assessments: <%= hide_mq_assessments_with_same_date(@company_presenter.mq_assessments).size %>

<% if @company_presenter.mq_assessments.any? %> <%= render 'mq_level', level: @company.mq_level, status: @company.mq_status %> @@ -170,7 +170,7 @@ name: 'mq_assessment_id', remote: true, url: mq_assessment_tpi_company_path(@company), - data: @company_presenter.mq_assessments.map {|v| {label: v['assessment_date']&.strftime('%d %B %Y'), value: v['id']}}, + data: hide_mq_assessments_with_same_date(@company_presenter.mq_assessments).map {|v| {label: v['assessment_date']&.strftime('%d %B %Y'), value: v['id']}}, selected: params[:mq_assessment_id] }) %>
diff --git a/spec/services/api/charts/mq_assessment_spec.rb b/spec/services/api/charts/mq_assessment_spec.rb index f318fae5d..dcc8bd75c 100644 --- a/spec/services/api/charts/mq_assessment_spec.rb +++ b/spec/services/api/charts/mq_assessment_spec.rb @@ -17,6 +17,7 @@ create(:mq_assessment, company: company, assessment_date: '2018-08-08', publication_date: '2018-08-08', level: '2') create(:mq_assessment, company: company, assessment_date: '2019-02-02', publication_date: '2019-02-02', level: '4') create(:mq_assessment, company: company, assessment_date: '2020-03-03', publication_date: '2020-03-03', level: '3') + create(:mq_assessment, company: company, assessment_date: '2021-03-03', publication_date: '2021-03-03', level: '4') # beta scores create(:mq_assessment, company: company, @@ -43,7 +44,8 @@ ['01/01/2018', 3], ['08/08/2018', 2], ['02/02/2019', 4], - ['03/03/2020', 3] + ['03/03/2020', 3], + ['03/03/2021', 4] ], name: 'Level' }, @@ -64,7 +66,7 @@ context 'when beta assessments are enabled' do subject do described_class.new( - company.mq_assessments.where(assessment_date: '2021-03-03').first, + company.mq_assessments.where(assessment_date: '2021-03-03', methodology_version: beta_methodology).first, enable_beta_mq_assessments: true ) end