mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 04:22:32 +10:00
## Description Updated the email content based on whether the document owner is a recipient or not. If the document owner is a recipient (self-signer): * the email subject will be `Please view/sign/approve your document` * the email header will be `Please view/sign/approve your document "<your-doc-title>"` * the email content will be `You have initiated the document "<your-doc-title>" that requires you to view/sign/approve it.` Otherwise: * the email subject will be `Please view/sign/approve this document` * the email header will be `<doc-owner> has invited you to view/sign/approve "<doc-title>"` * the email content will be `<doc-owner> has invited you to view/sign/approve the document "<doc-title>".` ## Related Issue Related to #1091 ## Testing Performed Tested the feature with a different number of recipients (including and excluding the document owner - self-signer). Tested both the sending and resending functionality. ## Checklist - [x] I have tested these changes locally and they work as expected. - [ ] I have added/updated tests that prove the effectiveness of these changes. - [ ] I have updated the documentation to reflect these changes, if applicable. - [x] I have followed the project's coding style guidelines. - [ ] I have addressed the code review feedback from the previous submission, if applicable. ## UI Screenshots     <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit - **New Features** - Enhanced the document invitation components to support scenarios where the recipient is also the sender, providing customized email content and subject lines. - Introduced new properties in email templates to improve clarity and relevance based on the user's role in the document signing process. - **Refactor** - Updated components to use a more flexible `headerContent` property for displaying invitation headers, replacing previous individual inviter details. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
166 lines
5.0 KiB
TypeScript
166 lines
5.0 KiB
TypeScript
import { createElement } from 'react';
|
|
|
|
import { mailer } from '@documenso/email/mailer';
|
|
import { render } from '@documenso/email/render';
|
|
import { DocumentInviteEmailTemplate } from '@documenso/email/templates/document-invite';
|
|
import { FROM_ADDRESS, FROM_NAME } from '@documenso/lib/constants/email';
|
|
import {
|
|
RECIPIENT_ROLES_DESCRIPTION,
|
|
RECIPIENT_ROLE_TO_EMAIL_TYPE,
|
|
} from '@documenso/lib/constants/recipient-roles';
|
|
import { DOCUMENT_AUDIT_LOG_TYPE } from '@documenso/lib/types/document-audit-logs';
|
|
import type { RequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
|
import { createDocumentAuditLogData } from '@documenso/lib/utils/document-audit-logs';
|
|
import { renderCustomEmailTemplate } from '@documenso/lib/utils/render-custom-email-template';
|
|
import { prisma } from '@documenso/prisma';
|
|
import { DocumentStatus, RecipientRole, SigningStatus } from '@documenso/prisma/client';
|
|
import type { Prisma } from '@documenso/prisma/client';
|
|
|
|
import { NEXT_PUBLIC_WEBAPP_URL } from '../../constants/app';
|
|
import { getDocumentWhereInput } from './get-document-by-id';
|
|
|
|
export type ResendDocumentOptions = {
|
|
documentId: number;
|
|
userId: number;
|
|
recipients: number[];
|
|
teamId?: number;
|
|
requestMetadata: RequestMetadata;
|
|
};
|
|
|
|
export const resendDocument = async ({
|
|
documentId,
|
|
userId,
|
|
recipients,
|
|
teamId,
|
|
requestMetadata,
|
|
}: ResendDocumentOptions) => {
|
|
const user = await prisma.user.findFirstOrThrow({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
});
|
|
|
|
const documentWhereInput: Prisma.DocumentWhereUniqueInput = await getDocumentWhereInput({
|
|
documentId,
|
|
userId,
|
|
teamId,
|
|
});
|
|
|
|
const document = await prisma.document.findUnique({
|
|
where: documentWhereInput,
|
|
include: {
|
|
Recipient: {
|
|
where: {
|
|
id: {
|
|
in: recipients,
|
|
},
|
|
signingStatus: SigningStatus.NOT_SIGNED,
|
|
},
|
|
},
|
|
documentMeta: true,
|
|
},
|
|
});
|
|
|
|
const customEmail = document?.documentMeta;
|
|
|
|
if (!document) {
|
|
throw new Error('Document not found');
|
|
}
|
|
|
|
if (document.Recipient.length === 0) {
|
|
throw new Error('Document has no recipients');
|
|
}
|
|
|
|
if (document.status === DocumentStatus.DRAFT) {
|
|
throw new Error('Can not send draft document');
|
|
}
|
|
|
|
if (document.status === DocumentStatus.COMPLETED) {
|
|
throw new Error('Can not send completed document');
|
|
}
|
|
|
|
await Promise.all(
|
|
document.Recipient.map(async (recipient) => {
|
|
if (recipient.role === RecipientRole.CC) {
|
|
return;
|
|
}
|
|
|
|
const recipientEmailType = RECIPIENT_ROLE_TO_EMAIL_TYPE[recipient.role];
|
|
|
|
const { email, name } = recipient;
|
|
const selfSigner = email === user.email;
|
|
|
|
const selfSignerCustomEmail = `You have initiated the document ${`"${document.title}"`} that requires you to ${RECIPIENT_ROLES_DESCRIPTION[
|
|
recipient.role
|
|
].actionVerb.toLowerCase()} it.`;
|
|
|
|
const customEmailTemplate = {
|
|
'signer.name': name,
|
|
'signer.email': email,
|
|
'document.name': document.title,
|
|
};
|
|
|
|
const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
|
|
const signDocumentLink = `${NEXT_PUBLIC_WEBAPP_URL()}/sign/${recipient.token}`;
|
|
|
|
const template = createElement(DocumentInviteEmailTemplate, {
|
|
documentName: document.title,
|
|
inviterName: user.name || undefined,
|
|
inviterEmail: user.email,
|
|
assetBaseUrl,
|
|
signDocumentLink,
|
|
customBody: renderCustomEmailTemplate(
|
|
selfSigner ? selfSignerCustomEmail : customEmail?.message || '',
|
|
customEmailTemplate,
|
|
),
|
|
role: recipient.role,
|
|
selfSigner,
|
|
});
|
|
|
|
const { actionVerb } = RECIPIENT_ROLES_DESCRIPTION[recipient.role];
|
|
|
|
const emailSubject = selfSigner
|
|
? `Reminder: Please ${actionVerb.toLowerCase()} your document`
|
|
: `Reminder: Please ${actionVerb.toLowerCase()} this document`;
|
|
|
|
await prisma.$transaction(
|
|
async (tx) => {
|
|
await mailer.sendMail({
|
|
to: {
|
|
address: email,
|
|
name,
|
|
},
|
|
from: {
|
|
name: FROM_NAME,
|
|
address: FROM_ADDRESS,
|
|
},
|
|
subject: customEmail?.subject
|
|
? renderCustomEmailTemplate(customEmail.subject, customEmailTemplate)
|
|
: emailSubject,
|
|
html: render(template),
|
|
text: render(template, { plainText: true }),
|
|
});
|
|
|
|
await tx.documentAuditLog.create({
|
|
data: createDocumentAuditLogData({
|
|
type: DOCUMENT_AUDIT_LOG_TYPE.EMAIL_SENT,
|
|
documentId: document.id,
|
|
user,
|
|
requestMetadata,
|
|
data: {
|
|
emailType: recipientEmailType,
|
|
recipientEmail: recipient.email,
|
|
recipientName: recipient.name,
|
|
recipientRole: recipient.role,
|
|
recipientId: recipient.id,
|
|
isResending: true,
|
|
},
|
|
}),
|
|
});
|
|
},
|
|
{ timeout: 30_000 },
|
|
);
|
|
}),
|
|
);
|
|
};
|