import { z } from "zod"; import { dateTimeSchema, emailSchema, hexColourSchema, idSchema, phoneSchema, slugSchema, timeOfDaySchema, timezoneSchema, urlSchema } from "./common"; import { schemaName } from "./schema-name"; import { brandStyleSchema } from "./style"; /** * `crosscode.church/1` — the church profile, set once and shared by every product of a church * (ROADMAP-v3 §2): name, logo, colours, campuses, service times and contact details. The Website, * App and Accounts' sign-in page are branded from it. * * Public information only: the church's own contact details, never a person's. */ export const CHURCH_SCHEMA = schemaName("church", 1); export const WEEKDAYS = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"] as const; export const SOCIAL_NETWORKS = ["facebook", "instagram", "youtube", "tiktok", "x", "spotify", "whatsapp-channel", "other"] as const; export const addressSchema = z.object({ line1: z.string().trim().min(1).max(200), line2: z.string().trim().max(200).nullable(), suburb: z.string().trim().max(100).nullable(), city: z.string().trim().min(1).max(100), province: z.string().trim().max(100).nullable(), postalCode: z.string().trim().max(20).nullable(), /** ISO 3166-1 alpha-2, e.g. `ZA`. */ country: z.string().regex(/^[A-Z]{2}$/, "Expected an ISO 3166-1 alpha-2 code"), /** For a map pin. */ geo: z.object({ lat: z.number().min(-90).max(90), lng: z.number().min(-180).max(180) }).nullable(), }); export type Address = z.infer; export const campusSchema = z.object({ slug: slugSchema, name: z.string().trim().min(1).max(200), address: addressSchema.nullable(), }); export const serviceTimeSchema = z.object({ day: z.enum(WEEKDAYS), /** Local time in the church's timezone. */ time: timeOfDaySchema, /** E.g. "Morning service", "Afrikaans service". */ label: z.string().trim().max(100).nullable(), /** The campus's slug, or `null` when the church has no campuses. */ campus: slugSchema.nullable(), /** E.g. "Kids church during the service". */ note: z.string().trim().max(300).nullable(), }); /** The profile's fields without the cross-field checks, for building forms that edit part of it (contracts 1.5.0). */ export const churchObjectSchema = z .object({ schema: z.literal(CHURCH_SCHEMA), /** The Hub customer id: the same for every product of the church. */ customerId: idSchema, updatedAt: dateTimeSchema, name: z.string().trim().min(1).max(200), /** For tight spaces (app icon label, sign-in page title). */ shortName: z.string().trim().max(30).nullable(), timezone: timezoneSchema, /** BCP 47, e.g. `en-ZA`. */ locale: z.string().regex(/^[a-z]{2,3}(-[A-Z]{2})?$/, "Expected a BCP 47 locale like en-ZA"), branding: z.object({ logoUrl: urlSchema.nullable(), /** Square, at least 512×512: app icon and favicon. */ iconUrl: urlSchema.nullable(), colours: z.object({ primary: hexColourSchema, accent: hexColourSchema.nullable(), }), /** * Font pairing and corners, shared by every product (contracts 1.17.0, `./style`). Optional: * a profile from an Accounts before 1.7.0 has none, which means the defaults. */ style: brandStyleSchema.optional(), }), /** Empty when campuses are off. */ campuses: z.array(campusSchema).max(50), serviceTimes: z.array(serviceTimeSchema).max(100), contact: z.object({ email: emailSchema.nullable(), phone: phoneSchema.nullable(), whatsapp: phoneSchema.nullable(), address: addressSchema.nullable(), }), social: z.array(z.object({ network: z.enum(SOCIAL_NETWORKS), url: urlSchema })).max(20), /** The church's products, so each can link to the others. */ links: z.object({ website: urlSchema.nullable(), events: urlSchema.nullable(), app: urlSchema.nullable(), accounts: urlSchema, }), }); export const churchSchema = churchObjectSchema.superRefine((church, ctx) => { const slugs = new Set(church.campuses.map((c) => c.slug)); if (slugs.size !== church.campuses.length) ctx.addIssue({ code: "custom", path: ["campuses"], message: "Campus slugs must be unique" }); church.serviceTimes.forEach((s, i) => { if (s.campus !== null && !slugs.has(s.campus)) ctx.addIssue({ code: "custom", path: ["serviceTimes", i, "campus"], message: `Unknown campus "${s.campus}"` }); }); }); export type Church = z.infer; export type Campus = z.infer; export type ServiceTime = z.infer; /** * Where the church profile lives (contracts 1.5.0): **Accounts owns it** (docs/ACCOUNTS-1.0-PLAN.md * §7, decision 1) and serves it publicly at `profile`; products read it from there. When it * changes, Accounts sends `church.changed` (signed with the link's secret, `LINK_HEADERS`) to * each linked product at `changed`, which then fetches the profile again. */ export const CHURCH_PATHS = { profile: "/api/church/v1", changed: "/api/church/v1/changed" } as const; /** The `church.changed` webhook: a hint to fetch `GET /api/church/v1` again. */ export const churchChangedSchema = z.object({ schema: z.literal(CHURCH_SCHEMA), type: z.literal("church.changed"), occurredAt: dateTimeSchema, updatedAt: dateTimeSchema, }); export type ChurchChanged = z.infer;