mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 00:32:43 +10:00
feat: add attachments (#2091)
This commit is contained in:
@ -0,0 +1,50 @@
|
||||
import { EnvelopeType } from '@prisma/client';
|
||||
|
||||
import { createAttachment } from '@documenso/lib/server-only/envelope-attachment/create-attachment';
|
||||
import { getEnvelopeById } from '@documenso/lib/server-only/envelope/get-envelope-by-id';
|
||||
|
||||
import { authenticatedProcedure } from '../../trpc';
|
||||
import {
|
||||
ZCreateAttachmentRequestSchema,
|
||||
ZCreateAttachmentResponseSchema,
|
||||
} from './create-attachment.types';
|
||||
|
||||
export const createAttachmentRoute = authenticatedProcedure
|
||||
.meta({
|
||||
openapi: {
|
||||
method: 'POST',
|
||||
path: '/document/attachment/create',
|
||||
summary: 'Create attachment',
|
||||
description: 'Create a new attachment for a document',
|
||||
tags: ['Document'],
|
||||
},
|
||||
})
|
||||
.input(ZCreateAttachmentRequestSchema)
|
||||
.output(ZCreateAttachmentResponseSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { teamId } = ctx;
|
||||
const userId = ctx.user.id;
|
||||
|
||||
const { documentId, data } = input;
|
||||
|
||||
ctx.logger.info({
|
||||
input: { documentId, label: data.label },
|
||||
});
|
||||
|
||||
const envelope = await getEnvelopeById({
|
||||
id: {
|
||||
type: 'documentId',
|
||||
id: documentId,
|
||||
},
|
||||
userId,
|
||||
teamId,
|
||||
type: EnvelopeType.DOCUMENT,
|
||||
});
|
||||
|
||||
await createAttachment({
|
||||
envelopeId: envelope.id,
|
||||
teamId,
|
||||
userId,
|
||||
data,
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,14 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const ZCreateAttachmentRequestSchema = z.object({
|
||||
documentId: z.number(),
|
||||
data: z.object({
|
||||
label: z.string().min(1, 'Label is required'),
|
||||
data: z.string().url('Must be a valid URL'),
|
||||
}),
|
||||
});
|
||||
|
||||
export const ZCreateAttachmentResponseSchema = z.void();
|
||||
|
||||
export type TCreateAttachmentRequest = z.infer<typeof ZCreateAttachmentRequestSchema>;
|
||||
export type TCreateAttachmentResponse = z.infer<typeof ZCreateAttachmentResponseSchema>;
|
||||
@ -0,0 +1,36 @@
|
||||
import { deleteAttachment } from '@documenso/lib/server-only/envelope-attachment/delete-attachment';
|
||||
|
||||
import { authenticatedProcedure } from '../../trpc';
|
||||
import {
|
||||
ZDeleteAttachmentRequestSchema,
|
||||
ZDeleteAttachmentResponseSchema,
|
||||
} from './delete-attachment.types';
|
||||
|
||||
export const deleteAttachmentRoute = authenticatedProcedure
|
||||
.meta({
|
||||
openapi: {
|
||||
method: 'POST',
|
||||
path: '/document/attachment/delete',
|
||||
summary: 'Delete attachment',
|
||||
description: 'Delete an attachment from a document',
|
||||
tags: ['Document'],
|
||||
},
|
||||
})
|
||||
.input(ZDeleteAttachmentRequestSchema)
|
||||
.output(ZDeleteAttachmentResponseSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { teamId } = ctx;
|
||||
const userId = ctx.user.id;
|
||||
|
||||
const { id } = input;
|
||||
|
||||
ctx.logger.info({
|
||||
input: { id },
|
||||
});
|
||||
|
||||
await deleteAttachment({
|
||||
id,
|
||||
userId,
|
||||
teamId,
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,10 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const ZDeleteAttachmentRequestSchema = z.object({
|
||||
id: z.string(),
|
||||
});
|
||||
|
||||
export const ZDeleteAttachmentResponseSchema = z.void();
|
||||
|
||||
export type TDeleteAttachmentRequest = z.infer<typeof ZDeleteAttachmentRequestSchema>;
|
||||
export type TDeleteAttachmentResponse = z.infer<typeof ZDeleteAttachmentResponseSchema>;
|
||||
@ -0,0 +1,52 @@
|
||||
import { EnvelopeType } from '@prisma/client';
|
||||
|
||||
import { findAttachmentsByEnvelopeId } from '@documenso/lib/server-only/envelope-attachment/find-attachments-by-envelope-id';
|
||||
import { getEnvelopeById } from '@documenso/lib/server-only/envelope/get-envelope-by-id';
|
||||
|
||||
import { authenticatedProcedure } from '../../trpc';
|
||||
import {
|
||||
ZFindAttachmentsRequestSchema,
|
||||
ZFindAttachmentsResponseSchema,
|
||||
} from './find-attachments.types';
|
||||
|
||||
export const findAttachmentsRoute = authenticatedProcedure
|
||||
.meta({
|
||||
openapi: {
|
||||
method: 'GET',
|
||||
path: '/document/attachment',
|
||||
summary: 'Find attachments',
|
||||
description: 'Find all attachments for a document',
|
||||
tags: ['Document'],
|
||||
},
|
||||
})
|
||||
.input(ZFindAttachmentsRequestSchema)
|
||||
.output(ZFindAttachmentsResponseSchema)
|
||||
.query(async ({ input, ctx }) => {
|
||||
const { documentId } = input;
|
||||
const { teamId } = ctx;
|
||||
const userId = ctx.user.id;
|
||||
|
||||
ctx.logger.info({
|
||||
input: { documentId },
|
||||
});
|
||||
|
||||
const envelope = await getEnvelopeById({
|
||||
id: {
|
||||
type: 'documentId',
|
||||
id: documentId,
|
||||
},
|
||||
userId,
|
||||
teamId,
|
||||
type: EnvelopeType.DOCUMENT,
|
||||
});
|
||||
|
||||
const data = await findAttachmentsByEnvelopeId({
|
||||
envelopeId: envelope.id,
|
||||
teamId,
|
||||
userId,
|
||||
});
|
||||
|
||||
return {
|
||||
data,
|
||||
};
|
||||
});
|
||||
@ -0,0 +1,21 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import { ZEnvelopeAttachmentTypeSchema } from '@documenso/lib/types/envelope-attachment';
|
||||
|
||||
export const ZFindAttachmentsRequestSchema = z.object({
|
||||
documentId: z.number(),
|
||||
});
|
||||
|
||||
export const ZFindAttachmentsResponseSchema = z.object({
|
||||
data: z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
type: ZEnvelopeAttachmentTypeSchema,
|
||||
label: z.string(),
|
||||
data: z.string(),
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
export type TFindAttachmentsRequest = z.infer<typeof ZFindAttachmentsRequestSchema>;
|
||||
export type TFindAttachmentsResponse = z.infer<typeof ZFindAttachmentsResponseSchema>;
|
||||
@ -0,0 +1,37 @@
|
||||
import { updateAttachment } from '@documenso/lib/server-only/envelope-attachment/update-attachment';
|
||||
|
||||
import { authenticatedProcedure } from '../../trpc';
|
||||
import {
|
||||
ZUpdateAttachmentRequestSchema,
|
||||
ZUpdateAttachmentResponseSchema,
|
||||
} from './update-attachment.types';
|
||||
|
||||
export const updateAttachmentRoute = authenticatedProcedure
|
||||
.meta({
|
||||
openapi: {
|
||||
method: 'POST',
|
||||
path: '/document/attachment/update',
|
||||
summary: 'Update attachment',
|
||||
description: 'Update an existing attachment',
|
||||
tags: ['Document'],
|
||||
},
|
||||
})
|
||||
.input(ZUpdateAttachmentRequestSchema)
|
||||
.output(ZUpdateAttachmentResponseSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { teamId } = ctx;
|
||||
const userId = ctx.user.id;
|
||||
|
||||
const { id, data } = input;
|
||||
|
||||
ctx.logger.info({
|
||||
input: { id },
|
||||
});
|
||||
|
||||
await updateAttachment({
|
||||
id,
|
||||
userId,
|
||||
teamId,
|
||||
data,
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,14 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const ZUpdateAttachmentRequestSchema = z.object({
|
||||
id: z.string(),
|
||||
data: z.object({
|
||||
label: z.string().min(1, 'Label is required'),
|
||||
data: z.string().url('Must be a valid URL'),
|
||||
}),
|
||||
});
|
||||
|
||||
export const ZUpdateAttachmentResponseSchema = z.void();
|
||||
|
||||
export type TUpdateAttachmentRequest = z.infer<typeof ZUpdateAttachmentRequestSchema>;
|
||||
export type TUpdateAttachmentResponse = z.infer<typeof ZUpdateAttachmentResponseSchema>;
|
||||
Reference in New Issue
Block a user