- Acceso del alumno o tutor con link de un solo uso (hash SHA-256, consumo atómico, el GET no consume), sesión propia de 90 días deslizante, passkeys opcionales (SimpleWebAuthn) y mail de recuperación con rate limit. - Lado alumno (/i/:token, /acceso, /alumno/*): inicio, clases, avisar falta, pagos, informar pago con comprobante (S3 privado, magic bytes) y perfil. - Lado profe: acceso a la app en la ficha del alumno (invitar/reinvitar), "Pagos para verificar" en Cobros con badge, faltas avisadas en la ficha de grupo y datos para transferir en Ajustes. - "Requiere atención" y el recordatorio de WhatsApp excluyen cuotas informadas. - Zona horaria por defecto: Buenos Aires. - Migración add_student_access, tests contra Postgres real (test:db) y docs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
249 lines
9.4 KiB
SQL
249 lines
9.4 KiB
SQL
-- CreateEnum
|
|
CREATE TYPE "StudentTokenKind" AS ENUM ('INVITATION', 'RECOVERY', 'EMAIL_CONFIRMATION');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "WebauthnChallengePurpose" AS ENUM ('REGISTER', 'LOGIN');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "AbsenceReason" AS ENUM ('HEALTH', 'TRAVEL', 'WORK_STUDY', 'OTHER');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "ReportedPaymentStatus" AS ENUM ('REPORTED', 'CONFIRMED', 'REJECTED');
|
|
|
|
-- AlterTable
|
|
ALTER TABLE "users" ADD COLUMN "transfer_alias" TEXT,
|
|
ADD COLUMN "transfer_holder" TEXT;
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "student_accounts" (
|
|
"id" TEXT NOT NULL,
|
|
"name" TEXT NOT NULL,
|
|
"recovery_email" TEXT,
|
|
"recovery_email_verified_at" TIMESTAMP(3),
|
|
"pending_recovery_email" TEXT,
|
|
"welcomed_at" TIMESTAMP(3),
|
|
"created_by_id" TEXT NOT NULL,
|
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"updated_at" TIMESTAMP(3) NOT NULL,
|
|
|
|
CONSTRAINT "student_accounts_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "student_account_attendees" (
|
|
"account_id" TEXT NOT NULL,
|
|
"attendee_id" TEXT NOT NULL,
|
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "student_account_attendees_pkey" PRIMARY KEY ("account_id","attendee_id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "student_access_tokens" (
|
|
"id" TEXT NOT NULL,
|
|
"token_hash" TEXT NOT NULL,
|
|
"account_id" TEXT NOT NULL,
|
|
"kind" "StudentTokenKind" NOT NULL,
|
|
"email" TEXT,
|
|
"expires_at" TIMESTAMP(3) NOT NULL,
|
|
"used_at" TIMESTAMP(3),
|
|
"revoked_at" TIMESTAMP(3),
|
|
"created_by_id" TEXT,
|
|
"used_ip" TEXT,
|
|
"used_user_agent" TEXT,
|
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "student_access_tokens_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "student_sessions" (
|
|
"id" TEXT NOT NULL,
|
|
"account_id" TEXT NOT NULL,
|
|
"token_hash" TEXT NOT NULL,
|
|
"device" TEXT NOT NULL,
|
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"last_used_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"expires_at" TIMESTAMP(3) NOT NULL,
|
|
"revoked_at" TIMESTAMP(3),
|
|
|
|
CONSTRAINT "student_sessions_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "student_passkeys" (
|
|
"id" TEXT NOT NULL,
|
|
"account_id" TEXT NOT NULL,
|
|
"credential_id" TEXT NOT NULL,
|
|
"public_key" BYTEA NOT NULL,
|
|
"counter" INTEGER NOT NULL DEFAULT 0,
|
|
"transports" TEXT[],
|
|
"device" TEXT NOT NULL,
|
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"last_used_at" TIMESTAMP(3),
|
|
|
|
CONSTRAINT "student_passkeys_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "student_webauthn_challenges" (
|
|
"id" TEXT NOT NULL,
|
|
"challenge" TEXT NOT NULL,
|
|
"account_id" TEXT,
|
|
"purpose" "WebauthnChallengePurpose" NOT NULL,
|
|
"expires_at" TIMESTAMP(3) NOT NULL,
|
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "student_webauthn_challenges_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "rate_limit_events" (
|
|
"id" TEXT NOT NULL,
|
|
"key" TEXT NOT NULL,
|
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "rate_limit_events_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "absence_notices" (
|
|
"id" TEXT NOT NULL,
|
|
"attendee_id" TEXT NOT NULL,
|
|
"class_session_id" TEXT NOT NULL,
|
|
"account_id" TEXT,
|
|
"reason" "AbsenceReason",
|
|
"message" TEXT,
|
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"cancelled_at" TIMESTAMP(3),
|
|
|
|
CONSTRAINT "absence_notices_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "reported_payments" (
|
|
"id" TEXT NOT NULL,
|
|
"attendee_id" TEXT NOT NULL,
|
|
"group_id" TEXT NOT NULL,
|
|
"account_id" TEXT,
|
|
"total_amount" DECIMAL(10,2) NOT NULL,
|
|
"currency" TEXT NOT NULL,
|
|
"receipt_key" TEXT,
|
|
"receipt_mime" TEXT NOT NULL,
|
|
"receipt_name" TEXT NOT NULL,
|
|
"receipt_size" INTEGER NOT NULL,
|
|
"status" "ReportedPaymentStatus" NOT NULL DEFAULT 'REPORTED',
|
|
"reviewed_by_id" TEXT,
|
|
"reviewed_at" TIMESTAMP(3),
|
|
"rejection_reason" TEXT,
|
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"updated_at" TIMESTAMP(3) NOT NULL,
|
|
|
|
CONSTRAINT "reported_payments_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "reported_payment_items" (
|
|
"reported_payment_id" TEXT NOT NULL,
|
|
"payment_id" TEXT NOT NULL,
|
|
|
|
CONSTRAINT "reported_payment_items_pkey" PRIMARY KEY ("reported_payment_id","payment_id")
|
|
);
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "student_accounts_recovery_email_idx" ON "student_accounts"("recovery_email");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "student_accounts_created_by_id_idx" ON "student_accounts"("created_by_id");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "student_account_attendees_attendee_id_idx" ON "student_account_attendees"("attendee_id");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "student_access_tokens_token_hash_key" ON "student_access_tokens"("token_hash");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "student_access_tokens_account_id_kind_idx" ON "student_access_tokens"("account_id", "kind");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "student_sessions_token_hash_key" ON "student_sessions"("token_hash");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "student_sessions_account_id_idx" ON "student_sessions"("account_id");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "student_passkeys_credential_id_key" ON "student_passkeys"("credential_id");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "student_passkeys_account_id_idx" ON "student_passkeys"("account_id");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "student_webauthn_challenges_expires_at_idx" ON "student_webauthn_challenges"("expires_at");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "rate_limit_events_key_created_at_idx" ON "rate_limit_events"("key", "created_at");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "absence_notices_attendee_id_class_session_id_idx" ON "absence_notices"("attendee_id", "class_session_id");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "absence_notices_class_session_id_idx" ON "absence_notices"("class_session_id");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "reported_payments_group_id_status_idx" ON "reported_payments"("group_id", "status");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "reported_payments_attendee_id_idx" ON "reported_payments"("attendee_id");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "reported_payment_items_payment_id_idx" ON "reported_payment_items"("payment_id");
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "student_accounts" ADD CONSTRAINT "student_accounts_created_by_id_fkey" FOREIGN KEY ("created_by_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "student_account_attendees" ADD CONSTRAINT "student_account_attendees_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "student_accounts"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "student_account_attendees" ADD CONSTRAINT "student_account_attendees_attendee_id_fkey" FOREIGN KEY ("attendee_id") REFERENCES "attendees"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "student_access_tokens" ADD CONSTRAINT "student_access_tokens_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "student_accounts"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "student_sessions" ADD CONSTRAINT "student_sessions_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "student_accounts"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "student_passkeys" ADD CONSTRAINT "student_passkeys_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "student_accounts"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "student_webauthn_challenges" ADD CONSTRAINT "student_webauthn_challenges_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "student_accounts"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "absence_notices" ADD CONSTRAINT "absence_notices_attendee_id_fkey" FOREIGN KEY ("attendee_id") REFERENCES "attendees"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "absence_notices" ADD CONSTRAINT "absence_notices_class_session_id_fkey" FOREIGN KEY ("class_session_id") REFERENCES "class_sessions"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "absence_notices" ADD CONSTRAINT "absence_notices_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "student_accounts"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "reported_payments" ADD CONSTRAINT "reported_payments_attendee_id_fkey" FOREIGN KEY ("attendee_id") REFERENCES "attendees"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "reported_payments" ADD CONSTRAINT "reported_payments_group_id_fkey" FOREIGN KEY ("group_id") REFERENCES "groups"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "reported_payments" ADD CONSTRAINT "reported_payments_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "student_accounts"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "reported_payment_items" ADD CONSTRAINT "reported_payment_items_reported_payment_id_fkey" FOREIGN KEY ("reported_payment_id") REFERENCES "reported_payments"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "reported_payment_items" ADD CONSTRAINT "reported_payment_items_payment_id_fkey" FOREIGN KEY ("payment_id") REFERENCES "payments"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- At most one active (not cancelled) absence notice per attendee and class.
|
|
-- Partial index: Prisma can't express it in the schema.
|
|
CREATE UNIQUE INDEX "absence_notices_active_attendee_class_key" ON "absence_notices"("attendee_id", "class_session_id") WHERE "cancelled_at" IS NULL;
|