NGU-Web/server/src/notify.js
Zaldimmar 8fd3a9b15b Add a test notice button to Site settings
Panel → Site settings → Notices → Send a test notice emails the
addresses in the box now and shows where it came from, or Google's
reason for refusing, so the Gmail setup can be checked without the
journal. Superadmins only (POST /api/admin/panel/test-notice).

The docs now say the sender must be a Workspace user: a group can't
be acted as through delegation, though it can receive notices.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 17:18:43 -05:00

44 lines
2 KiB
JavaScript

/* ═══════════════════════════════════════════════════════════════
NEW-REQUEST NOTICES
An email to the people in Panel → Site settings → "Email new
requests to" when a content request (/request) or a data request
(the Privacy page) comes in. Sent after the row is saved and never
waited on: a mail problem is logged, the submission stands.
Off until Gmail is set up (googleService.js) and someone is listed.
═══════════════════════════════════════════════════════════════ */
import { mailReady, sendMail } from "./googleService.js";
import { siteFor } from "./pageMeta.js";
const EMAIL = /^[^\s@,;<>]+@[^\s@,;<>]+\.[^\s@,;<>]+$/;
/* The usable addresses in a list separated by commas, semicolons or
spaces; anything else is left out. */
export const addresses = (text) =>
String(text ?? "")
.split(/[\s,;]+/)
.filter((address) => EMAIL.test(address));
export function recipients(db) {
return addresses(db.prepare(`SELECT notify_emails FROM site_settings WHERE id = 'site'`).get()?.notify_emails);
}
/* queue: the admin path of the queue it landed in, eg. /admin/requests */
export function notifyNew(db, { id, subject, lines, replyTo, queue }) {
if (!mailReady()) return;
const to = recipients(db);
if (to.length === 0) return;
let origin = "";
try {
origin = (siteFor(db).url ?? "").replace(/\/$/, "");
} catch {
// No site.json: the notice goes without a link.
}
const text = [...lines, "", origin ? `Open it: ${origin}${queue}` : `In the admin: Forms, ${queue}`, ""].join("\n");
sendMail({ to, subject, text, replyTo: EMAIL.test(replyTo ?? "") ? replyTo : undefined })
.then(() => console.log(`feedback #${id}: notice sent to ${to.length}`))
.catch((err) => console.warn(`feedback #${id}: notice failed: ${err.message}`));
}