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>
140 lines
5.9 KiB
TypeScript
140 lines
5.9 KiB
TypeScript
import { z } from "zod";
|
|
import { dateTimeSchema, emailSchema, idSchema, phoneSchema, productSchema } from "./common";
|
|
import { schemaName } from "./schema-name";
|
|
|
|
/**
|
|
* `crosscode.identity/1` — what Accounts tells the products about a person (ROADMAP-v3 §3):
|
|
* the claims in the sign-in token (and userinfo), and the back-channel calls Accounts makes to
|
|
* each linked product (`user.updated`, `user.deleted`, `user.export`).
|
|
*
|
|
* Roles are never in here: each product keeps its own, keyed by `sub`.
|
|
* Back-channel calls are signed (see `@crosscode/contracts/signing`) and retried through pg-boss.
|
|
*/
|
|
|
|
export const IDENTITY_SCHEMA = schemaName("identity", 1);
|
|
|
|
/** 2FA methods (Cross Events 2.2.0 adds `totp`, the authenticator app). */
|
|
export const TWO_FACTOR_METHODS = ["email", "whatsapp", "totp"] as const;
|
|
export const twoFactorMethodSchema = z.enum(TWO_FACTOR_METHODS);
|
|
export type TwoFactorMethod = z.infer<typeof twoFactorMethodSchema>;
|
|
|
|
export const newsConsentSchema = z.object({
|
|
email: z.boolean(),
|
|
whatsapp: z.boolean(),
|
|
});
|
|
export type NewsConsent = z.infer<typeof newsConsentSchema>;
|
|
|
|
/** The profile every product keeps a local copy of (for lists and messages). */
|
|
export const profileSchema = z.object({
|
|
name: z.string().trim().min(1).max(200),
|
|
given_name: z.string().trim().max(100).nullable(),
|
|
family_name: z.string().trim().max(100).nullable(),
|
|
email: emailSchema,
|
|
email_verified: z.boolean(),
|
|
phone_number: phoneSchema.nullable(),
|
|
phone_number_verified: z.boolean(),
|
|
/** Whether the church may send this person news, per channel. */
|
|
news_consent: newsConsentSchema,
|
|
/**
|
|
* 1.20.0: the person replied STOP to the church's WhatsApp number (APP-1.0-PLAN §8, decision
|
|
* 11). No product sends them WhatsApp of any kind, reminders included, until they turn it back
|
|
* on in Accounts. Accounts keeps it for every product. Absent (an older Accounts): false.
|
|
*/
|
|
whatsapp_opted_out: z.boolean().optional(),
|
|
});
|
|
export type Profile = z.infer<typeof profileSchema>;
|
|
|
|
/**
|
|
* The `crosscode.identity/1` claims in the ID token and the userinfo response, next to the
|
|
* standard JWT claims (`iss`, `aud`, `exp`, …) that openid-client checks.
|
|
*/
|
|
export const identityClaimsSchema = profileSchema.extend({
|
|
schema: z.literal(IDENTITY_SCHEMA),
|
|
/** The account id. Each product's user record uses it as its id. */
|
|
sub: idSchema,
|
|
/** How this sign-in was done (RFC 8176): `pwd`, plus `otp` / `mfa` when 2FA was used. */
|
|
amr: z.array(z.string().min(1).max(20)).max(10),
|
|
/** The account's 2FA method, or `null` when 2FA is off. Admins see it per staff account. */
|
|
two_factor: twoFactorMethodSchema.nullable(),
|
|
/** The privacy notice version the person has accepted. Accounts asks again before returning when there's a newer one. */
|
|
privacy_notice_version: z.int().positive(),
|
|
});
|
|
export type IdentityClaims = z.infer<typeof identityClaimsSchema>;
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
// Back-channel calls: Accounts → product, `POST <product>/api/accounts/v1/events`
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
const callBase = z.object({
|
|
schema: z.literal(IDENTITY_SCHEMA),
|
|
/** Unique per call; repeated on a retry, so products can ignore a call they've already handled. */
|
|
callId: idSchema,
|
|
occurredAt: dateTimeSchema,
|
|
sub: idSchema,
|
|
});
|
|
|
|
/** The person's profile or consent changed. Products update their local copy. */
|
|
export const userUpdatedSchema = callBase.extend({
|
|
type: z.literal("user.updated"),
|
|
profile: profileSchema,
|
|
});
|
|
|
|
/**
|
|
* An admin approved deleting the account. Products anonymise their data for `sub` (as Cross
|
|
* Events does today) and answer with {@link userDeletedReplySchema}.
|
|
*/
|
|
export const userDeletedSchema = callBase.extend({
|
|
type: z.literal("user.deleted"),
|
|
approvedAt: dateTimeSchema,
|
|
});
|
|
|
|
/** "Download my data": each product sends its part, which Accounts bundles. */
|
|
export const userExportSchema = callBase.extend({
|
|
type: z.literal("user.export"),
|
|
/** The export this part belongs to. */
|
|
exportId: idSchema,
|
|
});
|
|
|
|
export const accountsCallSchema = z.discriminatedUnion("type", [userUpdatedSchema, userDeletedSchema, userExportSchema]);
|
|
export type AccountsCall = z.infer<typeof accountsCallSchema>;
|
|
export type UserUpdated = z.infer<typeof userUpdatedSchema>;
|
|
export type UserDeleted = z.infer<typeof userDeletedSchema>;
|
|
export type UserExport = z.infer<typeof userExportSchema>;
|
|
|
|
/** The reply to `user.updated`. */
|
|
export const userUpdatedReplySchema = z.object({ schema: z.literal(IDENTITY_SCHEMA), ok: z.literal(true) });
|
|
|
|
/**
|
|
* The reply to `user.deleted`. `deferred` when the product can't anonymise yet (Cross Events:
|
|
* an open registration for an upcoming event); Accounts asks again after `retryAfter`.
|
|
*/
|
|
export const userDeletedReplySchema = z.discriminatedUnion("status", [
|
|
z.object({ schema: z.literal(IDENTITY_SCHEMA), status: z.literal("deleted") }),
|
|
z.object({
|
|
schema: z.literal(IDENTITY_SCHEMA),
|
|
status: z.literal("deferred"),
|
|
/** Shown to the admin, e.g. "Has a registration for Youth Camp (14 March)". */
|
|
reason: z.string().trim().min(1).max(500),
|
|
retryAfter: dateTimeSchema,
|
|
}),
|
|
]);
|
|
export type UserDeletedReply = z.infer<typeof userDeletedReplySchema>;
|
|
|
|
/** One part of a product's export, e.g. "Registrations" or "Prayer commitments". */
|
|
export const exportSectionSchema = z.object({
|
|
key: z.string().regex(/^[a-z][a-z0-9_]*$/).max(50),
|
|
title: z.string().trim().min(1).max(100),
|
|
records: z.array(z.record(z.string(), z.unknown())),
|
|
});
|
|
|
|
/** The reply to `user.export`: the product's part of "Download my data". */
|
|
export const userExportReplySchema = z.object({
|
|
schema: z.literal(IDENTITY_SCHEMA),
|
|
exportId: idSchema,
|
|
sub: idSchema,
|
|
product: productSchema,
|
|
generatedAt: dateTimeSchema,
|
|
sections: z.array(exportSectionSchema).max(100),
|
|
});
|
|
export type UserExportReply = z.infer<typeof userExportReplySchema>;
|