feat: update reminder email jobs to support multiple intervals; remove monthly and weekly jobs

This commit is contained in:
Ephraim Atta-Duncan
2025-04-15 09:37:37 +00:00
parent 7c234edf87
commit 8343f03bd2
5 changed files with 21 additions and 59 deletions

View File

@ -18,7 +18,13 @@ export const SEND_DAILY_REMINDER_EMAIL_JOB = {
await handler.run({ await handler.run({
io, io,
interval: DocumentReminderInterval.DAILY, intervals: [
DocumentReminderInterval.DAILY,
DocumentReminderInterval.EVERY_3_DAYS,
DocumentReminderInterval.WEEKLY,
DocumentReminderInterval.EVERY_2_WEEKS,
DocumentReminderInterval.MONTHLY,
],
}); });
}, },
} as const satisfies JobDefinition<typeof SEND_DAILY_REMINDER_EMAIL_JOB_ID>; } as const satisfies JobDefinition<typeof SEND_DAILY_REMINDER_EMAIL_JOB_ID>;

View File

@ -10,8 +10,7 @@ export const SEND_HOURLY_REMINDER_EMAIL_JOB = {
version: '1.0.0', version: '1.0.0',
trigger: { trigger: {
type: 'cron', type: 'cron',
// schedule: '0 * * * *', schedule: '0 * * * *',
schedule: '*/2 * * * *',
name: SEND_HOURLY_REMINDER_EMAIL_JOB_ID, name: SEND_HOURLY_REMINDER_EMAIL_JOB_ID,
}, },
handler: async ({ io }) => { handler: async ({ io }) => {
@ -19,7 +18,11 @@ export const SEND_HOURLY_REMINDER_EMAIL_JOB = {
await handler.run({ await handler.run({
io, io,
interval: DocumentReminderInterval.EVERY_1_HOUR, intervals: [
DocumentReminderInterval.EVERY_1_HOUR,
DocumentReminderInterval.EVERY_6_HOURS,
DocumentReminderInterval.EVERY_12_HOURS,
],
}); });
}, },
} as const satisfies JobDefinition<typeof SEND_HOURLY_REMINDER_EMAIL_JOB_ID>; } as const satisfies JobDefinition<typeof SEND_HOURLY_REMINDER_EMAIL_JOB_ID>;

View File

@ -1,24 +0,0 @@
import { DocumentReminderInterval } from '@documenso/prisma/client';
import type { JobDefinition } from '../../client/_internal/job';
const SEND_MONTHLY_REMINDER_EMAIL_JOB_ID = 'send.monthly.reminder.email';
export const SEND_MONTHLY_REMINDER_EMAIL_JOB = {
id: SEND_MONTHLY_REMINDER_EMAIL_JOB_ID,
name: 'Send Monthly Reminder Email',
version: '1.0.0',
trigger: {
type: 'cron',
schedule: '0 0 1 * *',
name: SEND_MONTHLY_REMINDER_EMAIL_JOB_ID,
},
handler: async ({ io }) => {
const handler = await import('./send-reminder.handler');
await handler.run({
io,
interval: DocumentReminderInterval.MONTHLY,
});
},
} as const satisfies JobDefinition<typeof SEND_MONTHLY_REMINDER_EMAIL_JOB_ID>;

View File

@ -21,21 +21,22 @@ import type { JobRunIO } from '../../client/_internal/job';
export type SendReminderHandlerOptions = { export type SendReminderHandlerOptions = {
io: JobRunIO; io: JobRunIO;
interval: DocumentReminderInterval; intervals: DocumentReminderInterval[];
}; };
export async function run({ io, interval }: SendReminderHandlerOptions) { export async function run({ io, intervals }: SendReminderHandlerOptions) {
const now = new Date(); const now = new Date();
const intervalsString = intervals.join(',').toLowerCase();
const documentsToSendReminders = await io.runTask( const documentsToSendReminders = await io.runTask(
`find-documents-for-${interval.toLocaleUpperCase()}-reminder`, `find-documents-for-${intervalsString}-reminder`,
async () => { async () => {
const documents = await prisma.document.findMany({ const documents = await prisma.document.findMany({
where: { where: {
status: DocumentStatus.PENDING, status: DocumentStatus.PENDING,
documentMeta: { documentMeta: {
reminderInterval: { reminderInterval: {
equals: interval, in: intervals,
}, },
}, },
deletedAt: null, deletedAt: null,
@ -72,7 +73,7 @@ export async function run({ io, interval }: SendReminderHandlerOptions) {
}); });
io.logger.info( io.logger.info(
`Found ${filteredDocuments.length} documents after filtering for interval ${interval}.`, `Found ${filteredDocuments.length} documents after filtering for interval ${intervalsString}.`,
filteredDocuments.map((d) => ({ id: d.id })), filteredDocuments.map((d) => ({ id: d.id })),
); );
@ -81,12 +82,12 @@ export async function run({ io, interval }: SendReminderHandlerOptions) {
); );
if (documentsToSendReminders.length === 0) { if (documentsToSendReminders.length === 0) {
io.logger.info(`No documents found needing ${interval.toLocaleUpperCase()} reminders.`); io.logger.info(`No documents found needing ${intervalsString} reminders.`);
return; return;
} }
io.logger.info( io.logger.info(
`Found ${documentsToSendReminders.length} documents needing ${interval.toLocaleUpperCase()} reminders.`, `Found ${documentsToSendReminders.length} documents needing ${intervalsString} reminders.`,
); );
for (const document of documentsToSendReminders) { for (const document of documentsToSendReminders) {

View File

@ -1,24 +0,0 @@
import { DocumentReminderInterval } from '@documenso/prisma/client';
import type { JobDefinition } from '../../client/_internal/job';
const SEND_WEEKLY_REMINDER_EMAIL_JOB_ID = 'send.weekly.reminder.email';
export const SEND_WEEKLY_REMINDER_EMAIL_JOB = {
id: SEND_WEEKLY_REMINDER_EMAIL_JOB_ID,
name: 'Send Weekly Reminder Email',
version: '1.0.0',
trigger: {
type: 'cron',
schedule: '0 0 * * 0',
name: SEND_WEEKLY_REMINDER_EMAIL_JOB_ID,
},
handler: async ({ io }) => {
const handler = await import('./send-reminder.handler');
await handler.run({
io,
interval: DocumentReminderInterval.WEEKLY,
});
},
} as const satisfies JobDefinition<typeof SEND_WEEKLY_REMINDER_EMAIL_JOB_ID>;