2023-09-07 19:58:48 +00:00
|
|
|
import { createElement } from 'react';
|
|
|
|
|
|
2024-11-05 11:52:54 +11:00
|
|
|
import { msg } from '@lingui/macro';
|
|
|
|
|
|
2023-09-07 19:58:48 +00:00
|
|
|
import { mailer } from '@documenso/email/mailer';
|
|
|
|
|
import { DocumentPendingEmailTemplate } from '@documenso/email/templates/document-pending';
|
2023-09-07 20:38:18 +00:00
|
|
|
import { prisma } from '@documenso/prisma';
|
2023-09-07 19:58:48 +00:00
|
|
|
|
2024-11-05 11:52:54 +11:00
|
|
|
import { getI18nInstance } from '../../client-only/providers/i18n.server';
|
2024-01-31 22:32:42 +11:00
|
|
|
import { NEXT_PUBLIC_WEBAPP_URL } from '../../constants/app';
|
2024-11-05 11:52:54 +11:00
|
|
|
import { renderEmailWithI18N } from '../../utils/render-email-with-i18n';
|
2024-01-31 22:32:42 +11:00
|
|
|
|
2023-09-07 19:58:48 +00:00
|
|
|
export interface SendPendingEmailOptions {
|
2023-09-07 20:38:18 +00:00
|
|
|
documentId: number;
|
|
|
|
|
recipientId: number;
|
2023-09-07 19:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-07 20:38:18 +00:00
|
|
|
export const sendPendingEmail = async ({ documentId, recipientId }: SendPendingEmailOptions) => {
|
|
|
|
|
const document = await prisma.document.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
id: documentId,
|
|
|
|
|
Recipient: {
|
|
|
|
|
some: {
|
|
|
|
|
id: recipientId,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
Recipient: {
|
|
|
|
|
where: {
|
|
|
|
|
id: recipientId,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-11-05 11:52:54 +11:00
|
|
|
documentMeta: true,
|
2023-09-07 20:38:18 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!document) {
|
|
|
|
|
throw new Error('Document not found');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (document.Recipient.length === 0) {
|
|
|
|
|
throw new Error('Document has no recipients');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [recipient] = document.Recipient;
|
|
|
|
|
|
2023-09-07 19:58:48 +00:00
|
|
|
const { email, name } = recipient;
|
|
|
|
|
|
2024-01-31 22:32:42 +11:00
|
|
|
const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
|
2023-09-07 19:58:48 +00:00
|
|
|
|
|
|
|
|
const template = createElement(DocumentPendingEmailTemplate, {
|
|
|
|
|
documentName: document.title,
|
|
|
|
|
assetBaseUrl,
|
|
|
|
|
});
|
|
|
|
|
|
2024-11-05 11:52:54 +11:00
|
|
|
const [html, text] = await Promise.all([
|
|
|
|
|
renderEmailWithI18N(template, { lang: document.documentMeta?.language }),
|
|
|
|
|
renderEmailWithI18N(template, { lang: document.documentMeta?.language, plainText: true }),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const i18n = await getI18nInstance();
|
|
|
|
|
|
2023-09-07 19:58:48 +00:00
|
|
|
await mailer.sendMail({
|
|
|
|
|
to: {
|
|
|
|
|
address: email,
|
|
|
|
|
name,
|
|
|
|
|
},
|
|
|
|
|
from: {
|
|
|
|
|
name: process.env.NEXT_PRIVATE_SMTP_FROM_NAME || 'Documenso',
|
|
|
|
|
address: process.env.NEXT_PRIVATE_SMTP_FROM_ADDRESS || 'noreply@documenso.com',
|
|
|
|
|
},
|
2024-11-05 11:52:54 +11:00
|
|
|
subject: i18n._(msg`Waiting for others to complete signing.`),
|
|
|
|
|
html,
|
|
|
|
|
text,
|
2023-09-07 19:58:48 +00:00
|
|
|
});
|
|
|
|
|
};
|