-- BUG-704: rectification activity must advance chat_sessions.updated_at the -- same way append_consultation_question does. Open / GET / metadata PATCH -- still must not bump it (BUG-553). begin; do $migration$ begin if current_user <> 'schema_owner' then raise exception 'rectification_touch_chat_session_requires_schema_owner' using errcode = '42501'; end if; end $migration$; create or replace function public.touch_chat_session_from_rectification_case() returns trigger language plpgsql security definer set search_path = '' as $$ begin if tg_op = 'UPDATE' and new.last_activity_at is not distinct from old.last_activity_at then return new; end if; update public.chat_sessions set updated_at = new.last_activity_at where id = new.session_id and user_id = new.user_id and session_type = 'birth_time_rectification' and updated_at is distinct from new.last_activity_at and updated_at < new.last_activity_at; return new; end; $$; drop trigger if exists agentic_rectification_cases_touch_chat_session on public.agentic_rectification_cases; create trigger agentic_rectification_cases_touch_chat_session after update of last_activity_at on public.agentic_rectification_cases for each row execute function public.touch_chat_session_from_rectification_case(); revoke all on function public.touch_chat_session_from_rectification_case() from public, anon, authenticated; grant execute on function public.touch_chat_session_from_rectification_case() to schema_owner; commit;