From 7281f3ec1af1ba526a02b7ff57aa09c455acc460 Mon Sep 17 00:00:00 2001 From: Kara Diaby Date: Tue, 21 May 2024 09:57:43 +0000 Subject: [PATCH] SQL pour renvoyer les zones dans ProcedureDetail --- app/controllers/administrateurs/procedures_controller.rb | 3 ++- app/models/procedure_detail.rb | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/controllers/administrateurs/procedures_controller.rb b/app/controllers/administrateurs/procedures_controller.rb index 9b1b2b1b9..4dde5c6c2 100644 --- a/app/controllers/administrateurs/procedures_controller.rb +++ b/app/controllers/administrateurs/procedures_controller.rb @@ -482,7 +482,8 @@ module Administrateurs procedures_result = procedures_result.where('unaccent(libelle) ILIKE unaccent(?)', "%#{filter.libelle}%") if filter.libelle.present? procedures_sql = procedures_result.to_sql - sql = "select id, libelle, published_at, aasm_state, estimated_dossiers_count, template, count(administrateurs_procedures.administrateur_id) as admin_count from administrateurs_procedures inner join procedures on procedures.id = administrateurs_procedures.procedure_id where procedures.id in (#{procedures_sql}) group by procedures.id order by published_at desc" + sql = "select procedures.id, libelle, published_at, aasm_state, estimated_dossiers_count, template, array_agg(distinct latest_labels.name) filter (where latest_labels.name is not null) as latest_zone_labels from administrateurs_procedures inner join procedures on procedures.id = administrateurs_procedures.procedure_id left join procedures_zones ON procedures.id = procedures_zones.procedure_id left join zones ON zones.id = procedures_zones.zone_id left join (select zone_id, name from zone_labels where (zone_id, designated_on) in (select zone_id, max(designated_on) from zone_labels group by zone_id)) as latest_labels on zones.id = latest_labels.zone_id + where procedures.id in (#{procedures_sql}) group by procedures.id order by published_at desc" ActiveRecord::Base.connection.execute(sql) end diff --git a/app/models/procedure_detail.rb b/app/models/procedure_detail.rb index c590787b3..ebe6c3f6d 100644 --- a/app/models/procedure_detail.rb +++ b/app/models/procedure_detail.rb @@ -1,4 +1,4 @@ -ProcedureDetail = Struct.new(:id, :libelle, :published_at, :aasm_state, :estimated_dossiers_count, :admin_count, :template, keyword_init: true) do +ProcedureDetail = Struct.new(:id, :libelle, :published_at, :aasm_state, :estimated_dossiers_count, :admin_count, :template, :latest_zone_labels, keyword_init: true) do include SpreadsheetArchitect def spreadsheet_columns @@ -12,4 +12,11 @@ ProcedureDetail = Struct.new(:id, :libelle, :published_at, :aasm_state, :estimat def administrateurs AdministrateursCounter.new(admin_count) end + + def parsed_latest_zone_labels + # Replace curly braces with square brackets to make it a valid JSON array + JSON.parse(latest_zone_labels.tr('{', '[').tr('}', ']')) + rescue JSON::ParserError + [] + end end