Accounts: deploy hooks, env template, export, docs
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>
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 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();
|
||||
@@ -19,6 +19,7 @@ import { LOGOUT_JS } from './oidc/pages.js';
|
||||
import { publicRouter } from './public/routes.js';
|
||||
import { setupRouter } from './setup/routes.js';
|
||||
import { health } from './hub/client.js';
|
||||
import { appVersion } from './lib/version.js';
|
||||
|
||||
export function createApp(ctx: Ctx, provider: Provider) {
|
||||
const app = express();
|
||||
@@ -35,7 +36,7 @@ export function createApp(ctx: Ctx, provider: Provider) {
|
||||
|
||||
app.get('/api/health', async (_req, res) => {
|
||||
const h = await health(ctx);
|
||||
res.status(h.db ? 200 : 503).json({ status: h.db ? 'ok' : 'error', product: 'accounts', version: process.env.npm_package_version || '1.0.0', db: h.db });
|
||||
res.status(h.db ? 200 : 503).json({ status: h.db ? 'ok' : 'error', product: 'accounts', version: appVersion(), db: h.db });
|
||||
});
|
||||
|
||||
app.get('/api/static/logout.js', (_req, res) => {
|
||||
|
||||
@@ -9,6 +9,7 @@ import { hub } from '@crosscode/contracts';
|
||||
import type { Ctx } from '../context.js';
|
||||
import { hubPublicKey, verifyToken } from './licence.js';
|
||||
import { keyPair, saveLinkedProducts } from '../links/store.js';
|
||||
import { appVersion } from '../lib/version.js';
|
||||
|
||||
export const PROTOCOL_VERSION = 2;
|
||||
const TIMEOUT_MS = 15000;
|
||||
@@ -41,9 +42,7 @@ async function post(ctx: Ctx, path: string, body: unknown, sign?: { installId: s
|
||||
return data;
|
||||
}
|
||||
|
||||
function version(): string {
|
||||
return process.env.npm_package_version || '1.0.0';
|
||||
}
|
||||
const version = appVersion;
|
||||
|
||||
export async function health(ctx: Ctx) {
|
||||
let db = true;
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* The running version, from the backend's package.json (src/lib → ../../package.json in development,
|
||||
* dist/lib → ../../package.json when built), so /api/health tells `crosscode deploy` the truth
|
||||
* however the process was started (PM2 doesn't set npm_package_version).
|
||||
*/
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
let cached: string | null = null;
|
||||
|
||||
export function appVersion(): string {
|
||||
if (!cached) {
|
||||
try {
|
||||
const file = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..', 'package.json');
|
||||
cached = (JSON.parse(fs.readFileSync(file, 'utf8')) as { version: string }).version;
|
||||
} catch {
|
||||
cached = '0.0.0';
|
||||
}
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
@@ -17,5 +17,5 @@
|
||||
"outDir": "dist",
|
||||
"types": ["node"]
|
||||
},
|
||||
"include": ["src", "tests", "prisma.config.ts"]
|
||||
"include": ["src", "tests", "scripts", "prisma.config.ts"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user