Give finished Rules a print path, folded launch counts, and a two-day follow-up.
Export is PDF, RULE.md, and Print on US Letter, with every shared term, no member names, and a takeaway that points at Ask an organizer. Finished Rules keep type and place so the team can read folded counts without a database, Umami can sit beside the app with no cookies, and the person who holds the pen gets working PDF and RULE.md links 48 hours later.
This commit is contained in:
1 parent
0efc907633
commit
d9c475a967
74 files changed
+2401
-630
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { apiRoute } from "../../../../lib/server/apiRoute";
|
||||
import { prisma } from "../../../../lib/server/db";
|
||||
import {
|
||||
getOpsPruneToken,
|
||||
getRuleAdminEmails,
|
||||
isDatabaseConfigured,
|
||||
} from "../../../../lib/server/env";
|
||||
import {
|
||||
dbUnavailable,
|
||||
notFound,
|
||||
unauthorized,
|
||||
} from "../../../../lib/server/responses";
|
||||
import { isRuleAdminEmail } from "../../../../lib/server/ruleAdmins";
|
||||
import { aggregateRuleLaunchCounts } from "../../../../lib/server/ruleLaunchCounts";
|
||||
import { getSessionUser } from "../../../../lib/server/session";
|
||||
|
||||
export const GET = apiRoute("ops.ruleCounts", async (request: NextRequest) => {
|
||||
if (getRuleAdminEmails().length === 0 && !getOpsPruneToken()) {
|
||||
return notFound();
|
||||
}
|
||||
if (!isDatabaseConfigured()) return dbUnavailable();
|
||||
|
||||
const header = request.headers.get("authorization") ?? "";
|
||||
const opsToken = getOpsPruneToken();
|
||||
const bearerOk = Boolean(opsToken) && header === `Bearer ${opsToken}`;
|
||||
if (!bearerOk) {
|
||||
const user = await getSessionUser();
|
||||
if (!user || !isRuleAdminEmail(user.email)) return unauthorized();
|
||||
}
|
||||
|
||||
const rows = await prisma.publishedRule.findMany({
|
||||
select: { orgType: true, communityLocation: true },
|
||||
});
|
||||
const counts = aggregateRuleLaunchCounts(
|
||||
rows.map((row) => ({
|
||||
orgType: row.orgType,
|
||||
place: row.communityLocation,
|
||||
})),
|
||||
);
|
||||
return NextResponse.json(counts);
|
||||
});
|
||||
@@ -0,0 +1,112 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { apiRoute } from "../../../../../../lib/server/apiRoute";
|
||||
import { prisma } from "../../../../../../lib/server/db";
|
||||
import {
|
||||
getSessionPepper,
|
||||
isDatabaseConfigured,
|
||||
} from "../../../../../../lib/server/env";
|
||||
import { getPublicOrigin } from "../../../../../../lib/server/publicOrigin";
|
||||
import {
|
||||
dbUnavailable,
|
||||
forbidden,
|
||||
notFound,
|
||||
unauthorized,
|
||||
} from "../../../../../../lib/server/responses";
|
||||
import { isRuleAdminEmail } from "../../../../../../lib/server/ruleAdmins";
|
||||
import { ruleExportTokenMatches } from "../../../../../../lib/server/ruleExportToken";
|
||||
import { getSessionUser } from "../../../../../../lib/server/session";
|
||||
import {
|
||||
RULE_MD_FILENAME,
|
||||
buildStoredRulePdfBytes,
|
||||
exportPdfFilename,
|
||||
exportStoredRuleAsMarkdown,
|
||||
} from "../../../../../../lib/create/ruleExport";
|
||||
|
||||
type RouteContext = { params: Promise<{ id: string; format: string }> };
|
||||
|
||||
function attachmentDisposition(filename: string): string {
|
||||
const ascii = filename.replace(/[^\x20-\x7E]/g, "_").replace(/"/g, "");
|
||||
const encoded = encodeURIComponent(filename);
|
||||
return `attachment; filename="${ascii}"; filename*=UTF-8''${encoded}`;
|
||||
}
|
||||
|
||||
function documentRecord(value: unknown): Record<string, unknown> | null {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
export const GET = apiRoute<RouteContext>(
|
||||
"rules.export",
|
||||
async (request: NextRequest, context) => {
|
||||
if (!isDatabaseConfigured()) return dbUnavailable();
|
||||
|
||||
const { id, format } = await context.params;
|
||||
if (format !== "pdf" && format !== "md") return notFound();
|
||||
|
||||
const row = await prisma.publishedRule.findUnique({
|
||||
where: { id },
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
summary: true,
|
||||
document: true,
|
||||
createdAt: true,
|
||||
userId: true,
|
||||
},
|
||||
});
|
||||
if (!row) return notFound();
|
||||
|
||||
const token = request.nextUrl.searchParams.get("t") ?? "";
|
||||
let tokenOk = false;
|
||||
if (token.length > 0) {
|
||||
try {
|
||||
tokenOk = ruleExportTokenMatches(row.id, token, getSessionPepper());
|
||||
} catch {
|
||||
tokenOk = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!tokenOk) {
|
||||
const user = await getSessionUser();
|
||||
if (!user) return unauthorized();
|
||||
const isOwner = row.userId === user.id;
|
||||
if (!isOwner && !isRuleAdminEmail(user.email)) {
|
||||
return forbidden("You do not have permission to export this rule");
|
||||
}
|
||||
}
|
||||
|
||||
const document = documentRecord(row.document);
|
||||
if (!document) return notFound();
|
||||
|
||||
const origin = getPublicOrigin(request);
|
||||
const source = {
|
||||
id: row.id,
|
||||
title: row.title,
|
||||
summary: row.summary,
|
||||
document,
|
||||
finishedAt: row.createdAt,
|
||||
};
|
||||
|
||||
if (format === "pdf") {
|
||||
const bytes = buildStoredRulePdfBytes(source, origin);
|
||||
return new NextResponse(Buffer.from(bytes), {
|
||||
headers: {
|
||||
"Content-Type": "application/pdf",
|
||||
"Content-Disposition": attachmentDisposition(
|
||||
exportPdfFilename(row.title),
|
||||
),
|
||||
"Cache-Control": "private, no-store",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const markdown = exportStoredRuleAsMarkdown(source, origin);
|
||||
return new NextResponse(markdown, {
|
||||
headers: {
|
||||
"Content-Type": "text/markdown; charset=utf-8",
|
||||
"Content-Disposition": attachmentDisposition(RULE_MD_FILENAME),
|
||||
"Cache-Control": "private, no-store",
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -16,6 +16,7 @@ import { linkUploadsToRule } from "../../../../lib/server/uploads/linkUploads";
|
||||
import { publishRuleBodySchema } from "../../../../lib/server/validation/createFlowSchemas";
|
||||
import { readLimitedJson } from "../../../../lib/server/validation/requestBody";
|
||||
import { jsonFromZodError } from "../../../../lib/server/validation/zodHttp";
|
||||
import { publishedRuleAnalyticsColumns } from "../../../../lib/server/publishedRulePersist";
|
||||
|
||||
type RouteContext = { params: Promise<{ id: string }> };
|
||||
|
||||
@@ -88,6 +89,7 @@ export const PATCH = apiRoute<RouteContext>(
|
||||
title,
|
||||
summary,
|
||||
document: document as Prisma.InputJsonValue,
|
||||
...publishedRuleAnalyticsColumns(document),
|
||||
},
|
||||
});
|
||||
await linkUploadsToRule({
|
||||
|
||||
@@ -34,6 +34,7 @@ import {
|
||||
} from "../../../lib/server/validation/createFlowSchemas";
|
||||
import { readLimitedJson } from "../../../lib/server/validation/requestBody";
|
||||
import { jsonFromZodError } from "../../../lib/server/validation/zodHttp";
|
||||
import { publishedRuleAnalyticsColumns } from "../../../lib/server/publishedRulePersist";
|
||||
|
||||
/** One anonymous publish per IP per minute (in-memory limiter). */
|
||||
const ANONYMOUS_PUBLISH_IP_MIN_INTERVAL_MS = 60_000;
|
||||
@@ -113,6 +114,7 @@ export const POST = apiRoute(
|
||||
summary,
|
||||
document: document as Prisma.InputJsonValue,
|
||||
claimTokenHash,
|
||||
...publishedRuleAnalyticsColumns(document),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -160,6 +162,7 @@ export const POST = apiRoute(
|
||||
title,
|
||||
summary,
|
||||
document: document as Prisma.InputJsonValue,
|
||||
...publishedRuleAnalyticsColumns(document),
|
||||
},
|
||||
});
|
||||
await linkUploadsToRule({
|
||||
@@ -196,6 +199,7 @@ export const POST = apiRoute(
|
||||
title,
|
||||
summary,
|
||||
document: document as Prisma.InputJsonValue,
|
||||
...publishedRuleAnalyticsColumns(document),
|
||||
},
|
||||
});
|
||||
const toSend: { email: string; token: string }[] = [];
|
||||
|
||||
Reference in new issue
Block a user