chore: template attachments

This commit is contained in:
Catalin Pit
2025-07-07 13:31:55 +03:00
parent 30b240cba2
commit e19da93ce2
19 changed files with 583 additions and 195 deletions

View File

@ -1,26 +1,32 @@
import { findAttachments } from '@documenso/lib/server-only/attachment/find-attachments';
import { findDocumentAttachments } from '@documenso/lib/server-only/attachment/find-document-attachments';
import { findTemplateAttachments } from '@documenso/lib/server-only/attachment/find-template-attachments';
import { setDocumentAttachments } from '@documenso/lib/server-only/attachment/set-document-attachments';
import { setTemplateAttachments } from '@documenso/lib/server-only/attachment/set-template-attachments';
import { authenticatedProcedure, router } from '../trpc';
import {
ZGetDocumentAttachmentsResponseSchema,
ZGetDocumentAttachmentsSchema,
ZGetTemplateAttachmentsResponseSchema,
ZGetTemplateAttachmentsSchema,
ZSetDocumentAttachmentsResponseSchema,
ZSetDocumentAttachmentsSchema,
ZSetTemplateAttachmentsResponseSchema,
ZSetTemplateAttachmentsSchema,
} from './schema';
export const attachmentRouter = router({
/**
* @private
*/
getAttachments: authenticatedProcedure
getDocumentAttachments: authenticatedProcedure
.input(ZGetDocumentAttachmentsSchema)
.output(ZGetDocumentAttachmentsResponseSchema)
.query(async ({ input, ctx }) => {
const { documentId } = input;
const { user } = ctx;
const attachments = await findAttachments({
const attachments = await findDocumentAttachments({
documentId,
userId: user.id,
teamId: ctx.teamId,
@ -42,6 +48,40 @@ export const attachmentRouter = router({
attachments,
});
return updatedAttachments;
}),
/**
* @private
*/
getTemplateAttachments: authenticatedProcedure
.input(ZGetTemplateAttachmentsSchema)
.output(ZGetTemplateAttachmentsResponseSchema)
.query(async ({ input, ctx }) => {
const { templateId } = input;
const attachments = await findTemplateAttachments({
templateId,
userId: ctx.user.id,
teamId: ctx.teamId,
});
return attachments;
}),
/**
* @private
*/
setTemplateAttachments: authenticatedProcedure
.input(ZSetTemplateAttachmentsSchema)
.output(ZSetTemplateAttachmentsResponseSchema)
.mutation(async ({ input, ctx }) => {
const { templateId, attachments } = input;
const updatedAttachments = await setTemplateAttachments({
templateId,
attachments,
});
return updatedAttachments;
}),
});

View File

@ -15,6 +15,19 @@ export const ZGetDocumentAttachmentsResponseSchema = z.array(
}),
);
export const ZGetTemplateAttachmentsSchema = z.object({
templateId: z.number(),
});
export const ZGetTemplateAttachmentsResponseSchema = z.array(
z.object({
id: z.string(),
label: z.string(),
url: z.string(),
type: z.nativeEnum(AttachmentType),
}),
);
export const ZSetDocumentAttachmentsSchema = z.object({
documentId: z.number(),
attachments: z.array(
@ -37,3 +50,26 @@ export const ZSetDocumentAttachmentsResponseSchema = z.array(
type: z.nativeEnum(AttachmentType),
}),
);
export const ZSetTemplateAttachmentsSchema = z.object({
templateId: z.number(),
attachments: z.array(
z.object({
id: z.string(),
label: z.string().min(1, 'Label is required'),
url: z.string().url('Invalid URL'),
type: z.nativeEnum(AttachmentType),
}),
),
});
export type TSetTemplateAttachmentsSchema = z.infer<typeof ZSetTemplateAttachmentsSchema>;
export const ZSetTemplateAttachmentsResponseSchema = z.array(
z.object({
id: z.string(),
label: z.string(),
url: z.string(),
type: z.nativeEnum(AttachmentType),
}),
);

View File

@ -14,7 +14,6 @@ import {
ZTemplateManySchema,
ZTemplateSchema,
} from '@documenso/lib/types/template';
import AttachmentSchema from '@documenso/prisma/generated/zod/modelSchema/AttachmentSchema';
import { TemplateDirectLinkSchema } from '@documenso/prisma/generated/zod/modelSchema/TemplateDirectLinkSchema';
import {
@ -156,16 +155,6 @@ export const ZUpdateTemplateRequestSchema = z.object({
.optional(),
type: z.nativeEnum(TemplateType).optional(),
useLegacyFieldInsertion: z.boolean().optional(),
attachments: AttachmentSchema.pick({
id: true,
label: true,
url: true,
})
.extend({
formId: z.string().min(1),
})
.array()
.optional(),
})
.optional(),
meta: z