feat: migrate templates and documents to envelope model

This commit is contained in:
David Nguyen
2025-09-11 18:23:38 +10:00
parent eec2307634
commit bf89bc781b
234 changed files with 8677 additions and 6054 deletions

View File

@ -1,8 +1,11 @@
import { EnvelopeType } from '@prisma/client';
import { P, match } from 'ts-pattern';
import { prisma } from '@documenso/prisma';
import { AppError, AppErrorCode } from '../../errors/app-error';
import { alphaid } from '../../universal/id';
import { unsafeBuildEnvelopeIdQuery } from '../../utils/envelope';
export type CreateSharingIdOptions =
| {
@ -15,12 +18,29 @@ export type CreateSharingIdOptions =
};
export const createOrGetShareLink = async ({ documentId, ...options }: CreateSharingIdOptions) => {
const envelope = await prisma.envelope.findUnique({
where: unsafeBuildEnvelopeIdQuery(
{
type: 'documentId',
id: documentId,
},
EnvelopeType.DOCUMENT,
),
select: {
id: true,
},
});
if (!envelope) {
throw new AppError(AppErrorCode.NOT_FOUND);
}
const email = await match(options)
.with({ token: P.string }, async ({ token }) => {
return await prisma.recipient
.findFirst({
where: {
documentId,
envelopeId: envelope.id,
token,
},
})
@ -32,6 +52,9 @@ export const createOrGetShareLink = async ({ documentId, ...options }: CreateSha
where: {
id: userId,
},
select: {
email: true,
},
})
.then((user) => user?.email);
})
@ -43,14 +66,14 @@ export const createOrGetShareLink = async ({ documentId, ...options }: CreateSha
return await prisma.documentShareLink.upsert({
where: {
documentId_email: {
envelopeId_email: {
envelopeId: envelope.id,
email,
documentId,
},
},
create: {
email,
documentId,
envelopeId: envelope.id,
slug: alphaid(14),
},
update: {},

View File

@ -1,13 +0,0 @@
import { prisma } from '@documenso/prisma';
export type GetShareLinkBySlugOptions = {
slug: string;
};
export const getShareLinkBySlug = async ({ slug }: GetShareLinkBySlugOptions) => {
return await prisma.documentShareLink.findFirstOrThrow({
where: {
slug,
},
});
};