Files
crosscode-platform/packages/contracts/src/church-export.ts
T

160 lines
6.6 KiB
TypeScript

import { z } from "zod";
import { dateTimeSchema, productSchema, timezoneSchema, urlSchema } from "./common";
import { websiteTemplateSchema } from "./style";
/**
* crosscode.church-export/1: a church's data when it leaves (docs/AGENT-1.0-PLAN.md §9). Written
* for people, in plain terms with our own field names: no database tables, Prisma models or
* internal ids. Records point at each other with references made for the export (`p12`, `e3`,
* `r40`), which mean nothing outside the file.
*
* Shipped as a ZIP: `export.json` (this), `uploads/` (the files in `files`) and `README.txt`.
*/
const ref = (prefix: string) => z.string().regex(new RegExp(`^${prefix}[0-9]+$`));
const text = (max = 20000) => z.string().max(max);
const when = dateTimeSchema.nullable();
const cents = z.int();
const uploadPath = z.string().regex(/^uploads\/[A-Za-z0-9._/-]+$/);
export const exportPersonSchema = z.object({
ref: ref("p"),
name: text(300),
email: text(254).nullable(),
phone: text(40).nullable(),
role: text(40),
createdAt: when,
consent: z.object({ email: z.boolean().nullable(), whatsapp: z.boolean().nullable(), privacyNoticeAcceptedAt: when }),
});
export const exportEventSchema = z.object({
ref: ref("e"),
title: text(500),
description: text().nullable(),
startsAt: when,
endsAt: when,
location: text(500).nullable(),
sessions: z.array(z.object({ label: text(300).nullable(), startsAt: when, endsAt: when })),
ticketTypes: z.array(z.object({ ref: ref("t"), name: text(300), priceCents: cents, currency: z.string().length(3) })),
capacity: z.int().nullable(),
published: z.boolean(),
});
export const exportRegistrationSchema = z.object({
ref: ref("r"),
eventRef: ref("e"),
personRef: ref("p").nullable(),
status: text(40),
createdAt: when,
attendees: z.array(z.object({ name: text(300), email: text(254).nullable(), ticketTypeRef: ref("t").nullable(), checkedInAt: when })),
answers: z.array(z.object({ question: text(500), answer: text(5000) })).default([]),
});
export const exportPaymentSchema = z.object({
registrationRef: ref("r").nullable(),
amountCents: cents,
currency: z.string().length(3),
method: text(40),
status: text(40),
paidAt: when,
reference: text(200).nullable(),
});
export const exportCheckInSchema = z.object({ eventRef: ref("e"), personRef: ref("p").nullable(), name: text(300).nullable(), at: when });
export const exportRosterSchema = z.object({ eventRef: ref("e").nullable(), role: text(200), personRef: ref("p").nullable(), startsAt: when, endsAt: when });
// The Website's part (contracts 1.17.0, WEBSITE-1.0-PLAN §11): its content as the admins left it
// (the working copy, including changes not yet published), the publish history's notes, messages,
// simple events and the media library. Images are files in `uploads/`, listed in `files`.
/** A section of a page: its type and layout, and what the admin filled in (text, links, image paths). */
export const exportWebsiteSectionSchema = z.object({
/** E.g. `hero`, `service-times`, `ministries`, `text`, `gallery`. */
type: text(60),
/** E.g. `photo`, `cards`. */
variant: text(60),
hidden: z.boolean(),
content: z.record(z.string().max(100), z.json()),
});
export const exportWebsitePageSchema = z.object({
ref: ref("g"),
title: text(300),
/** The page's path on the site: `/` for home, `/about`, `/youth-camp`. */
path: z.string().regex(/^\/[a-z0-9/-]*$/).max(300),
/** `home`, `about`, `visit`, `contact`, `give`, … or `custom` for a page the church added. */
kind: text(40),
inNavigation: z.boolean(),
seo: z.object({ title: text(300).nullable(), description: text(500).nullable() }),
sections: z.array(exportWebsiteSectionSchema),
});
export const exportWebsiteMessageSchema = z.object({
ref: ref("m"),
title: text(500),
date: when,
speaker: text(300).nullable(),
series: text(300).nullable(),
passage: text(300).nullable(),
notes: text().nullable(),
/** The YouTube or Spotify link (audio and video are never uploaded). */
url: urlSchema,
/** `youtube-feed` when it came in from the channel or playlist by itself. */
source: z.enum(["youtube-feed", "manual"]),
hidden: z.boolean(),
});
/** A simple event made in the Website (Cross Events' own events are in its export's `events`). */
export const exportWebsiteEventSchema = z.object({
ref: ref("w"),
title: text(500),
description: text().nullable(),
startsAt: when,
endsAt: when,
location: text(500).nullable(),
link: urlSchema.nullable(),
image: uploadPath.nullable(),
published: z.boolean(),
});
export const exportWebsiteSchema = z.object({
template: websiteTemplateSchema,
/** Null when the site was never published (it showed Coming soon). */
firstPublishedAt: when,
/** The Website-only style settings (section colour, spacing, buttons, image treatment, header). */
style: z.record(z.string().max(100), z.string().max(200)),
pages: z.array(exportWebsitePageSchema),
messages: z.array(exportWebsiteMessageSchema),
events: z.array(exportWebsiteEventSchema),
/** Every publish, oldest first. The snapshots aren't exported: `pages` is the content. */
versions: z.array(z.object({ number: z.int().positive(), publishedAt: when, by: text(300).nullable(), note: text(1000).nullable() })),
/** The media library: alt text and captions of the files in `uploads/`. */
media: z.array(z.object({ path: uploadPath, alt: text(500).nullable(), caption: text(1000).nullable() })),
});
export type ExportWebsite = z.infer<typeof exportWebsiteSchema>;
export const churchExportSchema = z.object({
schema: z.literal("crosscode.church-export/1"),
exportedAt: dateTimeSchema,
product: productSchema,
productVersion: z.string().max(40),
church: z.object({ name: text(300), timezone: timezoneSchema.nullable() }),
people: z.array(exportPersonSchema),
events: z.array(exportEventSchema),
registrations: z.array(exportRegistrationSchema),
payments: z.array(exportPaymentSchema),
checkIns: z.array(exportCheckInSchema),
rosters: z.array(exportRosterSchema),
/** The site's own settings that matter to a church (name, contact, branding), never secrets. */
settings: z.record(z.string().max(100), z.string().max(5000)),
/** Uploaded files in the ZIP's `uploads/` folder. */
files: z.array(z.object({ path: uploadPath, bytes: z.int().nonnegative() })),
/** Only in the Website's export (contracts 1.17.0). Its editors are in `people`, with their roles. */
website: exportWebsiteSchema.optional(),
});
export type ChurchExport = z.infer<typeof churchExportSchema>;
/** Keys that look like internal ids anywhere in an export (the export test fails on any). */
export const INTERNAL_ID_KEYS = /^(id|.*Id|uuid|_id)$/;