documenso/packages/lib/server-only/document/send-delete-email.ts

93 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-03-13 05:24:53 +00:00
import { createElement } from 'react';
2025-01-02 04:33:37 +00:00
import { msg } from '@lingui/core/macro';
2024-03-13 05:24:53 +00:00
import { mailer } from '@documenso/email/mailer';
2024-03-28 07:01:57 +00:00
import { DocumentSuperDeleteEmailTemplate } from '@documenso/email/templates/document-super-delete';
2024-03-13 05:24:53 +00:00
import { prisma } from '@documenso/prisma';
2025-01-02 04:33:37 +00:00
import { getI18nInstance } from '../../client-only/providers/i18n-server';
2024-03-13 05:24:53 +00:00
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';
2025-06-10 01:49:52 +00:00
import { getEmailContext } from '../email/get-email-context';
2024-03-13 05:24:53 +00:00
export interface SendDeleteEmailOptions {
documentId: number;
reason: string;
}
export const sendDeleteEmail = async ({ documentId, reason }: SendDeleteEmailOptions) => {
const document = await prisma.document.findFirst({
where: {
id: documentId,
},
include: {
2025-08-24 22:23:12 +00:00
user: {
select: {
id: true,
email: true,
name: true,
},
},
documentMeta: true,
2024-03-13 05:24:53 +00:00
},
});
if (!document) {
throw new AppError(AppErrorCode.NOT_FOUND, {
message: 'Document not found',
});
2024-03-13 05:24:53 +00:00
}
const isDocumentDeletedEmailEnabled = extractDerivedDocumentEmailSettings(
document.documentMeta,
).documentDeleted;
if (!isDocumentDeletedEmailEnabled) {
return;
}
const { branding, emailLanguage, senderEmail } = await getEmailContext({
emailType: 'INTERNAL',
2025-06-10 01:49:52 +00:00
source: {
type: 'team',
teamId: document.teamId,
},
meta: document.documentMeta,
2025-06-10 01:49:52 +00:00
});
2025-01-13 02:41:53 +00:00
const { email, name } = document.user;
2024-03-13 05:24:53 +00:00
const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
2024-03-28 07:01:57 +00:00
const template = createElement(DocumentSuperDeleteEmailTemplate, {
2024-03-13 05:24:53 +00:00
documentName: document.title,
reason,
2024-03-13 05:24:53 +00:00
assetBaseUrl,
});
const [html, text] = await Promise.all([
renderEmailWithI18N(template, { lang: emailLanguage, branding }),
feat: add global settings for teams (#1391) ## Description This PR introduces global settings for teams. At the moment, it allows team admins to configure the following: * The default visibility of the documents uploaded to the team account * Whether to include the document owner (sender) details when sending emails to the recipients. ### Include Sender Details If the Sender Details setting is enabled, the emails sent by the team will include the sender's name: > "Example User" on behalf of "Example Team" has invited you to sign "document.pdf" Otherwise, the email will say: > "Example Team" has invited you to sign "document.pdf" ### Default Document Visibility This new option allows users to set the default visibility for the documents uploaded to the team account. It can have the following values: * Everyone * Manager and above * Admins only If the default document visibility isn't set, the document will be set to the role of the user who created the document: * If a user with the "User" role creates a document, the document's visibility is set to "Everyone". * Manager role -> "Manager and above" * Admin role -> "Admins only" Otherwise, if there is a default document visibility value, it uses that value. #### Gotcha To avoid issues, the `document owner` and the `recipient` can access the document irrespective of their role. For example: * If a team member with the role "Member" uploads a document and the default document visibility is "Admins", only the document owner and admins can access the document. * Similar to the other scenarios. * If an admin uploads a document and the default document visibility is "Admins", the recipient can access the document. * The admins have access to all the documents. * Managers have access to documents with the visibility set to "Everyone" and "Manager and above" * Members have access only to the documents with the visibility set to "Everyone". ## Testing Performed Tested it locally.
2024-11-08 11:50:49 +00:00
renderEmailWithI18N(template, {
lang: emailLanguage,
feat: add global settings for teams (#1391) ## Description This PR introduces global settings for teams. At the moment, it allows team admins to configure the following: * The default visibility of the documents uploaded to the team account * Whether to include the document owner (sender) details when sending emails to the recipients. ### Include Sender Details If the Sender Details setting is enabled, the emails sent by the team will include the sender's name: > "Example User" on behalf of "Example Team" has invited you to sign "document.pdf" Otherwise, the email will say: > "Example Team" has invited you to sign "document.pdf" ### Default Document Visibility This new option allows users to set the default visibility for the documents uploaded to the team account. It can have the following values: * Everyone * Manager and above * Admins only If the default document visibility isn't set, the document will be set to the role of the user who created the document: * If a user with the "User" role creates a document, the document's visibility is set to "Everyone". * Manager role -> "Manager and above" * Admin role -> "Admins only" Otherwise, if there is a default document visibility value, it uses that value. #### Gotcha To avoid issues, the `document owner` and the `recipient` can access the document irrespective of their role. For example: * If a team member with the role "Member" uploads a document and the default document visibility is "Admins", only the document owner and admins can access the document. * Similar to the other scenarios. * If an admin uploads a document and the default document visibility is "Admins", the recipient can access the document. * The admins have access to all the documents. * Managers have access to documents with the visibility set to "Everyone" and "Manager and above" * Members have access only to the documents with the visibility set to "Everyone". ## Testing Performed Tested it locally.
2024-11-08 11:50:49 +00:00
branding,
plainText: true,
}),
]);
const i18n = await getI18nInstance(emailLanguage);
2024-03-13 05:24:53 +00:00
await mailer.sendMail({
to: {
address: email,
name: name || '',
},
from: senderEmail,
subject: i18n._(msg`Document Deleted!`),
html,
text,
2024-03-13 05:24:53 +00:00
});
};