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>
86 lines
4.4 KiB
TypeScript
86 lines
4.4 KiB
TypeScript
/**
|
|
* Email and WhatsApp for the church's Accounts (3.1.0). Accounts sends its own sign-in codes and
|
|
* password links, with its own settings, which a move doesn't bring along: a church that moved
|
|
* sign-in without them couldn't sign in at all. Settings → Sign-in therefore shows what Accounts can
|
|
* send, offers this site's own SMTP and WAWP settings to copy (the password and token stay on the
|
|
* server: blank means "use this site's"), tests them through Accounts, and saves them there only
|
|
* once the test worked (`crosscode.identity-request/1` messaging.*, contracts 1.9.0). The move
|
|
* itself needs Accounts' email to work (move.ts).
|
|
*/
|
|
import type { MessagingStatusReply, MessagingTestReply } from '@crosscode/contracts';
|
|
import { accounts } from './client';
|
|
|
|
/* eslint-disable @typescript-eslint/no-require-imports -- CommonJS */
|
|
const { getSetting } = require('../utils/settingsCache') as { getSetting(k: string, f?: string): Promise<string> };
|
|
const whatsapp = require('../utils/whatsapp') as { getConfig(): Promise<{ token: string; instanceId: string }> };
|
|
/* eslint-enable @typescript-eslint/no-require-imports */
|
|
|
|
type Smtp = { host: string; port: number; secure: boolean; user: string; pass: string; from: string };
|
|
|
|
/** This site's SMTP settings, as utils/email.js reads them (the database, then the .env). */
|
|
async function eventsSmtp(): Promise<Smtp | null> {
|
|
const env = process.env;
|
|
const host = await getSetting('smtp_host', env.SMTP_HOST || env.EMAIL_HOST || '');
|
|
if (!host) return null;
|
|
const port = parseInt(await getSetting('smtp_port', env.SMTP_PORT || env.EMAIL_PORT || '587'), 10) || 587;
|
|
return {
|
|
host,
|
|
port,
|
|
secure: String(await getSetting('smtp_secure', env.SMTP_SECURE || env.EMAIL_SECURE || 'false')).toLowerCase() === 'true',
|
|
user: await getSetting('smtp_user', env.SMTP_USER || env.EMAIL_USER || ''),
|
|
pass: await getSetting('smtp_pass', env.SMTP_PASS || env.EMAIL_PASS || ''),
|
|
from: await getSetting('smtp_from', env.MAIL_FROM || env.EMAIL_FROM || ''),
|
|
};
|
|
}
|
|
|
|
/** What this site has, to offer for copying: never the password or token themselves. */
|
|
export async function eventsMessaging() {
|
|
const smtp = await eventsSmtp();
|
|
const wa = await whatsapp.getConfig().catch(() => ({ token: '', instanceId: '' }));
|
|
return {
|
|
email: smtp ? { host: smtp.host, port: smtp.port, secure: smtp.secure, user: smtp.user, from: smtp.from || smtp.user, hasPass: !!smtp.pass } : null,
|
|
whatsapp: wa.instanceId ? { instanceId: wa.instanceId, hasToken: !!wa.token } : null,
|
|
};
|
|
}
|
|
|
|
/** What Accounts can send now. */
|
|
export async function accountsMessaging(): Promise<Omit<MessagingStatusReply, 'schema'>> {
|
|
const r = await (await accounts()).request<MessagingStatusReply>({ type: 'messaging.status', actor: null });
|
|
return { email: r.email, whatsapp: r.whatsapp };
|
|
}
|
|
|
|
export type MessagingInput = {
|
|
email?: { host: string; port: number; secure: boolean; user: string; from: string; pass?: string | null; to: string };
|
|
whatsapp?: { instanceId: string; accessToken?: string | null; to: string };
|
|
};
|
|
|
|
/**
|
|
* Tests (and with `save`, saves) settings in Accounts. A blank password or token means this site's
|
|
* own; Accounts gets it sealed with the link's secret. Returns Accounts' answer per part.
|
|
*/
|
|
export async function sendToAccounts(input: MessagingInput, opts: { save: boolean; actor: string | null }): Promise<Omit<MessagingTestReply, 'schema'>> {
|
|
const auth = await accounts();
|
|
let email;
|
|
if (input.email) {
|
|
const own = await eventsSmtp();
|
|
const pass = input.email.pass ? input.email.pass : (own?.pass ?? '');
|
|
email = {
|
|
settings: { host: input.email.host.trim(), port: input.email.port, secure: input.email.secure, user: input.email.user.trim(), from: input.email.from.trim(), pass: pass ? auth.seal(pass) : null },
|
|
to: input.email.to.trim(),
|
|
};
|
|
}
|
|
let wa;
|
|
if (input.whatsapp) {
|
|
const own = await whatsapp.getConfig().catch(() => ({ token: '', instanceId: '' }));
|
|
const token = input.whatsapp.accessToken ? input.whatsapp.accessToken : own.token;
|
|
wa = { settings: { instanceId: input.whatsapp.instanceId.trim(), accessToken: token ? auth.seal(token) : null }, to: input.whatsapp.to.trim() };
|
|
}
|
|
const r = await auth.request<MessagingTestReply>({
|
|
type: opts.save ? 'messaging.set' : 'messaging.test',
|
|
actor: opts.actor,
|
|
...(email ? { email } : {}),
|
|
...(wa ? { whatsapp: wa } : {}),
|
|
}, { timeoutMs: 60000 });
|
|
return { email: r.email, whatsapp: r.whatsapp, status: r.status };
|
|
}
|