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,12 +1,17 @@
import { EnvelopeType } from '@prisma/client';
import { match } from 'ts-pattern';
import { prisma } from '@documenso/prisma';
import { AppError, AppErrorCode } from '../../errors/app-error';
import { mapSecondaryIdToDocumentId, mapSecondaryIdToTemplateId } from '../../utils/envelope';
import { buildTeamWhereQuery } from '../../utils/teams';
export type GetRecipientByIdOptions = {
recipientId: number;
userId: number;
teamId: number;
type: EnvelopeType;
};
/**
@ -17,16 +22,23 @@ export const getRecipientById = async ({
recipientId,
userId,
teamId,
type,
}: GetRecipientByIdOptions) => {
const recipient = await prisma.recipient.findFirst({
where: {
id: recipientId,
document: {
envelope: {
type,
team: buildTeamWhereQuery({ teamId, userId }),
},
},
include: {
fields: true,
envelope: {
select: {
secondaryId: true,
},
},
},
});
@ -36,5 +48,24 @@ export const getRecipientById = async ({
});
}
return recipient;
const legacyId = match(type)
.with(EnvelopeType.DOCUMENT, () => ({
documentId: mapSecondaryIdToDocumentId(recipient.envelope.secondaryId),
}))
.with(EnvelopeType.TEMPLATE, () => ({
templateId: mapSecondaryIdToTemplateId(recipient.envelope.secondaryId),
}))
.exhaustive();
// Backwards compatibility mapping.
return {
...recipient,
...legacyId,
// eslint-disable-next-line unused-imports/no-unused-vars
fields: recipient.fields.map((field) => ({
...field,
...legacyId,
})),
};
};