mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
feat: add template and field endpoints (#1572)
This commit is contained in:
@ -1,24 +1,114 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import {
|
||||
ZRecipientAccessAuthTypesSchema,
|
||||
ZRecipientActionAuthSchema,
|
||||
ZRecipientActionAuthTypesSchema,
|
||||
} from '@documenso/lib/types/document-auth';
|
||||
import { RecipientRole } from '@documenso/prisma/client';
|
||||
import { FieldSchema, RecipientSchema } from '@documenso/prisma/generated/zod';
|
||||
|
||||
export const ZGetRecipientQuerySchema = z.object({
|
||||
recipientId: z.number(),
|
||||
teamId: z.number().optional(),
|
||||
});
|
||||
|
||||
export const ZAddSignersMutationSchema = z
|
||||
const ZCreateRecipientSchema = z.object({
|
||||
email: z.string().toLowerCase().email().min(1),
|
||||
name: z.string(),
|
||||
role: z.nativeEnum(RecipientRole),
|
||||
signingOrder: z.number().optional(),
|
||||
accessAuth: ZRecipientAccessAuthTypesSchema.optional().nullable(),
|
||||
actionAuth: ZRecipientActionAuthTypesSchema.optional().nullable(),
|
||||
});
|
||||
|
||||
const ZUpdateRecipientSchema = z.object({
|
||||
id: z.number().describe('The ID of the recipient to update.'),
|
||||
email: z.string().toLowerCase().email().min(1).optional(),
|
||||
name: z.string().optional(),
|
||||
role: z.nativeEnum(RecipientRole).optional(),
|
||||
signingOrder: z.number().optional(),
|
||||
accessAuth: ZRecipientAccessAuthTypesSchema.optional().nullable(),
|
||||
actionAuth: ZRecipientActionAuthTypesSchema.optional().nullable(),
|
||||
});
|
||||
|
||||
/**
|
||||
* Use this when returning base recipients from the API.
|
||||
*/
|
||||
export const ZRecipientBaseResponseSchema = RecipientSchema.pick({
|
||||
id: true,
|
||||
documentId: true,
|
||||
templateId: true,
|
||||
email: true,
|
||||
name: true,
|
||||
token: true,
|
||||
documentDeletedAt: true,
|
||||
expired: true,
|
||||
signedAt: true,
|
||||
authOptions: true,
|
||||
signingOrder: true,
|
||||
rejectionReason: true,
|
||||
role: true,
|
||||
readStatus: true,
|
||||
signingStatus: true,
|
||||
sendStatus: true,
|
||||
});
|
||||
|
||||
/**
|
||||
* Use this when returning a full recipient from the API.
|
||||
*/
|
||||
export const ZRecipientResponseSchema = ZRecipientBaseResponseSchema.extend({
|
||||
Field: FieldSchema.array(),
|
||||
});
|
||||
|
||||
export const ZCreateDocumentRecipientRequestSchema = z.object({
|
||||
documentId: z.number(),
|
||||
recipient: ZCreateRecipientSchema,
|
||||
});
|
||||
|
||||
export const ZCreateDocumentRecipientResponseSchema = ZRecipientBaseResponseSchema;
|
||||
|
||||
export const ZCreateDocumentRecipientsRequestSchema = z.object({
|
||||
documentId: z.number(),
|
||||
recipients: z.array(ZCreateRecipientSchema).refine((recipients) => {
|
||||
const emails = recipients.map((recipient) => recipient.email.toLowerCase());
|
||||
|
||||
return new Set(emails).size === emails.length;
|
||||
}),
|
||||
});
|
||||
|
||||
export const ZUpdateDocumentRecipientRequestSchema = z.object({
|
||||
documentId: z.number(),
|
||||
recipient: ZUpdateRecipientSchema,
|
||||
});
|
||||
|
||||
export const ZUpdateDocumentRecipientResponseSchema = ZRecipientResponseSchema;
|
||||
|
||||
export const ZUpdateDocumentRecipientsRequestSchema = z.object({
|
||||
documentId: z.number(),
|
||||
recipients: z.array(ZUpdateRecipientSchema).refine((recipients) => {
|
||||
const emails = recipients
|
||||
.filter((recipient) => recipient.email !== undefined)
|
||||
.map((recipient) => recipient.email?.toLowerCase());
|
||||
|
||||
return new Set(emails).size === emails.length;
|
||||
}),
|
||||
});
|
||||
|
||||
export const ZUpdateDocumentRecipientsResponseSchema = z.object({
|
||||
recipients: z.array(ZRecipientResponseSchema),
|
||||
});
|
||||
|
||||
export const ZDeleteDocumentRecipientRequestSchema = z.object({
|
||||
recipientId: z.number(),
|
||||
});
|
||||
|
||||
export const ZSetDocumentRecipientsRequestSchema = z
|
||||
.object({
|
||||
documentId: z.number(),
|
||||
teamId: z.number().optional(),
|
||||
signers: z.array(
|
||||
recipients: z.array(
|
||||
z.object({
|
||||
nativeId: z.number().optional(),
|
||||
email: z.string().email().min(1),
|
||||
email: z.string().toLowerCase().email().min(1),
|
||||
name: z.string(),
|
||||
role: z.nativeEnum(RecipientRole),
|
||||
signingOrder: z.number().optional(),
|
||||
@ -28,7 +118,7 @@ export const ZAddSignersMutationSchema = z
|
||||
})
|
||||
.refine(
|
||||
(schema) => {
|
||||
const emails = schema.signers.map((signer) => signer.email.toLowerCase());
|
||||
const emails = schema.recipients.map((recipient) => recipient.email.toLowerCase());
|
||||
|
||||
return new Set(emails).size === emails.length;
|
||||
},
|
||||
@ -36,16 +126,59 @@ export const ZAddSignersMutationSchema = z
|
||||
{ message: 'Signers must have unique emails', path: ['signers__root'] },
|
||||
);
|
||||
|
||||
export type TAddSignersMutationSchema = z.infer<typeof ZAddSignersMutationSchema>;
|
||||
export const ZCreateTemplateRecipientRequestSchema = z.object({
|
||||
templateId: z.number(),
|
||||
recipient: ZCreateRecipientSchema,
|
||||
});
|
||||
|
||||
export const ZAddTemplateSignersMutationSchema = z
|
||||
export const ZCreateTemplateRecipientResponseSchema = ZRecipientBaseResponseSchema;
|
||||
|
||||
export const ZCreateTemplateRecipientsRequestSchema = z.object({
|
||||
templateId: z.number(),
|
||||
recipients: z.array(ZCreateRecipientSchema).refine((recipients) => {
|
||||
const emails = recipients.map((recipient) => recipient.email);
|
||||
|
||||
return new Set(emails).size === emails.length;
|
||||
}),
|
||||
});
|
||||
|
||||
export const ZUpdateTemplateRecipientRequestSchema = z.object({
|
||||
templateId: z.number(),
|
||||
recipient: ZUpdateRecipientSchema,
|
||||
});
|
||||
|
||||
export const ZUpdateTemplateRecipientResponseSchema = ZRecipientResponseSchema;
|
||||
|
||||
export const ZUpdateTemplateRecipientsRequestSchema = z.object({
|
||||
templateId: z.number(),
|
||||
recipients: z.array(ZUpdateRecipientSchema).refine((recipients) => {
|
||||
const emails = recipients
|
||||
.filter((recipient) => recipient.email !== undefined)
|
||||
.map((recipient) => recipient.email);
|
||||
|
||||
return new Set(emails).size === emails.length;
|
||||
}),
|
||||
});
|
||||
|
||||
export const ZUpdateTemplateRecipientsResponseSchema = z.object({
|
||||
recipients: z.array(ZRecipientResponseSchema).refine((recipients) => {
|
||||
const emails = recipients.map((recipient) => recipient.email);
|
||||
|
||||
return new Set(emails).size === emails.length;
|
||||
}),
|
||||
});
|
||||
|
||||
export const ZDeleteTemplateRecipientRequestSchema = z.object({
|
||||
recipientId: z.number(),
|
||||
});
|
||||
|
||||
export const ZSetTemplateRecipientsRequestSchema = z
|
||||
.object({
|
||||
teamId: z.number().optional(),
|
||||
templateId: z.number(),
|
||||
signers: z.array(
|
||||
recipients: z.array(
|
||||
z.object({
|
||||
nativeId: z.number().optional(),
|
||||
email: z.string().email().min(1),
|
||||
email: z.string().toLowerCase().email().min(1),
|
||||
name: z.string(),
|
||||
role: z.nativeEnum(RecipientRole),
|
||||
signingOrder: z.number().optional(),
|
||||
@ -55,16 +188,14 @@ export const ZAddTemplateSignersMutationSchema = z
|
||||
})
|
||||
.refine(
|
||||
(schema) => {
|
||||
const emails = schema.signers.map((signer) => signer.email.toLowerCase());
|
||||
const emails = schema.recipients.map((recipient) => recipient.email);
|
||||
|
||||
return new Set(emails).size === emails.length;
|
||||
},
|
||||
// Dirty hack to handle errors when .root is populated for an array type
|
||||
{ message: 'Signers must have unique emails', path: ['signers__root'] },
|
||||
{ message: 'Recipients must have unique emails', path: ['recipients__root'] },
|
||||
);
|
||||
|
||||
export type TAddTemplateSignersMutationSchema = z.infer<typeof ZAddTemplateSignersMutationSchema>;
|
||||
|
||||
export const ZCompleteDocumentWithTokenMutationSchema = z.object({
|
||||
token: z.string(),
|
||||
documentId: z.number(),
|
||||
@ -85,3 +216,32 @@ export const ZRejectDocumentWithTokenMutationSchema = z.object({
|
||||
export type TRejectDocumentWithTokenMutationSchema = z.infer<
|
||||
typeof ZRejectDocumentWithTokenMutationSchema
|
||||
>;
|
||||
|
||||
/**
|
||||
* Legacy schema. Remove after deployment (when addSigners trpc is removed).
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
export const ZAddSignersMutationSchema = z
|
||||
.object({
|
||||
documentId: z.number(),
|
||||
signers: z.array(
|
||||
z.object({
|
||||
nativeId: z.number().optional(),
|
||||
email: z.string().toLowerCase().email().min(1),
|
||||
name: z.string(),
|
||||
role: z.nativeEnum(RecipientRole),
|
||||
signingOrder: z.number().optional(),
|
||||
actionAuth: ZRecipientActionAuthTypesSchema.optional().nullable(),
|
||||
}),
|
||||
),
|
||||
})
|
||||
.refine(
|
||||
(schema) => {
|
||||
const emails = schema.signers.map((signer) => signer.email.toLowerCase());
|
||||
|
||||
return new Set(emails).size === emails.length;
|
||||
},
|
||||
// Dirty hack to handle errors when .root is populated for an array type
|
||||
{ message: 'Signers must have unique emails', path: ['signers__root'] },
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user