-- Journal de la console : les détails des connexions avaient été enregistrés comme texte JSON (et non objet), -- affichés lettre par lettre. Le journal ne peut pas être modifié : la lecture les remet en forme. Idempotent. create or replace function admin.as_object(d jsonb) returns jsonb language plpgsql immutable set search_path = '' as $$ begin if jsonb_typeof(d) = 'string' then d := (d #>> '{}')::jsonb; end if; return case when jsonb_typeof(d) = 'object' then d else '{}'::jsonb end; exception when others then return '{}'::jsonb; end; $$; comment on function admin.as_object(jsonb) is 'Détails du journal toujours sous forme d''objet (anciennes lignes enregistrées en texte).'; create or replace function admin.journal_list(p_offset integer) returns jsonb language sql stable set search_path = '' as $$ select coalesce(jsonb_agg(j.row order by j.id desc), '[]'::jsonb) from ( select j.id, jsonb_build_object('id', j.id, 'at', j.at, 'action', j.action, 'target', j.target, 'details', admin.as_object(j.details), 'admin', (select username from public.profiles where id = j.admin_id)) as row from admin.journal j order by j.id desc limit 50 offset greatest(coalesce(p_offset, 0), 0) ) j; $$; revoke all on function admin.as_object(jsonb) from public, anon, authenticated, service_role;