mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 16:51:38 +10:00
fix: add /api/v1 backwards compat
This commit is contained in:
@ -100,13 +100,21 @@ export type TCreateDocumentMutationResponseSchema = z.infer<
|
|||||||
|
|
||||||
export const ZCreateDocumentFromTemplateMutationSchema = z.object({
|
export const ZCreateDocumentFromTemplateMutationSchema = z.object({
|
||||||
title: z.string().min(1),
|
title: z.string().min(1),
|
||||||
recipients: z.array(
|
recipients: z.union([
|
||||||
z.object({
|
z.array(
|
||||||
name: z.string().min(1),
|
z.object({
|
||||||
email: z.string().email().min(1),
|
id: z.number(),
|
||||||
role: z.nativeEnum(RecipientRole).optional().default(RecipientRole.SIGNER),
|
name: z.string().min(1),
|
||||||
}),
|
email: z.string().email().min(1),
|
||||||
),
|
}),
|
||||||
|
),
|
||||||
|
z.array(
|
||||||
|
z.object({
|
||||||
|
name: z.string().min(1),
|
||||||
|
email: z.string().email().min(1),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
]),
|
||||||
meta: z
|
meta: z
|
||||||
.object({
|
.object({
|
||||||
subject: z.string(),
|
subject: z.string(),
|
||||||
|
|||||||
@ -1,15 +1,23 @@
|
|||||||
import { nanoid } from '@documenso/lib/universal/id';
|
import { nanoid } from '@documenso/lib/universal/id';
|
||||||
import { prisma } from '@documenso/prisma';
|
import { prisma } from '@documenso/prisma';
|
||||||
|
import type { Recipient } from '@documenso/prisma/client';
|
||||||
|
|
||||||
|
type RecipientWithId = {
|
||||||
|
id: number;
|
||||||
|
name?: string;
|
||||||
|
email: string;
|
||||||
|
};
|
||||||
|
|
||||||
export type CreateDocumentFromTemplateOptions = {
|
export type CreateDocumentFromTemplateOptions = {
|
||||||
templateId: number;
|
templateId: number;
|
||||||
userId: number;
|
userId: number;
|
||||||
teamId?: number;
|
teamId?: number;
|
||||||
recipients: {
|
recipients:
|
||||||
id: number;
|
| RecipientWithId[]
|
||||||
name?: string;
|
| {
|
||||||
email: string;
|
name?: string;
|
||||||
}[];
|
email: string;
|
||||||
|
}[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createDocumentFromTemplate = async ({
|
export const createDocumentFromTemplate = async ({
|
||||||
@ -48,19 +56,38 @@ export const createDocumentFromTemplate = async ({
|
|||||||
throw new Error('Template not found.');
|
throw new Error('Template not found.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const finalRecipients = template.Recipient.map((templateRecipient) => {
|
if (recipients.length === 0 || recipients.length !== template.Recipient.length) {
|
||||||
const foundRecipient = recipients.find((recipient) => recipient.id === templateRecipient.id);
|
throw new Error('Invalid number of recipients.');
|
||||||
|
}
|
||||||
|
|
||||||
if (!foundRecipient) {
|
let finalRecipients: Pick<Recipient, 'name' | 'email' | 'role'>[] = [];
|
||||||
throw new Error('Recipient not found.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
if (Object.prototype.hasOwnProperty.call(recipients[0], 'id')) {
|
||||||
name: foundRecipient.name,
|
finalRecipients = template.Recipient.map((templateRecipient) => {
|
||||||
email: foundRecipient.email,
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
||||||
role: templateRecipient.role,
|
const foundRecipient = (recipients as RecipientWithId[]).find(
|
||||||
};
|
(recipient) => recipient.id === templateRecipient.id,
|
||||||
});
|
);
|
||||||
|
|
||||||
|
if (!foundRecipient) {
|
||||||
|
throw new Error('Recipient not found.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: foundRecipient.name ?? '',
|
||||||
|
email: foundRecipient.email,
|
||||||
|
role: templateRecipient.role,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Backwards compatible logic for /v1/ API where we use the index to associate
|
||||||
|
// the provided recipient with the template recipient.
|
||||||
|
finalRecipients = recipients.map((recipient, index) => ({
|
||||||
|
name: recipient.name ?? '',
|
||||||
|
email: recipient.email,
|
||||||
|
role: template.Recipient[index].role,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
const documentData = await prisma.documentData.create({
|
const documentData = await prisma.documentData.create({
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
Reference in New Issue
Block a user