mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 16:23:06 +10:00
fix: update sending logic
This commit is contained in:
@ -12,7 +12,6 @@ import { DocumentStatus, RecipientRole, SigningStatus } from '@documenso/prisma/
|
|||||||
import { WebhookTriggerEvents } from '@documenso/prisma/client';
|
import { WebhookTriggerEvents } from '@documenso/prisma/client';
|
||||||
import { signPdf } from '@documenso/signing';
|
import { signPdf } from '@documenso/signing';
|
||||||
|
|
||||||
import { getRequiredServerComponentSession } from '../../next-auth/get-server-component-session';
|
|
||||||
import type { RequestMetadata } from '../../universal/extract-request-metadata';
|
import type { RequestMetadata } from '../../universal/extract-request-metadata';
|
||||||
import { getFile } from '../../universal/upload/get-file';
|
import { getFile } from '../../universal/upload/get-file';
|
||||||
import { putFile } from '../../universal/upload/put-file';
|
import { putFile } from '../../universal/upload/put-file';
|
||||||
@ -47,8 +46,6 @@ export const sealDocument = async ({
|
|||||||
|
|
||||||
const { documentData } = document;
|
const { documentData } = document;
|
||||||
|
|
||||||
const { user: documentOwner } = await getRequiredServerComponentSession();
|
|
||||||
|
|
||||||
if (!documentData) {
|
if (!documentData) {
|
||||||
throw new Error(`Document ${document.id} has no document data`);
|
throw new Error(`Document ${document.id} has no document data`);
|
||||||
}
|
}
|
||||||
@ -172,7 +169,7 @@ export const sealDocument = async ({
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (sendEmail && !isResealing) {
|
if (sendEmail && !isResealing) {
|
||||||
await sendCompletedEmail({ documentId, requestMetadata, documentOwner });
|
await sendCompletedEmail({ documentId, requestMetadata });
|
||||||
}
|
}
|
||||||
|
|
||||||
await triggerWebhook({
|
await triggerWebhook({
|
||||||
|
|||||||
@ -4,7 +4,6 @@ import { mailer } from '@documenso/email/mailer';
|
|||||||
import { render } from '@documenso/email/render';
|
import { render } from '@documenso/email/render';
|
||||||
import { DocumentCompletedEmailTemplate } from '@documenso/email/templates/document-completed';
|
import { DocumentCompletedEmailTemplate } from '@documenso/email/templates/document-completed';
|
||||||
import { prisma } from '@documenso/prisma';
|
import { prisma } from '@documenso/prisma';
|
||||||
import type { User } from '@documenso/prisma/client';
|
|
||||||
|
|
||||||
import { NEXT_PUBLIC_WEBAPP_URL } from '../../constants/app';
|
import { NEXT_PUBLIC_WEBAPP_URL } from '../../constants/app';
|
||||||
import { DOCUMENT_AUDIT_LOG_TYPE } from '../../types/document-audit-logs';
|
import { DOCUMENT_AUDIT_LOG_TYPE } from '../../types/document-audit-logs';
|
||||||
@ -15,14 +14,9 @@ import { createDocumentAuditLogData } from '../../utils/document-audit-logs';
|
|||||||
export interface SendDocumentOptions {
|
export interface SendDocumentOptions {
|
||||||
documentId: number;
|
documentId: number;
|
||||||
requestMetadata?: RequestMetadata;
|
requestMetadata?: RequestMetadata;
|
||||||
documentOwner: User;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const sendCompletedEmail = async ({
|
export const sendCompletedEmail = async ({ documentId, requestMetadata }: SendDocumentOptions) => {
|
||||||
documentId,
|
|
||||||
requestMetadata,
|
|
||||||
documentOwner,
|
|
||||||
}: SendDocumentOptions) => {
|
|
||||||
const document = await prisma.document.findUnique({
|
const document = await prisma.document.findUnique({
|
||||||
where: {
|
where: {
|
||||||
id: documentId,
|
id: documentId,
|
||||||
@ -30,6 +24,13 @@ export const sendCompletedEmail = async ({
|
|||||||
include: {
|
include: {
|
||||||
documentData: true,
|
documentData: true,
|
||||||
Recipient: true,
|
Recipient: true,
|
||||||
|
User: true,
|
||||||
|
team: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
url: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -41,69 +42,116 @@ export const sendCompletedEmail = async ({
|
|||||||
throw new Error('Document has no recipients');
|
throw new Error('Document has no recipients');
|
||||||
}
|
}
|
||||||
|
|
||||||
const buffer = await getFile(document.documentData);
|
const { User: owner } = document;
|
||||||
|
|
||||||
|
const completedDocument = await getFile(document.documentData);
|
||||||
|
|
||||||
|
const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
|
||||||
|
|
||||||
|
let documentOwnerDownloadLink = `${NEXT_PUBLIC_WEBAPP_URL()}/documents/${document.id}`;
|
||||||
|
|
||||||
|
if (document.team?.url) {
|
||||||
|
documentOwnerDownloadLink = `${NEXT_PUBLIC_WEBAPP_URL()}/t/${document.team.url}/documents/${
|
||||||
|
document.id
|
||||||
|
}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the document owner is not a recipient then send the email to them separately
|
||||||
|
if (!document.Recipient.find((recipient) => recipient.email === owner.email)) {
|
||||||
|
const template = createElement(DocumentCompletedEmailTemplate, {
|
||||||
|
documentName: document.title,
|
||||||
|
assetBaseUrl,
|
||||||
|
downloadLink: documentOwnerDownloadLink,
|
||||||
|
});
|
||||||
|
|
||||||
|
await mailer.sendMail({
|
||||||
|
to: [
|
||||||
|
{
|
||||||
|
name: owner.name || '',
|
||||||
|
address: owner.email,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
from: {
|
||||||
|
name: process.env.NEXT_PRIVATE_SMTP_FROM_NAME || 'Documenso',
|
||||||
|
address: process.env.NEXT_PRIVATE_SMTP_FROM_ADDRESS || 'noreply@documenso.com',
|
||||||
|
},
|
||||||
|
subject: 'Signing Complete!',
|
||||||
|
html: render(template),
|
||||||
|
text: render(template, { plainText: true }),
|
||||||
|
attachments: [
|
||||||
|
{
|
||||||
|
filename: document.title,
|
||||||
|
content: Buffer.from(completedDocument),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
await prisma.documentAuditLog.create({
|
||||||
|
data: createDocumentAuditLogData({
|
||||||
|
type: DOCUMENT_AUDIT_LOG_TYPE.EMAIL_SENT,
|
||||||
|
documentId: document.id,
|
||||||
|
user: null,
|
||||||
|
requestMetadata,
|
||||||
|
data: {
|
||||||
|
emailType: 'DOCUMENT_COMPLETED',
|
||||||
|
recipientEmail: owner.email,
|
||||||
|
recipientName: owner.name,
|
||||||
|
recipientId: owner.id,
|
||||||
|
recipientRole: 'OWNER',
|
||||||
|
isResending: false,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
document.Recipient.map(async (recipient) => {
|
document.Recipient.map(async (recipient) => {
|
||||||
const { email, name, token } = recipient;
|
const downloadLink = `${NEXT_PUBLIC_WEBAPP_URL()}/sign/${recipient.token}/complete`;
|
||||||
|
|
||||||
const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
|
|
||||||
|
|
||||||
const template = createElement(DocumentCompletedEmailTemplate, {
|
const template = createElement(DocumentCompletedEmailTemplate, {
|
||||||
documentName: document.title,
|
documentName: document.title,
|
||||||
assetBaseUrl,
|
assetBaseUrl,
|
||||||
downloadLink: `${NEXT_PUBLIC_WEBAPP_URL()}/sign/${token}/complete`,
|
downloadLink: recipient.email === owner.email ? documentOwnerDownloadLink : downloadLink,
|
||||||
});
|
});
|
||||||
|
|
||||||
await prisma.$transaction(
|
await mailer.sendMail({
|
||||||
async (tx) => {
|
to: [
|
||||||
await mailer.sendMail({
|
{
|
||||||
to: [
|
name: recipient.name,
|
||||||
// To send email to recipient of the document
|
address: recipient.email,
|
||||||
{
|
},
|
||||||
address: email,
|
],
|
||||||
name,
|
from: {
|
||||||
},
|
name: process.env.NEXT_PRIVATE_SMTP_FROM_NAME || 'Documenso',
|
||||||
// To send email to owner of the document
|
address: process.env.NEXT_PRIVATE_SMTP_FROM_ADDRESS || 'noreply@documenso.com',
|
||||||
{
|
|
||||||
address: documentOwner.email,
|
|
||||||
name: documentOwner.name!,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
from: {
|
|
||||||
name: process.env.NEXT_PRIVATE_SMTP_FROM_NAME || 'Documenso',
|
|
||||||
address: process.env.NEXT_PRIVATE_SMTP_FROM_ADDRESS || 'noreply@documenso.com',
|
|
||||||
},
|
|
||||||
subject: 'Signing Complete!',
|
|
||||||
html: render(template),
|
|
||||||
text: render(template, { plainText: true }),
|
|
||||||
attachments: [
|
|
||||||
{
|
|
||||||
filename: document.title,
|
|
||||||
content: Buffer.from(buffer),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
await tx.documentAuditLog.create({
|
|
||||||
data: createDocumentAuditLogData({
|
|
||||||
type: DOCUMENT_AUDIT_LOG_TYPE.EMAIL_SENT,
|
|
||||||
documentId: document.id,
|
|
||||||
user: null,
|
|
||||||
requestMetadata,
|
|
||||||
data: {
|
|
||||||
emailType: 'DOCUMENT_COMPLETED',
|
|
||||||
recipientEmail: recipient.email,
|
|
||||||
recipientName: recipient.name,
|
|
||||||
recipientId: recipient.id,
|
|
||||||
recipientRole: recipient.role,
|
|
||||||
isResending: false,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
{ timeout: 30_000 },
|
subject: 'Signing Complete!',
|
||||||
);
|
html: render(template),
|
||||||
|
text: render(template, { plainText: true }),
|
||||||
|
attachments: [
|
||||||
|
{
|
||||||
|
filename: document.title,
|
||||||
|
content: Buffer.from(completedDocument),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
await prisma.documentAuditLog.create({
|
||||||
|
data: createDocumentAuditLogData({
|
||||||
|
type: DOCUMENT_AUDIT_LOG_TYPE.EMAIL_SENT,
|
||||||
|
documentId: document.id,
|
||||||
|
user: null,
|
||||||
|
requestMetadata,
|
||||||
|
data: {
|
||||||
|
emailType: 'DOCUMENT_COMPLETED',
|
||||||
|
recipientEmail: recipient.email,
|
||||||
|
recipientName: recipient.name,
|
||||||
|
recipientId: recipient.id,
|
||||||
|
recipientRole: recipient.role,
|
||||||
|
isResending: false,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user