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:
adilallo committed 2026-11-10 18:32:33 -07:00
1 parent 0efc907633
commit d9c475a967
74 files changed
+2401 -630

No files matched your search

+89
View File
@@ -0,0 +1,89 @@
import type { PrismaClient } from "@prisma/client";
import { logger } from "../logger";
import { originFromHost } from "../siteMetadata";
import { ruleExportMarkdownUrl, ruleExportPdfUrl } from "../sharePaths";
import { getSessionPepper } from "./env";
import { sendRuleFollowUpEmail } from "./mail";
import { ruleExportToken } from "./ruleExportToken";
export const RULE_FOLLOW_UP_AFTER_MS = 48 * 60 * 60 * 1000;
export const RULE_FOLLOW_UP_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
export type RuleFollowUpResult = {
sent: number;
failed: number;
skipped: number;
};
export async function sendDueRuleFollowUpEmails(
db: PrismaClient,
now = new Date(),
): Promise<RuleFollowUpResult> {
const result: RuleFollowUpResult = { sent: 0, failed: 0, skipped: 0 };
let pepper: string;
try {
pepper = getSessionPepper();
} catch (err) {
logger.error({
scope: "ruleFollowUp",
message: err instanceof Error ? err.message : "SESSION_SECRET missing",
});
return result;
}
const origin = originFromHost(null);
const dueBy = new Date(now.getTime() - RULE_FOLLOW_UP_AFTER_MS);
const notBefore = new Date(now.getTime() - RULE_FOLLOW_UP_MAX_AGE_MS);
const rows = await db.publishedRule.findMany({
where: {
followUpEmailSentAt: null,
userId: { not: null },
createdAt: { lte: dueBy, gte: notBefore },
},
select: {
id: true,
title: true,
user: { select: { email: true } },
},
});
for (const row of rows) {
const email = row.user?.email?.trim().toLowerCase() ?? "";
if (!email.includes("@")) {
result.skipped += 1;
continue;
}
const token = ruleExportToken(row.id, pepper);
try {
await sendRuleFollowUpEmail({
to: email,
origin,
communityName: row.title,
pdfUrl: ruleExportPdfUrl(origin, row.id, token),
markdownUrl: ruleExportMarkdownUrl(origin, row.id, token),
});
await db.publishedRule.update({
where: { id: row.id },
data: { followUpEmailSentAt: now },
});
result.sent += 1;
} catch (err) {
result.failed += 1;
logger.error({
scope: "ruleFollowUp",
message: err instanceof Error ? err.message : "send failed",
});
}
}
logger.info({
scope: "ruleFollowUp",
sent: result.sent,
failed: result.failed,
skipped: result.skipped,
});
return result;
}