scripts/products/accounts.sh (backup with pg_dump, build with the shared packages, PM2, export, keep, failed-migration recovery); accounts in SUPPORTED_PRODUCTS so deploy updates it where a church has it. Env template in both places, PM2 example, export script, /api/health version from package.json. README, CHANGELOG, WHATS-NEW; the plan's changes while building and what's still to do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
43 lines
2.0 KiB
TypeScript
43 lines
2.0 KiB
TypeScript
/**
|
|
* Accounts' part of a church's export when it leaves (`crosscode export` / uninstall, AGENT-1.0-PLAN
|
|
* §9): `accounts.json` in --out-dir, with the church profile and the people in plain terms (the
|
|
* same person fields as `crosscode.church-export/1`; no passwords, 2FA secrets or sign-in logs).
|
|
*
|
|
* npx tsx scripts/export-church.ts --out-dir <dir>
|
|
*/
|
|
import 'dotenv/config';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { PrismaPg } from '@prisma/adapter-pg';
|
|
import { PrismaClient } from '../src/generated/prisma/client.js';
|
|
import { createCtx } from '../src/context.js';
|
|
import { loadConfig } from '../src/lib/config.js';
|
|
import { church } from '../src/church/profile.js';
|
|
|
|
const i = process.argv.indexOf('--out-dir');
|
|
const out = i > 0 ? process.argv[i + 1] : null;
|
|
if (!out) {
|
|
console.error('Usage: export-church.ts --out-dir <dir>');
|
|
process.exit(2);
|
|
}
|
|
|
|
const db = new PrismaClient({ adapter: new PrismaPg({ connectionString: process.env.DATABASE_URL! }) });
|
|
const ctx = createCtx(loadConfig(), db);
|
|
const people = await db.account.findMany({ where: { status: { not: 'deleted' } }, orderBy: { name: 'asc' } });
|
|
const acceptances = await db.noticeAcceptance.findMany({ orderBy: { acceptedAt: 'asc' } });
|
|
const accepted = new Map(acceptances.map((a) => [a.accountId, a.acceptedAt]));
|
|
const data = {
|
|
product: 'accounts',
|
|
generatedAt: new Date().toISOString(),
|
|
church: await church(ctx),
|
|
people: people.map((p, n) => ({
|
|
ref: `p${n + 1}`, name: p.name, email: p.email, phone: p.phone, role: p.role === 'admin' ? 'accounts admin' : 'member',
|
|
createdAt: p.createdAt.toISOString(),
|
|
consent: { email: p.newsEmail, whatsapp: p.newsWhatsApp, privacyNoticeAcceptedAt: accepted.get(p.id)?.toISOString() ?? null },
|
|
})),
|
|
};
|
|
fs.mkdirSync(out, { recursive: true });
|
|
fs.writeFileSync(path.join(out, 'accounts.json'), JSON.stringify(data, null, 2), { mode: 0o600 });
|
|
console.log(` accounts.json: ${data.people.length} people`);
|
|
await db.$disconnect();
|