mirror of
https://github.com/documenso/documenso.git
synced 2025-11-12 15:53:02 +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.
159 lines
4.4 KiB
TypeScript
159 lines
4.4 KiB
TypeScript
import { createElement } from 'react';
|
|
|
|
import { msg } from '@lingui/core/macro';
|
|
import { EnvelopeType, SendStatus } from '@prisma/client';
|
|
|
|
import { mailer } from '@documenso/email/mailer';
|
|
import RecipientRemovedFromDocumentTemplate from '@documenso/email/templates/recipient-removed-from-document';
|
|
import { DOCUMENT_AUDIT_LOG_TYPE } from '@documenso/lib/types/document-audit-logs';
|
|
import type { ApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
|
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 { createDocumentAuditLogData } from '../../utils/document-audit-logs';
|
|
import { renderEmailWithI18N } from '../../utils/render-email-with-i18n';
|
|
import { buildTeamWhereQuery } from '../../utils/teams';
|
|
import { getEmailContext } from '../email/get-email-context';
|
|
|
|
export interface DeleteDocumentRecipientOptions {
|
|
userId: number;
|
|
teamId: number;
|
|
recipientId: number;
|
|
requestMetadata: ApiRequestMetadata;
|
|
}
|
|
|
|
export const deleteDocumentRecipient = async ({
|
|
userId,
|
|
teamId,
|
|
recipientId,
|
|
requestMetadata,
|
|
}: DeleteDocumentRecipientOptions) => {
|
|
const envelope = await prisma.envelope.findFirst({
|
|
where: {
|
|
type: EnvelopeType.DOCUMENT,
|
|
recipients: {
|
|
some: {
|
|
id: recipientId,
|
|
},
|
|
},
|
|
team: buildTeamWhereQuery({ teamId, userId }),
|
|
},
|
|
include: {
|
|
documentMeta: true,
|
|
team: true,
|
|
recipients: {
|
|
where: {
|
|
id: recipientId,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const user = await prisma.user.findFirst({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
email: true,
|
|
},
|
|
});
|
|
|
|
if (!envelope) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
message: 'Document not found',
|
|
});
|
|
}
|
|
|
|
if (envelope.completedAt) {
|
|
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
|
message: 'Document already complete',
|
|
});
|
|
}
|
|
|
|
if (!user) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
message: 'User not found',
|
|
});
|
|
}
|
|
|
|
const recipientToDelete = envelope.recipients[0];
|
|
|
|
if (!recipientToDelete || recipientToDelete.id !== recipientId) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
message: 'Recipient not found',
|
|
});
|
|
}
|
|
|
|
const deletedRecipient = await prisma.$transaction(async (tx) => {
|
|
await tx.documentAuditLog.create({
|
|
data: createDocumentAuditLogData({
|
|
type: DOCUMENT_AUDIT_LOG_TYPE.RECIPIENT_DELETED,
|
|
envelopeId: envelope.id,
|
|
metadata: requestMetadata,
|
|
data: {
|
|
recipientEmail: recipientToDelete.email,
|
|
recipientName: recipientToDelete.name,
|
|
recipientId: recipientToDelete.id,
|
|
recipientRole: recipientToDelete.role,
|
|
},
|
|
}),
|
|
});
|
|
|
|
return await tx.recipient.delete({
|
|
where: {
|
|
id: recipientId,
|
|
},
|
|
});
|
|
});
|
|
|
|
const isRecipientRemovedEmailEnabled = extractDerivedDocumentEmailSettings(
|
|
envelope.documentMeta,
|
|
).recipientRemoved;
|
|
|
|
// Send email to deleted recipient.
|
|
if (recipientToDelete.sendStatus === SendStatus.SENT && isRecipientRemovedEmailEnabled) {
|
|
const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
|
|
|
|
const template = createElement(RecipientRemovedFromDocumentTemplate, {
|
|
documentName: envelope.title,
|
|
inviterName: envelope.team?.name || user.name || undefined,
|
|
assetBaseUrl,
|
|
});
|
|
|
|
const { branding, emailLanguage, senderEmail, replyToEmail } = await getEmailContext({
|
|
emailType: 'RECIPIENT',
|
|
source: {
|
|
type: 'team',
|
|
teamId: envelope.teamId,
|
|
},
|
|
meta: envelope.documentMeta,
|
|
});
|
|
|
|
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: recipientToDelete.email,
|
|
name: recipientToDelete.name,
|
|
},
|
|
from: senderEmail,
|
|
replyTo: replyToEmail,
|
|
subject: i18n._(msg`You have been removed from a document`),
|
|
html,
|
|
text,
|
|
});
|
|
}
|
|
|
|
return deletedRecipient;
|
|
};
|