102 lines
2.7 KiB
TypeScript
102 lines
2.7 KiB
TypeScript
import { createElement } from 'react';
|
|
|
|
import { msg } from '@lingui/core/macro';
|
|
|
|
import { mailer } from '@documenso/email/mailer';
|
|
import { DocumentPendingEmailTemplate } from '@documenso/email/templates/document-pending';
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
import { getI18nInstance } from '../../client-only/providers/i18n-server';
|
|
import { NEXT_PUBLIC_WEBAPP_URL } from '../../constants/app';
|
|
import { extractDerivedDocumentEmailSettings } from '../../types/document-email';
|
|
import { env } from '../../utils/env';
|
|
import { renderEmailWithI18N } from '../../utils/render-email-with-i18n';
|
|
import { teamGlobalSettingsToBranding } from '../../utils/team-global-settings-to-branding';
|
|
|
|
export interface SendPendingEmailOptions {
|
|
documentId: number;
|
|
recipientId: number;
|
|
}
|
|
|
|
export const sendPendingEmail = async ({ documentId, recipientId }: SendPendingEmailOptions) => {
|
|
const document = await prisma.document.findFirst({
|
|
where: {
|
|
id: documentId,
|
|
recipients: {
|
|
some: {
|
|
id: recipientId,
|
|
},
|
|
},
|
|
},
|
|
include: {
|
|
recipients: {
|
|
where: {
|
|
id: recipientId,
|
|
},
|
|
},
|
|
documentMeta: true,
|
|
team: {
|
|
include: {
|
|
teamGlobalSettings: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!document) {
|
|
throw new Error('Document not found');
|
|
}
|
|
|
|
if (document.recipients.length === 0) {
|
|
throw new Error('Document has no recipients');
|
|
}
|
|
|
|
const isDocumentPendingEmailEnabled = extractDerivedDocumentEmailSettings(
|
|
document.documentMeta,
|
|
).documentPending;
|
|
|
|
if (!isDocumentPendingEmailEnabled) {
|
|
return;
|
|
}
|
|
|
|
const [recipient] = document.recipients;
|
|
|
|
const { email, name } = recipient;
|
|
|
|
const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
|
|
|
|
const template = createElement(DocumentPendingEmailTemplate, {
|
|
documentName: document.title,
|
|
assetBaseUrl,
|
|
});
|
|
|
|
const branding = document.team?.teamGlobalSettings
|
|
? teamGlobalSettingsToBranding(document.team.teamGlobalSettings)
|
|
: undefined;
|
|
|
|
const [html, text] = await Promise.all([
|
|
renderEmailWithI18N(template, { lang: document.documentMeta?.language, branding }),
|
|
renderEmailWithI18N(template, {
|
|
lang: document.documentMeta?.language,
|
|
branding,
|
|
plainText: true,
|
|
}),
|
|
]);
|
|
|
|
const i18n = await getI18nInstance(document.documentMeta?.language);
|
|
|
|
await mailer.sendMail({
|
|
to: {
|
|
address: email,
|
|
name,
|
|
},
|
|
from: {
|
|
name: env('NEXT_PRIVATE_SMTP_FROM_NAME') || 'Documenso',
|
|
address: env('NEXT_PRIVATE_SMTP_FROM_ADDRESS') || 'noreply@sign.bls.media',
|
|
},
|
|
subject: i18n._(msg`Waiting for others to complete signing.`),
|
|
html,
|
|
text,
|
|
});
|
|
};
|