mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 16:51:38 +10:00
feat: migrate templates and documents to envelope model
This commit is contained in:
@ -1,6 +1,9 @@
|
||||
import { EnvelopeType } from '@prisma/client';
|
||||
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
import { AppError, AppErrorCode } from '../../errors/app-error';
|
||||
import { mapSecondaryIdToTemplateId } from '../../utils/envelope';
|
||||
|
||||
export interface GetTemplateByDirectLinkTokenOptions {
|
||||
token: string;
|
||||
@ -9,8 +12,9 @@ export interface GetTemplateByDirectLinkTokenOptions {
|
||||
export const getTemplateByDirectLinkToken = async ({
|
||||
token,
|
||||
}: GetTemplateByDirectLinkTokenOptions) => {
|
||||
const template = await prisma.template.findFirst({
|
||||
const envelope = await prisma.envelope.findFirst({
|
||||
where: {
|
||||
type: EnvelopeType.TEMPLATE,
|
||||
directLink: {
|
||||
token,
|
||||
enabled: true,
|
||||
@ -23,21 +27,53 @@ export const getTemplateByDirectLinkToken = async ({
|
||||
fields: true,
|
||||
},
|
||||
},
|
||||
templateDocumentData: true,
|
||||
templateMeta: true,
|
||||
envelopeItems: {
|
||||
include: {
|
||||
documentData: true,
|
||||
},
|
||||
},
|
||||
documentMeta: true,
|
||||
},
|
||||
});
|
||||
|
||||
const directLink = template?.directLink;
|
||||
const directLink = envelope?.directLink;
|
||||
|
||||
// Todo: Envelopes
|
||||
const firstDocumentData = envelope?.envelopeItems[0]?.documentData;
|
||||
|
||||
// Doing this to enforce type safety for directLink.
|
||||
if (!directLink) {
|
||||
if (!directLink || !firstDocumentData) {
|
||||
throw new AppError(AppErrorCode.NOT_FOUND);
|
||||
}
|
||||
|
||||
const recipientsWithMappedFields = envelope.recipients.map((recipient) => ({
|
||||
...recipient,
|
||||
fields: recipient.fields.map((field) => ({
|
||||
...field,
|
||||
templateId: mapSecondaryIdToTemplateId(envelope.secondaryId),
|
||||
documentId: undefined,
|
||||
})),
|
||||
}));
|
||||
|
||||
// Backwards compatibility mapping.
|
||||
return {
|
||||
...template,
|
||||
id: mapSecondaryIdToTemplateId(envelope.secondaryId),
|
||||
type: envelope.templateType,
|
||||
visibility: envelope.visibility,
|
||||
externalId: envelope.externalId,
|
||||
title: envelope.title,
|
||||
userId: envelope.userId,
|
||||
teamId: envelope.teamId,
|
||||
authOptions: envelope.authOptions,
|
||||
createdAt: envelope.createdAt,
|
||||
updatedAt: envelope.updatedAt,
|
||||
publicTitle: envelope.publicTitle,
|
||||
publicDescription: envelope.publicDescription,
|
||||
folderId: envelope.folderId,
|
||||
templateDocumentData: firstDocumentData,
|
||||
directLink,
|
||||
fields: template.recipients.map((recipient) => recipient.fields).flat(),
|
||||
templateMeta: envelope.documentMeta,
|
||||
recipients: recipientsWithMappedFields,
|
||||
fields: recipientsWithMappedFields.flatMap((recipient) => recipient.fields),
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user