diff --git a/app/models/support/tower_case.rb b/app/models/support/tower_case.rb index ced17db46..0f6e9120a 100644 --- a/app/models/support/tower_case.rb +++ b/app/models/support/tower_case.rb @@ -2,8 +2,8 @@ module Support class TowerCase < ApplicationRecord UNSPECIFIED_VALUE = 99 enum state: Case.states, _prefix: :state - enum stage: Procurement.stages.merge(unspecified: UNSPECIFIED_VALUE), _prefix: :stage enum support_level: Case.support_levels.merge(unspecified: UNSPECIFIED_VALUE), _prefix: :support_level belongs_to :support_tower, class_name: "Support::Tower", foreign_key: :tower_id + belongs_to :procurement_stage, class_name: "Support::ProcurementStage" end end diff --git a/app/models/support/tower_statistics.rb b/app/models/support/tower_statistics.rb index 58967ecc0..c52c43e32 100644 --- a/app/models/support/tower_statistics.rb +++ b/app/models/support/tower_statistics.rb @@ -15,32 +15,49 @@ def open_cases = @tower_overview.open_cases def on_hold_cases = @tower_overview.on_hold_cases def new_cases = @tower_overview.new_cases def missing_level_cases = TowerCase.where(tower_slug:, support_level: TowerCase::UNSPECIFIED_VALUE).count - def missing_stage_cases = TowerCase.where(tower_slug:, procurement_stage: TowerCase::UNSPECIFIED_VALUE).count + def missing_stage_cases = TowerCase.where(tower_slug:, procurement_stage_id: nil).count def missing_value_cases = TowerCase.where(tower_slug:, value: nil).count def missing_org_cases = TowerCase.where(tower_slug:, organisation_id: nil).count def breakdown_of_cases_by_stage sql = <<-SQL - SELECT - procurement_stage.stage, - SUM(CASE WHEN support_tower_cases.state = 1 THEN 1 ELSE 0 END) AS open_cases, - SUM(CASE WHEN support_tower_cases.state = 3 THEN 1 ELSE 0 END) AS on_hold_cases, - SUM(CASE WHEN support_tower_cases.state = 0 THEN 1 ELSE 0 END) AS new_cases, - SUM(COALESCE(support_tower_cases.value, 0.0)) AS live_value, - '' AS id + SELECT sub.procurement_stage_id, sub.procurement_stage_title, sub.open_cases, sub.on_hold_cases, sub.new_cases, sub.live_value, sub.id FROM ( - SELECT * FROM GENERATE_SERIES(0, 6) AS stage - UNION SELECT :unspecified AS stage - ORDER BY stage - ) procurement_stage - LEFT JOIN support_tower_cases - ON support_tower_cases.procurement_stage = procurement_stage.stage - AND support_tower_cases.tower_slug = :tower_slug - GROUP BY procurement_stage.stage - ORDER BY procurement_stage.stage + SELECT + sps.id::text AS procurement_stage_id, + sps.title AS procurement_stage_title, + sps.stage AS procurement_stage_stage, + sps.updated_at AS procurement_stage_updated_at, + SUM(CASE WHEN stc.state = 1 THEN 1 ELSE 0 END) AS open_cases, + SUM(CASE WHEN stc.state = 3 THEN 1 ELSE 0 END) AS on_hold_cases, + SUM(CASE WHEN stc.state = 0 THEN 1 ELSE 0 END) AS new_cases, + SUM(COALESCE(stc.value, 0.0)) AS live_value, + '' AS id + FROM support_tower_cases stc + RIGHT JOIN support_procurement_stages sps + ON stc.procurement_stage_id = sps.id + AND stc.tower_slug = :tower_slug + GROUP BY sps.id + + UNION + + SELECT + 'unspecified' AS procurement_stage_id, + 'Unspecified' AS procurement_stage_title, + NULL AS procurement_stage_stage, + NULL AS procurement_stage_updated_at, + SUM(CASE WHEN stc.state = 1 THEN 1 ELSE 0 END) AS open_cases, + SUM(CASE WHEN stc.state = 3 THEN 1 ELSE 0 END) AS on_hold_cases, + SUM(CASE WHEN stc.state = 0 THEN 1 ELSE 0 END) AS new_cases, + SUM(COALESCE(stc.value, 0.0)) AS live_value, + '' AS id + FROM support_tower_cases stc + WHERE stc.procurement_stage_id IS NULL AND stc.tower_slug = :tower_slug + ) AS sub + ORDER BY sub.procurement_stage_stage, sub.procurement_stage_updated_at SQL - TowerCase.find_by_sql([sql, { tower_slug:, unspecified: TowerCase::UNSPECIFIED_VALUE }]) + TowerCase.find_by_sql([sql, { tower_slug: }]) end def breakdown_of_cases_by_level diff --git a/app/views/support/case_statistics/towers/show.html.erb b/app/views/support/case_statistics/towers/show.html.erb index 5d7f7576a..fb9f7d96d 100644 --- a/app/views/support/case_statistics/towers/show.html.erb +++ b/app/views/support/case_statistics/towers/show.html.erb @@ -77,10 +77,10 @@
<% @tower_statistics.breakdown_of_cases_by_stage.each do |stage| %><%= link_to I18n.t("support.case_statistics.missing_data.no_value", count: @tower_statistics.missing_value_cases), "#", class: "govuk-link govuk-link--no-visited-state" %>
<% =end%> -<%= link_to I18n.t("support.case_statistics.missing_data.no_stage", count: @tower_statistics.missing_stage_cases).html_safe, filtered_tower_path(@tower, stage: :unspecified, state: "live"), class: "govuk-link govuk-link--no-visited-state" %>
+<%= link_to I18n.t("support.case_statistics.missing_data.no_stage", count: @tower_statistics.missing_stage_cases).html_safe, filtered_tower_path(@tower, procurement_stage: :unspecified, state: "live"), class: "govuk-link govuk-link--no-visited-state" %>
<%= link_to I18n.t("support.case_statistics.missing_data.no_level", count: @tower_statistics.missing_level_cases).html_safe, filtered_tower_path(@tower, level: :unspecified, state: "live"), class: "govuk-link govuk-link--no-visited-state" %>
<%= link_to I18n.t("support.case_statistics.missing_data.no_org", count: @tower_statistics.missing_org_cases).html_safe, filtered_tower_path(@tower, has_org: :false, state: "live"), class: "govuk-link govuk-link--no-visited-state" %>
diff --git a/db/migrate/20230830101649_update_support_tower_cases_to_version_4.rb b/db/migrate/20230830101649_update_support_tower_cases_to_version_4.rb new file mode 100644 index 000000000..fe0bf77eb --- /dev/null +++ b/db/migrate/20230830101649_update_support_tower_cases_to_version_4.rb @@ -0,0 +1,5 @@ +class UpdateSupportTowerCasesToVersion4 < ActiveRecord::Migration[7.0] + def change + update_view :support_tower_cases, version: 4, revert_to_version: 3 + end +end diff --git a/db/schema.rb b/db/schema.rb index 447d65958..c43fe089b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_08_25_101049) do +ActiveRecord::Schema[7.0].define(version: 2023_08_30_101649) do # These are extensions that must be enabled in order to support this database enable_extension "citext" enable_extension "pg_trgm" @@ -995,15 +995,14 @@ sc.value, sc.procurement_id, sc.organisation_id, - COALESCE(sp.stage, 99) AS procurement_stage, + sc.procurement_stage_id, COALESCE(sc.support_level, 99) AS support_level, COALESCE(tow.title, 'No Tower'::character varying) AS tower_name, lower(replace((COALESCE(tow.title, 'No Tower'::character varying))::text, ' '::text, '-'::text)) AS tower_slug, tow.id AS tower_id, sc.created_at, sc.updated_at - FROM (((support_cases sc - JOIN support_procurements sp ON ((sp.id = sc.procurement_id))) + FROM ((support_cases sc LEFT JOIN support_categories cat ON ((sc.category_id = cat.id))) LEFT JOIN support_towers tow ON ((cat.support_tower_id = tow.id))) WHERE (sc.state = ANY (ARRAY[0, 1, 3])); diff --git a/db/views/support_tower_cases_v04.sql b/db/views/support_tower_cases_v04.sql new file mode 100644 index 000000000..2a55f9021 --- /dev/null +++ b/db/views/support_tower_cases_v04.sql @@ -0,0 +1,16 @@ +SELECT sc.id, + sc.state, + sc.value, + sc.procurement_id, + sc.organisation_id, + sc.procurement_stage_id AS procurement_stage_id, + COALESCE(sc.support_level, 99) AS support_level, + COALESCE(tow.title, 'No Tower') AS tower_name, + LOWER(REPLACE(COALESCE(tow.title, 'No Tower'), ' ', '-')) AS tower_slug, + tow.id AS tower_id, + sc.created_at, + sc.updated_at +FROM support_cases sc +LEFT JOIN support_categories AS cat ON sc.category_id = cat.id +LEFT JOIN support_towers AS tow ON cat.support_tower_id = tow.id +WHERE sc.state IN (0, 1, 3) -- new, opened, on-hold