mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
Adds document access tokens and QR code functionality to enable secure document sharing via URLs. It includes a new document access page that allows viewing and downloading documents through tokenized links.
95 lines
2.0 KiB
TypeScript
95 lines
2.0 KiB
TypeScript
import { DocumentSource, type Prisma } from '@prisma/client';
|
|
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
import { AppError, AppErrorCode } from '../../errors/app-error';
|
|
import { prefixedId } from '../../universal/id';
|
|
import { getDocumentWhereInput } from './get-document-by-id';
|
|
|
|
export interface DuplicateDocumentOptions {
|
|
documentId: number;
|
|
userId: number;
|
|
teamId?: number;
|
|
}
|
|
|
|
export const duplicateDocument = async ({
|
|
documentId,
|
|
userId,
|
|
teamId,
|
|
}: DuplicateDocumentOptions) => {
|
|
const documentWhereInput = await getDocumentWhereInput({
|
|
documentId,
|
|
userId,
|
|
teamId,
|
|
});
|
|
|
|
const document = await prisma.document.findFirst({
|
|
where: documentWhereInput,
|
|
select: {
|
|
title: true,
|
|
userId: true,
|
|
documentData: {
|
|
select: {
|
|
data: true,
|
|
initialData: true,
|
|
type: true,
|
|
},
|
|
},
|
|
documentMeta: {
|
|
select: {
|
|
message: true,
|
|
subject: true,
|
|
dateFormat: true,
|
|
password: true,
|
|
timezone: true,
|
|
redirectUrl: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!document) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
message: 'Document not found',
|
|
});
|
|
}
|
|
|
|
const createDocumentArguments: Prisma.DocumentCreateArgs = {
|
|
data: {
|
|
title: document.title,
|
|
qrToken: prefixedId('qr'),
|
|
user: {
|
|
connect: {
|
|
id: document.userId,
|
|
},
|
|
},
|
|
documentData: {
|
|
create: {
|
|
...document.documentData,
|
|
data: document.documentData.initialData,
|
|
},
|
|
},
|
|
documentMeta: {
|
|
create: {
|
|
...document.documentMeta,
|
|
},
|
|
},
|
|
source: DocumentSource.DOCUMENT,
|
|
},
|
|
};
|
|
|
|
if (teamId !== undefined) {
|
|
createDocumentArguments.data.team = {
|
|
connect: {
|
|
id: teamId,
|
|
},
|
|
};
|
|
}
|
|
|
|
const createdDocument = await prisma.document.create(createDocumentArguments);
|
|
|
|
return {
|
|
documentId: createdDocument.id,
|
|
};
|
|
};
|