mirror of
https://github.com/documenso/documenso.git
synced 2025-11-23 05:01:54 +10:00
feat: attachments
This commit is contained in:
@ -81,6 +81,7 @@ export const getDocumentAndSenderByToken = async ({
|
||||
token,
|
||||
},
|
||||
},
|
||||
attachments: true,
|
||||
team: {
|
||||
select: {
|
||||
name: true,
|
||||
|
||||
@ -287,6 +287,7 @@ export const createDocumentFromTemplate = async ({
|
||||
fields: true,
|
||||
},
|
||||
},
|
||||
attachments: true,
|
||||
templateDocumentData: true,
|
||||
templateMeta: true,
|
||||
},
|
||||
@ -377,6 +378,15 @@ export const createDocumentFromTemplate = async ({
|
||||
}),
|
||||
visibility: template.visibility || settings.documentVisibility,
|
||||
useLegacyFieldInsertion: template.useLegacyFieldInsertion ?? false,
|
||||
attachments: {
|
||||
create: template.attachments.map((attachment) => ({
|
||||
type: attachment.type,
|
||||
label: attachment.label,
|
||||
url: attachment.url,
|
||||
createdAt: attachment.createdAt,
|
||||
updatedAt: attachment.updatedAt,
|
||||
})),
|
||||
},
|
||||
documentMeta: {
|
||||
create: extractDerivedDocumentMeta(settings, {
|
||||
subject: override?.subject || template.templateMeta?.subject,
|
||||
|
||||
@ -40,6 +40,7 @@ export const ZDocumentAuditLogTypeSchema = z.enum([
|
||||
'DOCUMENT_TITLE_UPDATED', // When the document title is updated.
|
||||
'DOCUMENT_EXTERNAL_ID_UPDATED', // When the document external ID is updated.
|
||||
'DOCUMENT_MOVED_TO_TEAM', // When the document is moved to a team.
|
||||
'DOCUMENT_ATTACHMENTS_UPDATED', // When the document attachments are updated.
|
||||
]);
|
||||
|
||||
export const ZDocumentAuditLogEmailTypeSchema = z.enum([
|
||||
@ -598,6 +599,29 @@ export const ZDocumentAuditLogEventDocumentMovedToTeamSchema = z.object({
|
||||
}),
|
||||
});
|
||||
|
||||
/**
|
||||
* Event: Document attachments updated.
|
||||
*/
|
||||
export const ZDocumentAuditLogEventDocumentAttachmentsUpdatedSchema = z.object({
|
||||
type: z.literal(DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_ATTACHMENTS_UPDATED),
|
||||
data: z.object({
|
||||
from: z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
label: z.string(),
|
||||
url: z.string(),
|
||||
}),
|
||||
),
|
||||
to: z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
label: z.string(),
|
||||
url: z.string(),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
});
|
||||
|
||||
export const ZDocumentAuditLogBaseSchema = z.object({
|
||||
id: z.string(),
|
||||
createdAt: z.date(),
|
||||
@ -630,6 +654,7 @@ export const ZDocumentAuditLogSchema = ZDocumentAuditLogBaseSchema.and(
|
||||
ZDocumentAuditLogEventDocumentSentSchema,
|
||||
ZDocumentAuditLogEventDocumentTitleUpdatedSchema,
|
||||
ZDocumentAuditLogEventDocumentExternalIdUpdatedSchema,
|
||||
ZDocumentAuditLogEventDocumentAttachmentsUpdatedSchema,
|
||||
ZDocumentAuditLogEventFieldCreatedSchema,
|
||||
ZDocumentAuditLogEventFieldRemovedSchema,
|
||||
ZDocumentAuditLogEventFieldUpdatedSchema,
|
||||
|
||||
@ -423,6 +423,10 @@ export const formatDocumentAuditLogAction = (
|
||||
anonymous: msg`Document completed`,
|
||||
identified: msg`Document completed`,
|
||||
}))
|
||||
.with({ type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_ATTACHMENTS_UPDATED }, () => ({
|
||||
anonymous: msg`Document attachments updated`,
|
||||
identified: msg`${prefix} updated the document attachments`,
|
||||
}))
|
||||
.exhaustive();
|
||||
|
||||
return {
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
-- CreateEnum
|
||||
CREATE TYPE "AttachmentType" AS ENUM ('FILE', 'VIDEO', 'AUDIO', 'IMAGE', 'LINK', 'OTHER');
|
||||
|
||||
-- DropForeignKey
|
||||
ALTER TABLE "Document" DROP CONSTRAINT "Document_folderId_fkey";
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Attachment" (
|
||||
"id" TEXT NOT NULL,
|
||||
"type" "AttachmentType" NOT NULL DEFAULT 'LINK',
|
||||
"label" TEXT NOT NULL,
|
||||
"url" TEXT NOT NULL,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"documentId" INTEGER,
|
||||
"templateId" INTEGER,
|
||||
|
||||
CONSTRAINT "Attachment_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Attachment" ADD CONSTRAINT "Attachment_documentId_fkey" FOREIGN KEY ("documentId") REFERENCES "Document"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Attachment" ADD CONSTRAINT "Attachment_templateId_fkey" FOREIGN KEY ("templateId") REFERENCES "Template"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Document" ADD CONSTRAINT "Document_folderId_fkey" FOREIGN KEY ("folderId") REFERENCES "Folder"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@ -333,6 +333,32 @@ enum DocumentVisibility {
|
||||
ADMIN
|
||||
}
|
||||
|
||||
// Only "LINK" is supported for now.
|
||||
// All other attachment types are not yet supported.
|
||||
enum AttachmentType {
|
||||
FILE
|
||||
VIDEO
|
||||
AUDIO
|
||||
IMAGE
|
||||
LINK
|
||||
OTHER
|
||||
}
|
||||
|
||||
model Attachment {
|
||||
id String @id @default(uuid())
|
||||
type AttachmentType @default(LINK)
|
||||
label String
|
||||
url String
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @default(now()) @updatedAt
|
||||
|
||||
documentId Int?
|
||||
document Document? @relation(fields: [documentId], references: [id], onDelete: Cascade)
|
||||
|
||||
templateId Int?
|
||||
template Template? @relation(fields: [templateId], references: [id], onDelete: Cascade)
|
||||
}
|
||||
|
||||
enum FolderType {
|
||||
DOCUMENT
|
||||
TEMPLATE
|
||||
@ -391,14 +417,15 @@ model Document {
|
||||
templateId Int?
|
||||
source DocumentSource
|
||||
|
||||
useLegacyFieldInsertion Boolean @default(false)
|
||||
auditLogs DocumentAuditLog[]
|
||||
attachments Attachment[]
|
||||
useLegacyFieldInsertion Boolean @default(false)
|
||||
|
||||
documentData DocumentData @relation(fields: [documentDataId], references: [id], onDelete: Cascade)
|
||||
template Template? @relation(fields: [templateId], references: [id], onDelete: SetNull)
|
||||
|
||||
auditLogs DocumentAuditLog[]
|
||||
folder Folder? @relation(fields: [folderId], references: [id], onDelete: SetNull)
|
||||
folderId String?
|
||||
folder Folder? @relation(fields: [folderId], references: [id], onDelete: Cascade)
|
||||
folderId String?
|
||||
|
||||
@@unique([documentDataId])
|
||||
@@index([userId])
|
||||
@ -892,10 +919,11 @@ model Template {
|
||||
|
||||
templateDocumentData DocumentData @relation(fields: [templateDocumentDataId], references: [id], onDelete: Cascade)
|
||||
|
||||
recipients Recipient[]
|
||||
fields Field[]
|
||||
directLink TemplateDirectLink?
|
||||
documents Document[]
|
||||
recipients Recipient[]
|
||||
fields Field[]
|
||||
directLink TemplateDirectLink?
|
||||
documents Document[]
|
||||
attachments Attachment[]
|
||||
|
||||
folder Folder? @relation(fields: [folderId], references: [id], onDelete: SetNull)
|
||||
folderId String?
|
||||
|
||||
@ -0,0 +1,47 @@
|
||||
import { buildTeamWhereQuery } from '@documenso/lib/utils/teams';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
import { authenticatedProcedure } from '../trpc';
|
||||
import {
|
||||
ZGetDocumentAttachmentsResponseSchema,
|
||||
ZGetDocumentAttachmentsSchema,
|
||||
} from './find-document-attachments.types';
|
||||
|
||||
export const findDocumentAttachmentsRoute = authenticatedProcedure
|
||||
.input(ZGetDocumentAttachmentsSchema)
|
||||
.output(ZGetDocumentAttachmentsResponseSchema)
|
||||
.query(async ({ input, ctx }) => {
|
||||
const { documentId } = input;
|
||||
const { user } = ctx;
|
||||
|
||||
const attachments = await findDocumentAttachments({
|
||||
documentId,
|
||||
userId: user.id,
|
||||
teamId: ctx.teamId,
|
||||
});
|
||||
|
||||
return attachments;
|
||||
});
|
||||
|
||||
export type FindDocumentAttachmentsOptions = {
|
||||
documentId?: number;
|
||||
userId: number;
|
||||
teamId: number;
|
||||
};
|
||||
|
||||
export const findDocumentAttachments = async ({
|
||||
documentId,
|
||||
userId,
|
||||
teamId,
|
||||
}: FindDocumentAttachmentsOptions) => {
|
||||
const attachments = await prisma.attachment.findMany({
|
||||
where: {
|
||||
document: {
|
||||
id: documentId,
|
||||
team: buildTeamWhereQuery({ teamId, userId }),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return attachments;
|
||||
};
|
||||
@ -0,0 +1,16 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import { AttachmentType } from '@documenso/prisma/generated/types';
|
||||
|
||||
export const ZGetDocumentAttachmentsSchema = z.object({
|
||||
documentId: z.number(),
|
||||
});
|
||||
|
||||
export const ZGetDocumentAttachmentsResponseSchema = z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
label: z.string(),
|
||||
url: z.string(),
|
||||
type: z.nativeEnum(AttachmentType),
|
||||
}),
|
||||
);
|
||||
@ -27,6 +27,7 @@ import { getPresignPostUrl } from '@documenso/lib/universal/upload/server-action
|
||||
import { isDocumentCompleted } from '@documenso/lib/utils/document';
|
||||
|
||||
import { authenticatedProcedure, procedure, router } from '../trpc';
|
||||
import { findDocumentAttachmentsRoute } from './find-document-attachments';
|
||||
import { findInboxRoute } from './find-inbox';
|
||||
import { getInboxCountRoute } from './get-inbox-count';
|
||||
import {
|
||||
@ -55,6 +56,7 @@ import {
|
||||
ZSetSigningOrderForDocumentMutationSchema,
|
||||
ZSuccessResponseSchema,
|
||||
} from './schema';
|
||||
import { setDocumentAttachmentsRoute } from './set-document-attachments';
|
||||
import { updateDocumentRoute } from './update-document';
|
||||
|
||||
export const documentRouter = router({
|
||||
@ -63,6 +65,10 @@ export const documentRouter = router({
|
||||
getCount: getInboxCountRoute,
|
||||
},
|
||||
updateDocument: updateDocumentRoute,
|
||||
attachments: {
|
||||
find: findDocumentAttachmentsRoute,
|
||||
set: setDocumentAttachmentsRoute,
|
||||
},
|
||||
|
||||
/**
|
||||
* @private
|
||||
|
||||
124
packages/trpc/server/document-router/set-document-attachments.ts
Normal file
124
packages/trpc/server/document-router/set-document-attachments.ts
Normal file
@ -0,0 +1,124 @@
|
||||
import type { Attachment, User } from '@prisma/client';
|
||||
|
||||
import { AppError } from '@documenso/lib/errors/app-error';
|
||||
import { AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||
import { DOCUMENT_AUDIT_LOG_TYPE } from '@documenso/lib/types/document-audit-logs';
|
||||
import type { RequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
||||
import { createDocumentAuditLogData } from '@documenso/lib/utils/document-audit-logs';
|
||||
import { buildTeamWhereQuery } from '@documenso/lib/utils/teams';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
import { authenticatedProcedure } from '../trpc';
|
||||
import {
|
||||
ZSetDocumentAttachmentsResponseSchema,
|
||||
ZSetDocumentAttachmentsSchema,
|
||||
} from './set-document-attachments.types';
|
||||
|
||||
export const setDocumentAttachmentsRoute = authenticatedProcedure
|
||||
.input(ZSetDocumentAttachmentsSchema)
|
||||
.output(ZSetDocumentAttachmentsResponseSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { documentId, attachments } = input;
|
||||
|
||||
const updatedAttachments = await setDocumentAttachments({
|
||||
documentId,
|
||||
attachments,
|
||||
user: ctx.user,
|
||||
teamId: ctx.teamId,
|
||||
requestMetadata: ctx.metadata.requestMetadata,
|
||||
});
|
||||
|
||||
return updatedAttachments;
|
||||
});
|
||||
|
||||
export type CreateAttachmentsOptions = {
|
||||
documentId: number;
|
||||
attachments: Pick<Attachment, 'id' | 'label' | 'url' | 'type'>[];
|
||||
user: Pick<User, 'id' | 'email' | 'name'>;
|
||||
teamId: number;
|
||||
requestMetadata: RequestMetadata;
|
||||
};
|
||||
|
||||
export const setDocumentAttachments = async ({
|
||||
documentId,
|
||||
attachments,
|
||||
user,
|
||||
teamId,
|
||||
requestMetadata,
|
||||
}: CreateAttachmentsOptions) => {
|
||||
const document = await prisma.document.findUnique({
|
||||
where: {
|
||||
id: documentId,
|
||||
team: buildTeamWhereQuery({ teamId, userId: user.id }),
|
||||
},
|
||||
});
|
||||
|
||||
if (!document) {
|
||||
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||
message: 'Document not found',
|
||||
});
|
||||
}
|
||||
|
||||
const existingAttachments = await prisma.attachment.findMany({
|
||||
where: {
|
||||
documentId,
|
||||
},
|
||||
});
|
||||
|
||||
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, documentId: document.id },
|
||||
update: {
|
||||
label: attachment.label,
|
||||
url: attachment.url,
|
||||
type: attachment.type,
|
||||
},
|
||||
create: {
|
||||
label: attachment.label,
|
||||
url: attachment.url,
|
||||
type: attachment.type,
|
||||
documentId,
|
||||
},
|
||||
});
|
||||
upsertedAttachments.push(updated);
|
||||
}
|
||||
|
||||
const isAttachmentsSame = upsertedAttachments.every((attachment) => {
|
||||
const existingAttachment = existingAttachments.find((a) => a.id === attachment.id);
|
||||
return (
|
||||
existingAttachment?.label === attachment.label &&
|
||||
existingAttachment?.url === attachment.url &&
|
||||
existingAttachment?.type === attachment.type
|
||||
);
|
||||
});
|
||||
|
||||
if (!isAttachmentsSame) {
|
||||
await prisma.documentAuditLog.create({
|
||||
data: createDocumentAuditLogData({
|
||||
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_ATTACHMENTS_UPDATED,
|
||||
documentId: document.id,
|
||||
user,
|
||||
data: {
|
||||
from: existingAttachments,
|
||||
to: upsertedAttachments,
|
||||
},
|
||||
requestMetadata,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
return upsertedAttachments;
|
||||
};
|
||||
@ -0,0 +1,26 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import { AttachmentType } from '@documenso/prisma/generated/types';
|
||||
|
||||
export const ZSetDocumentAttachmentsSchema = z.object({
|
||||
documentId: 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 TSetDocumentAttachmentsSchema = z.infer<typeof ZSetDocumentAttachmentsSchema>;
|
||||
|
||||
export const ZSetDocumentAttachmentsResponseSchema = z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
label: z.string(),
|
||||
url: z.string(),
|
||||
type: z.nativeEnum(AttachmentType),
|
||||
}),
|
||||
);
|
||||
@ -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