feat: harden bootstrap and add tests

This commit is contained in:
2026-06-28 16:43:03 -05:00
parent 01ea9e391d
commit 991e73fcff
39 changed files with 3399 additions and 459 deletions
+19 -8
View File
@@ -257,14 +257,25 @@ export async function seed() {
throw new Error('Seeding is disabled in production');
}
const adminPassword = process.env.BOOTSTRAP_ADMIN_PASSWORD;
const userPassword = process.env.BOOTSTRAP_USER_PASSWORD;
const wifiPassword = process.env.BOOTSTRAP_WIFI_PASSWORD;
const tagline = process.env.BOOTSTRAP_TAGLINE || 'Hotel · Restaurant · Andean Highlands';
const { rows: adminRows } = await db.query(`SELECT id FROM users WHERE username = 'admin'`);
if (adminRows.length === 0) {
const adminHash = bcrypt.hashSync('admin', 10);
const userHash = bcrypt.hashSync('user', 10);
if (adminRows.length === 0 && adminPassword) {
const adminHash = bcrypt.hashSync(adminPassword, 10);
const seedRows: Array<[string, string, string]> = [['admin', adminHash, 'admin']];
if (userPassword) {
seedRows.push(['user', bcrypt.hashSync(userPassword, 10), 'user']);
}
await db.query(
`INSERT INTO users (username, password_hash, role) VALUES ($1, $2, $3), ($4, $5, $6)`,
['admin', adminHash, 'admin', 'user', userHash, 'user']
`INSERT INTO users (username, password_hash, role) VALUES ${seedRows
.map((_, index) => `($${index * 3 + 1}, $${index * 3 + 2}, $${index * 3 + 3})`)
.join(', ')}`,
seedRows.flat()
);
}
@@ -295,13 +306,13 @@ export async function seed() {
}
const { rows: configWifiRows } = await db.query(`SELECT key FROM config WHERE key = 'wifi_password'`);
if (configWifiRows.length === 0) {
await db.query(`INSERT INTO config (key, value) VALUES ($1, $2)`, ['wifi_password', 'LaHuascaGuest2026']);
if (configWifiRows.length === 0 && wifiPassword) {
await db.query(`INSERT INTO config (key, value) VALUES ($1, $2)`, ['wifi_password', wifiPassword]);
}
const { rows: configTaglineRows } = await db.query(`SELECT key FROM config WHERE key = 'tagline'`);
if (configTaglineRows.length === 0) {
await db.query(`INSERT INTO config (key, value) VALUES ($1, $2)`, ['tagline', 'Hotel · Restaurant · Andean Highlands']);
await db.query(`INSERT INTO config (key, value) VALUES ($1, $2)`, ['tagline', tagline]);
}
}