-- 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);