mirror of
https://github.com/documenso/documenso.git
synced 2025-11-15 01:01:49 +10:00
118 lines
3.6 KiB
TypeScript
118 lines
3.6 KiB
TypeScript
import { createElement } from 'react';
|
|
|
|
import { msg } from '@lingui/core/macro';
|
|
import { EnvelopeType, ReadStatus, SendStatus, SigningStatus } from '@prisma/client';
|
|
|
|
import { mailer } from '@documenso/email/mailer';
|
|
import DocumentCancelTemplate from '@documenso/email/templates/document-cancel';
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
import { getI18nInstance } from '../../../client-only/providers/i18n-server';
|
|
import { NEXT_PUBLIC_WEBAPP_URL } from '../../../constants/app';
|
|
import { getEmailContext } from '../../../server-only/email/get-email-context';
|
|
import { extractDerivedDocumentEmailSettings } from '../../../types/document-email';
|
|
import { unsafeBuildEnvelopeIdQuery } from '../../../utils/envelope';
|
|
import { renderEmailWithI18N } from '../../../utils/render-email-with-i18n';
|
|
import type { JobRunIO } from '../../client/_internal/job';
|
|
import type { TSendDocumentCancelledEmailsJobDefinition } from './send-document-cancelled-emails';
|
|
|
|
export const run = async ({
|
|
payload,
|
|
io,
|
|
}: {
|
|
payload: TSendDocumentCancelledEmailsJobDefinition;
|
|
io: JobRunIO;
|
|
}) => {
|
|
const { documentId, cancellationReason } = payload;
|
|
|
|
const envelope = await prisma.envelope.findFirstOrThrow({
|
|
where: unsafeBuildEnvelopeIdQuery(
|
|
{
|
|
type: 'documentId',
|
|
id: documentId,
|
|
},
|
|
EnvelopeType.DOCUMENT,
|
|
),
|
|
include: {
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
email: true,
|
|
name: true,
|
|
},
|
|
},
|
|
documentMeta: true,
|
|
recipients: true,
|
|
team: {
|
|
select: {
|
|
teamEmail: true,
|
|
name: true,
|
|
url: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const { branding, emailLanguage, senderEmail, replyToEmail } = await getEmailContext({
|
|
emailType: 'RECIPIENT',
|
|
source: {
|
|
type: 'team',
|
|
teamId: envelope.teamId,
|
|
},
|
|
meta: envelope.documentMeta,
|
|
});
|
|
|
|
const { documentMeta, user: documentOwner } = envelope;
|
|
|
|
// Check if document cancellation emails are enabled
|
|
const isEmailEnabled = extractDerivedDocumentEmailSettings(documentMeta).documentDeleted;
|
|
|
|
if (!isEmailEnabled) {
|
|
return;
|
|
}
|
|
|
|
const i18n = await getI18nInstance(emailLanguage);
|
|
|
|
// Send cancellation emails to all recipients who have been sent the document or viewed it
|
|
const recipientsToNotify = envelope.recipients.filter(
|
|
(recipient) =>
|
|
(recipient.sendStatus === SendStatus.SENT || recipient.readStatus === ReadStatus.OPENED) &&
|
|
recipient.signingStatus !== SigningStatus.REJECTED,
|
|
);
|
|
|
|
await io.runTask('send-cancellation-emails', async () => {
|
|
await Promise.all(
|
|
recipientsToNotify.map(async (recipient) => {
|
|
const template = createElement(DocumentCancelTemplate, {
|
|
documentName: envelope.title,
|
|
inviterName: documentOwner.name || undefined,
|
|
inviterEmail: documentOwner.email,
|
|
assetBaseUrl: NEXT_PUBLIC_WEBAPP_URL(),
|
|
cancellationReason: cancellationReason || 'The document has been cancelled.',
|
|
});
|
|
|
|
const [html, text] = await Promise.all([
|
|
renderEmailWithI18N(template, { lang: emailLanguage, branding }),
|
|
renderEmailWithI18N(template, {
|
|
lang: emailLanguage,
|
|
branding,
|
|
plainText: true,
|
|
}),
|
|
]);
|
|
|
|
await mailer.sendMail({
|
|
to: {
|
|
name: recipient.name,
|
|
address: recipient.email,
|
|
},
|
|
from: senderEmail,
|
|
replyTo: replyToEmail,
|
|
subject: i18n._(msg`Document "${envelope.title}" Cancelled`),
|
|
html,
|
|
text,
|
|
});
|
|
}),
|
|
);
|
|
});
|
|
};
|