app: the Church App's foundation; docs for Hub 2.26.1 and two years of devotionals
apps/app (not released; app@1.0.0 is built in slices, APP-1.0-PLAN §15): - backend (Express, Prisma, pg-boss, as the Website): the Hub client (CCP-, product app, the active-member band in the heartbeat), links and church.changed, sign-in through Accounts (everyone a Member; 8 days renewed up to 90; every role only with 2FA; the first Owner from HUB_ADMIN_EMAIL), Accounts' user.updated/export/deleted, active members once a day, modules on and off, GET /api/v1/app. Migration init. - frontend (Next.js 16): header, tab bar, footer, manifest and icons from the church profile, service worker, Add to home screen, Me, the admin's first dashboard. - env.template.json, PM2 template, README, CHANGELOG, WHATS-NEW; 27 tests. Docs: the Hub runs 2.26.1 with two years of Cross Code's devotionals; an App gets 2 weeks, today included. HANDOVER lists the App's slices. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import type { AppInfo } from "./load";
|
||||
|
||||
/** The sizes the App's icon is drawn at (APP-1.0-PLAN §2): the manifest's, Apple's and the favicon. */
|
||||
export const ICON_SIZES = { "192": 192, "512": 512, maskable: 512, apple: 180, favicon: 64 } as const;
|
||||
export type IconKind = keyof typeof ICON_SIZES;
|
||||
|
||||
/** "Acacia Hill" → "AH", "Northpoint" → "N": the icon of a church without one of its own. */
|
||||
export function initials(name: string): string {
|
||||
const words = name.replace(/[^\p{L}\p{N}\s]/gu, " ").trim().split(/\s+/).filter(Boolean);
|
||||
const letters = words.length > 1 ? [words[0]!, words[1]!].map((w) => [...w][0]!) : [...(words[0] ?? "")].slice(0, 1);
|
||||
return letters.join("").toUpperCase() || "+";
|
||||
}
|
||||
|
||||
/** The church's own icon when the image renderer can draw it (https, not SVG), else null. */
|
||||
export function churchIconUrl(app: AppInfo): string | null {
|
||||
const url = app.church?.branding.iconUrl;
|
||||
return url && /^https:\/\//.test(url) && !/\.svg(\?|$)/i.test(url) ? url : null;
|
||||
}
|
||||
|
||||
/** A short, stable version for the icons' addresses, so a new icon shows at once. */
|
||||
export function iconVersion(app: AppInfo): string {
|
||||
const s = `${app.church?.branding.iconUrl ?? ""}|${app.palette.brand}|${app.palette.onBrand}|${app.church?.shortName ?? app.church?.name ?? ""}`;
|
||||
let h = 0;
|
||||
for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) | 0;
|
||||
return (h >>> 0).toString(36);
|
||||
}
|
||||
|
||||
export const iconHref = (kind: IconKind, app: AppInfo) => `/app-icon/${kind}?v=${iconVersion(app)}`;
|
||||
|
||||
/** "31 78 121" → "rgb(31,78,121)". */
|
||||
export const rgb = (triple: string) => `rgb(${triple.split(" ").join(",")})`;
|
||||
@@ -0,0 +1,106 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { cache } from "react";
|
||||
|
||||
/** The backend on this server (APP_API_URL; 5400 in development). */
|
||||
export const api = (process.env.APP_API_URL || "http://127.0.0.1:5400").replace(/\/+$/, "");
|
||||
|
||||
export type AppModule = "house-of-prayer" | "prayer-requests" | "announcements" | "devotionals" | "events";
|
||||
|
||||
export type Church = {
|
||||
name: string;
|
||||
shortName: string | null;
|
||||
timezone: string;
|
||||
locale: string;
|
||||
branding: { logoUrl: string | null; iconUrl: string | null; colours: { primary: string; accent: string | null } };
|
||||
serviceTimes: { day: string; time: string; label: string | null }[];
|
||||
contact: {
|
||||
email: string | null;
|
||||
phone: string | null;
|
||||
whatsapp: string | null;
|
||||
address: { line1: string; line2: string | null; suburb: string | null; city: string; postalCode: string | null } | null;
|
||||
};
|
||||
social: { network: string; url: string }[];
|
||||
links: { website: string | null; events: string | null; app: string | null; accounts: string };
|
||||
};
|
||||
|
||||
export type Palette = { brand: string; brandDark: string; onBrand: string; brandText: string; brandTint: string; accent: string; onAccent: string };
|
||||
|
||||
/** What the backend's /api/v1/app answers (backend/src/public/app.ts). */
|
||||
export type AppInfo = {
|
||||
church: Church | null;
|
||||
shortName: string;
|
||||
palette: Palette;
|
||||
style: { fontPairing: string; corners: string };
|
||||
modules: AppModule[];
|
||||
timezone: string;
|
||||
};
|
||||
|
||||
const FALLBACK: AppInfo = {
|
||||
church: null,
|
||||
shortName: "Church App",
|
||||
palette: { brand: "31 78 121", brandDark: "25 62 97", onBrand: "255 255 255", brandText: "31 78 121", brandTint: "241 244 248", accent: "87 113 148", onAccent: "255 255 255" },
|
||||
style: { fontPairing: "warm", corners: "soft" },
|
||||
modules: ["house-of-prayer", "prayer-requests", "announcements", "devotionals", "events"],
|
||||
timezone: "Africa/Johannesburg",
|
||||
};
|
||||
|
||||
/**
|
||||
* The church, its colours and the modules on: one fetch per request (React `cache`), kept by Next
|
||||
* for 30 seconds in production. While the backend can't answer, the App still shows (in Cross
|
||||
* Code's colours), never an error page.
|
||||
*/
|
||||
export const loadApp = cache(async (): Promise<AppInfo> => {
|
||||
try {
|
||||
const res = await fetch(`${api}/api/v1/app`, process.env.NODE_ENV === "production" ? { next: { revalidate: 30 } } : { cache: "no-store" });
|
||||
if (res.ok) return (await res.json()) as AppInfo;
|
||||
} catch {
|
||||
// Starting or down.
|
||||
}
|
||||
return FALLBACK;
|
||||
});
|
||||
|
||||
export const ROLE_NAMES = {
|
||||
owner: "Owner",
|
||||
church_admin: "Church admin",
|
||||
content_admin: "Content admin",
|
||||
devotional_writer: "Devotional writer",
|
||||
prayer_coordinator: "Prayer coordinator",
|
||||
pastoral_care: "Pastoral care",
|
||||
} as const;
|
||||
|
||||
export type Role = keyof typeof ROLE_NAMES;
|
||||
|
||||
/** The signed-in member (the backend's /api/auth/me). */
|
||||
export type Me = {
|
||||
member: { id: string; name: string; email: string; roles: Role[] };
|
||||
/** Whether this sign-in had a second step: every role needs it (APP-1.0-PLAN §2). */
|
||||
mfa: boolean;
|
||||
activeRoles: Role[];
|
||||
};
|
||||
|
||||
export const SESSION_COOKIE = "cca_session";
|
||||
|
||||
/** Null when signed out (or the backend can't say). */
|
||||
export const loadMe = cache(async (): Promise<Me | null> => {
|
||||
const session = (await cookies()).get(SESSION_COOKIE)?.value;
|
||||
if (!session) return null;
|
||||
try {
|
||||
const res = await fetch(`${api}/api/auth/me`, { headers: { Cookie: `${SESSION_COOKIE}=${encodeURIComponent(session)}` }, cache: "no-store" });
|
||||
if (res.ok) return (await res.json()) as Me;
|
||||
} catch {
|
||||
// The backend is starting or down: treated as signed out.
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
/** Where "Sign in" goes: Accounts, and back to that exact page (ROADMAP-v3 §3). `mfa` for the admin. */
|
||||
export const signInHref = (returnTo: string, mfa = false) => `/api/auth/signin?returnTo=${encodeURIComponent(returnTo)}${mfa ? "&mfa=1" : ""}`;
|
||||
|
||||
/** The first name for a greeting ("Good morning, Thandi"). */
|
||||
export const firstName = (name: string) => name.trim().split(/\s+/)[0] ?? name;
|
||||
|
||||
/** Morning, afternoon or evening in the church's timezone. */
|
||||
export function greeting(timezone: string, now = new Date()): string {
|
||||
const hour = Number(new Intl.DateTimeFormat("en-ZA", { timeZone: timezone, hour: "numeric", hourCycle: "h23" }).format(now));
|
||||
return hour < 12 ? "Good morning" : hour < 18 ? "Good afternoon" : "Good evening";
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
/**
|
||||
* A 303 to a page of the App with a relative Location ("/me"), so the browser stays on the address
|
||||
* it's on. `new URL(path, request.url)` would use the address the request reached Next.js on, which
|
||||
* behind the proxy is the server's own (http://localhost:<web port>), as the Website found in 1.0.2.
|
||||
*/
|
||||
export function localRedirect(path: string): NextResponse {
|
||||
if (!path.startsWith("/") || path.startsWith("//")) throw new Error(`Not a path on this site: ${path}`);
|
||||
return new NextResponse(null, { status: 303, headers: { Location: path } });
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { notFound } from "next/navigation";
|
||||
import { loadApp, type AppModule } from "./load";
|
||||
|
||||
/** A module's pages are "not found" while the church has it switched off (APP-1.0-PLAN §2). */
|
||||
export async function requireModule(module: AppModule): Promise<void> {
|
||||
if (!(await loadApp()).modules.includes(module)) notFound();
|
||||
}
|
||||
Reference in New Issue
Block a user