/** * The Website's part of a church's export when it leaves (`crosscode export` / uninstall, * WEBSITE-1.0-PLAN ยง11): `crosscode.church-export/1` with its `website` part, in plain terms and * our own field names (no table names or internal ids; records refer to each other by refs made * for the export). The uploads are copied next to it by the crosscode script (`uploads/`). * * The content is the working copy, including changes not yet published, as the admins left it. */ import { churchExportSchema, type ChurchExport, type ExportWebsite, type WebsiteTemplate, WEBSITE_TEMPLATES } from '@crosscode/contracts'; import type { Ctx } from '../context.js'; import { appVersion } from '../lib/version.js'; const iso = (d: Date | null | undefined) => (d ? d.toISOString() : null); const uploadPath = (filename: string) => `uploads/${filename.replace(/[^A-Za-z0-9._/-]/g, '-')}`; const ROLE_NAMES: Record = { owner: 'website owner', admin: 'website admin', editor: 'website editor', ministry_leader: 'ministry leader', viewer: 'website viewer' }; export async function buildExport(ctx: Ctx): Promise { const db = ctx.db; const [design, pages, messages, events, versions, media, users] = await Promise.all([ db.siteDesign.findUnique({ where: { id: 1 } }), db.page.findMany({ where: { archivedAt: null }, orderBy: [{ position: 'asc' }, { title: 'asc' }], include: { sections: { orderBy: { position: 'asc' } } } }), db.message.findMany({ orderBy: { date: 'asc' }, include: { series: true } }), db.simpleEvent.findMany({ orderBy: { startsAt: 'asc' } }), db.siteVersion.findMany({ orderBy: { number: 'asc' } }), db.mediaAsset.findMany({ orderBy: { createdAt: 'asc' } }), db.websiteUser.findMany({ orderBy: { name: 'asc' } }), ]); const mediaPath = new Map(media.map((m) => [m.id, uploadPath(m.filename)])); const names = new Map(users.map((u) => [u.id, u.name])); const template: WebsiteTemplate = (WEBSITE_TEMPLATES as readonly string[]).includes(design?.template ?? '') ? (design!.template as WebsiteTemplate) : 'heritage'; const website: ExportWebsite = { template, firstPublishedAt: iso(versions[0]?.publishedAt), style: Object.fromEntries(Object.entries((design?.style ?? {}) as Record).filter(([, v]) => typeof v === 'string')) as Record, pages: pages.map((p, n) => ({ ref: `g${n + 1}`, title: p.title, path: p.kind === 'home' ? '/' : `/${p.slug}`, kind: p.kind, inNavigation: p.inNav, seo: { title: p.seoTitle, description: p.description }, sections: p.sections.map((s) => ({ type: s.type, variant: s.variant ?? '', hidden: s.hidden, content: (s.data ?? {}) as Record })), })), messages: messages.map((m, n) => ({ ref: `m${n + 1}`, title: m.title, date: iso(m.date), speaker: m.speaker, series: m.series?.title ?? null, passage: m.passage, notes: m.notes, url: m.mediaUrl, source: m.source === 'youtube_feed' ? 'youtube-feed' as const : 'manual' as const, hidden: m.hidden, })), events: events.map((e, n) => ({ ref: `w${n + 1}`, title: e.title, description: e.description, startsAt: iso(e.startsAt), endsAt: iso(e.endsAt), location: e.location, link: e.link, image: e.imageId ? mediaPath.get(e.imageId) ?? null : null, published: e.publishedAt !== null, })), versions: versions.map((v) => ({ number: v.number, publishedAt: iso(v.publishedAt), by: names.get(v.publishedBy) ?? null, note: v.note })), media: media.map((m) => ({ path: uploadPath(m.filename), alt: m.alt || null, caption: null })), }; return churchExportSchema.parse({ schema: 'crosscode.church-export/1', exportedAt: ctx.now().toISOString(), product: 'website', productVersion: appVersion(), church: { name: (await ctx.settings.get('church_name')) || 'Church Website', timezone: ctx.config.timezone }, people: users.map((u, n) => ({ ref: `p${n + 1}`, name: u.name, email: u.email, phone: null, role: ROLE_NAMES[u.role] ?? u.role, createdAt: iso(u.createdAt), consent: { email: null, whatsapp: null, privacyNoticeAcceptedAt: null }, })), events: [], registrations: [], payments: [], checkIns: [], rosters: [], settings: { address: ctx.config.publicUrl }, files: media.map((m) => ({ path: uploadPath(m.filename), bytes: m.bytes })), website, }); }