Files
Matheus a05104a05f feat(agents): agentes, tiers e pausas — call center completo
Fecha agente.md secao 45-49/52. Depois desta fase, um usuario autenticado
consegue logar como agente, entrar numa fila real, se pausar e voltar,
tudo refletido de verdade no FreeSWITCH.

Schema (migration 20260828124245_agents):
- agents (tenant-scoped, RLS): User -> Extension -> identidade de agente,
  state (enum AgentState de 8 valores) espelhando o estado real, so
  alterado via login/logout/pause/resume, nunca escrito direto pela API.
- tiers: Queue<->Agent (level/position 1:1 com mod_callcenter).
- agent_sessions: um ciclo login->logout por linha.
- agent_state_events: historico de transicoes de estado.
- pause_reasons / agent_pause_events (secao 48).

Dois bugs reais corrigidos em FreeSwitchTelephonyProvider, presentes desde
a fase de Event Socket original:
- "queue add/del member" nao existe no mod_callcenter — membership de
  fila usa tier add/tier del. So foi pego agora ao confirmar de novo a
  sintaxe via `help callcenter_config` antes de codar esta fase.
- addAgent/removeAgent nao existiam ainda (agent add/del).

Mecanismo de sync: agentes e tiers nao tem representacao em XML, so
comando ESL direto — diferente do padrao "regenera todos os arquivos"
usado em Trunks/Queues. apps/api publica uma mensagem por acao com
payload (b2bcall:agents:sync, b2bcall:tiers:sync); b2bcall-fs-config
aplica o comando correspondente (agent-sync.ts).

Achados confirmados manualmente contra o FreeSWITCH real antes de codar:
- `agent add`/`tier add` nao sao idempotentes (erro em duplicata) — sync
  ignora esse erro (.catch), condicao esperada em resync.
- `agent del`/`tier del` em algo inexistente nao da erro — seguro chamar
  sem checar existencia antes.
- `agent set status` so aceita 3 valores exatos (Available/On Break/
  Logged Out) — testado deliberadamente com valor invalido.
- Corrida real: atribuir tier antes do primeiro login do agente falha
  silenciosamente do lado do FreeSWITCH (agente so existe la a partir do
  `agent add` no login). Login sempre re-sincroniza todos os tiers do
  agente depois de garantir que ele existe — auto-correcao confirmada no
  teste ponta a ponta.

apps/api: AgentsController (CRUD), AgentsMeController (login/logout/
pause/resume — sempre resolve o agente via JWT, nunca um agentId
arbitrario do client), PauseReasonsController (CRUD), QueueAgentsController
(POST/DELETE de tier em /queues/:id/agents).

Verificado ponta a ponta via curl + fs_cli contra o FreeSWITCH real:
login -> Available, pause -> On Break, resume -> Available, logout ->
Logged Out, todos batendo entre Agent.state (banco) e `agent list`
(FreeSWITCH). typecheck do workspace inteiro limpo. ~350MB de memoria
total (docker stats).

Documentado em docs/AGENTS.md, incluindo lacuna conhecida: estados
derivados de chamada (RINGING/IN_CALL/WRAP_UP/RESERVED) dependem do
evento CUSTOM callcenter::info, ainda nao comprovado chegando em
fs-events nesta sessao (mesma lacuna de sofia::gateway_state ja
documentada em docs/TRUNKS.md) — precisa de uma chamada real passando
pela fila pra investigar.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1HxY46WGU4G1zmVDNKcWw
2026-08-28 09:58:45 -03:00

188 lines
7.5 KiB
SQL

