import "./_env"; import postgres from "postgres"; import { hashPassword } from "../src/lib/auth/password"; /** Creates the owner account from OWNER_EMAIL / OWNER_INITIAL_PASSWORD if no user exists; idempotent. Also seeds default settings. */ const sql = postgres(process.env.DATABASE_URL!, { max: 1, onnotice: () => {} }); const email = process.env.OWNER_EMAIL, pw = process.env.OWNER_INITIAL_PASSWORD; const [{ n }] = await sql`SELECT count(*)::int AS n FROM users`; if (n === 0) { if (!email || !pw) { console.error("OWNER_EMAIL / OWNER_INITIAL_PASSWORD missing"); process.exit(1); } await sql`INSERT INTO users (email, display_name, password_hash, role, must_change_password) VALUES (${email.toLowerCase()}, ${"Owner"}, ${await hashPassword(pw)}, 'owner', true)`; await sql`INSERT INTO audit_log (actor_label, action, entity_type, entity_id, after) VALUES ('bootstrap', 'user.create', 'user', ${email.toLowerCase()}, ${sql.json({ role: "owner" })})`; console.log(`owner ${email} created (must change password at first login)`); } else console.log(`users present (${n}); nothing to do`); const defaults: Record = { "site.attribution": { en: "This observatory is an independent publication portal maintained by Simon-Pierre Boucher, professor at the Université du Québec en Outaouais (Département des sciences administratives). It is not an official UQO institutional repository. Working papers are circulated for discussion and comment; they have not necessarily undergone formal peer review.", fr: "Cet observatoire est un portail de publication indépendant maintenu par Simon-Pierre Boucher, professeur à l'Université du Québec en Outaouais (Département des sciences administratives). Ce n'est pas un dépôt institutionnel officiel de l'UQO. Les cahiers de recherche circulent aux fins de discussion et de commentaires ; ils n'ont pas nécessairement fait l'objet d'une évaluation par les pairs." }, "site.institution": { name_en: "Université du Québec en Outaouais", name_fr: "Université du Québec en Outaouais", department_en: "Département des sciences administratives", department_fr: "Département des sciences administratives", url: "https://uqo.ca", logo_authorized: false }, "site.contact": { email: "simon-pierre.boucher@uqo.ca", website: "https://www.spboucher.ai" }, "home.featured_limit": 4, "ingest.extra_dirs": [], }; for (const [k, v] of Object.entries(defaults)) await sql`INSERT INTO settings (key, value) VALUES (${k}, ${sql.json(v as never)}) ON CONFLICT (key) DO NOTHING`; await sql.end();