'use strict'; // For a new V2 database only. V1 accounts are imported separately by sync.py. const fs = require('fs'); const path = require('path'); const crypto = require('crypto'); const bcrypt = require('../../app/node_modules/bcryptjs'); const db = require('../../app/core/db'); async function main() { await db.initDb(); await db.runMigrations(); if (await db.models.Organization.count()) { console.log('V2 organization already exists; bootstrap skipped.'); await db.getSequelize().close(); return; } const credentials = { username: 'v2-system-admin', password: crypto.randomBytes(24).toString('base64url'), }; const credentialPath = path.resolve(__dirname, '../../system-admin-credentials.json'); // Never print generated credentials or overwrite credentials from a prior run. fs.writeFileSync(credentialPath, JSON.stringify(credentials, null, 2) + '\n', { flag: 'wx', mode: 0o600 }); await db.getSequelize().transaction(async transaction => { const org = await db.models.Organization.create({ name: 'Vitality Fitness', timezone: 'America/New_York', isActive: true, }, { transaction }); await db.models.Branding.create({ organizationId: org.id }, { transaction }); await db.models.User.create({ username: credentials.username, password: await bcrypt.hash(credentials.password, 12), role: 'SystemAdmin', organizationId: null, isActive: true, }, { transaction }); }); console.log('V2 initialized; SystemAdmin credentials saved to ' + credentialPath); await db.getSequelize().close(); } main().catch(error => { console.error(error.message); process.exit(1); });