-- CreateEnum
CREATE TYPE "agent_state" AS ENUM ('OFFLINE', 'LOGGED_IN', 'AVAILABLE', 'RESERVED', 'RINGING', 'IN_CALL', 'WRAP_UP', 'PAUSED');
-- CreateTable
CREATE TABLE "agents" (
"id" UUID NOT NULL,
"tenant_id" UUID NOT NULL,
"user_id" UUID NOT NULL,
"extension_id" UUID,
"name" TEXT NOT NULL,
"max_no_answer" INTEGER NOT NULL DEFAULT 3,
"wrap_up_time" INTEGER NOT NULL DEFAULT 10,
"reject_delay_time" INTEGER NOT NULL DEFAULT 10,
"busy_delay_time" INTEGER NOT NULL DEFAULT 60,
"no_answer_delay_time" INTEGER NOT NULL DEFAULT 10,
"state" "agent_state" NOT NULL DEFAULT 'OFFLINE',
"state_updated_at" TIMESTAMP(3),
"enabled" BOOLEAN NOT NULL DEFAULT true,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"deleted_at" TIMESTAMP(3),
CONSTRAINT "agents_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "tiers" (
"id" UUID NOT NULL,
"tenant_id" UUID NOT NULL,
"queue_id" UUID NOT NULL,
"agent_id" UUID NOT NULL,
"level" INTEGER NOT NULL DEFAULT 1,
"position" INTEGER NOT NULL DEFAULT 1,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "tiers_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "agent_sessions" (
"id" UUID NOT NULL,
"tenant_id" UUID NOT NULL,
"agent_id" UUID NOT NULL,
"started_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"ended_at" TIMESTAMP(3),
CONSTRAINT "agent_sessions_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "agent_state_events" (
"id" UUID NOT NULL,
"tenant_id" UUID NOT NULL,
"agent_id" UUID NOT NULL,
"state" "agent_state" NOT NULL,
"occurred_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "agent_state_events_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "pause_reasons" (
"id" UUID NOT NULL,
"tenant_id" UUID NOT NULL,
"name" TEXT NOT NULL,
"code" TEXT NOT NULL,
"description" TEXT,
"max_duration" INTEGER,
"paid" BOOLEAN NOT NULL DEFAULT false,
"enabled" BOOLEAN NOT NULL DEFAULT true,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
CONSTRAINT "pause_reasons_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "agent_pause_events" (
"id" UUID NOT NULL,
"tenant_id" UUID NOT NULL,
"agent_id" UUID NOT NULL,
"pause_reason_id" UUID NOT NULL,
"started_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"ended_at" TIMESTAMP(3),
CONSTRAINT "agent_pause_events_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "agents_tenant_id_idx" ON "agents"("tenant_id");
-- CreateIndex
CREATE UNIQUE INDEX "agents_tenant_id_user_id_key" ON "agents"("tenant_id", "user_id");
-- CreateIndex
CREATE INDEX "tiers_tenant_id_idx" ON "tiers"("tenant_id");
-- CreateIndex
CREATE UNIQUE INDEX "tiers_queue_id_agent_id_key" ON "tiers"("queue_id", "agent_id");
-- CreateIndex
CREATE INDEX "agent_sessions_tenant_id_agent_id_idx" ON "agent_sessions"("tenant_id", "agent_id");
-- CreateIndex
CREATE INDEX "agent_state_events_tenant_id_agent_id_occurred_at_idx" ON "agent_state_events"("tenant_id", "agent_id", "occurred_at");
-- CreateIndex
CREATE INDEX "pause_reasons_tenant_id_idx" ON "pause_reasons"("tenant_id");
-- CreateIndex
CREATE UNIQUE INDEX "pause_reasons_tenant_id_code_key" ON "pause_reasons"("tenant_id", "code");
-- CreateIndex
CREATE INDEX "agent_pause_events_tenant_id_agent_id_idx" ON "agent_pause_events"("tenant_id", "agent_id");
-- AddForeignKey
ALTER TABLE "agents" ADD CONSTRAINT "agents_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "tenants"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "agents" ADD CONSTRAINT "agents_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "agents" ADD CONSTRAINT "agents_extension_id_fkey" FOREIGN KEY ("extension_id") REFERENCES "extensions"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "tiers" ADD CONSTRAINT "tiers_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "tenants"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "tiers" ADD CONSTRAINT "tiers_queue_id_fkey" FOREIGN KEY ("queue_id") REFERENCES "queues"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "tiers" ADD CONSTRAINT "tiers_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "agents"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "agent_sessions" ADD CONSTRAINT "agent_sessions_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "tenants"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "agent_sessions" ADD CONSTRAINT "agent_sessions_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "agents"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "agent_state_events" ADD CONSTRAINT "agent_state_events_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "tenants"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "agent_state_events" ADD CONSTRAINT "agent_state_events_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "agents"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "pause_reasons" ADD CONSTRAINT "pause_reasons_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "tenants"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "agent_pause_events" ADD CONSTRAINT "agent_pause_events_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "tenants"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "agent_pause_events" ADD CONSTRAINT "agent_pause_events_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "agents"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "agent_pause_events" ADD CONSTRAINT "agent_pause_events_pause_reason_id_fkey" FOREIGN KEY ("pause_reason_id") REFERENCES "pause_reasons"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- Tabelas de negocio tenant-scoped: RLS obrigatorio em todas (ver docs/TENANT_ISOLATION.md).
ALTER TABLE "agents" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "agents" FORCE ROW LEVEL SECURITY;
CREATE POLICY "tenant_isolation" ON "agents"
USING (tenant_id = NULLIF(current_setting('app.current_tenant_id', true), '')::uuid);
ALTER TABLE "tiers" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "tiers" FORCE ROW LEVEL SECURITY;
CREATE POLICY "tenant_isolation" ON "tiers"
USING (tenant_id = NULLIF(current_setting('app.current_tenant_id', true), '')::uuid);
ALTER TABLE "agent_sessions" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "agent_sessions" FORCE ROW LEVEL SECURITY;
CREATE POLICY "tenant_isolation" ON "agent_sessions"
USING (tenant_id = NULLIF(current_setting('app.current_tenant_id', true), '')::uuid);
ALTER TABLE "agent_state_events" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "agent_state_events" FORCE ROW LEVEL SECURITY;
CREATE POLICY "tenant_isolation" ON "agent_state_events"
USING (tenant_id = NULLIF(current_setting('app.current_tenant_id', true), '')::uuid);
ALTER TABLE "pause_reasons" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "pause_reasons" FORCE ROW LEVEL SECURITY;
CREATE POLICY "tenant_isolation" ON "pause_reasons"
USING (tenant_id = NULLIF(current_setting('app.current_tenant_id', true), '')::uuid);
ALTER TABLE "agent_pause_events" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "agent_pause_events" FORCE ROW LEVEL SECURITY;
CREATE POLICY "tenant_isolation" ON "agent_pause_events"
USING (tenant_id = NULLIF(current_setting('app.current_tenant_id', true), '')::uuid);