alter table public.challenge_retry_requests add column if not exists retry_job_id uuid; create or replace function public.claim_approved_retry( p_request_id uuid, p_round_id uuid, p_course_id text, p_auth_provider text, p_provider_user_id text, p_job_id uuid ) returns jsonb language plpgsql security definer set search_path = public, pg_temp as $$ declare v_hints_limit integer; v_hints_used integer; v_request_id uuid; begin select hints_per_student into v_hints_limit from public.challenge_rounds where round_id = p_round_id and course_id = p_course_id and visibility = 'private' and status in ('open', 'closed') for update; if not found then return jsonb_build_object('admitted', false, 'reason', 'round_unavailable'); end if; select hints_used into v_hints_used from public.round_student_quotas where round_id = p_round_id and auth_provider = p_auth_provider and provider_user_id = p_provider_user_id for update; if not found then return jsonb_build_object('admitted', false, 'reason', 'quota_missing'); end if; update public.challenge_retry_requests set retry_job_id = p_job_id where request_id = p_request_id and round_id = p_round_id and course_id = p_course_id and auth_provider = p_auth_provider and provider_user_id = p_provider_user_id and status = 'approved' and retry_job_id is null returning request_id into v_request_id; if not found then return jsonb_build_object('admitted', false, 'reason', 'retry_unavailable'); end if; return jsonb_build_object('admitted', true, 'hints_per_student', v_hints_limit, 'hints_used', v_hints_used); end; $$; revoke all on function public.claim_approved_retry(uuid, uuid, text, text, text, uuid) from public, anon, authenticated; grant execute on function public.claim_approved_retry(uuid, uuid, text, text, text, uuid) to service_role; create or replace function public.release_approved_retry( p_request_id uuid, p_course_id text, p_auth_provider text, p_provider_user_id text, p_job_id uuid ) returns jsonb language plpgsql security definer set search_path = public, pg_temp as $$ begin update public.challenge_retry_requests set retry_job_id = null where request_id = p_request_id and course_id = p_course_id and auth_provider = p_auth_provider and provider_user_id = p_provider_user_id and retry_job_id = p_job_id and status = 'approved'; return jsonb_build_object('released', found); end; $$; revoke all on function public.release_approved_retry(uuid, text, text, text, uuid) from public, anon, authenticated; grant execute on function public.release_approved_retry(uuid, text, text, text, uuid) to service_role; create or replace function public.claim_submission_score_review( p_submission_id text, p_auth_provider text, p_provider_user_id text, p_operation_id uuid ) returns jsonb language plpgsql security definer set search_path = public, pg_temp as $$ declare v_round_id uuid; v_round_status text; v_review public.submission_score_reviews%rowtype; begin perform pg_advisory_xact_lock(hashtextextended(p_submission_id, 0)); select round_id into v_round_id from public.submissions where submission_id = p_submission_id and auth_provider = p_auth_provider and provider_user_id = p_provider_user_id; if not found then return jsonb_build_object('claimed', false, 'reason', 'submission_not_found'); end if; select * into v_review from public.submission_score_reviews where submission_id = p_submission_id; if found then return jsonb_build_object('claimed', false, 'status', v_review.status, 'llm_score', v_review.llm_score, 'explanation', v_review.explanation, 'error_code', v_review.error_code, 'units', v_review.units); end if; if v_round_id is not null then select status into v_round_status from public.challenge_rounds where round_id = v_round_id and course_id = ( select course_id from public.submissions where submission_id = p_submission_id ) and (status = 'open' or (status = 'closed' and exists ( select 1 from public.submissions s join public.challenge_retry_requests r on r.round_id = s.round_id and r.course_id = s.course_id and r.auth_provider = s.auth_provider and r.provider_user_id = s.provider_user_id and r.retry_job_id::text = s.workspace_id and r.status = 'approved' where s.submission_id = p_submission_id ))) for update; if not found then return jsonb_build_object('claimed', false, 'reason', 'round_unavailable'); end if; end if; insert into public.submission_score_reviews(submission_id, auth_provider, provider_user_id, round_id, operation_id, status, units) values (p_submission_id, p_auth_provider, p_provider_user_id, v_round_id, p_operation_id, 'processing', case when v_round_id is null then 0 else 2 end); return jsonb_build_object('claimed', true, 'status', 'processing', 'units', case when v_round_id is null then 0 else 2 end); end; $$; revoke all on function public.claim_submission_score_review(text, text, text, uuid) from public, anon, authenticated; grant execute on function public.claim_submission_score_review(text, text, text, uuid) to service_role;