DROP FUNCTION IF EXISTS public.get_email_template(INT);
CREATE OR REPLACE FUNCTION public.get_email_template(p_template_id INT)
RETURNS JSONB
LANGUAGE plpgsql
AS $function$
DECLARE
    result JSONB;
BEGIN
    SELECT 
	    jsonb_build_object(
	        'notification_template', to_jsonb(et),
	        'notification_template_process', to_jsonb(etp)
	    ) INTO result
	FROM notification_templates et
	INNER JOIN notification_template_process etp 
	    ON etp.id = et.notification_template_process_id
	WHERE et.id = p_template_id and et.notification_type = 'email';

    RETURN result;
EXCEPTION
    WHEN OTHERS THEN
        RAISE EXCEPTION 'Unexpected error in get_email_template: %', SQLERRM;
END;
$function$;
