push to maple-street

This commit is contained in:
Tiel Coder
2026-11-20 04:07:02 +00:00
parent 2cec220a24
commit 5adf1cef1f
345 changed files with 86105 additions and 1079 deletions
+109
View File
@@ -0,0 +1,109 @@
import { NextRequest } from "next/server";
import { z } from "zod";
import { gateOrUser } from "@/auth/api";
import { db } from "@/db";
import { writeAudit } from "@/db/audit";
import {
getEmailSettings,
saveEmailSettings,
type EmailSettings,
} from "@/db/settings";
import { normalizeAppPassword } from "@/lib/secret";
const JSON_HEADERS = { "content-type": "application/json" };
const settingsSchema = z.object({
emailFrom: z.string().email(),
notificationEmail: z.string().email().or(z.literal("")),
/** Empty string disconnects Gmail; omitted leaves the stored credentials. */
smtpUser: z.union([z.literal(""), z.string().email()]).optional(),
/** Omitted/blank keeps the stored app password. */
smtpAppPassword: z.string().max(200).optional(),
});
/** What goes in the audit trail — never the password. */
function summary(settings: EmailSettings) {
return {
emailFrom: settings.emailFrom,
notificationEmail: settings.notificationEmail,
smtpUser: settings.smtpUser,
smtpConfigured: settings.smtpConfigured,
};
}
const bad = (message: string, status = 400) =>
new Response(JSON.stringify({ error: message }), {
status,
headers: JSON_HEADERS,
});
/**
* GET /api/dev/settings — email identity, and whether a Gmail account is
* connected (the app password itself is never returned).
*/
export const GET = async () => {
const gate = await gateOrUser("admin.settings");
if (gate instanceof Response) return gate;
return new Response(JSON.stringify({ settings: await getEmailSettings() }), {
status: 200,
headers: JSON_HEADERS,
});
};
/**
* PUT /api/dev/settings
* { emailFrom, notificationEmail, smtpUser?, smtpAppPassword? }
*
* Saves the sender identity and, when given, the Gmail credentials. A blank
* app password keeps the stored one; `smtpUser: ""` disconnects Gmail and
* deletes both credential rows. Every save writes an audit row with the
* address but never the secret.
*/
export const PUT = async (req: NextRequest) => {
const gate = await gateOrUser("admin.settings");
if (gate instanceof Response) return gate;
const body = await req.json().catch(() => null);
const parsed = settingsSchema.safeParse(body);
if (!parsed.success) return bad(parsed.error.message);
const before = await getEmailSettings();
const smtpUser = parsed.data.smtpUser;
const password = normalizeAppPassword(parsed.data.smtpAppPassword ?? "");
if (smtpUser !== undefined && smtpUser !== "") {
if (password !== "" && password.length < 8) {
return bad(
"That does not look like a Google app password — it should be 16 characters.",
);
}
if (password === "" && !before.smtpConfigured) {
return bad("Enter the 16-character app password for this Gmail account.");
}
if (password === "" && smtpUser !== before.smtpUser) {
return bad("Enter the app password for the new Gmail account.");
}
}
const settings = await saveEmailSettings({
emailFrom: parsed.data.emailFrom,
notificationEmail: parsed.data.notificationEmail,
smtpUser,
smtpAppPassword: password,
});
await writeAudit(db, {
actorId: gate.id,
action: "settings.email.updated",
entityType: "app_settings",
entityId: "email",
before: summary(before),
after: summary(settings),
});
return new Response(JSON.stringify({ ok: true, settings }), {
status: 200,
headers: JSON_HEADERS,
});
};