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,4 +1,4 @@
|
||||
import type { Recipient } from '@prisma/client';
|
||||
import { EnvelopeType, type Recipient } from '@prisma/client';
|
||||
import { nanoid } from 'nanoid';
|
||||
|
||||
import {
|
||||
@ -8,50 +8,55 @@ import {
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
import { AppError, AppErrorCode } from '../../errors/app-error';
|
||||
import { buildTeamWhereQuery } from '../../utils/teams';
|
||||
import { type EnvelopeIdOptions, mapSecondaryIdToTemplateId } from '../../utils/envelope';
|
||||
import { getEnvelopeWhereInput } from '../envelope/get-envelope-by-id';
|
||||
|
||||
export type CreateTemplateDirectLinkOptions = {
|
||||
templateId: number;
|
||||
id: EnvelopeIdOptions;
|
||||
userId: number;
|
||||
teamId: number;
|
||||
directRecipientId?: number;
|
||||
};
|
||||
|
||||
export const createTemplateDirectLink = async ({
|
||||
templateId,
|
||||
id,
|
||||
userId,
|
||||
teamId,
|
||||
directRecipientId,
|
||||
}: CreateTemplateDirectLinkOptions) => {
|
||||
const template = await prisma.template.findFirst({
|
||||
where: {
|
||||
id: templateId,
|
||||
team: buildTeamWhereQuery({ teamId, userId }),
|
||||
},
|
||||
const { envelopeWhereInput } = await getEnvelopeWhereInput({
|
||||
id,
|
||||
type: EnvelopeType.TEMPLATE,
|
||||
userId,
|
||||
teamId,
|
||||
});
|
||||
|
||||
const envelope = await prisma.envelope.findFirst({
|
||||
where: envelopeWhereInput,
|
||||
include: {
|
||||
recipients: true,
|
||||
directLink: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!template) {
|
||||
if (!envelope) {
|
||||
throw new AppError(AppErrorCode.NOT_FOUND, { message: 'Template not found' });
|
||||
}
|
||||
|
||||
if (template.directLink) {
|
||||
if (envelope.directLink) {
|
||||
throw new AppError(AppErrorCode.ALREADY_EXISTS, { message: 'Direct template already exists' });
|
||||
}
|
||||
|
||||
if (
|
||||
directRecipientId &&
|
||||
!template.recipients.find((recipient) => recipient.id === directRecipientId)
|
||||
!envelope.recipients.find((recipient) => recipient.id === directRecipientId)
|
||||
) {
|
||||
throw new AppError(AppErrorCode.NOT_FOUND, { message: 'Recipient not found' });
|
||||
}
|
||||
|
||||
if (
|
||||
!directRecipientId &&
|
||||
template.recipients.find(
|
||||
envelope.recipients.find(
|
||||
(recipient) => recipient.email.toLowerCase() === DIRECT_TEMPLATE_RECIPIENT_EMAIL,
|
||||
)
|
||||
) {
|
||||
@ -60,13 +65,13 @@ export const createTemplateDirectLink = async ({
|
||||
});
|
||||
}
|
||||
|
||||
return await prisma.$transaction(async (tx) => {
|
||||
const createdDirectLink = await prisma.$transaction(async (tx) => {
|
||||
let recipient: Recipient | undefined;
|
||||
|
||||
if (directRecipientId) {
|
||||
recipient = await tx.recipient.update({
|
||||
where: {
|
||||
templateId,
|
||||
envelopeId: envelope.id,
|
||||
id: directRecipientId,
|
||||
},
|
||||
data: {
|
||||
@ -77,7 +82,7 @@ export const createTemplateDirectLink = async ({
|
||||
} else {
|
||||
recipient = await tx.recipient.create({
|
||||
data: {
|
||||
templateId,
|
||||
envelopeId: envelope.id,
|
||||
name: DIRECT_TEMPLATE_RECIPIENT_NAME,
|
||||
email: DIRECT_TEMPLATE_RECIPIENT_EMAIL,
|
||||
token: nanoid(),
|
||||
@ -87,11 +92,20 @@ export const createTemplateDirectLink = async ({
|
||||
|
||||
return await tx.templateDirectLink.create({
|
||||
data: {
|
||||
templateId,
|
||||
envelopeId: envelope.id,
|
||||
enabled: true,
|
||||
token: nanoid(),
|
||||
directTemplateRecipientId: recipient.id,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
id: createdDirectLink.id,
|
||||
token: createdDirectLink.token,
|
||||
createdAt: createdDirectLink.createdAt,
|
||||
enabled: createdDirectLink.enabled,
|
||||
directTemplateRecipientId: createdDirectLink.directTemplateRecipientId,
|
||||
templateId: mapSecondaryIdToTemplateId(envelope.secondaryId),
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user