Files
redefined-designs/backend/src/routes/adminEmailTemplates.ts
T
bermudalamb 2f7268704a feat(admin): make the token lifetimes, cart hold and greeting configurable (#136)
Giving the customer emails a tab of their own (#135) made a family of related holes visible: an admin could edit the wording of every customer email, but not the facts that wording asserted, and two templates could not address the customer at all.

Both token lifetimes were hardcoded twice. VERIFY_TOKEN_TTL_MS sat in routes/customers.ts while the verification body separately said "This link expires in 24 hours", and RESET_TOKEN_TTL_MS sat beside a body separately saying "one hour". The prose was not derived from the constant, it was a second hand-written copy of the same fact — so making the constants configurable without addressing that would have made things worse, not better: the setting moves to two hours and the email keeps confidently promising one. Both are now settings, and both templates state their lifetime through an {{expiresIn}} placeholder rendered from the setting.

The cart reminder gains {{holdDuration}} for the same reason. Per-item deadlines were already inside {{itemList}}, but there was no way to write a sentence about the hold itself without hardcoding a number the cart expiry setting could change underneath the author.

All three durations render through one formatDuration(), so the reset email and the cart reminder say "one hour" the same way rather than in two authors' phrasing. A fractional hour drops to minutes, because "0.5 hours" reads badly and "1.5 hours" reads worse in a sentence a customer is meant to act on.

Every template now offers greeting, firstName and lastName. favoriteSold and favoriteWithdrawn previously offered only itemName and siteUrl and could not address anyone — the query behind them never selected a name, so it does now.

The greeting itself is two settings, a format and a fallback, rather than the wording baked into greeting(). The fallback is separate rather than the format with the name edited out: that editing is guesswork that has to be right every time, and getting it wrong ships "Hi ," to everyone who registered while first names were still optional (#106). Those customers exist, which is why greeting() guarded the case in the first place.

The admin preview renders the durations and the greeting from the live settings rather than from a static sample. The preview exists so an admin sees the email that will be sent, and a sample reading "one hour" while the setting says two is the precise failure these placeholders were added to remove.

Settings are read through a new adminSettings accessor. cart_expiry_hours was previously read by an inline query in two places, each with its own `|| '24'`; with five settings and read sites across three routes and the cron job, a default written twice is a default that will eventually disagree with itself. Values are stored as text, so each definition declares how to read it back — numbers were the only kind until the greeting format arrived.

The new placeholders are available but never required, so every template an admin has already saved keeps rendering and keeps sending.

Also fixes updateAdminSettings announcing success for a save the server refused: it returned the 400 body as though it were the saved settings, so the form reported "Settings saved" either way.

Closes #136
2026-08-23 08:55:53 -05:00

184 lines
6.6 KiB
TypeScript

import { Router, Request, Response } from 'express';
import { pool } from '../db';
import { asyncRoute } from '../asyncRoute';
import {
TEMPLATES,
TemplateKey,
StoredTemplate,
missingPlaceholders,
renderTemplate,
formatDuration,
greeting,
SAMPLE_VALUES
} from '../emailTemplates';
import { getSettings } from '../adminSettings';
const router = Router();
const KEYS = Object.keys(TEMPLATES) as TemplateKey[];
// Stored in admin_settings rather than a table of their own: it is already a
// key/value store with a settled read/write shape, and five templates is not a
// schema.
const settingKey = (key: TemplateKey, part: 'subject' | 'body') => `email_${key}_${part}`;
function isTemplateKey(value: unknown): value is TemplateKey {
return typeof value === 'string' && (KEYS as string[]).includes(value);
}
export async function loadStoredTemplate(key: TemplateKey): Promise<StoredTemplate> {
const { rows } = await pool.query(`SELECT key, value FROM admin_settings WHERE key = ANY($1)`, [
[settingKey(key, 'subject'), settingKey(key, 'body')]
]);
const stored: StoredTemplate = {};
for (const row of rows) {
if (row.key === settingKey(key, 'subject')) stored.subject = row.value;
if (row.key === settingKey(key, 'body')) stored.body = row.value;
}
return stored;
}
// Returns the definitions alongside whatever is stored, so the admin screen can
// show the placeholders a template accepts and which of them it must keep,
// rather than the editor having to know.
router.get('/', asyncRoute(async (_req: Request, res: Response) => {
const { rows } = await pool.query(
`SELECT key, value FROM admin_settings WHERE key LIKE 'email\\_%'`
);
const stored = new Map<string, string>(rows.map((r) => [r.key, r.value]));
res.json(
KEYS.map((key) => ({
key,
label: TEMPLATES[key].label,
required: TEMPLATES[key].required,
available: TEMPLATES[key].available,
defaultSubject: TEMPLATES[key].defaultSubject,
defaultBody: TEMPLATES[key].defaultBody,
// Null rather than the default, so the admin can tell "not customised"
// from "customised to exactly the default text".
subject: stored.get(settingKey(key, 'subject')) ?? null,
body: stored.get(settingKey(key, 'body')) ?? null
}))
);
}));
// Renders what an email would look like, from the subject and body in the
// editor rather than from what is stored — so an admin sees the effect of an
// edit before committing to it.
//
// Rendered here rather than in the browser, deliberately. renderTemplate is the
// only thing that turns this markdown into HTML, and markdown-it is configured
// with html: false, which is what stops an admin putting script into a
// customer's inbox. A second renderer in the frontend would be a second place
// for that setting to be wrong, and a preview that differs from the mailer is
// worse than no preview.
//
// Deliberately does not enforce required placeholders. Saving refuses a body
// that dropped one; previewing it is how an admin sees what they have done.
/**
* The sample values, with the three duration placeholders replaced by what the
* settings actually hold.
*
* The preview exists so an admin sees the email that will be sent. A duration
* drawn from a static sample would show "one hour" while the setting said two,
* which is the precise failure this placeholder was added to remove.
*/
async function previewValues(key: TemplateKey): Promise<Record<string, string>> {
const {
cartExpiryHours,
verifyTokenHours,
passwordResetHours,
greetingFormat,
greetingFallback
} = await getSettings();
// `expiresIn` names one placeholder but two different lifetimes, so the value
// depends on which template is being previewed. The route knows the key.
const expiresIn = key === 'passwordReset' ? passwordResetHours : verifyTokenHours;
return {
...SAMPLE_VALUES,
holdDuration: formatDuration(cartExpiryHours),
expiresIn: formatDuration(expiresIn),
// Built from the configured format for the same reason as the durations:
// the preview is meant to show the email that will be sent.
greeting: greeting(SAMPLE_VALUES.firstName, greetingFormat, greetingFallback, SAMPLE_VALUES.lastName)
};
}
router.post('/:key/preview', asyncRoute(async (req: Request, res: Response) => {
const key = req.params.key;
if (!isTemplateKey(key)) {
return res.status(404).json({ error: 'unknown template' });
}
const { subject, body } = req.body ?? {};
const rendered = renderTemplate(
key,
{
subject: typeof subject === 'string' ? subject : null,
body: typeof body === 'string' ? body : null
},
await previewValues(key)
);
res.json(rendered);
}));
router.put('/:key', asyncRoute(async (req: Request, res: Response) => {
const key = req.params.key;
if (!isTemplateKey(key)) {
return res.status(404).json({ error: 'unknown template' });
}
const subject = typeof req.body?.subject === 'string' ? req.body.subject.trim() : '';
const body = typeof req.body?.body === 'string' ? req.body.body.trim() : '';
if (!subject) {
return res.status(400).json({ error: 'a subject is required' });
}
if (!body) {
return res.status(400).json({ error: 'a body is required' });
}
// The rule that makes this feature safe rather than a way to break password
// resets from a settings screen. A body without its link still sends, still
// looks correct in the log, and is useless to everyone who receives it — so
// the save is refused rather than warned about.
const missing = missingPlaceholders(key, body);
if (missing.length) {
const named = missing.map((name) => '{{' + name + '}}').join(' and ');
return res.status(400).json({ error: `the body must keep ${named}` });
}
for (const [part, value] of [
['subject', subject],
['body', body]
] as const) {
await pool.query(
`INSERT INTO admin_settings (key, value, updated_at) VALUES ($1, $2, now())
ON CONFLICT (key) DO UPDATE SET value = $2, updated_at = now()`,
[settingKey(key, part), value]
);
}
res.json({ key, subject, body });
}));
// Restores the built-in copy by removing the stored rows, rather than by
// writing the default into them — so "not customised" stays distinguishable
// from "customised back to the original wording".
router.delete('/:key', asyncRoute(async (req: Request, res: Response) => {
const key = req.params.key;
if (!isTemplateKey(key)) {
return res.status(404).json({ error: 'unknown template' });
}
await pool.query(`DELETE FROM admin_settings WHERE key = ANY($1)`, [
[settingKey(key, 'subject'), settingKey(key, 'body')]
]);
res.json({ key, subject: null, body: null });
}));
export default router;