mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 16:23:06 +10:00
This PR is handles the changes required to support envelopes. The new envelope editor/signing page will be hidden during release. The core changes here is to migrate the documents and templates model to a centralized envelopes model. Even though Documents and Templates are removed, from the user perspective they will still exist as we remap envelopes to documents and templates.
94 lines
2.4 KiB
TypeScript
94 lines
2.4 KiB
TypeScript
import { createElement } from 'react';
|
|
|
|
import { msg } from '@lingui/core/macro';
|
|
|
|
import { mailer } from '@documenso/email/mailer';
|
|
import { DocumentSuperDeleteEmailTemplate } from '@documenso/email/templates/document-super-delete';
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
import { getI18nInstance } from '../../client-only/providers/i18n-server';
|
|
import { NEXT_PUBLIC_WEBAPP_URL } from '../../constants/app';
|
|
import { AppError, AppErrorCode } from '../../errors/app-error';
|
|
import { extractDerivedDocumentEmailSettings } from '../../types/document-email';
|
|
import { renderEmailWithI18N } from '../../utils/render-email-with-i18n';
|
|
import { getEmailContext } from '../email/get-email-context';
|
|
|
|
export interface SendDeleteEmailOptions {
|
|
envelopeId: string;
|
|
reason: string;
|
|
}
|
|
|
|
// Note: Currently only sent by Admin function
|
|
export const sendDeleteEmail = async ({ envelopeId, reason }: SendDeleteEmailOptions) => {
|
|
const envelope = await prisma.envelope.findFirst({
|
|
where: {
|
|
id: envelopeId,
|
|
},
|
|
include: {
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
email: true,
|
|
name: true,
|
|
},
|
|
},
|
|
documentMeta: true,
|
|
},
|
|
});
|
|
|
|
if (!envelope) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
message: 'Document not found',
|
|
});
|
|
}
|
|
|
|
const isDocumentDeletedEmailEnabled = extractDerivedDocumentEmailSettings(
|
|
envelope.documentMeta,
|
|
).documentDeleted;
|
|
|
|
if (!isDocumentDeletedEmailEnabled) {
|
|
return;
|
|
}
|
|
|
|
const { branding, emailLanguage, senderEmail } = await getEmailContext({
|
|
emailType: 'INTERNAL',
|
|
source: {
|
|
type: 'team',
|
|
teamId: envelope.teamId,
|
|
},
|
|
meta: envelope.documentMeta,
|
|
});
|
|
|
|
const { email, name } = envelope.user;
|
|
|
|
const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
|
|
|
|
const template = createElement(DocumentSuperDeleteEmailTemplate, {
|
|
documentName: envelope.title,
|
|
reason,
|
|
assetBaseUrl,
|
|
});
|
|
|
|
const [html, text] = await Promise.all([
|
|
renderEmailWithI18N(template, { lang: emailLanguage, branding }),
|
|
renderEmailWithI18N(template, {
|
|
lang: emailLanguage,
|
|
branding,
|
|
plainText: true,
|
|
}),
|
|
]);
|
|
|
|
const i18n = await getI18nInstance(emailLanguage);
|
|
|
|
await mailer.sendMail({
|
|
to: {
|
|
address: email,
|
|
name: name || '',
|
|
},
|
|
from: senderEmail,
|
|
subject: i18n._(msg`Document Deleted!`),
|
|
html,
|
|
text,
|
|
});
|
|
};
|