project-bifrost-platform/scripts/seed.js

141 lines
4.5 KiB
JavaScript

#!/usr/bin/env node
import Database from 'better-sqlite3';
import bcrypt from 'bcryptjs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const db = new Database(join(__dirname, '..', 'bifrost.db'));
db.pragma('journal_mode = WAL');
db.pragma('foreign_keys = ON');
// Verify schema exists
try {
db.prepare('SELECT id FROM users LIMIT 1').get();
} catch {
console.error('Error: database tables not found. Run `pnpm db:migrate` first.');
process.exit(1);
}
// Wipe existing seed data (idempotent)
db.exec(`
DELETE FROM reactions;
DELETE FROM replies;
DELETE FROM contributions;
DELETE FROM attendance;
DELETE FROM sessions;
DELETE FROM invites;
DELETE FROM users;
`);
const ROUNDS = 10;
const users = [
{
email: 'mette@ssi.dk',
password: 'pilot123',
name: 'Mette Hansen',
organisation: 'Statens Serum Institut',
role: 'pilot',
},
{
email: 'lars@rigspolitiet.dk',
password: 'cab123',
name: 'Lars Thomsen',
organisation: 'Rigspolitiet',
role: 'cab',
},
{
email: 'jonathan@fenja.ai',
password: 'fenja123',
name: 'Jonathan',
organisation: 'Fenja AI',
role: 'fenja',
},
];
const insertUser = db.prepare(`
INSERT INTO users (email, password_hash, name, organisation, role, bio)
VALUES (?, ?, ?, ?, ?, ?)
`);
const userIds = {};
for (const u of users) {
const hash = bcrypt.hashSync(u.password, ROUNDS);
const result = insertUser.run(u.email, hash, u.name, u.organisation, u.role, '');
userIds[u.role] = Number(result.lastInsertRowid);
console.log(` created user: ${u.name} (${u.role}) — password: ${u.password}`);
}
// Sample contributions
const insertContrib = db.prepare(`
INSERT INTO contributions (user_id, type, body_md, created_at)
VALUES (?, ?, ?, ?)
`);
const now = new Date();
const minus20 = new Date(now.getTime() - 20 * 60 * 1000).toISOString();
const minus2h = new Date(now.getTime() - 2 * 60 * 60 * 1000).toISOString();
const minus1d = new Date(now.getTime() - 24 * 60 * 60 * 1000).toISOString();
const minus3d = new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000).toISOString();
const c1 = insertContrib.run(
userIds['pilot'],
'idea',
'What if the system could **auto-summarise meetings** and link the summary back to the relevant roadmap item? Would save a lot of manual work after each CAB session.',
minus1d
);
const c2 = insertContrib.run(
userIds['cab'],
'question',
'How will the system handle documents in **multiple languages**? Most of our operational docs are in Danish, but some legal references are in English or German.',
minus2h
);
const c3 = insertContrib.run(
userIds['pilot'],
'inspiration',
'Came across this framing of knowledge management in regulated industries — the idea of an "institutional memory" that stays even when staff rotate. Feels very aligned with what Fenja is building.',
minus3d
);
const c4 = insertContrib.run(
userIds['fenja'],
'idea',
'Consider department-level access scoping for v1.1 — some organisations may want certain documents visible only to specific teams, even within the pilot group.',
minus20
);
// Reactions
const insertReaction = db.prepare(`
INSERT INTO reactions (user_id, contribution_id) VALUES (?, ?)
`);
insertReaction.run(userIds['cab'], c1.lastInsertRowid);
insertReaction.run(userIds['fenja'], c1.lastInsertRowid);
insertReaction.run(userIds['pilot'], c3.lastInsertRowid);
// Fenja reply on c1
db.prepare(`
INSERT INTO replies (contribution_id, user_id, body_md)
VALUES (?, ?, ?)
`).run(
c1.lastInsertRowid,
userIds['fenja'],
'Great idea — meeting summaries are on our Q3 roadmap. The link back to roadmap items is something we hadn\'t explicitly planned; adding it to the backlog now.'
);
// Sample attendance
const insertAttendance = db.prepare(`
INSERT OR REPLACE INTO attendance (user_id, meeting_slug, status, updated_at)
VALUES (?, ?, ?, datetime('now'))
`);
insertAttendance.run(userIds['pilot'], '2026-03-20-kickoff', 'yes');
insertAttendance.run(userIds['cab'], '2026-03-20-kickoff', 'yes');
insertAttendance.run(userIds['fenja'], '2026-03-20-kickoff', 'yes');
insertAttendance.run(userIds['pilot'], '2026-04-25-cab-q2-session', 'yes');
insertAttendance.run(userIds['cab'], '2026-04-25-cab-q2-session', 'no');
console.log('\n Seed complete.');
console.log('\n Test credentials:');
console.log(' mette@ssi.dk / pilot123 (pilot)');
console.log(' lars@rigspolitiet.dk / cab123 (cab)');
console.log(' jonathan@fenja.ai / fenja123 (fenja)');
db.close();