Files
crosscode-platform/packages/contracts/src/bible.ts
T
joshuaandClaude Opus 5.5 514a6d8bc0 contracts 1.20.0: devotionals, Bible passages, messaging.get, WhatsApp opt-out, App export
The contracts for Church App 1.0.0 (docs/APP-1.0-PLAN.md §12). Additive.

- crosscode.devotional/1, the Hub's devotionals reply, and the devotionals CSV
  import (parseCsv, checkDevotionalCsv, template), shared by the Hub and the App
- Bible books (USFM, English and Afrikaans), seven translations with YouVersion
  ids and copyright lines, parseBibleReference, youVersionUrl
- messaging.get and the messaging.changed hint; whatsapp.optout and
  whatsapp_opted_out on the profile
- APP_MODULES, hub.DEVOTIONALS_PATH, agent.APP_DEMO_PROFILES, app in DEMO_PRODUCTS
- the app section of the church export, people[].roles
- Accounts refuses the two new requests until 1.9.0

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 09:54:25 +02:00

245 lines
16 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { z } from "zod";
/**
* Bible passages for the Church App's devotionals (contracts 1.20.0, docs/APP-1.0-PLAN.md §6,
* decisions 5 and 10): the books (USFM codes, as YouVersion uses them), the translations a
* devotional can quote, reading a reference someone typed ("John 3:16-18", "Johannes 3:16–18",
* "1 Kor 13:4-7"), and the "Read in YouVersion" link.
*
* Shared by the Hub and the App, so a CSV row the Hub accepts is one the App accepts.
*/
// ─── Translations ────────────────────────────────────────────────────────────
/**
* The translations a devotional can use. An id never changes once released; more can be added.
* `youVersionId` is the version's number on bible.com (`bible.com/bible/<id>/JHN.3.16.<abbr>`).
* `copyright` is shown under the verse wherever it's quoted. Check each publisher's terms for use
* in an app before Cross Code's devotionals go out (APP-1.0-PLAN, "Still open").
*/
export const BIBLE_TRANSLATIONS = [
{
id: "NLT", name: "New Living Translation", language: "en", youVersionId: 116,
copyright: "Scripture quotations are taken from the Holy Bible, New Living Translation, copyright © 1996, 2004, 2015 by Tyndale House Foundation. Used by permission of Tyndale House Publishers, Carol Stream, Illinois 60188. All rights reserved.",
},
{
id: "ESV", name: "English Standard Version", language: "en", youVersionId: 59,
copyright: "Scripture quotations are from the ESV® Bible (The Holy Bible, English Standard Version®), © 2001 by Crossway, a publishing ministry of Good News Publishers. Used by permission. All rights reserved.",
},
{
id: "NIV", name: "New International Version", language: "en", youVersionId: 111,
copyright: "Scripture quotations taken from The Holy Bible, New International Version® NIV®. Copyright © 1973, 1978, 1984, 2011 by Biblica, Inc.™ Used by permission. All rights reserved worldwide.",
},
{
id: "NKJV", name: "New King James Version", language: "en", youVersionId: 114,
copyright: "Scripture taken from the New King James Version®. Copyright © 1982 by Thomas Nelson. Used by permission. All rights reserved.",
},
{
id: "KJV", name: "King James Version", language: "en", youVersionId: 1,
copyright: "King James Version. Public domain.",
},
{
id: "AFR53", name: "Afrikaans 1933/1953-vertaling", language: "af", youVersionId: 5,
copyright: "Die Bybel in Afrikaans, 1933/1953-vertaling. © Bybelgenootskap van Suid-Afrika. Gebruik met toestemming.",
},
{
id: "AFR83", name: "Afrikaans 1983-vertaling", language: "af", youVersionId: 6,
copyright: "Die Bybel in Afrikaans, 1983-vertaling. © Bybelgenootskap van Suid-Afrika 1983. Gebruik met toestemming.",
},
] as const;
export type BibleTranslation = (typeof BIBLE_TRANSLATIONS)[number];
export const BIBLE_TRANSLATION_IDS = BIBLE_TRANSLATIONS.map((t) => t.id) as [BibleTranslation["id"], ...BibleTranslation["id"][]];
export const bibleTranslationIdSchema = z.enum(BIBLE_TRANSLATION_IDS);
export type BibleTranslationId = z.infer<typeof bibleTranslationIdSchema>;
export function bibleTranslation(id: BibleTranslationId): BibleTranslation {
return BIBLE_TRANSLATIONS.find((t) => t.id === id)!;
}
/** A passage longer than this gets a warning: quote the passage, not whole chapters (§6). */
export const PASSAGE_WARN_VERSES = 25;
// ─── Books ───────────────────────────────────────────────────────────────────
/**
* The 66 books in order: USFM code, English and Afrikaans names, chapters (English numbering) and
* the other names and abbreviations a reference may use. Matching ignores case, accents, dots and
* spaces, so "1 Kor.", "1kor" and "1 Korintiërs" are the same book.
*/
export const BIBLE_BOOKS = [
{ code: "GEN", en: "Genesis", af: "Genesis", chapters: 50, aliases: ["Gen", "Ge", "Gn"] },
{ code: "EXO", en: "Exodus", af: "Eksodus", chapters: 40, aliases: ["Exod", "Exo", "Ex", "Eks"] },
{ code: "LEV", en: "Leviticus", af: "Levitikus", chapters: 27, aliases: ["Lev", "Le", "Lv"] },
{ code: "NUM", en: "Numbers", af: "Numeri", chapters: 36, aliases: ["Num", "Nu", "Nm", "Nb"] },
{ code: "DEU", en: "Deuteronomy", af: "Deuteronomium", chapters: 34, aliases: ["Deut", "Deu", "Dt", "Dtn"] },
{ code: "JOS", en: "Joshua", af: "Josua", chapters: 24, aliases: ["Josh", "Jos", "Jsh"] },
{ code: "JDG", en: "Judges", af: "Rigters", chapters: 21, aliases: ["Judg", "Jdg", "Jdgs", "Rig"] },
{ code: "RUT", en: "Ruth", af: "Rut", chapters: 4, aliases: ["Rth", "Ru"] },
{ code: "1SA", en: "1 Samuel", af: "1 Samuel", chapters: 31, aliases: ["1 Sam", "1 Sa", "1Sm"] },
{ code: "2SA", en: "2 Samuel", af: "2 Samuel", chapters: 24, aliases: ["2 Sam", "2 Sa", "2Sm"] },
{ code: "1KI", en: "1 Kings", af: "1 Konings", chapters: 22, aliases: ["1 Kgs", "1 Ki", "1 Kon"] },
{ code: "2KI", en: "2 Kings", af: "2 Konings", chapters: 25, aliases: ["2 Kgs", "2 Ki", "2 Kon"] },
{ code: "1CH", en: "1 Chronicles", af: "1 Kronieke", chapters: 29, aliases: ["1 Chron", "1 Chr", "1 Ch", "1 Kron", "1 Kr"] },
{ code: "2CH", en: "2 Chronicles", af: "2 Kronieke", chapters: 36, aliases: ["2 Chron", "2 Chr", "2 Ch", "2 Kron", "2 Kr"] },
{ code: "EZR", en: "Ezra", af: "Esra", chapters: 10, aliases: ["Ezr", "Esr"] },
{ code: "NEH", en: "Nehemiah", af: "Nehemia", chapters: 13, aliases: ["Neh", "Ne"] },
{ code: "EST", en: "Esther", af: "Ester", chapters: 10, aliases: ["Esth", "Est", "Es"] },
{ code: "JOB", en: "Job", af: "Job", chapters: 42, aliases: ["Jb"] },
{ code: "PSA", en: "Psalms", af: "Psalms", chapters: 150, aliases: ["Psalm", "Ps", "Psa", "Pss", "Psalmen"] },
{ code: "PRO", en: "Proverbs", af: "Spreuke", chapters: 31, aliases: ["Prov", "Pro", "Prv", "Pr", "Spr"] },
{ code: "ECC", en: "Ecclesiastes", af: "Prediker", chapters: 12, aliases: ["Eccl", "Ecc", "Ec", "Qoh", "Pred", "Pr"] },
{ code: "SNG", en: "Song of Songs", af: "Hooglied", chapters: 8, aliases: ["Song of Solomon", "Song", "Sos", "Sng", "Hgl", "Hoogl"] },
{ code: "ISA", en: "Isaiah", af: "Jesaja", chapters: 66, aliases: ["Isa", "Is", "Jes"] },
{ code: "JER", en: "Jeremiah", af: "Jeremia", chapters: 52, aliases: ["Jer", "Je", "Jr"] },
{ code: "LAM", en: "Lamentations", af: "Klaagliedere", chapters: 5, aliases: ["Lam", "La", "Klaagl", "Kla"] },
{ code: "EZK", en: "Ezekiel", af: "Esegiël", chapters: 48, aliases: ["Ezek", "Eze", "Ezk", "Eseg", "Ese"] },
{ code: "DAN", en: "Daniel", af: "Daniël", chapters: 12, aliases: ["Dan", "Da", "Dn"] },
{ code: "HOS", en: "Hosea", af: "Hosea", chapters: 14, aliases: ["Hos", "Ho"] },
{ code: "JOL", en: "Joel", af: "Joël", chapters: 3, aliases: ["Joe", "Jl"] },
{ code: "AMO", en: "Amos", af: "Amos", chapters: 9, aliases: ["Am", "Amo"] },
{ code: "OBA", en: "Obadiah", af: "Obadja", chapters: 1, aliases: ["Obad", "Ob", "Oba"] },
{ code: "JON", en: "Jonah", af: "Jona", chapters: 4, aliases: ["Jnh", "Jon"] },
{ code: "MIC", en: "Micah", af: "Miga", chapters: 7, aliases: ["Mic", "Mi", "Mig"] },
{ code: "NAM", en: "Nahum", af: "Nahum", chapters: 3, aliases: ["Nah", "Na"] },
{ code: "HAB", en: "Habakkuk", af: "Habakuk", chapters: 3, aliases: ["Hab", "Hb"] },
{ code: "ZEP", en: "Zephaniah", af: "Sefanja", chapters: 3, aliases: ["Zeph", "Zep", "Zp", "Sef"] },
{ code: "HAG", en: "Haggai", af: "Haggai", chapters: 2, aliases: ["Hag", "Hg"] },
{ code: "ZEC", en: "Zechariah", af: "Sagaria", chapters: 14, aliases: ["Zech", "Zec", "Zc", "Sag"] },
{ code: "MAL", en: "Malachi", af: "Maleagi", chapters: 4, aliases: ["Mal", "Ml"] },
{ code: "MAT", en: "Matthew", af: "Matteus", chapters: 28, aliases: ["Matt", "Mat", "Mt", "Mattheus", "Matt."] },
{ code: "MRK", en: "Mark", af: "Markus", chapters: 16, aliases: ["Mrk", "Mar", "Mk", "Mr", "Mark."] },
{ code: "LUK", en: "Luke", af: "Lukas", chapters: 24, aliases: ["Luk", "Lk", "Lu"] },
{ code: "JHN", en: "John", af: "Johannes", chapters: 21, aliases: ["Jhn", "Jn", "Joh"] },
{ code: "ACT", en: "Acts", af: "Handelinge", chapters: 28, aliases: ["Act", "Ac", "Hand", "Hnd"] },
{ code: "ROM", en: "Romans", af: "Romeine", chapters: 16, aliases: ["Rom", "Ro", "Rm"] },
{ code: "1CO", en: "1 Corinthians", af: "1 Korintiërs", chapters: 16, aliases: ["1 Cor", "1 Co", "1 Kor", "1 Ko"] },
{ code: "2CO", en: "2 Corinthians", af: "2 Korintiërs", chapters: 13, aliases: ["2 Cor", "2 Co", "2 Kor", "2 Ko"] },
{ code: "GAL", en: "Galatians", af: "Galasiërs", chapters: 6, aliases: ["Gal", "Ga"] },
{ code: "EPH", en: "Ephesians", af: "Efesiërs", chapters: 6, aliases: ["Eph", "Ephes", "Ef"] },
{ code: "PHP", en: "Philippians", af: "Filippense", chapters: 4, aliases: ["Phil", "Php", "Pp", "Fil"] },
{ code: "COL", en: "Colossians", af: "Kolossense", chapters: 4, aliases: ["Col", "Co", "Kol"] },
{ code: "1TH", en: "1 Thessalonians", af: "1 Tessalonisense", chapters: 5, aliases: ["1 Thess", "1 Thes", "1 Th", "1 Tess", "1 Tes"] },
{ code: "2TH", en: "2 Thessalonians", af: "2 Tessalonisense", chapters: 3, aliases: ["2 Thess", "2 Thes", "2 Th", "2 Tess", "2 Tes"] },
{ code: "1TI", en: "1 Timothy", af: "1 Timoteus", chapters: 6, aliases: ["1 Tim", "1 Ti", "1 Tm"] },
{ code: "2TI", en: "2 Timothy", af: "2 Timoteus", chapters: 4, aliases: ["2 Tim", "2 Ti", "2 Tm"] },
{ code: "TIT", en: "Titus", af: "Titus", chapters: 3, aliases: ["Tit", "Ti"] },
{ code: "PHM", en: "Philemon", af: "Filemon", chapters: 1, aliases: ["Philem", "Phm", "Pm", "Filem", "Flm"] },
{ code: "HEB", en: "Hebrews", af: "Hebreërs", chapters: 13, aliases: ["Heb", "He"] },
{ code: "JAS", en: "James", af: "Jakobus", chapters: 5, aliases: ["Jas", "Jm", "Jak"] },
{ code: "1PE", en: "1 Peter", af: "1 Petrus", chapters: 5, aliases: ["1 Pet", "1 Pe", "1 Pt", "1 Petr"] },
{ code: "2PE", en: "2 Peter", af: "2 Petrus", chapters: 3, aliases: ["2 Pet", "2 Pe", "2 Pt", "2 Petr"] },
{ code: "1JN", en: "1 John", af: "1 Johannes", chapters: 5, aliases: ["1 Jn", "1 Jhn", "1 Joh"] },
{ code: "2JN", en: "2 John", af: "2 Johannes", chapters: 1, aliases: ["2 Jn", "2 Jhn", "2 Joh"] },
{ code: "3JN", en: "3 John", af: "3 Johannes", chapters: 1, aliases: ["3 Jn", "3 Jhn", "3 Joh"] },
{ code: "JUD", en: "Jude", af: "Judas", chapters: 1, aliases: ["Jud", "Jd"] },
{ code: "REV", en: "Revelation", af: "Openbaring", chapters: 22, aliases: ["Rev", "Re", "Rv", "Revelations", "Op", "Openb"] },
] as const;
export type BibleBook = (typeof BIBLE_BOOKS)[number];
export const BIBLE_BOOK_CODES = BIBLE_BOOKS.map((b) => b.code) as [BibleBook["code"], ...BibleBook["code"][]];
export const bibleBookCodeSchema = z.enum(BIBLE_BOOK_CODES);
export type BibleBookCode = z.infer<typeof bibleBookCodeSchema>;
export function bibleBook(code: BibleBookCode): BibleBook {
return BIBLE_BOOKS.find((b) => b.code === code)!;
}
/** "1 Korintiërs." → "1korintiers": lower case, no accents, dots or spaces. */
function bookKey(name: string): string {
return name.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase().replace(/[.\s]/g, "");
}
/**
* Name → book. An abbreviation two books share (`Pr`: Proverbs and Prediker, `Co`, `Ti`) goes to
* the first, so the full names always work and the common short forms mostly do.
*/
const BOOK_BY_KEY: ReadonlyMap<string, BibleBook> = (() => {
const map = new Map<string, BibleBook>();
for (const book of BIBLE_BOOKS) {
for (const name of [book.code, book.en, book.af, ...book.aliases]) {
const key = bookKey(name);
if (!map.has(key)) map.set(key, book);
}
}
return map;
})();
/** The book a name or abbreviation means, or null. */
export function findBibleBook(name: string): BibleBook | null {
return BOOK_BY_KEY.get(bookKey(name)) ?? null;
}
// ─── Passages ────────────────────────────────────────────────────────────────
/**
* Where a passage is: a book, a start verse and an end verse (the same chapter or a later one).
* A whole chapter isn't a passage here: devotionals quote the verses they're about (§6).
*/
export const passageLocationSchema = z.object({
book: bibleBookCodeSchema,
chapter: z.int().min(1).max(150),
verse: z.int().min(1).max(176),
endChapter: z.int().min(1).max(150),
endVerse: z.int().min(1).max(176),
}).refine((p) => p.endChapter > p.chapter || (p.endChapter === p.chapter && p.endVerse >= p.verse), "The passage ends before it starts")
.refine((p) => p.endChapter <= bibleBook(p.book).chapters, "The book has fewer chapters");
export type PassageLocation = z.infer<typeof passageLocationSchema>;
export type ParsedReference = { ok: true; passage: PassageLocation } | { ok: false; error: string };
/**
* Reads a reference as people type it: `John 3:16`, `John 3:16-18`, `Johannes 3:16–18`,
* `1 Kor. 13:4-7`, `Rom 8:38-9:1`, `Ps 23 1-3` (a space for the colon), `Jude 3` and `Jude 1:3`
* (one-chapter books). Dashes may be `-`, `–` or `—`; `.` or `,` are taken for the colon too.
*/
export function parseBibleReference(input: string): ParsedReference {
const text = input.trim().replace(/\s+/g, " ");
const match = /^((?:[1-3]\s?)?[^\d\s][^\d]*?)\s*(\d+)(?:(?:\s*[:.,]\s*|\s+)(\d+))?(?:\s*[-–—]\s*(\d+)(?:\s*[:.,]\s*(\d+))?)?$/.exec(text);
if (!match) return { ok: false, error: `"${input}" isn't a Bible reference (e.g. John 3:16-18)` };
const [, name = "", a = "", b, c, d] = match;
const book = findBibleBook(name);
if (!book) return { ok: false, error: `"${name.trim()}" isn't a book of the Bible` };
let chapter: number, verse: number, endChapter: number, endVerse: number;
if (b === undefined) {
// "Jude 3" / "Jude 3-5": a verse in a one-chapter book. "John 3" alone is a whole chapter.
if (book.chapters !== 1) return { ok: false, error: `Give the verses as well as the chapter (e.g. ${book.en} ${a}:1-12)` };
if (d !== undefined) return { ok: false, error: `"${input}" isn't a Bible reference (e.g. ${book.en} 1:3-5)` };
chapter = 1; verse = Number(a); endChapter = 1; endVerse = c === undefined ? verse : Number(c);
} else {
chapter = Number(a); verse = Number(b);
if (d !== undefined) { endChapter = Number(c); endVerse = Number(d); }
else { endChapter = chapter; endVerse = c === undefined ? verse : Number(c); }
}
if (chapter > book.chapters || endChapter > book.chapters) {
return { ok: false, error: `${book.en} has ${book.chapters} chapter${book.chapters === 1 ? "" : "s"}` };
}
const parsed = passageLocationSchema.safeParse({ book: book.code, chapter, verse, endChapter, endVerse });
if (!parsed.success) return { ok: false, error: parsed.error.issues[0]?.message ?? "Not a passage" };
return { ok: true, passage: parsed.data };
}
/** `John 3:16–18`, `Johannes 3:16`, `Romans 8:38–9:1`, in the translation's language. */
export function formatBibleReference(p: PassageLocation, language: "en" | "af" = "en"): string {
const book = bibleBook(p.book);
const start = `${language === "af" ? book.af : book.en} ${p.chapter}:${p.verse}`;
if (p.endChapter !== p.chapter) return `${start}–${p.endChapter}:${p.endVerse}`;
return p.endVerse === p.verse ? start : `${start}–${p.endVerse}`;
}
/** How many verses, when the passage stays in one chapter (null across chapters: verse counts per chapter aren't kept). */
export function passageVerseCount(p: PassageLocation): number | null {
return p.endChapter === p.chapter ? p.endVerse - p.verse + 1 : null;
}
/**
* "Read in YouVersion": `https://www.bible.com/bible/116/JHN.3.16-18.NLT`. YouVersion opens its
* app instead where it's installed. Across chapters it opens at the first verse's chapter.
*/
export function youVersionUrl(p: PassageLocation, translation: BibleTranslationId): string {
const t = bibleTranslation(translation);
const verses = p.endChapter === p.chapter && p.endVerse !== p.verse ? `${p.verse}-${p.endVerse}` : `${p.verse}`;
return `https://www.bible.com/bible/${t.youVersionId}/${p.book}.${p.chapter}.${verses}.${t.id}`;
}