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.
90 lines
2.4 KiB
TypeScript
90 lines
2.4 KiB
TypeScript
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;
|
|
}
|