mirror of
https://github.com/documenso/documenso.git
synced 2025-11-18 18:51:37 +10:00
feat: attachments
This commit is contained in:
@ -0,0 +1,46 @@
|
||||
import { buildTeamWhereQuery } from '@documenso/lib/utils/teams';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
import { authenticatedProcedure } from '../trpc';
|
||||
import {
|
||||
ZGetTemplateAttachmentsResponseSchema,
|
||||
ZGetTemplateAttachmentsSchema,
|
||||
} from './find-template-attachments.types';
|
||||
|
||||
export const findTemplateAttachmentsRoute = 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;
|
||||
});
|
||||
|
||||
export type FindTemplateAttachmentsOptions = {
|
||||
templateId: number;
|
||||
userId: number;
|
||||
teamId: number;
|
||||
};
|
||||
|
||||
export const findTemplateAttachments = async ({
|
||||
templateId,
|
||||
userId,
|
||||
teamId,
|
||||
}: FindTemplateAttachmentsOptions) => {
|
||||
const attachments = await prisma.attachment.findMany({
|
||||
where: {
|
||||
template: {
|
||||
id: templateId,
|
||||
team: buildTeamWhereQuery({ teamId, userId }),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return attachments;
|
||||
};
|
||||
@ -0,0 +1,16 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import { AttachmentType } from '@documenso/prisma/generated/types';
|
||||
|
||||
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),
|
||||
}),
|
||||
);
|
||||
@ -29,6 +29,7 @@ import { getPresignPostUrl } from '@documenso/lib/universal/upload/server-action
|
||||
|
||||
import { ZGenericSuccessResponse, ZSuccessResponseSchema } from '../document-router/schema';
|
||||
import { authenticatedProcedure, maybeAuthenticatedProcedure, router } from '../trpc';
|
||||
import { findTemplateAttachmentsRoute } from './find-template-attachments';
|
||||
import {
|
||||
ZBulkSendTemplateMutationSchema,
|
||||
ZCreateDocumentFromDirectTemplateRequestSchema,
|
||||
@ -52,8 +53,14 @@ import {
|
||||
ZUpdateTemplateRequestSchema,
|
||||
ZUpdateTemplateResponseSchema,
|
||||
} from './schema';
|
||||
import { setTemplateAttachmentsRoute } from './set-template-attachments';
|
||||
|
||||
export const templateRouter = router({
|
||||
attachments: {
|
||||
find: findTemplateAttachmentsRoute,
|
||||
set: setTemplateAttachmentsRoute,
|
||||
},
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
|
||||
@ -0,0 +1,95 @@
|
||||
import type { Attachment } from '@prisma/client';
|
||||
|
||||
import { AppError } from '@documenso/lib/errors/app-error';
|
||||
import { AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||
import { buildTeamWhereQuery } from '@documenso/lib/utils/teams';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
import { authenticatedProcedure } from '../trpc';
|
||||
import {
|
||||
ZSetTemplateAttachmentsResponseSchema,
|
||||
ZSetTemplateAttachmentsSchema,
|
||||
} from './set-template-attachments.types';
|
||||
|
||||
export const setTemplateAttachmentsRoute = authenticatedProcedure
|
||||
.input(ZSetTemplateAttachmentsSchema)
|
||||
.output(ZSetTemplateAttachmentsResponseSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { templateId, attachments } = input;
|
||||
|
||||
const updatedAttachments = await setTemplateAttachments({
|
||||
templateId,
|
||||
userId: ctx.user.id,
|
||||
teamId: ctx.teamId,
|
||||
attachments,
|
||||
});
|
||||
|
||||
return updatedAttachments;
|
||||
});
|
||||
|
||||
export type CreateAttachmentsOptions = {
|
||||
templateId: number;
|
||||
attachments: Pick<Attachment, 'id' | 'label' | 'url' | 'type'>[];
|
||||
userId: number;
|
||||
teamId: number;
|
||||
};
|
||||
|
||||
export const setTemplateAttachments = async ({
|
||||
templateId,
|
||||
attachments,
|
||||
userId,
|
||||
teamId,
|
||||
}: CreateAttachmentsOptions) => {
|
||||
const template = await prisma.template.findUnique({
|
||||
where: {
|
||||
id: templateId,
|
||||
team: buildTeamWhereQuery({ teamId, userId }),
|
||||
},
|
||||
});
|
||||
|
||||
if (!template) {
|
||||
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||
message: 'Template not found',
|
||||
});
|
||||
}
|
||||
|
||||
const existingAttachments = await prisma.attachment.findMany({
|
||||
where: {
|
||||
templateId,
|
||||
},
|
||||
});
|
||||
|
||||
const newIds = attachments.map((a) => a.id).filter(Boolean);
|
||||
const toDelete = existingAttachments.filter((existing) => !newIds.includes(existing.id));
|
||||
|
||||
if (toDelete.length > 0) {
|
||||
await prisma.attachment.deleteMany({
|
||||
where: {
|
||||
id: { in: toDelete.map((a) => a.id) },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const upsertedAttachments: Attachment[] = [];
|
||||
|
||||
for (const attachment of attachments) {
|
||||
const updated = await prisma.attachment.upsert({
|
||||
where: { id: attachment.id, templateId: template.id },
|
||||
update: {
|
||||
label: attachment.label,
|
||||
url: attachment.url,
|
||||
type: attachment.type,
|
||||
templateId,
|
||||
},
|
||||
create: {
|
||||
label: attachment.label,
|
||||
url: attachment.url,
|
||||
type: attachment.type,
|
||||
templateId,
|
||||
},
|
||||
});
|
||||
upsertedAttachments.push(updated);
|
||||
}
|
||||
|
||||
return upsertedAttachments;
|
||||
};
|
||||
@ -0,0 +1,26 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import { AttachmentType } from '@documenso/prisma/generated/types';
|
||||
|
||||
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),
|
||||
}),
|
||||
);
|
||||
Reference in New Issue
Block a user