\q
--
-- PostgreSQL database dump
--

-- Dumped from database version 15.13
-- Dumped by pg_dump version 15.13

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: cleanup_expired_emails(); Type: FUNCTION; Schema: cron; Owner: postgres
--

CREATE FUNCTION cron.cleanup_expired_emails() RETURNS void
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
    affected_users TEXT[];
    nullified_emails_count INT;
    deleted_confirmation_tokens INT;
    deleted_recovery_tokens INT;
    user_details RECORD;
BEGIN
    -- Collect user details before nullifying emails
    SELECT array_agg(DISTINCT au.user_id || ':' || COALESCE(au.email, 'NULL') || ':' || au.username) 
    INTO affected_users
    FROM legacy.all_user au
    WHERE au.confirmed = 0 
    AND au.user_id IN (
        SELECT DISTINCT ci.user_id 
        FROM public.confirmation_inner ci 
        WHERE ci.exp < NOW() - INTERVAL '1 day'
        AND ci.used = false
    )
    AND au.email IS NOT NULL;

    -- Nullify emails for users with expired unconfirmed tokens
    UPDATE legacy.all_user 
    SET email = NULL
    WHERE confirmed = 0 
    AND user_id IN (
        SELECT DISTINCT ci.user_id 
        FROM public.confirmation_inner ci 
        WHERE ci.exp < NOW() - INTERVAL '1 day'
        AND ci.used = false
    )
    AND email IS NOT NULL;
    
    GET DIAGNOSTICS nullified_emails_count = ROW_COUNT;
    
    -- Delete expired confirmation tokens (older than 2 days for safety)
    DELETE FROM public.confirmation_inner 
    WHERE exp < NOW() - INTERVAL '2 days';
    
    GET DIAGNOSTICS deleted_confirmation_tokens = ROW_COUNT;
    
    -- Delete expired password recovery tokens
    DELETE FROM legacy.recovery_inner 
    WHERE exp < NOW();
    
    GET DIAGNOSTICS deleted_recovery_tokens = ROW_COUNT;
    
    -- Log summary
    RAISE LOG 'Email cleanup completed: % emails nullified, % confirmation tokens deleted, % recovery tokens deleted', 
        nullified_emails_count, deleted_confirmation_tokens, deleted_recovery_tokens;
    
    -- Log details of affected users
    IF affected_users IS NOT NULL AND array_length(affected_users, 1) > 0 THEN
        RAISE LOG 'Nullified emails for users: %', array_to_string(affected_users, ', ');
    END IF;
    
    -- Log if no action was taken
    IF nullified_emails_count = 0 AND deleted_confirmation_tokens = 0 AND deleted_recovery_tokens = 0 THEN
        RAISE LOG 'No expired emails or tokens found for cleanup';
    END IF;
END;
$$;


ALTER FUNCTION cron.cleanup_expired_emails() OWNER TO postgres;

--
-- Name: update_token(); Type: FUNCTION; Schema: cron; Owner: postgres
--

CREATE FUNCTION cron.update_token() RETURNS void
    LANGUAGE plpgsql
    AS $$
BEGIN
    UPDATE vinn.storage SET dataimpulse_jwt = 'Bearer ' || dataimpulse.get_token();
END;
$$;


ALTER FUNCTION cron.update_token() OWNER TO postgres;

--
-- PostgreSQL database dump complete
--

