91 lines
2.7 KiB
JavaScript
91 lines
2.7 KiB
JavaScript
#!/usr/bin/env node
|
|
'use strict';
|
|
|
|
require('dotenv').config({ path: require('path').join(__dirname, '..', '.env') });
|
|
|
|
const readline = require('readline');
|
|
const bcrypt = require('bcryptjs');
|
|
|
|
const rl = rl_create();
|
|
|
|
function rl_create() {
|
|
return readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
}
|
|
|
|
function ask(question) {
|
|
return new Promise(resolve => rl.question(question, resolve));
|
|
}
|
|
|
|
async function main() {
|
|
console.log('\nVitFit V2 — First-Time Setup\n');
|
|
|
|
const { initDb, runMigrations, models } = require('../core/db');
|
|
|
|
await initDb();
|
|
await runMigrations();
|
|
|
|
// Refuse if any org exists
|
|
const orgCount = await models.Organization.count();
|
|
if (orgCount > 0) {
|
|
console.error('ERROR: An organization already exists. Setup can only run on a fresh install.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const orgName = await ask('Organization name (e.g. CrossFit Downtown): ');
|
|
const timezone = await ask('Timezone (e.g. America/New_York) [UTC]: ') || 'UTC';
|
|
const ownerUser = await ask('Owner username: ');
|
|
const ownerPass = await ask('Owner password (min 8 chars): ');
|
|
const ownerEmail = await ask('Owner email (optional): ');
|
|
const saUser = await ask('SystemAdmin username: ');
|
|
const saPass = await ask('SystemAdmin password (min 8 chars): ');
|
|
|
|
rl.close();
|
|
|
|
if (ownerPass.length < 8 || saPass.length < 8) {
|
|
console.error('ERROR: Passwords must be at least 8 characters.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const { sequelize } = require('../core/db');
|
|
const transaction = await sequelize.transaction();
|
|
|
|
try {
|
|
const org = await models.Organization.create({ name: orgName, timezone }, { transaction });
|
|
await models.Branding.create({ organizationId: org.id }, { transaction });
|
|
|
|
const ownerHash = await bcrypt.hash(ownerPass, 12);
|
|
await models.User.create({
|
|
organizationId: org.id,
|
|
username: ownerUser,
|
|
email: ownerEmail || null,
|
|
password: ownerHash,
|
|
role: 'Owner',
|
|
isActive: true,
|
|
}, { transaction });
|
|
|
|
const saHash = await bcrypt.hash(saPass, 12);
|
|
await models.User.create({
|
|
organizationId: null,
|
|
username: saUser,
|
|
password: saHash,
|
|
role: 'SystemAdmin',
|
|
isActive: true,
|
|
}, { transaction });
|
|
|
|
await transaction.commit();
|
|
|
|
console.log('\n✓ Setup complete.');
|
|
console.log(` Organization: ${orgName} (id: ${org.id})`);
|
|
console.log(` Owner: ${ownerUser}`);
|
|
console.log(` SystemAdmin: ${saUser}`);
|
|
console.log('\nStart the app with: npm start\n');
|
|
process.exit(0);
|
|
} catch (err) {
|
|
await transaction.rollback();
|
|
console.error('Setup failed:', err.message);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
main().catch(err => { console.error(err); process.exit(1); });
|