Torna o engine de automação realmente autônomo e seguro: - métricas: lê COMPRA de verdade (actions/purchase_roas) e calcula CPA; novas métricas 'cpa' e 'purchases'; regras padrão afinadas p/ low-ticket - orçamento: ações de budget agora agem no CONJUNTO (não na campanha, que lançava erro); usa listAdSets + updateAdSetBudget - segurança: "substituir via IA" desativado (criava campanha com page_id/ link inválidos) — agora só pausa e recomenda - modo de autonomia 'mixed': pausar/notificar executa; orçamento/IA viram só recomendação (com cooldown que evita re-notificar) - persistência (Supabase): regras, logs e token (app_config) — store.ts com fallback gracioso; SQL em docs/automation.sql - endpoint /api/engine/cron p/ o n8n acionar (CRON_SECRET), avalia, grava logs e dispara webhook (N8N_WEBHOOK_URL); modo summary p/ resumo diário - token de login persistido no callback p/ rodar sem cookie - aba Atividade agora lê logs reais via /api/engine/logs Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
54 lines
2.1 KiB
SQL
54 lines
2.1 KiB
SQL
-- ============================================================
|
|
-- Tabelas da Automação (rode no Supabase → SQL Editor)
|
|
-- ============================================================
|
|
-- O app acessa o Supabase com a chave anon (server-side). Mantemos RLS
|
|
-- desabilitado nestas tabelas para o engine ler/escrever, igual ao restante
|
|
-- do app (tabela agent_campaigns).
|
|
--
|
|
-- ⚠️ SEGURANÇA: app_config guarda o token da Meta. Com RLS off + chave anon,
|
|
-- quem tiver a anon key consegue ler. Para uma ferramenta privada de um único
|
|
-- dono está ok; para travar de vez, prefira definir META_SYSTEM_TOKEN por env
|
|
-- (token de System User) e NÃO depender do token salvo aqui.
|
|
|
|
-- Regras de automação ----------------------------------------
|
|
create table if not exists automation_rules (
|
|
id uuid primary key default gen_random_uuid(),
|
|
name text not null,
|
|
enabled boolean not null default true,
|
|
metric text not null,
|
|
operator text not null,
|
|
threshold numeric not null,
|
|
min_spend numeric not null default 0,
|
|
min_impressions integer not null default 0,
|
|
action text not null,
|
|
action_value numeric,
|
|
cooldown_hours integer not null default 24,
|
|
created_at timestamptz not null default now(),
|
|
updated_at timestamptz not null default now()
|
|
);
|
|
|
|
-- Logs de atividade do engine --------------------------------
|
|
create table if not exists automation_logs (
|
|
id uuid primary key default gen_random_uuid(),
|
|
rule_id text,
|
|
rule_name text,
|
|
campaign_id text not null,
|
|
campaign_name text,
|
|
action text,
|
|
details jsonb,
|
|
status text,
|
|
created_at timestamptz not null default now()
|
|
);
|
|
create index if not exists automation_logs_created_idx on automation_logs (created_at desc);
|
|
|
|
-- Config chave/valor (token da Meta, etc.) -------------------
|
|
create table if not exists app_config (
|
|
key text primary key,
|
|
value text,
|
|
updated_at timestamptz not null default now()
|
|
);
|
|
|
|
-- Mantém o acesso via chave anon (igual ao resto do app).
|
|
alter table automation_rules disable row level security;
|
|
alter table automation_logs disable row level security;
|
|
alter table app_config disable row level security;
|