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:
@@ -20,6 +20,8 @@ export type Config = {
|
||||
hubAdminEmail: string | null;
|
||||
timezone: string;
|
||||
smtp: { host: string; port: number; user: string; pass: string; from: string; secure: boolean } | null;
|
||||
/** 1.2.0: WAWP from the .env when the database has none (as Cross Events). */
|
||||
wawp: { accessToken: string; instanceId: string } | null;
|
||||
/** Local testing only: linked products on http or private addresses. */
|
||||
allowPrivateLinks: boolean;
|
||||
jobsEnabled: boolean;
|
||||
@@ -67,6 +69,7 @@ export function loadConfig(env: Record<string, string | undefined> = process.env
|
||||
smtp: env.SMTP_HOST
|
||||
? { host: env.SMTP_HOST, port: Number(env.SMTP_PORT || 587), user: env.SMTP_USER || '', pass: env.SMTP_PASS || '', from: env.SMTP_FROM || env.SMTP_USER || '', secure: bool(env.SMTP_SECURE, Number(env.SMTP_PORT) === 465) }
|
||||
: null,
|
||||
wawp: env.WAWP_ACCESS_TOKEN && env.WAWP_INSTANCE_ID ? { accessToken: env.WAWP_ACCESS_TOKEN, instanceId: env.WAWP_INSTANCE_ID } : null,
|
||||
allowPrivateLinks: bool(env.LINKS_ALLOW_PRIVATE, false),
|
||||
jobsEnabled: bool(env.JOBS_ENABLED, true),
|
||||
};
|
||||
|
||||
@@ -8,11 +8,49 @@ import type { Ctx } from '../context.js';
|
||||
export type Email = { to: string; subject: string; text: string };
|
||||
export type WhatsApp = { to: string; text: string };
|
||||
|
||||
/** SMTP settings to try before saving them (1.2.0), as kept in `smtp_*`. */
|
||||
export type SmtpTry = { host: string; port: number; secure: boolean; user: string; pass: string; from: string };
|
||||
/** WAWP settings to try before saving them (1.2.0). */
|
||||
export type WawpTry = { accessToken: string; instanceId: string };
|
||||
|
||||
export interface Messenger {
|
||||
email(msg: Email): Promise<void>;
|
||||
whatsapp(msg: WhatsApp): Promise<void>;
|
||||
/** Whether email is set up (sign-in codes, password links and new accounts need it). */
|
||||
emailReady(): Promise<boolean>;
|
||||
/** Whether WhatsApp is set up, so it's offered as a 2FA method. */
|
||||
whatsappReady(): Promise<boolean>;
|
||||
/** Sends one email with settings that aren't saved (a test before saving them). Throws why it failed. */
|
||||
emailWith(settings: SmtpTry, msg: Email): Promise<void>;
|
||||
/** Sends one WhatsApp message with settings that aren't saved. Throws why it failed. */
|
||||
whatsappWith(settings: WawpTry, msg: WhatsApp): Promise<void>;
|
||||
}
|
||||
|
||||
/** Thrown when a message can't be sent: the sign-in pages show it instead of failing. */
|
||||
export class SendError extends Error {
|
||||
status = 503;
|
||||
constructor(message: string, public code = 'NOT_SENT') {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
function transportFor(s: SmtpTry) {
|
||||
return nodemailer.createTransport({
|
||||
host: s.host, port: s.port, secure: s.secure, auth: s.user ? { user: s.user, pass: s.pass } : undefined,
|
||||
connectionTimeout: 15000, greetingTimeout: 15000, socketTimeout: 20000,
|
||||
});
|
||||
}
|
||||
|
||||
async function wawpSend(s: WawpTry, msg: WhatsApp) {
|
||||
const number = waNumber(msg.to);
|
||||
if (!number) throw new Error('That isn\'t a WhatsApp number.');
|
||||
const res = await fetch('https://api.wawp.net/v2/send/text', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ access_token: s.accessToken, instance_id: s.instanceId, chatId: `${number}@c.us`, message: msg.text }),
|
||||
signal: AbortSignal.timeout(15000),
|
||||
});
|
||||
if (!res.ok) throw new Error(`WhatsApp couldn't send the message (${res.status}).`);
|
||||
}
|
||||
|
||||
/** 0821234567 / +27 82 123 4567 → 27821234567 (South African mobiles; others as given, digits only). */
|
||||
@@ -52,23 +90,33 @@ export class LiveMessenger implements Messenger {
|
||||
await smtp.transport.sendMail({ from: church ? `"${church.replace(/"/g, '')}" <${smtp.from}>` : smtp.from, to: msg.to, subject: msg.subject, text: msg.text });
|
||||
}
|
||||
|
||||
async emailReady(): Promise<boolean> {
|
||||
return !!(await this.smtp());
|
||||
}
|
||||
|
||||
private async wawp(): Promise<WawpTry | null> {
|
||||
const accessToken = (await this.ctx.settings.get('wawp_access_token')) || this.ctx.config.wawp?.accessToken;
|
||||
const instanceId = (await this.ctx.settings.get('wawp_instance_id')) || this.ctx.config.wawp?.instanceId;
|
||||
return accessToken && instanceId ? { accessToken, instanceId } : null;
|
||||
}
|
||||
|
||||
async whatsappReady(): Promise<boolean> {
|
||||
return !!((await this.ctx.settings.get('wawp_access_token')) && (await this.ctx.settings.get('wawp_instance_id')));
|
||||
return !!(await this.wawp());
|
||||
}
|
||||
|
||||
async whatsapp(msg: WhatsApp): Promise<void> {
|
||||
const token = await this.ctx.settings.get('wawp_access_token');
|
||||
const instance = await this.ctx.settings.get('wawp_instance_id');
|
||||
const number = waNumber(msg.to);
|
||||
if (!token || !instance) throw new Error('WhatsApp isn\'t set up.');
|
||||
if (!number) throw new Error('That isn\'t a WhatsApp number.');
|
||||
const res = await fetch('https://api.wawp.net/v2/send/text', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ access_token: token, instance_id: instance, chatId: `${number}@c.us`, message: msg.text }),
|
||||
signal: AbortSignal.timeout(15000),
|
||||
});
|
||||
if (!res.ok) throw new Error(`WhatsApp couldn't send the message (${res.status}).`);
|
||||
const s = await this.wawp();
|
||||
if (!s) throw new Error('WhatsApp isn\'t set up.');
|
||||
await wawpSend(s, msg);
|
||||
}
|
||||
|
||||
async emailWith(settings: SmtpTry, msg: Email): Promise<void> {
|
||||
const church = (await this.ctx.settings.json<{ name?: string }>('church_profile', {})).name;
|
||||
await transportFor(settings).sendMail({ from: church ? `"${church.replace(/"/g, '')}" <${settings.from}>` : settings.from, to: msg.to, subject: msg.subject, text: msg.text });
|
||||
}
|
||||
|
||||
async whatsappWith(settings: WawpTry, msg: WhatsApp): Promise<void> {
|
||||
await wawpSend(settings, msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,9 +124,30 @@ export class MemoryMessenger implements Messenger {
|
||||
emails: Email[] = [];
|
||||
whatsapps: WhatsApp[] = [];
|
||||
whatsappOn = false;
|
||||
async email(msg: Email) { this.emails.push(msg); }
|
||||
async whatsapp(msg: WhatsApp) { this.whatsapps.push(msg); }
|
||||
/** Tests: false makes email fail as an unset SMTP does in production. */
|
||||
emailOn = true;
|
||||
/** Tests: settings tried with emailWith / whatsappWith, and a host that fails. */
|
||||
tried: { kind: 'email' | 'whatsapp'; settings: SmtpTry | WawpTry; to: string }[] = [];
|
||||
failHost: string | null = null;
|
||||
async email(msg: Email) {
|
||||
if (!this.emailOn) throw new Error('Email isn\'t set up (Admin → Email and WhatsApp).');
|
||||
this.emails.push(msg);
|
||||
}
|
||||
async whatsapp(msg: WhatsApp) {
|
||||
if (!this.whatsappOn) throw new Error('WhatsApp isn\'t set up.');
|
||||
this.whatsapps.push(msg);
|
||||
}
|
||||
async emailReady() { return this.emailOn; }
|
||||
async whatsappReady() { return this.whatsappOn; }
|
||||
async emailWith(settings: SmtpTry, msg: Email) {
|
||||
this.tried.push({ kind: 'email', settings, to: msg.to });
|
||||
if (settings.host === this.failHost) throw new Error(`connect ECONNREFUSED ${settings.host}:${settings.port}`);
|
||||
this.emails.push(msg);
|
||||
}
|
||||
async whatsappWith(settings: WawpTry, msg: WhatsApp) {
|
||||
this.tried.push({ kind: 'whatsapp', settings, to: msg.to });
|
||||
this.whatsapps.push(msg);
|
||||
}
|
||||
/** The last 6-digit code sent to `to` (tests). */
|
||||
lastCode(to: string): string | null {
|
||||
const all = [...this.emails.filter((e) => e.to === to).map((e) => e.text), ...this.whatsapps.filter((w) => w.to === to).map((w) => w.text)];
|
||||
|
||||
Reference in New Issue
Block a user