Claim each print follow-up before it is sent, and copy launch counts from existing Rules.
This commit is contained in:
1 parent
d9c475a967
commit
214da262df
5 files changed
+108
-21
No files matched your search
@@ -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
|
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
|
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
|
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
|
links. Guest Rules with no account are skipped until claimed. Rules
|
||||||
Rules finished in the last seven days are considered so a first run after
|
finished more than seven days before this mail existed are closed in the
|
||||||
deploy does not mail every historic Rule.
|
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:
|
Manual run:
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import { sendRuleFollowUpEmail } from "./mail";
|
|||||||
import { ruleExportToken } from "./ruleExportToken";
|
import { ruleExportToken } from "./ruleExportToken";
|
||||||
|
|
||||||
export const RULE_FOLLOW_UP_AFTER_MS = 48 * 60 * 60 * 1000;
|
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 = {
|
export type RuleFollowUpResult = {
|
||||||
sent: number;
|
sent: number;
|
||||||
@@ -34,13 +33,15 @@ export async function sendDueRuleFollowUpEmails(
|
|||||||
|
|
||||||
const origin = originFromHost(null);
|
const origin = originFromHost(null);
|
||||||
const dueBy = new Date(now.getTime() - RULE_FOLLOW_UP_AFTER_MS);
|
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({
|
const rows = await db.publishedRule.findMany({
|
||||||
where: {
|
where: {
|
||||||
followUpEmailSentAt: null,
|
followUpEmailSentAt: null,
|
||||||
userId: { not: null },
|
userId: { not: null },
|
||||||
createdAt: { lte: dueBy, gte: notBefore },
|
createdAt: { lte: dueBy },
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
@@ -56,6 +57,15 @@ export async function sendDueRuleFollowUpEmails(
|
|||||||
continue;
|
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);
|
const token = ruleExportToken(row.id, pepper);
|
||||||
try {
|
try {
|
||||||
await sendRuleFollowUpEmail({
|
await sendRuleFollowUpEmail({
|
||||||
@@ -65,10 +75,6 @@ export async function sendDueRuleFollowUpEmails(
|
|||||||
pdfUrl: ruleExportPdfUrl(origin, row.id, token),
|
pdfUrl: ruleExportPdfUrl(origin, row.id, token),
|
||||||
markdownUrl: ruleExportMarkdownUrl(origin, row.id, token),
|
markdownUrl: ruleExportMarkdownUrl(origin, row.id, token),
|
||||||
});
|
});
|
||||||
await db.publishedRule.update({
|
|
||||||
where: { id: row.id },
|
|
||||||
data: { followUpEmailSentAt: now },
|
|
||||||
});
|
|
||||||
result.sent += 1;
|
result.sent += 1;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
result.failed += 1;
|
result.failed += 1;
|
||||||
@@ -76,6 +82,20 @@ export async function sendDueRuleFollowUpEmails(
|
|||||||
scope: "ruleFollowUp",
|
scope: "ruleFollowUp",
|
||||||
message: err instanceof Error ? err.message : "send failed",
|
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 "orgType" TEXT;
|
||||||
ALTER TABLE "PublishedRule" ADD COLUMN "followUpEmailSentAt" TIMESTAMP(3);
|
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
|
-- CreateIndex
|
||||||
CREATE INDEX "PublishedRule_orgType_communityLocation_idx" ON "PublishedRule"("orgType", "communityLocation");
|
CREATE INDEX "PublishedRule_orgType_communityLocation_idx" ON "PublishedRule"("orgType", "communityLocation");
|
||||||
|
|
||||||
|
|||||||
@@ -98,7 +98,8 @@ model PublishedRule {
|
|||||||
communityLocation String?
|
communityLocation String?
|
||||||
/// Canonical group type id (`workersCoop`, `mutualAid`, …). Used for launch counts.
|
/// Canonical group type id (`workersCoop`, `mutualAid`, …). Used for launch counts.
|
||||||
orgType String?
|
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?
|
followUpEmailSentAt DateTime?
|
||||||
|
|
||||||
stakeholders RuleStakeholder[]
|
stakeholders RuleStakeholder[]
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
|
|||||||
const getSessionPepperMock = vi.fn();
|
const getSessionPepperMock = vi.fn();
|
||||||
const sendFollowUpMock = vi.fn();
|
const sendFollowUpMock = vi.fn();
|
||||||
const findManyMock = vi.fn();
|
const findManyMock = vi.fn();
|
||||||
const updateMock = vi.fn();
|
const updateManyMock = vi.fn();
|
||||||
const originFromHostMock = vi.fn();
|
const originFromHostMock = vi.fn();
|
||||||
|
|
||||||
vi.mock("../../lib/server/env", () => ({
|
vi.mock("../../lib/server/env", () => ({
|
||||||
@@ -27,7 +27,7 @@ import { sendDueRuleFollowUpEmails } from "../../lib/server/ruleFollowUp";
|
|||||||
const db = {
|
const db = {
|
||||||
publishedRule: {
|
publishedRule: {
|
||||||
findMany: (...args: unknown[]) => findManyMock(...args),
|
findMany: (...args: unknown[]) => findManyMock(...args),
|
||||||
update: (...args: unknown[]) => updateMock(...args),
|
updateMany: (...args: unknown[]) => updateManyMock(...args),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -35,12 +35,12 @@ beforeEach(() => {
|
|||||||
getSessionPepperMock.mockReset();
|
getSessionPepperMock.mockReset();
|
||||||
sendFollowUpMock.mockReset();
|
sendFollowUpMock.mockReset();
|
||||||
findManyMock.mockReset();
|
findManyMock.mockReset();
|
||||||
updateMock.mockReset();
|
updateManyMock.mockReset();
|
||||||
originFromHostMock.mockReset();
|
originFromHostMock.mockReset();
|
||||||
getSessionPepperMock.mockReturnValue("test-pepper-16chars");
|
getSessionPepperMock.mockReturnValue("test-pepper-16chars");
|
||||||
originFromHostMock.mockReturnValue("https://staging.communityrule.info");
|
originFromHostMock.mockReturnValue("https://staging.communityrule.info");
|
||||||
sendFollowUpMock.mockResolvedValue(undefined);
|
sendFollowUpMock.mockResolvedValue(undefined);
|
||||||
updateMock.mockResolvedValue({});
|
updateManyMock.mockResolvedValue({ count: 1 });
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("sendDueRuleFollowUpEmails", () => {
|
describe("sendDueRuleFollowUpEmails", () => {
|
||||||
@@ -67,10 +67,20 @@ describe("sendDueRuleFollowUpEmails", () => {
|
|||||||
markdownUrl: expect.stringContaining("/export/md?t="),
|
markdownUrl: expect.stringContaining("/export/md?t="),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
expect(updateMock).toHaveBeenCalledWith({
|
expect(updateManyMock).toHaveBeenCalledTimes(1);
|
||||||
where: { id: "rule-1" },
|
expect(updateManyMock).toHaveBeenCalledWith({
|
||||||
|
where: { id: "rule-1", followUpEmailSentAt: null },
|
||||||
data: { followUpEmailSentAt: now },
|
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 () => {
|
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);
|
const result = await sendDueRuleFollowUpEmails(db as never);
|
||||||
expect(result.skipped).toBe(1);
|
expect(result.skipped).toBe(1);
|
||||||
expect(sendFollowUpMock).not.toHaveBeenCalled();
|
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([
|
findManyMock.mockResolvedValueOnce([
|
||||||
{
|
{
|
||||||
id: "rule-3",
|
id: "rule-3",
|
||||||
@@ -92,8 +116,17 @@ describe("sendDueRuleFollowUpEmails", () => {
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
sendFollowUpMock.mockRejectedValueOnce(new Error("smtp down"));
|
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(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 },
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user