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
@@ -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",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user