push to maple-street
This commit is contained in:
@@ -1,35 +1,44 @@
|
||||
import { auth } from "@/auth/config";
|
||||
import { AuthError, requirePermission } from "@/auth/permissions";
|
||||
import type { SystemRole } from "@/auth/roles";
|
||||
import { gateOrUser } from "@/auth/api";
|
||||
import { resetAndSeed } from "@/db/seed";
|
||||
|
||||
const JSON_HEADERS = { "content-type": "application/json" };
|
||||
|
||||
/**
|
||||
* Dev console reset. Admin-only (admin.settings): truncates the seed-owned
|
||||
* tables and repopulates them, so E2E tests start from a known-clean state on
|
||||
* every run instead of accumulating the mutations of prior runs. Not wired into
|
||||
* any UI — it exists only for test harnesses.
|
||||
* Test-only database reset.
|
||||
*
|
||||
* Two gates, both required:
|
||||
* 1. `ALLOW_DEV_RESET=true` must be set explicitly. This keeps a destructive
|
||||
* truncate + reseed out of any deployment that did not ask for it.
|
||||
* 2. The caller must be a signed-in admin (`admin.settings`).
|
||||
*
|
||||
* E2E sets `ALLOW_DEV_RESET=true` in `playwright.config.ts`; production
|
||||
* compose does not.
|
||||
*/
|
||||
export const POST = async () => {
|
||||
try {
|
||||
const session = await auth();
|
||||
requirePermission(
|
||||
(session?.user as { systemRole: SystemRole } | null | undefined),
|
||||
"admin.settings",
|
||||
if (process.env.ALLOW_DEV_RESET !== "true") {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
error: "database reset is disabled in this environment",
|
||||
code: "RESET_DISABLED",
|
||||
}),
|
||||
{ status: 403, headers: JSON_HEADERS },
|
||||
);
|
||||
}
|
||||
|
||||
const gate = await gateOrUser("admin.settings");
|
||||
if (gate instanceof Response) return gate;
|
||||
|
||||
try {
|
||||
await resetAndSeed();
|
||||
return new Response(JSON.stringify({ ok: true }), {
|
||||
status: 200,
|
||||
headers: JSON_HEADERS,
|
||||
});
|
||||
} catch (e) {
|
||||
if (e instanceof AuthError) {
|
||||
return new Response(JSON.stringify({ error: e.message, code: e.code }), {
|
||||
status: e.status,
|
||||
headers: JSON_HEADERS,
|
||||
});
|
||||
}
|
||||
throw e;
|
||||
const message = e instanceof Error ? e.message : "reset failed";
|
||||
return new Response(JSON.stringify({ error: message }), {
|
||||
status: 500,
|
||||
headers: JSON_HEADERS,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user