2024-09-20 13:58:21 +10:00
|
|
|
import { createElement } from 'react';
|
|
|
|
|
|
2025-01-02 15:33:37 +11:00
|
|
|
import { msg } from '@lingui/core/macro';
|
|
|
|
|
import type { Recipient } from '@prisma/client';
|
|
|
|
|
import { RecipientRole } from '@prisma/client';
|
|
|
|
|
import { SendStatus, SigningStatus } from '@prisma/client';
|
2024-11-05 11:52:54 +11:00
|
|
|
|
2024-03-28 13:13:29 +08:00
|
|
|
import { isUserEnterprise } from '@documenso/ee/server-only/util/is-document-enterprise';
|
2024-09-20 13:58:21 +10:00
|
|
|
import { mailer } from '@documenso/email/mailer';
|
|
|
|
|
import RecipientRemovedFromDocumentTemplate from '@documenso/email/templates/recipient-removed-from-document';
|
2024-02-12 12:04:53 +11:00
|
|
|
import { DOCUMENT_AUDIT_LOG_TYPE } from '@documenso/lib/types/document-audit-logs';
|
2025-01-11 15:33:20 +11:00
|
|
|
import type { TRecipientAccessAuthTypes } from '@documenso/lib/types/document-auth';
|
2024-03-28 13:13:29 +08:00
|
|
|
import {
|
|
|
|
|
type TRecipientActionAuthTypes,
|
|
|
|
|
ZRecipientAuthOptionsSchema,
|
|
|
|
|
} from '@documenso/lib/types/document-auth';
|
2025-01-11 15:33:20 +11:00
|
|
|
import type { ApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
2024-02-12 12:04:53 +11:00
|
|
|
import { nanoid } from '@documenso/lib/universal/id';
|
|
|
|
|
import {
|
|
|
|
|
createDocumentAuditLogData,
|
|
|
|
|
diffRecipientChanges,
|
|
|
|
|
} from '@documenso/lib/utils/document-audit-logs';
|
2024-03-28 13:13:29 +08:00
|
|
|
import { createRecipientAuthOptions } from '@documenso/lib/utils/document-auth';
|
2023-06-21 23:49:23 +10:00
|
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
|
|
2025-01-02 15:33:37 +11:00
|
|
|
import { getI18nInstance } from '../../client-only/providers/i18n-server';
|
2024-09-20 13:58:21 +10:00
|
|
|
import { NEXT_PUBLIC_WEBAPP_URL } from '../../constants/app';
|
|
|
|
|
import { FROM_ADDRESS, FROM_NAME } from '../../constants/email';
|
2024-03-28 13:13:29 +08:00
|
|
|
import { AppError, AppErrorCode } from '../../errors/app-error';
|
2024-11-08 13:32:13 +09:00
|
|
|
import { extractDerivedDocumentEmailSettings } from '../../types/document-email';
|
2024-09-20 13:58:21 +10:00
|
|
|
import { canRecipientBeModified } from '../../utils/recipients';
|
2024-11-05 11:52:54 +11:00
|
|
|
import { renderEmailWithI18N } from '../../utils/render-email-with-i18n';
|
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 13:50:49 +02:00
|
|
|
import { teamGlobalSettingsToBranding } from '../../utils/team-global-settings-to-branding';
|
2024-03-28 13:13:29 +08:00
|
|
|
|
2025-01-11 15:33:20 +11:00
|
|
|
export interface SetDocumentRecipientsOptions {
|
2023-06-21 23:49:23 +10:00
|
|
|
userId: number;
|
2024-02-22 13:39:34 +11:00
|
|
|
teamId?: number;
|
2023-06-21 23:49:23 +10:00
|
|
|
documentId: number;
|
2024-09-20 13:58:21 +10:00
|
|
|
recipients: RecipientData[];
|
2025-01-11 15:33:20 +11:00
|
|
|
requestMetadata: ApiRequestMetadata;
|
2023-06-21 23:49:23 +10:00
|
|
|
}
|
|
|
|
|
|
2025-01-11 15:33:20 +11:00
|
|
|
export const setDocumentRecipients = async ({
|
2023-06-21 23:49:23 +10:00
|
|
|
userId,
|
2024-02-22 13:39:34 +11:00
|
|
|
teamId,
|
2023-06-21 23:49:23 +10:00
|
|
|
documentId,
|
|
|
|
|
recipients,
|
2024-02-12 12:04:53 +11:00
|
|
|
requestMetadata,
|
2025-01-14 00:43:35 +11:00
|
|
|
}: SetDocumentRecipientsOptions) => {
|
2023-06-21 23:49:23 +10:00
|
|
|
const document = await prisma.document.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
id: documentId,
|
2024-02-22 13:39:34 +11:00
|
|
|
...(teamId
|
|
|
|
|
? {
|
|
|
|
|
team: {
|
|
|
|
|
id: teamId,
|
|
|
|
|
members: {
|
|
|
|
|
some: {
|
|
|
|
|
userId,
|
|
|
|
|
},
|
2024-02-06 16:16:10 +11:00
|
|
|
},
|
|
|
|
|
},
|
2024-02-22 13:39:34 +11:00
|
|
|
}
|
|
|
|
|
: {
|
|
|
|
|
userId,
|
|
|
|
|
teamId: null,
|
|
|
|
|
}),
|
2023-06-21 23:49:23 +10:00
|
|
|
},
|
2024-09-20 13:58:21 +10:00
|
|
|
include: {
|
2025-01-13 13:41:53 +11:00
|
|
|
fields: true,
|
2024-11-05 11:52:54 +11:00
|
|
|
documentMeta: true,
|
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 13:50:49 +02:00
|
|
|
team: {
|
|
|
|
|
include: {
|
|
|
|
|
teamGlobalSettings: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-09-20 13:58:21 +10:00
|
|
|
},
|
2023-06-21 23:49:23 +10:00
|
|
|
});
|
|
|
|
|
|
2024-02-12 12:04:53 +11:00
|
|
|
const user = await prisma.user.findFirstOrThrow({
|
|
|
|
|
where: {
|
|
|
|
|
id: userId,
|
|
|
|
|
},
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
email: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2023-06-21 23:49:23 +10:00
|
|
|
if (!document) {
|
|
|
|
|
throw new Error('Document not found');
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-09 12:37:17 +11:00
|
|
|
if (document.completedAt) {
|
|
|
|
|
throw new Error('Document already complete');
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 13:13:29 +08:00
|
|
|
const recipientsHaveActionAuth = recipients.some((recipient) => recipient.actionAuth);
|
|
|
|
|
|
|
|
|
|
// Check if user has permission to set the global action auth.
|
|
|
|
|
if (recipientsHaveActionAuth) {
|
|
|
|
|
const isDocumentEnterprise = await isUserEnterprise({
|
|
|
|
|
userId,
|
|
|
|
|
teamId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!isDocumentEnterprise) {
|
2024-11-28 16:05:37 +07:00
|
|
|
throw new AppError(AppErrorCode.UNAUTHORIZED, {
|
|
|
|
|
message: 'You do not have permission to set the action auth',
|
|
|
|
|
});
|
2024-03-28 13:13:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-24 11:46:36 +10:00
|
|
|
const normalizedRecipients = recipients.map((recipient) => ({
|
|
|
|
|
...recipient,
|
|
|
|
|
email: recipient.email.toLowerCase(),
|
|
|
|
|
}));
|
|
|
|
|
|
2023-06-21 23:49:23 +10:00
|
|
|
const existingRecipients = await prisma.recipient.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
documentId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const removedRecipients = existingRecipients.filter(
|
|
|
|
|
(existingRecipient) =>
|
2023-09-24 11:46:36 +10:00
|
|
|
!normalizedRecipients.find(
|
2023-06-21 23:49:23 +10:00
|
|
|
(recipient) =>
|
|
|
|
|
recipient.id === existingRecipient.id || recipient.email === existingRecipient.email,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
2024-09-20 13:58:21 +10:00
|
|
|
const linkedRecipients = normalizedRecipients.map((recipient) => {
|
|
|
|
|
const existing = existingRecipients.find(
|
|
|
|
|
(existingRecipient) =>
|
|
|
|
|
existingRecipient.id === recipient.id || existingRecipient.email === recipient.email,
|
|
|
|
|
);
|
2023-06-21 23:49:23 +10:00
|
|
|
|
2024-09-20 13:58:21 +10:00
|
|
|
if (
|
|
|
|
|
existing &&
|
|
|
|
|
hasRecipientBeenChanged(existing, recipient) &&
|
2025-01-13 13:41:53 +11:00
|
|
|
!canRecipientBeModified(existing, document.fields)
|
2024-09-20 13:58:21 +10:00
|
|
|
) {
|
2024-11-28 16:05:37 +07:00
|
|
|
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
|
|
|
|
message: 'Cannot modify a recipient who has already interacted with the document',
|
|
|
|
|
});
|
2024-09-20 13:58:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...recipient,
|
|
|
|
|
_persisted: existing,
|
|
|
|
|
};
|
|
|
|
|
});
|
2023-06-21 23:49:23 +10:00
|
|
|
|
2024-02-12 12:04:53 +11:00
|
|
|
const persistedRecipients = await prisma.$transaction(async (tx) => {
|
2024-02-22 13:39:34 +11:00
|
|
|
return await Promise.all(
|
2024-02-12 12:04:53 +11:00
|
|
|
linkedRecipients.map(async (recipient) => {
|
2024-03-28 13:13:29 +08:00
|
|
|
let authOptions = ZRecipientAuthOptionsSchema.parse(recipient._persisted?.authOptions);
|
|
|
|
|
|
2025-01-11 15:33:20 +11:00
|
|
|
if (recipient.actionAuth !== undefined || recipient.accessAuth !== undefined) {
|
2024-03-28 13:13:29 +08:00
|
|
|
authOptions = createRecipientAuthOptions({
|
2025-01-11 15:33:20 +11:00
|
|
|
accessAuth: recipient.accessAuth || authOptions.accessAuth,
|
|
|
|
|
actionAuth: recipient.actionAuth || authOptions.actionAuth,
|
2024-03-28 13:13:29 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-12 12:04:53 +11:00
|
|
|
const upsertedRecipient = await tx.recipient.upsert({
|
|
|
|
|
where: {
|
|
|
|
|
id: recipient._persisted?.id ?? -1,
|
|
|
|
|
documentId,
|
|
|
|
|
},
|
|
|
|
|
update: {
|
|
|
|
|
name: recipient.name,
|
|
|
|
|
email: recipient.email,
|
|
|
|
|
role: recipient.role,
|
2024-09-16 12:36:45 +00:00
|
|
|
signingOrder: recipient.signingOrder,
|
2024-02-12 12:04:53 +11:00
|
|
|
documentId,
|
|
|
|
|
sendStatus: recipient.role === RecipientRole.CC ? SendStatus.SENT : SendStatus.NOT_SENT,
|
|
|
|
|
signingStatus:
|
|
|
|
|
recipient.role === RecipientRole.CC ? SigningStatus.SIGNED : SigningStatus.NOT_SIGNED,
|
2024-03-28 13:13:29 +08:00
|
|
|
authOptions,
|
2024-02-12 12:04:53 +11:00
|
|
|
},
|
|
|
|
|
create: {
|
|
|
|
|
name: recipient.name,
|
|
|
|
|
email: recipient.email,
|
|
|
|
|
role: recipient.role,
|
2024-09-16 12:36:45 +00:00
|
|
|
signingOrder: recipient.signingOrder,
|
2024-02-12 12:04:53 +11:00
|
|
|
token: nanoid(),
|
|
|
|
|
documentId,
|
|
|
|
|
sendStatus: recipient.role === RecipientRole.CC ? SendStatus.SENT : SendStatus.NOT_SENT,
|
|
|
|
|
signingStatus:
|
|
|
|
|
recipient.role === RecipientRole.CC ? SigningStatus.SIGNED : SigningStatus.NOT_SIGNED,
|
2024-03-28 13:13:29 +08:00
|
|
|
authOptions,
|
2024-02-12 12:04:53 +11:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const recipientId = upsertedRecipient.id;
|
|
|
|
|
|
|
|
|
|
// Clear all fields if the recipient role is changed to a type that cannot have fields.
|
|
|
|
|
if (
|
|
|
|
|
recipient._persisted &&
|
|
|
|
|
recipient._persisted.role !== recipient.role &&
|
|
|
|
|
(recipient.role === RecipientRole.CC || recipient.role === RecipientRole.VIEWER)
|
|
|
|
|
) {
|
|
|
|
|
await tx.field.deleteMany({
|
|
|
|
|
where: {
|
|
|
|
|
recipientId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const baseAuditLog = {
|
|
|
|
|
recipientEmail: upsertedRecipient.email,
|
|
|
|
|
recipientName: upsertedRecipient.name,
|
|
|
|
|
recipientId,
|
|
|
|
|
recipientRole: upsertedRecipient.role,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const changes = recipient._persisted
|
|
|
|
|
? diffRecipientChanges(recipient._persisted, upsertedRecipient)
|
|
|
|
|
: [];
|
|
|
|
|
|
|
|
|
|
// Handle recipient updated audit log.
|
|
|
|
|
if (recipient._persisted && changes.length > 0) {
|
|
|
|
|
await tx.documentAuditLog.create({
|
|
|
|
|
data: createDocumentAuditLogData({
|
|
|
|
|
type: DOCUMENT_AUDIT_LOG_TYPE.RECIPIENT_UPDATED,
|
|
|
|
|
documentId: documentId,
|
2025-01-11 15:33:20 +11:00
|
|
|
metadata: requestMetadata,
|
2024-02-12 12:04:53 +11:00
|
|
|
data: {
|
|
|
|
|
changes,
|
|
|
|
|
...baseAuditLog,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle recipient created audit log.
|
|
|
|
|
if (!recipient._persisted) {
|
|
|
|
|
await tx.documentAuditLog.create({
|
|
|
|
|
data: createDocumentAuditLogData({
|
|
|
|
|
type: DOCUMENT_AUDIT_LOG_TYPE.RECIPIENT_CREATED,
|
|
|
|
|
documentId: documentId,
|
2025-01-11 15:33:20 +11:00
|
|
|
metadata: requestMetadata,
|
2024-03-28 13:13:29 +08:00
|
|
|
data: {
|
|
|
|
|
...baseAuditLog,
|
2025-01-11 15:33:20 +11:00
|
|
|
accessAuth: recipient.accessAuth || undefined,
|
2024-03-28 13:13:29 +08:00
|
|
|
actionAuth: recipient.actionAuth || undefined,
|
|
|
|
|
},
|
2024-02-12 12:04:53 +11:00
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return upsertedRecipient;
|
2023-09-24 14:45:50 +10:00
|
|
|
}),
|
2024-02-12 12:04:53 +11:00
|
|
|
);
|
|
|
|
|
});
|
2023-06-21 23:49:23 +10:00
|
|
|
|
|
|
|
|
if (removedRecipients.length > 0) {
|
2024-02-12 12:04:53 +11:00
|
|
|
await prisma.$transaction(async (tx) => {
|
|
|
|
|
await tx.recipient.deleteMany({
|
|
|
|
|
where: {
|
|
|
|
|
id: {
|
|
|
|
|
in: removedRecipients.map((recipient) => recipient.id),
|
|
|
|
|
},
|
2023-06-21 23:49:23 +10:00
|
|
|
},
|
2024-02-12 12:04:53 +11:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await tx.documentAuditLog.createMany({
|
|
|
|
|
data: removedRecipients.map((recipient) =>
|
|
|
|
|
createDocumentAuditLogData({
|
|
|
|
|
type: DOCUMENT_AUDIT_LOG_TYPE.RECIPIENT_DELETED,
|
|
|
|
|
documentId: documentId,
|
2025-01-11 15:33:20 +11:00
|
|
|
metadata: requestMetadata,
|
2024-02-12 12:04:53 +11:00
|
|
|
data: {
|
|
|
|
|
recipientEmail: recipient.email,
|
|
|
|
|
recipientName: recipient.name,
|
|
|
|
|
recipientId: recipient.id,
|
|
|
|
|
recipientRole: recipient.role,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
});
|
2023-06-21 23:49:23 +10:00
|
|
|
});
|
2024-09-20 13:58:21 +10:00
|
|
|
|
2024-11-08 13:32:13 +09:00
|
|
|
const isRecipientRemovedEmailEnabled = extractDerivedDocumentEmailSettings(
|
|
|
|
|
document.documentMeta,
|
|
|
|
|
).recipientRemoved;
|
|
|
|
|
|
2024-09-20 13:58:21 +10:00
|
|
|
// Send emails to deleted recipients.
|
|
|
|
|
await Promise.all(
|
|
|
|
|
removedRecipients.map(async (recipient) => {
|
2024-11-08 13:32:13 +09:00
|
|
|
if (recipient.sendStatus !== SendStatus.SENT || !isRecipientRemovedEmailEnabled) {
|
2024-09-20 13:58:21 +10:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
|
|
|
|
|
|
|
|
|
|
const template = createElement(RecipientRemovedFromDocumentTemplate, {
|
|
|
|
|
documentName: document.title,
|
|
|
|
|
inviterName: user.name || undefined,
|
|
|
|
|
assetBaseUrl,
|
|
|
|
|
});
|
|
|
|
|
|
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 13:50:49 +02:00
|
|
|
const branding = document.team?.teamGlobalSettings
|
|
|
|
|
? teamGlobalSettingsToBranding(document.team.teamGlobalSettings)
|
|
|
|
|
: undefined;
|
|
|
|
|
|
2024-11-05 11:52:54 +11:00
|
|
|
const [html, text] = await Promise.all([
|
|
|
|
|
renderEmailWithI18N(template, { lang: document.documentMeta?.language }),
|
|
|
|
|
renderEmailWithI18N(template, { lang: document.documentMeta?.language, plainText: true }),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const i18n = await getI18nInstance(document.documentMeta?.language);
|
|
|
|
|
|
2024-09-20 13:58:21 +10:00
|
|
|
await mailer.sendMail({
|
|
|
|
|
to: {
|
|
|
|
|
address: recipient.email,
|
|
|
|
|
name: recipient.name,
|
|
|
|
|
},
|
|
|
|
|
from: {
|
|
|
|
|
name: FROM_NAME,
|
|
|
|
|
address: FROM_ADDRESS,
|
|
|
|
|
},
|
2024-11-05 11:52:54 +11:00
|
|
|
subject: i18n._(msg`You have been removed from a document`),
|
|
|
|
|
html,
|
|
|
|
|
text,
|
2024-09-20 13:58:21 +10:00
|
|
|
});
|
|
|
|
|
}),
|
|
|
|
|
);
|
2023-06-21 23:49:23 +10:00
|
|
|
}
|
|
|
|
|
|
2024-03-26 21:12:41 +08:00
|
|
|
// Filter out recipients that have been removed or have been updated.
|
|
|
|
|
const filteredRecipients: Recipient[] = existingRecipients.filter((recipient) => {
|
|
|
|
|
const isRemoved = removedRecipients.find(
|
|
|
|
|
(removedRecipient) => removedRecipient.id === recipient.id,
|
|
|
|
|
);
|
|
|
|
|
const isUpdated = persistedRecipients.find(
|
|
|
|
|
(persistedRecipient) => persistedRecipient.id === recipient.id,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return !isRemoved && !isUpdated;
|
|
|
|
|
});
|
|
|
|
|
|
2024-12-14 01:23:35 +09:00
|
|
|
return {
|
|
|
|
|
recipients: [...filteredRecipients, ...persistedRecipients],
|
|
|
|
|
};
|
2023-06-21 23:49:23 +10:00
|
|
|
};
|
2024-09-20 13:58:21 +10:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If you change this you MUST update the `hasRecipientBeenChanged` function.
|
|
|
|
|
*/
|
|
|
|
|
type RecipientData = {
|
|
|
|
|
id?: number | null;
|
|
|
|
|
email: string;
|
|
|
|
|
name: string;
|
|
|
|
|
role: RecipientRole;
|
|
|
|
|
signingOrder?: number | null;
|
2025-01-11 15:33:20 +11:00
|
|
|
accessAuth?: TRecipientAccessAuthTypes | null;
|
2024-09-20 13:58:21 +10:00
|
|
|
actionAuth?: TRecipientActionAuthTypes | null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const hasRecipientBeenChanged = (recipient: Recipient, newRecipientData: RecipientData) => {
|
|
|
|
|
const authOptions = ZRecipientAuthOptionsSchema.parse(recipient.authOptions);
|
|
|
|
|
|
2025-01-11 22:31:59 +11:00
|
|
|
const newRecipientAccessAuth = newRecipientData.accessAuth || null;
|
|
|
|
|
const newRecipientActionAuth = newRecipientData.actionAuth || null;
|
|
|
|
|
|
2024-09-20 13:58:21 +10:00
|
|
|
return (
|
|
|
|
|
recipient.email !== newRecipientData.email ||
|
|
|
|
|
recipient.name !== newRecipientData.name ||
|
|
|
|
|
recipient.role !== newRecipientData.role ||
|
|
|
|
|
recipient.signingOrder !== newRecipientData.signingOrder ||
|
2025-01-11 22:31:59 +11:00
|
|
|
authOptions.accessAuth !== newRecipientAccessAuth ||
|
|
|
|
|
authOptions.actionAuth !== newRecipientActionAuth
|
2024-09-20 13:58:21 +10:00
|
|
|
);
|
|
|
|
|
};
|