Files
documenso/packages/lib/jobs/definitions/internal/expire-recipients-sweep.handler.ts
T
2026-05-08 16:04:22 +10:00

48 lines
1.3 KiB
TypeScript

import { prisma } from '@documenso/prisma';
import { DocumentStatus, SigningStatus } from '@prisma/client';
import { jobs } from '../../client';
import type { JobRunIO } from '../../client/_internal/job';
import type { TExpireRecipientsSweepJobDefinition } from './expire-recipients-sweep';
export const run = async ({ io }: { payload: TExpireRecipientsSweepJobDefinition; io: JobRunIO }) => {
const now = new Date();
const expiredRecipients = await prisma.recipient.findMany({
where: {
expiresAt: {
lte: now,
},
expirationNotifiedAt: null,
signingStatus: {
notIn: [SigningStatus.SIGNED, SigningStatus.REJECTED],
},
envelope: {
status: DocumentStatus.PENDING,
},
},
select: {
id: true,
},
take: 1000, // Limit to 1000 to avoid long-running jobs. Will be picked up in the next run if there are more.
});
if (expiredRecipients.length === 0) {
io.logger.info('No expired recipients found');
return;
}
io.logger.info(`Found ${expiredRecipients.length} expired recipients`);
await Promise.allSettled(
expiredRecipients.map(async (recipient) => {
await jobs.triggerJob({
name: 'internal.process-recipient-expired',
payload: {
recipientId: recipient.id,
},
});
}),
);
};