chore: revert changes based on feedback

This commit is contained in:
Catalin Pit
2025-07-07 12:04:20 +03:00
parent 52b474d12b
commit eb78706f35
9 changed files with 154 additions and 107 deletions

View File

@ -0,0 +1,22 @@
import { prisma } from '@documenso/prisma';
import { buildTeamWhereQuery } from '../../utils/teams';
export type FindAttachmentsOptions = {
documentId: number;
userId: number;
teamId: number;
};
export const findAttachments = async ({ documentId, userId, teamId }: FindAttachmentsOptions) => {
const attachments = await prisma.attachment.findMany({
where: {
document: {
id: documentId,
team: buildTeamWhereQuery({ teamId, userId }),
},
},
});
return attachments;
};

View File

@ -0,0 +1,84 @@
import type { Attachment } from '@prisma/client';
import { prisma } from '@documenso/prisma';
import { AppError } from '../../errors/app-error';
import { AppErrorCode } from '../../errors/app-error';
export type CreateAttachmentsOptions = {
documentId?: number;
templateId?: number;
attachments: Pick<Attachment, 'id' | 'label' | 'url' | 'type'>[];
};
export const setDocumentAttachments = async ({
documentId,
templateId,
attachments,
}: CreateAttachmentsOptions) => {
const document = await prisma.document.findUnique({
where: {
id: documentId,
},
});
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) {
if (attachment.id) {
const updated = await prisma.attachment.upsert({
where: { id: attachment.id },
update: {
label: attachment.label,
url: attachment.url,
type: attachment.type,
templateId,
},
create: {
label: attachment.label,
url: attachment.url,
type: attachment.type,
templateId,
documentId,
},
});
upsertedAttachments.push(updated);
} else {
const created = await prisma.attachment.create({
data: {
label: attachment.label,
url: attachment.url,
type: attachment.type,
templateId,
documentId,
},
});
upsertedAttachments.push(created);
}
}
return upsertedAttachments;
};

View File

@ -7,7 +7,6 @@ import type { ApiRequestMetadata } from '@documenso/lib/universal/extract-reques
import type { CreateDocumentAuditLogDataResponse } from '@documenso/lib/utils/document-audit-logs';
import { createDocumentAuditLogData } from '@documenso/lib/utils/document-audit-logs';
import { prisma } from '@documenso/prisma';
import type { Attachment } from '@documenso/prisma/generated/zod/modelSchema/AttachmentSchema';
import { AppError, AppErrorCode } from '../../errors/app-error';
import type { TDocumentAccessAuthTypes, TDocumentActionAuthTypes } from '../../types/document-auth';
@ -25,7 +24,6 @@ export type UpdateDocumentOptions = {
globalAccessAuth?: TDocumentAccessAuthTypes[];
globalActionAuth?: TDocumentActionAuthTypes[];
useLegacyFieldInsertion?: boolean;
attachments?: Pick<Attachment, 'id' | 'label' | 'url'>[];
};
requestMetadata: ApiRequestMetadata;
};
@ -55,7 +53,6 @@ export const updateDocument = async ({
},
},
},
attachments: true,
},
});
@ -136,8 +133,6 @@ export const updateDocument = async ({
documentGlobalActionAuth === undefined || documentGlobalActionAuth === newGlobalActionAuth;
const isDocumentVisibilitySame =
data.visibility === undefined || data.visibility === document.visibility;
const isAttachmentsSame =
data.attachments === undefined || data.attachments === document.attachments;
const auditLogs: CreateDocumentAuditLogDataResponse[] = [];
@ -217,20 +212,6 @@ export const updateDocument = async ({
);
}
if (!isAttachmentsSame) {
auditLogs.push(
createDocumentAuditLogData({
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_ATTACHMENTS_UPDATED,
documentId,
metadata: requestMetadata,
data: {
from: document.attachments,
to: data.attachments ?? [],
},
}),
);
}
// Early return if nothing is required.
if (auditLogs.length === 0 && data.useLegacyFieldInsertion === undefined) {
return document;
@ -255,39 +236,6 @@ export const updateDocument = async ({
},
});
if (data.attachments) {
await tx.attachment.deleteMany({
where: {
documentId,
id: {
notIn: data.attachments.map((a) => a.id),
},
},
});
await Promise.all(
data.attachments.map(
async (attachment) =>
await tx.attachment.upsert({
where: {
id: attachment.id,
documentId,
},
update: {
label: attachment.label,
url: attachment.url,
},
create: {
id: attachment.id,
label: attachment.label,
url: attachment.url,
documentId,
},
}),
),
);
}
await tx.documentAuditLog.createMany({
data: auditLogs,
});

View File

@ -1,4 +1,4 @@
import type { Attachment, DocumentVisibility, Template, TemplateMeta } from '@prisma/client';
import type { DocumentVisibility, Template, TemplateMeta } from '@prisma/client';
import { prisma } from '@documenso/prisma';
@ -21,7 +21,6 @@ export type UpdateTemplateOptions = {
publicDescription?: string;
type?: Template['type'];
useLegacyFieldInsertion?: boolean;
attachments?: Pick<Attachment, 'id' | 'label' | 'url'>[];
};
meta?: Partial<Omit<TemplateMeta, 'id' | 'templateId'>>;
};
@ -40,7 +39,6 @@ export const updateTemplate = async ({
},
include: {
templateMeta: true,
attachments: true,
team: {
select: {
organisation: {
@ -100,29 +98,6 @@ export const updateTemplate = async ({
publicDescription: data?.publicDescription,
publicTitle: data?.publicTitle,
useLegacyFieldInsertion: data?.useLegacyFieldInsertion,
attachments: {
deleteMany: {
templateId,
id: {
notIn: data?.attachments?.map((attachment) => attachment.id),
},
},
upsert: data?.attachments?.map((attachment) => ({
where: {
id: attachment.id,
templateId,
},
update: {
label: attachment.label,
url: attachment.url,
},
create: {
id: attachment.id,
label: attachment.label,
url: attachment.url,
},
})),
},
authOptions,
templateMeta: {
upsert: {