-- CreateEnum CREATE TYPE "UserRole" AS ENUM ('USER', 'ADMIN'); -- CreateEnum CREATE TYPE "AuditAction" AS ENUM ('LOGIN_SUCCESS', 'LOGIN_FAILED', 'LOGOUT', 'SIGNUP', 'PASSWORD_RESET', 'PASSWORD_CHANGE', 'ACCOUNT_LOCKED', 'ACCOUNT_UNLOCKED', 'PROFILE_UPDATE', 'MFA_ENABLED', 'MFA_DISABLED', 'API_ACCESS', 'SYSTEM_MAINTENANCE'); -- CreateEnum CREATE TYPE "SmsType" AS ENUM ('DOMESTIC', 'INTERNATIONAL'); -- CreateTable CREATE TABLE "users" ( "id" TEXT NOT NULL, "email" TEXT NOT NULL, "password_hash" TEXT, "role" "UserRole" NOT NULL DEFAULT 'USER', "mfa_secret" TEXT, "email_verified" BOOLEAN NOT NULL DEFAULT false, "failed_login_attempts" INTEGER NOT NULL DEFAULT 0, "locked_until" TIMESTAMP(3), "last_login_at" TIMESTAMP(3), "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP(3) NOT NULL, CONSTRAINT "users_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "id_mappings" ( "user_id" TEXT NOT NULL, "whmcs_client_id" INTEGER NOT NULL, "sf_account_id" TEXT, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP(3) NOT NULL, CONSTRAINT "id_mappings_pkey" PRIMARY KEY ("user_id") ); -- CreateTable CREATE TABLE "audit_logs" ( "id" TEXT NOT NULL, "user_id" TEXT, "action" "AuditAction" NOT NULL, "resource" TEXT, "details" JSONB, "ip_address" TEXT, "user_agent" TEXT, "success" BOOLEAN NOT NULL DEFAULT true, "error" TEXT, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "audit_logs_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "sim_usage_daily" ( "id" SERIAL NOT NULL, "account" TEXT NOT NULL, "date" DATE NOT NULL, "usageMb" DOUBLE PRECISION NOT NULL, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP(3) NOT NULL, CONSTRAINT "sim_usage_daily_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "sim_voice_options" ( "account" TEXT NOT NULL, "voice_mail_enabled" BOOLEAN NOT NULL DEFAULT false, "call_waiting_enabled" BOOLEAN NOT NULL DEFAULT false, "international_roaming_enabled" BOOLEAN NOT NULL DEFAULT false, "network_type" TEXT NOT NULL DEFAULT '4G', "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP(3) NOT NULL, CONSTRAINT "sim_voice_options_pkey" PRIMARY KEY ("account") ); -- CreateTable CREATE TABLE "sim_call_history_domestic" ( "id" TEXT NOT NULL, "account" TEXT NOT NULL, "call_date" DATE NOT NULL, "call_time" TEXT NOT NULL, "called_to" TEXT NOT NULL, "location" TEXT, "duration_sec" INTEGER NOT NULL, "charge_yen" INTEGER NOT NULL, "month" TEXT NOT NULL, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "sim_call_history_domestic_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "sim_call_history_international" ( "id" TEXT NOT NULL, "account" TEXT NOT NULL, "call_date" DATE NOT NULL, "start_time" TEXT NOT NULL, "stop_time" TEXT, "country" TEXT, "called_to" TEXT NOT NULL, "duration_sec" INTEGER NOT NULL, "charge_yen" INTEGER NOT NULL, "month" TEXT NOT NULL, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "sim_call_history_international_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "sim_sms_history" ( "id" TEXT NOT NULL, "account" TEXT NOT NULL, "sms_date" DATE NOT NULL, "sms_time" TEXT NOT NULL, "sent_to" TEXT NOT NULL, "sms_type" "SmsType" NOT NULL DEFAULT 'DOMESTIC', "month" TEXT NOT NULL, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "sim_sms_history_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "sim_history_imports" ( "id" TEXT NOT NULL, "month" TEXT NOT NULL, "talk_file" TEXT, "sms_file" TEXT, "talk_records" INTEGER NOT NULL DEFAULT 0, "sms_records" INTEGER NOT NULL DEFAULT 0, "imported_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "status" TEXT NOT NULL DEFAULT 'completed', CONSTRAINT "sim_history_imports_pkey" PRIMARY KEY ("id") ); -- CreateIndex CREATE UNIQUE INDEX "users_email_key" ON "users"("email"); -- CreateIndex CREATE UNIQUE INDEX "id_mappings_whmcs_client_id_key" ON "id_mappings"("whmcs_client_id"); -- CreateIndex CREATE INDEX "audit_logs_user_id_action_idx" ON "audit_logs"("user_id", "action"); -- CreateIndex CREATE INDEX "audit_logs_action_created_at_idx" ON "audit_logs"("action", "created_at"); -- CreateIndex CREATE INDEX "audit_logs_created_at_idx" ON "audit_logs"("created_at"); -- CreateIndex CREATE INDEX "sim_usage_daily_account_date_idx" ON "sim_usage_daily"("account", "date"); -- CreateIndex CREATE UNIQUE INDEX "sim_usage_daily_account_date_key" ON "sim_usage_daily"("account", "date"); -- CreateIndex CREATE INDEX "sim_call_history_domestic_account_month_idx" ON "sim_call_history_domestic"("account", "month"); -- CreateIndex CREATE INDEX "sim_call_history_domestic_account_call_date_idx" ON "sim_call_history_domestic"("account", "call_date"); -- CreateIndex CREATE UNIQUE INDEX "sim_call_history_domestic_account_call_date_call_time_calle_key" ON "sim_call_history_domestic"("account", "call_date", "call_time", "called_to"); -- CreateIndex CREATE INDEX "sim_call_history_international_account_month_idx" ON "sim_call_history_international"("account", "month"); -- CreateIndex CREATE INDEX "sim_call_history_international_account_call_date_idx" ON "sim_call_history_international"("account", "call_date"); -- CreateIndex CREATE UNIQUE INDEX "sim_call_history_international_account_call_date_start_time_key" ON "sim_call_history_international"("account", "call_date", "start_time", "called_to"); -- CreateIndex CREATE INDEX "sim_sms_history_account_month_idx" ON "sim_sms_history"("account", "month"); -- CreateIndex CREATE INDEX "sim_sms_history_account_sms_date_idx" ON "sim_sms_history"("account", "sms_date"); -- CreateIndex CREATE UNIQUE INDEX "sim_sms_history_account_sms_date_sms_time_sent_to_key" ON "sim_sms_history"("account", "sms_date", "sms_time", "sent_to"); -- CreateIndex CREATE UNIQUE INDEX "sim_history_imports_month_key" ON "sim_history_imports"("month"); -- AddForeignKey ALTER TABLE "id_mappings" ADD CONSTRAINT "id_mappings_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "audit_logs" ADD CONSTRAINT "audit_logs_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;