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>
This commit is contained in:
parent
c63478a72a
commit
8fd3a9b15b
5 changed files with 148 additions and 9 deletions
|
|
@ -4,6 +4,7 @@
|
|||
GET /api/admin/panel/overview
|
||||
PATCH /api/admin/panel/users/:id role, is_active
|
||||
DELETE /api/admin/panel/users/:id/sessions sign out everywhere
|
||||
POST /api/admin/panel/test-notice { to }: a test email
|
||||
|
||||
Everything here is superadmin-only, enforced once at the top
|
||||
rather than per route; there's no read here that an ordinary
|
||||
|
|
@ -27,6 +28,8 @@ import { Hono } from "hono";
|
|||
import { requireAuth, requireRole, ROLES } from "../auth.js";
|
||||
import { tx } from "../db.js";
|
||||
import { NO_STORE } from "../shape.js";
|
||||
import { mailReady, sendMail } from "../googleService.js";
|
||||
import { addresses } from "../notify.js";
|
||||
|
||||
const panel = new Hono();
|
||||
|
||||
|
|
@ -214,4 +217,42 @@ function userRow(db, id) {
|
|||
.get(id);
|
||||
}
|
||||
|
||||
/* ── POST /test-notice ───────────────────────────────────────────
|
||||
Site settings → Notices → "Send a test notice": one email to the
|
||||
addresses in the box (saved or not), now, with Google's own words
|
||||
if it refuses, so the setup can be checked without the journal.
|
||||
───────────────────────────────────────────────────────────── */
|
||||
|
||||
panel.post("/test-notice", async (c) => {
|
||||
if (!mailReady()) {
|
||||
return c.json(
|
||||
{ error: "Mail isn't set up on the server: GOOGLE_SERVICE_ACCOUNT and GOOGLE_MAIL_FROM (docs/google-drive-and-mail.md)." },
|
||||
400,
|
||||
);
|
||||
}
|
||||
const body = (await c.req.json().catch(() => null)) ?? {};
|
||||
const to = addresses(body.to).slice(0, 10);
|
||||
if (to.length === 0) return c.json({ error: "Enter at least one email address first." }, 422);
|
||||
|
||||
const from = process.env.GOOGLE_MAIL_FROM;
|
||||
try {
|
||||
await sendMail({
|
||||
to,
|
||||
subject: "Test notice from the NGU website",
|
||||
text: [
|
||||
"This is a test from Panel → Site settings → Notices.",
|
||||
"",
|
||||
`Sent by ${c.get("user").email}, from ${from}.`,
|
||||
"New content requests and data requests will arrive like this.",
|
||||
"",
|
||||
].join("\n"),
|
||||
});
|
||||
} catch (err) {
|
||||
console.warn(`test notice by ${c.get("user").email} failed: ${err.message}`);
|
||||
return c.json({ error: `Google refused it: ${err.message}` }, 502);
|
||||
}
|
||||
console.log(`test notice sent by ${c.get("user").email} to ${to.length}`);
|
||||
return c.json({ sent: to, from }, 200, NO_STORE);
|
||||
});
|
||||
|
||||
export default panel;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue