feat(seed): örnek aile kullanıcıları, haneler ve roller (idempotent)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-05 02:14:10 +03:00
parent 38e2a7a26d
commit 2b648975ae
7 changed files with 369 additions and 53 deletions
+18 -52
View File
@@ -1,62 +1,25 @@
/**
* Seed betiği: npm run seed | Sıfırlayıp doldurma: npm run seed:reset
* Seed: npm run seed | Sıfırlayıp doldurma: npm run seed:reset (production'da reddedilir)
*
* 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).
* Oluşturulanlar: admin (SEED_ADMIN_*), 7 örnek kullanıcı (biri askıda, biri ikinci admin),
* 2 hane ve hane içi rolleri. Ayrıntı: scripts/seed/fixtures.ts
* `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 { getMongoClient } from "@/lib/db/mongo";
import { env } from "@/lib/env";
const reset = process.argv.includes("--reset");
import { closeSeedConnections, runSeed } from "./seed/run";
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 summary = await runSeed({ reset: process.argv.includes("--reset") });
const { users, households, memberships } = summary.created;
console.log(`\nOluşturulan: ${users} kullanıcı, ${households} hane, ${memberships} üyelik.`);
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)",
},
]);
// Hesap tablosu yalnızca geliştirme ortamında gösterilir.
if (env().NODE_ENV !== "production" && summary.accounts.length) {
console.log(
"\nGeliştirme hesapları (şifreler: admin → SEED_ADMIN_PASSWORD, diğerleri → SEED_USER_PASSWORD):",
);
console.table(summary.accounts);
}
}
@@ -65,4 +28,7 @@ main()
console.error(err instanceof Error ? err.message : err);
process.exitCode = 1;
})
.finally(() => getMongoClient().close());
.finally(async () => {
await closeSeedConnections();
await getMongoClient().close();
});