Events 3.1.0, Accounts 1.2.0, contracts 1.9.0: the move needs Accounts' email

A church moved sign-in to Accounts without email set up there: codes and
password links couldn't be sent, sign-in failed with a 500, nobody could
get in.

- contracts 1.9.0: messaging.status/test/set (SMTP, WAWP; secrets sealed);
  people.ensure answers emailSent.
- Accounts 1.2.0: SendError instead of a 500; WhatsApp codes fall back to
  email; forgot password says email isn't set up; setup only offers
  methods that work; messaging.* (test, save only what worked); test
  WhatsApp in admin; WAWP from .env.
- Events 3.1.0: Settings -> Sign-in shows what Accounts can send and
  copies this site's SMTP/WAWP into it (secrets stay on the server), test
  then save; Move disabled and refused (EMAIL_NOT_READY) without email.
Tests: Accounts 54, contracts 218, Events 774, test:accounts 16 (also on
Postgres).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-19 21:41:31 +02:00
co-authored by Claude Opus 5.5
parent 4b28fc485b
commit cc18b7b514
44 changed files with 2568 additions and 51 deletions
+66 -7
View File
@@ -227,14 +227,22 @@ async function handle(ctx: Ctx, provider: Provider, link: ProductLink, secret: s
const returnUrl = new URL(r.returnUrl).origin === new URL(link.url).origin ? r.returnUrl : link.url;
await ctx.db.emailToken.create({ data: { accountId: a.id, purpose: 'reset_password', tokenHash: hashToken(token), returnUrl, expiresAt: new Date(ctx.now().getTime() + SET_PASSWORD_DAYS * 86400e3) } });
const churchName = (await ctx.settings.json<{ name?: string }>('church_profile', {})).name || 'your church';
await ctx.messenger.email({
to: email,
subject: `Your account at ${churchName}`,
text: `Hello ${r.name},\n\n${churchName} has made you an account. Choose your password here (the link works for ${SET_PASSWORD_DAYS} days):\n${ctx.config.publicUrl}/reset?token=${token}\n\nIf you weren't expecting this, you can ignore this email.`,
});
// 1.2.0: the account stands even when the email can't go (they can use "Forgot your password?"
// once email works); the product is told.
let emailSent = true;
try {
await ctx.messenger.email({
to: email,
subject: `Your account at ${churchName}`,
text: `Hello ${r.name},\n\n${churchName} has made you an account. Choose your password here (the link works for ${SET_PASSWORD_DAYS} days):\n${ctx.config.publicUrl}/reset?token=${token}\n\nIf you weren't expecting this, you can ignore this email.`,
});
} catch (e) {
emailSent = false;
console.warn('[people.ensure] the "choose your password" email could not be sent:', (e as Error).message);
}
await loginEvent(ctx, a.id, 'account_created', req, { clientId: link.installId, detail: { by: 'product', product: link.product } });
await logRequest(ctx, link, r, 'person_created_by_product', a.id, req);
return reply({ sub: a.id, created: true });
await logRequest(ctx, link, r, 'person_created_by_product', a.id, req, { emailSent });
return reply({ sub: a.id, created: true, emailSent });
}
case 'people.update': {
const a = await knownAccount(ctx, link, r.sub);
@@ -282,6 +290,48 @@ async function handle(ctx: Ctx, provider: Provider, link: ProductLink, secret: s
await logRequest(ctx, link, r, r.active ? 'account_reactivated_by_product' : 'account_deactivated_by_product', a.id, req);
return reply({ ok: true as const });
}
case 'messaging.status':
return reply(await messagingStatus(ctx));
case 'messaging.test':
case 'messaging.set': {
const save = r.type === 'messaging.set';
const church = (await ctx.settings.json<{ name?: string }>('church_profile', {})).name || 'your church';
let email: { ok: boolean; error: string | null; saved: boolean } | null = null;
let whatsapp: { ok: boolean; error: string | null; saved: boolean } | null = null;
if (r.email) {
const s = r.email.settings;
const pass = s.pass === null ? ((await ctx.settings.get('smtp_pass')) ?? '') : opened(secret, s.pass, 'SMTP password');
const tryWith = { host: s.host, port: s.port, secure: s.secure, user: s.user, pass: pass ?? '', from: s.from };
try {
await ctx.messenger.emailWith(tryWith, { to: r.email.to, subject: `Test email from ${church}'s sign-in`, text: `This is a test from ${church}'s sign-in (Accounts), sent from ${link.product === 'events' ? 'Cross Events' : link.product}. Email works: sign-in codes and password links can be sent.` });
email = { ok: true, error: null, saved: false };
if (save) {
await ctx.settings.setMany({ smtp_host: s.host, smtp_port: String(s.port), smtp_secure: String(s.secure), smtp_user: s.user, smtp_from: s.from, ...(s.pass === null ? {} : { smtp_pass: pass }) });
email.saved = true;
}
} catch (e) {
email = { ok: false, error: String((e as Error).message || e).slice(0, 500), saved: false };
}
}
if (r.whatsapp) {
const s = r.whatsapp.settings;
const token = s.accessToken === null ? ((await ctx.settings.get('wawp_access_token')) ?? '') : opened(secret, s.accessToken, 'WAWP access token');
try {
if (!token) throw new Error('Give the WAWP access token.');
await ctx.messenger.whatsappWith({ accessToken: token, instanceId: s.instanceId }, { to: r.whatsapp.to, text: `This is a test from ${church}'s sign-in. WhatsApp works: sign-in codes can be sent.` });
whatsapp = { ok: true, error: null, saved: false };
if (save) {
await ctx.settings.setMany({ wawp_instance_id: s.instanceId, ...(s.accessToken === null ? {} : { wawp_access_token: token }) });
whatsapp.saved = true;
}
} catch (e) {
whatsapp = { ok: false, error: String((e as Error).message || e).slice(0, 500), saved: false };
}
}
await logRequest(ctx, link, r, save ? 'messaging_set_by_product' : 'messaging_tested_by_product', null, req, { email: email && { ok: email.ok, saved: email.saved }, whatsapp: whatsapp && { ok: whatsapp.ok, saved: whatsapp.saved } });
const status = await messagingStatus(ctx);
return reply({ email, whatsapp, status: { email: status.email, whatsapp: status.whatsapp } });
}
case 'people.newsOff': {
const a = await knownAccount(ctx, link, r.sub);
const off = r.channel === 'all' ? { email: false, whatsapp: false } : { [r.channel]: false };
@@ -328,6 +378,15 @@ export function requestsRouter(ctx: Ctx, provider: Provider) {
return r;
}
/** What can be sent now (no secrets), for a product's sign-in settings page. */
async function messagingStatus(ctx: Ctx) {
const emailReady = await ctx.messenger.emailReady();
return {
email: { ready: emailReady, host: emailReady ? ((await ctx.settings.get('smtp_host')) || ctx.config.smtp?.host || null) : null, from: emailReady ? ((await ctx.settings.get('smtp_from')) || ctx.config.smtp?.from || null) : null },
whatsapp: { ready: await ctx.messenger.whatsappReady(), instanceId: (await ctx.settings.get('wawp_instance_id')) || ctx.config.wawp?.instanceId || null },
};
}
/** Old remembered requests (the `cleanup` job). */
export async function forgetOldRequests(ctx: Ctx) {
return ctx.db.productRequest.deleteMany({ where: { createdAt: { lt: new Date(ctx.now().getTime() - KEEP_REQUESTS_DAYS * 86400e3) } } });