mirror of
https://github.com/lucasrcsantana/story-generator.git
synced 2025-12-17 05:47:52 +00:00
49 lines
1.4 KiB
PL/PgSQL
49 lines
1.4 KiB
PL/PgSQL
-- Criar a trigger function
|
|
create function handle_new_recording()
|
|
returns trigger
|
|
language plpgsql
|
|
security definer
|
|
as $$
|
|
declare
|
|
response json;
|
|
begin
|
|
-- Apenas processa registros com status pending_analysis
|
|
if NEW.status = 'pending_analysis' then
|
|
-- Chama a Edge Function
|
|
select
|
|
content into response
|
|
from
|
|
http((
|
|
'POST',
|
|
current_setting('app.settings.edge_function_url') || '/process-audio',
|
|
ARRAY[
|
|
('Authorization', 'Bearer ' || current_setting('app.settings.service_role_key'))::http_header,
|
|
('Content-Type', 'application/json')::http_header
|
|
],
|
|
'application/json',
|
|
json_build_object(
|
|
'record', json_build_object(
|
|
'id', NEW.id,
|
|
'story_id', NEW.story_id,
|
|
'student_id', NEW.student_id,
|
|
'audio_url', NEW.audio_url,
|
|
'status', NEW.status
|
|
)
|
|
)
|
|
)::http_request);
|
|
end if;
|
|
|
|
return NEW;
|
|
end;
|
|
$$;
|
|
|
|
-- Configurar as variáveis de ambiente
|
|
select set_config('app.settings.edge_function_url', 'https://bsjlbnyslxzsdwxvkaap.supabase.co/functions/v1', false);
|
|
select set_config('app.settings.service_role_key', 'seu_service_role_key', false);
|
|
|
|
-- Criar a trigger
|
|
create trigger process_new_recording
|
|
after insert or update
|
|
on story_recordings
|
|
for each row
|
|
execute function handle_new_recording(); |