/** * Loads a demo scenario into the App (APP-1.0-PLAN §12, src/demo/load.ts). * * npm run demo Acacia Hill (community-family), for development * npm run demo -- --profile [--force] one of the eight demo churches * npm run demo -- --profile --scenario --force as part of loading that scenario (the Hub, the agent) * npm run demo -- --list the scenarios * * It replaces the App's content and its demo members, so an App with real members (any address not * at example.com) needs --force. On a server, `crosscode demo app --profile …` runs it as * the App's user, after a backup. */ import 'dotenv/config'; import { PrismaPg } from '@prisma/adapter-pg'; import { agent, DEMO_EMAIL_DOMAIN } from '@crosscode/contracts'; import { PrismaClient } from '../src/generated/prisma/client.js'; import { createCtx } from '../src/context.js'; import { loadConfig } from '../src/lib/config.js'; import { loadDemo } from '../src/demo/load.js'; const arg = (name: string) => { const i = process.argv.indexOf(`--${name}`); return i > 0 ? process.argv[i + 1] : undefined; }; if (process.argv.includes('--list')) { for (const s of agent.DEMO_SCENARIOS) console.log(`${s.key.padEnd(18)} ${s.label}`); process.exit(0); } const profile = agent.appDemoProfileSchema.safeParse(arg('profile') ?? 'community-family'); if (!profile.success) { console.error(`Unknown profile "${arg('profile')}" (npm run demo -- --list).`); process.exit(2); } // A demo scenario loads the profile of the same name (contracts agent.DEMO_SCENARIOS). const scenarioArg = arg('scenario'); if (scenarioArg !== undefined && scenarioArg !== profile.data) { console.error(agent.demoScenarioSchema.safeParse(scenarioArg).success ? `The ${scenarioArg} scenario loads the ${scenarioArg} profile: --profile ${scenarioArg} --scenario ${scenarioArg}.` : `Unknown scenario "${scenarioArg}" (npm run demo -- --list).`); process.exit(2); } const db = new PrismaClient({ adapter: new PrismaPg({ connectionString: process.env.DATABASE_URL! }) }); const ctx = createCtx(loadConfig(), db); // Real members (the Hub's first Owner, say) are kept; what they made isn't. const real = (await db.member.findMany({ where: { NOT: { email: { endsWith: `@${DEMO_EMAIL_DOMAIN}` } } }, select: { id: true } })).map((m) => m.id); const where = { memberId: { in: real } }; const made = (await db.prayerCommitment.count({ where })) + (await db.prayerRequest.count({ where })) + (await db.testimony.count({ where })); if (made > 0 && !process.argv.includes('--force')) { console.error(`Real members have prayed, asked or shared in this App (${made} in all): loading the demo replaces the prayer wall, requests, announcements and events. Add --force to do it anyway.`); await db.$disconnect(); process.exit(1); } try { const r = await loadDemo(ctx, profile.data); console.log(`Demo ${r.scenario} loaded: ${r.church}, ${r.members} members, ${r.commitments} prayer commitments.`); const d = r.devotionals; if (d.source === 'church') console.log(`The church's own devotionals: ${d.saved}.`); else if (d.error) console.warn(`Cross Code's devotionals weren't fetched (${d.error}); the nightly fetch brings them.`); else console.log(`Cross Code's devotionals fetched from the Hub: ${d.saved} days.`); const e = r.events; if (e.from === 'app') console.log(`The App's own events: ${e.made}.`); else if (e.sync?.ok) console.log(`Showing ${e.from === 'events' ? 'Cross Events\'' : 'the Website\'s'} events: ${e.sync.imported} fetched.`); else console.warn(`${e.from === 'events' ? 'Cross Events\'' : 'The Website\'s'} events couldn't be fetched yet (${e.sync?.error}); the next sync, within 15 minutes, brings them.`); } finally { await db.$disconnect(); }