2024-03-28 13:13:29 +08:00
|
|
|
import { isUserEnterprise } from '@documenso/ee/server-only/util/is-document-enterprise';
|
2024-02-12 12:04:53 +11:00
|
|
|
import { DOCUMENT_AUDIT_LOG_TYPE } from '@documenso/lib/types/document-audit-logs';
|
2024-03-28 13:13:29 +08:00
|
|
|
import {
|
|
|
|
type TRecipientActionAuthTypes,
|
|
|
|
ZRecipientAuthOptionsSchema,
|
|
|
|
} from '@documenso/lib/types/document-auth';
|
2024-02-12 12:04:53 +11:00
|
|
|
import type { RequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
|
|
|
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';
|
2024-03-26 21:12:41 +08:00
|
|
|
import type { Recipient } from '@documenso/prisma/client';
|
2024-02-01 18:45:02 -05:00
|
|
|
import { RecipientRole } from '@documenso/prisma/client';
|
2023-06-21 23:49:23 +10:00
|
|
|
import { SendStatus, SigningStatus } from '@documenso/prisma/client';
|
|
|
|
|
2024-03-28 13:13:29 +08:00
|
|
|
import { AppError, AppErrorCode } from '../../errors/app-error';
|
|
|
|
|
2023-06-21 23:49:23 +10:00
|
|
|
export interface SetRecipientsForDocumentOptions {
|
|
|
|
userId: number;
|
2024-02-22 13:39:34 +11:00
|
|
|
teamId?: number;
|
2023-06-21 23:49:23 +10:00
|
|
|
documentId: number;
|
|
|
|
recipients: {
|
|
|
|
id?: number | null;
|
|
|
|
email: string;
|
|
|
|
name: string;
|
2024-02-01 18:45:02 -05:00
|
|
|
role: RecipientRole;
|
2024-09-16 12:36:45 +00:00
|
|
|
signingOrder?: number | null;
|
2024-03-28 13:13:29 +08:00
|
|
|
actionAuth?: TRecipientActionAuthTypes | null;
|
2023-06-21 23:49:23 +10:00
|
|
|
}[];
|
2024-02-12 12:04:53 +11:00
|
|
|
requestMetadata?: RequestMetadata;
|
2023-06-21 23:49:23 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
export const setRecipientsForDocument = async ({
|
|
|
|
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,
|
2024-03-26 21:12:41 +08:00
|
|
|
}: SetRecipientsForDocumentOptions): Promise<Recipient[]> => {
|
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-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) {
|
|
|
|
throw new AppError(
|
|
|
|
AppErrorCode.UNAUTHORIZED,
|
|
|
|
'You do not have permission to set the action auth',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2023-09-24 11:46:36 +10:00
|
|
|
const linkedRecipients = normalizedRecipients
|
2023-07-26 18:52:53 +10:00
|
|
|
.map((recipient) => {
|
|
|
|
const existing = existingRecipients.find(
|
|
|
|
(existingRecipient) =>
|
|
|
|
existingRecipient.id === recipient.id || existingRecipient.email === recipient.email,
|
|
|
|
);
|
2023-06-21 23:49:23 +10:00
|
|
|
|
2023-07-26 18:52:53 +10:00
|
|
|
return {
|
|
|
|
...recipient,
|
2023-09-28 12:35:21 +10:00
|
|
|
_persisted: existing,
|
2023-07-26 18:52:53 +10:00
|
|
|
};
|
|
|
|
})
|
|
|
|
.filter((recipient) => {
|
|
|
|
return (
|
2024-02-09 12:37:17 +11:00
|
|
|
recipient._persisted?.role === RecipientRole.CC ||
|
|
|
|
(recipient._persisted?.sendStatus !== SendStatus.SENT &&
|
|
|
|
recipient._persisted?.signingStatus !== SigningStatus.SIGNED)
|
2023-07-26 18:52:53 +10:00
|
|
|
);
|
|
|
|
});
|
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);
|
|
|
|
|
|
|
|
if (recipient.actionAuth !== undefined) {
|
|
|
|
authOptions = createRecipientAuthOptions({
|
|
|
|
accessAuth: authOptions.accessAuth,
|
|
|
|
actionAuth: recipient.actionAuth,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
user,
|
|
|
|
requestMetadata,
|
|
|
|
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,
|
|
|
|
user,
|
|
|
|
requestMetadata,
|
2024-03-28 13:13:29 +08:00
|
|
|
data: {
|
|
|
|
...baseAuditLog,
|
|
|
|
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,
|
|
|
|
user,
|
|
|
|
requestMetadata,
|
|
|
|
data: {
|
|
|
|
recipientEmail: recipient.email,
|
|
|
|
recipientName: recipient.name,
|
|
|
|
recipientId: recipient.id,
|
|
|
|
recipientRole: recipient.role,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
});
|
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;
|
|
|
|
});
|
|
|
|
|
|
|
|
return [...filteredRecipients, ...persistedRecipients];
|
2023-06-21 23:49:23 +10:00
|
|
|
};
|