/** * Seed betiği: npm run seed | Sıfırlayıp doldurma: npm run seed:reset * * Faz 3: yalnızca ilk admin (SEED_ADMIN_EMAIL / SEED_ADMIN_PASSWORD, kullanıcı adı "admin"). * Faz 4: örnek kullanıcılar, hane ve aile üyeleri eklenecek. * * İdempotent: kullanıcı e-postasına göre kontrol eder, tekrar çalıştırınca kopya oluşturmaz. * Kullanıcılar Better Auth API'siyle oluşturulur (şifre doğru hashlenir; koleksiyona elle yazılmaz). * `server-only` modülleri Next dışında yükleyebilmek için --conditions=react-server ile çalışır. */ import { getAuth } from "@/lib/auth"; import { getDb, getMongoClient } from "@/lib/db/mongo"; import { env } from "@/lib/env"; const reset = process.argv.includes("--reset"); async function main() { const e = env(); if (reset && e.NODE_ENV === "production") { throw new Error("seed:reset production ortamında çalıştırılamaz."); } if (!e.SEED_ADMIN_EMAIL || !e.SEED_ADMIN_PASSWORD) { throw new Error("SEED_ADMIN_EMAIL ve SEED_ADMIN_PASSWORD tanımlı olmalı (.env.local)."); } const db = getDb(); if (reset) { for (const name of ["user", "account", "session", "verification", "rateLimit"]) { await db.collection(name).deleteMany({}); } console.log("• Kimlik doğrulama koleksiyonları temizlendi."); } const email = e.SEED_ADMIN_EMAIL.toLowerCase(); const existing = await db.collection("user").findOne({ email }); if (existing) { console.log(`• Admin zaten var: ${email}`); } else { await getAuth().api.createUser({ body: { email, password: e.SEED_ADMIN_PASSWORD, name: "Yönetici", role: "admin", data: { username: "admin" }, }, }); console.log(`✓ Admin oluşturuldu: ${email} (kullanıcı adı: admin)`); } if (e.NODE_ENV !== "production") { console.table([ { rol: "admin", kullaniciAdi: "admin", eposta: email, sifre: "SEED_ADMIN_PASSWORD (.env.local)", }, ]); } } main() .catch((err: unknown) => { console.error(err instanceof Error ? err.message : err); process.exitCode = 1; }) .finally(() => getMongoClient().close());