mirror of
https://github.com/documenso/documenso.git
synced 2025-11-12 15:53:02 +10:00
Adds support for creating documents and templates using our embed components. Support is super primitive at the moment and is being polished.
64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
|
import { verifyEmbeddingPresignToken } from '@documenso/lib/server-only/embedding-presign/verify-embedding-presign-token';
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
import { procedure } from '../trpc';
|
|
import {
|
|
ZGetEmbeddingDocumentRequestSchema,
|
|
ZGetEmbeddingDocumentResponseSchema,
|
|
} from './get-embedding-document.types';
|
|
|
|
export const getEmbeddingDocumentRoute = procedure
|
|
.input(ZGetEmbeddingDocumentRequestSchema)
|
|
.output(ZGetEmbeddingDocumentResponseSchema)
|
|
.query(async ({ input, ctx: { req } }) => {
|
|
try {
|
|
const authorizationHeader = req.headers.get('authorization');
|
|
|
|
const [presignToken] = (authorizationHeader || '')
|
|
.split('Bearer ')
|
|
.filter((s) => s.length > 0);
|
|
|
|
if (!presignToken) {
|
|
throw new AppError(AppErrorCode.UNAUTHORIZED, {
|
|
message: 'No presign token provided',
|
|
});
|
|
}
|
|
|
|
const apiToken = await verifyEmbeddingPresignToken({ token: presignToken });
|
|
|
|
const { documentId } = input;
|
|
|
|
const document = await prisma.document.findFirst({
|
|
where: {
|
|
id: documentId,
|
|
userId: apiToken.userId,
|
|
...(apiToken.teamId ? { teamId: apiToken.teamId } : {}),
|
|
},
|
|
include: {
|
|
documentData: true,
|
|
recipients: true,
|
|
fields: true,
|
|
},
|
|
});
|
|
|
|
if (!document) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
message: 'Document not found',
|
|
});
|
|
}
|
|
|
|
return {
|
|
document,
|
|
};
|
|
} catch (error) {
|
|
if (error instanceof AppError) {
|
|
throw error;
|
|
}
|
|
|
|
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
|
message: 'Failed to get document',
|
|
});
|
|
}
|
|
});
|