Claim each print follow-up before it is sent, and copy launch counts from existing Rules.

This commit is contained in:
adilallo committed 2026-11-10 19:07:36 -07:00
1 parent d9c475a967
commit 214da262df
5 files changed
+108 -21

No files matched your search

+6 -3
View File
@@ -326,9 +326,12 @@ command under the app **Cron** tab (`20 7 * * *`, UTC).
The same scheduler addon also runs **`follow-up-mail`** at **15 minutes
past every hour**. It sends one print follow-up to the address that holds
the pen, 48 hours after the Rule was finished, with PDF and RULE.md
links. Guest Rules with no account are skipped until claimed, and only
Rules finished in the last seven days are considered so a first run after
deploy does not mail every historic Rule.
links. Guest Rules with no account are skipped until claimed. Rules
finished more than seven days before this mail existed are closed in the
migration and are not sent. A Rule that is already due stays eligible
until the job claims it, including after a scheduler outage longer than
seven days. The claim is written before the message is sent, and cleared
again if sending fails, so overlapping runs cannot deliver it twice.
Manual run:
+27 -7
View File
@@ -7,7 +7,6 @@ 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;
@@ -34,13 +33,15 @@ export async function sendDueRuleFollowUpEmails(
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);
// Rules finished more than seven days before this mail existed are stamped
// in the migration. Anything still unsent stays eligible until claimed, so
// a long scheduler outage does not drop the follow-up.
const rows = await db.publishedRule.findMany({
where: {
followUpEmailSentAt: null,
userId: { not: null },
createdAt: { lte: dueBy, gte: notBefore },
createdAt: { lte: dueBy },
},
select: {
id: true,
@@ -56,6 +57,15 @@ export async function sendDueRuleFollowUpEmails(
continue;
}
const claimed = await db.publishedRule.updateMany({
where: { id: row.id, followUpEmailSentAt: null },
data: { followUpEmailSentAt: now },
});
if (claimed.count !== 1) {
result.skipped += 1;
continue;
}
const token = ruleExportToken(row.id, pepper);
try {
await sendRuleFollowUpEmail({
@@ -65,10 +75,6 @@ export async function sendDueRuleFollowUpEmails(
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;
@@ -76,6 +82,20 @@ export async function sendDueRuleFollowUpEmails(
scope: "ruleFollowUp",
message: err instanceof Error ? err.message : "send failed",
});
try {
await db.publishedRule.updateMany({
where: { id: row.id, followUpEmailSentAt: now },
data: { followUpEmailSentAt: null },
});
} catch (releaseErr) {
logger.error({
scope: "ruleFollowUp",
message:
releaseErr instanceof Error
? releaseErr.message
: "claim release failed",
});
}
}
}
@@ -3,6 +3,36 @@ ALTER TABLE "PublishedRule" ADD COLUMN "communityLocation" TEXT;
ALTER TABLE "PublishedRule" ADD COLUMN "orgType" TEXT;
ALTER TABLE "PublishedRule" ADD COLUMN "followUpEmailSentAt" TIMESTAMP(3);
-- Copy place and group type already stored on the published document.
UPDATE "PublishedRule"
SET
"communityLocation" = LEFT(
NULLIF(
BTRIM(REGEXP_REPLACE(COALESCE("document"->>'communityLocation', ''), '[[:space:]]+', ' ', 'g')),
''
),
200
),
"orgType" = CASE
WHEN BTRIM(COALESCE("document"->>'orgType', '')) IN (
'dao',
'forProfit',
'mutualAid',
'nonprofit',
'openSource',
'workersCoop'
)
THEN BTRIM("document"->>'orgType')
ELSE NULL
END
WHERE jsonb_typeof("document") = 'object';
-- Close follow-ups for Rules finished more than seven days before this mail
-- existed. Newer unsent rows stay eligible until a run claims them.
UPDATE "PublishedRule"
SET "followUpEmailSentAt" = CURRENT_TIMESTAMP
WHERE "createdAt" < CURRENT_TIMESTAMP - INTERVAL '7 days';
-- CreateIndex
CREATE INDEX "PublishedRule_orgType_communityLocation_idx" ON "PublishedRule"("orgType", "communityLocation");
+2 -1
View File
@@ -98,7 +98,8 @@ model PublishedRule {
communityLocation String?
/// Canonical group type id (`workersCoop`, `mutualAid`, …). Used for launch counts.
orgType String?
/// Set when the 48-hour print follow-up has been sent to the pen-holder.
/// Set when the 48-hour print follow-up has been sent, or when a Rule
/// finished more than seven days before this mail existed and will not be sent.
followUpEmailSentAt DateTime?
stakeholders RuleStakeholder[]
+43 -10
View File
@@ -3,7 +3,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
const getSessionPepperMock = vi.fn();
const sendFollowUpMock = vi.fn();
const findManyMock = vi.fn();
const updateMock = vi.fn();
const updateManyMock = vi.fn();
const originFromHostMock = vi.fn();
vi.mock("../../lib/server/env", () => ({
@@ -27,7 +27,7 @@ import { sendDueRuleFollowUpEmails } from "../../lib/server/ruleFollowUp";
const db = {
publishedRule: {
findMany: (...args: unknown[]) => findManyMock(...args),
update: (...args: unknown[]) => updateMock(...args),
updateMany: (...args: unknown[]) => updateManyMock(...args),
},
};
@@ -35,12 +35,12 @@ beforeEach(() => {
getSessionPepperMock.mockReset();
sendFollowUpMock.mockReset();
findManyMock.mockReset();
updateMock.mockReset();
updateManyMock.mockReset();
originFromHostMock.mockReset();
getSessionPepperMock.mockReturnValue("test-pepper-16chars");
originFromHostMock.mockReturnValue("https://staging.communityrule.info");
sendFollowUpMock.mockResolvedValue(undefined);
updateMock.mockResolvedValue({});
updateManyMock.mockResolvedValue({ count: 1 });
});
describe("sendDueRuleFollowUpEmails", () => {
@@ -67,10 +67,20 @@ describe("sendDueRuleFollowUpEmails", () => {
markdownUrl: expect.stringContaining("/export/md?t="),
}),
);
expect(updateMock).toHaveBeenCalledWith({
where: { id: "rule-1" },
expect(updateManyMock).toHaveBeenCalledTimes(1);
expect(updateManyMock).toHaveBeenCalledWith({
where: { id: "rule-1", followUpEmailSentAt: null },
data: { followUpEmailSentAt: now },
});
expect(findManyMock).toHaveBeenCalledWith(
expect.objectContaining({
where: {
followUpEmailSentAt: null,
userId: { not: null },
createdAt: { lte: new Date("2026-11-08T12:00:00.000Z") },
},
}),
);
});
it("skips rows with no mailbox and does not mark them sent", async () => {
@@ -80,10 +90,24 @@ describe("sendDueRuleFollowUpEmails", () => {
const result = await sendDueRuleFollowUpEmails(db as never);
expect(result.skipped).toBe(1);
expect(sendFollowUpMock).not.toHaveBeenCalled();
expect(updateMock).not.toHaveBeenCalled();
expect(updateManyMock).not.toHaveBeenCalled();
});
it("leaves sentAt unset when mail fails so the hourly job can retry", async () => {
it("does not send when another run already claimed the row", async () => {
findManyMock.mockResolvedValueOnce([
{
id: "rule-4",
title: "Race",
user: { email: "pen@example.com" },
},
]);
updateManyMock.mockResolvedValueOnce({ count: 0 });
const result = await sendDueRuleFollowUpEmails(db as never);
expect(result).toEqual({ sent: 0, failed: 0, skipped: 1 });
expect(sendFollowUpMock).not.toHaveBeenCalled();
});
it("clears the claim when mail fails so the hourly job can retry", async () => {
findManyMock.mockResolvedValueOnce([
{
id: "rule-3",
@@ -92,8 +116,17 @@ describe("sendDueRuleFollowUpEmails", () => {
},
]);
sendFollowUpMock.mockRejectedValueOnce(new Error("smtp down"));
const result = await sendDueRuleFollowUpEmails(db as never);
const now = new Date("2026-11-10T12:00:00.000Z");
const result = await sendDueRuleFollowUpEmails(db as never, now);
expect(result.failed).toBe(1);
expect(updateMock).not.toHaveBeenCalled();
expect(sendFollowUpMock).toHaveBeenCalledTimes(1);
expect(updateManyMock).toHaveBeenNthCalledWith(1, {
where: { id: "rule-3", followUpEmailSentAt: null },
data: { followUpEmailSentAt: now },
});
expect(updateManyMock).toHaveBeenNthCalledWith(2, {
where: { id: "rule-3", followUpEmailSentAt: now },
data: { followUpEmailSentAt: null },
});
});
});