chore: implement feedback

This commit is contained in:
Catalin Pit
2025-07-23 14:42:16 +03:00
parent d710f53fb5
commit c0a72123bd
3 changed files with 44 additions and 55 deletions

View File

@ -7,11 +7,13 @@ import { AppError } from '../../errors/app-error';
import { AppErrorCode } from '../../errors/app-error';
import { DOCUMENT_AUDIT_LOG_TYPE } from '../../types/document-audit-logs';
import { createDocumentAuditLogData } from '../../utils/document-audit-logs';
import { buildTeamWhereQuery } from '../../utils/teams';
export type CreateAttachmentsOptions = {
documentId: number;
attachments: Pick<Attachment, 'id' | 'label' | 'url' | 'type'>[];
user: Pick<User, 'id' | 'email' | 'name'>;
teamId: number;
requestMetadata: RequestMetadata;
};
@ -19,11 +21,13 @@ 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 }),
},
});
@ -53,33 +57,21 @@ export const setDocumentAttachments = async ({
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,
},
create: {
label: attachment.label,
url: attachment.url,
type: attachment.type,
documentId,
},
});
upsertedAttachments.push(updated);
} else {
const created = await prisma.attachment.create({
data: {
label: attachment.label,
url: attachment.url,
type: attachment.type,
documentId,
},
});
upsertedAttachments.push(created);
}
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) => {

View File

@ -4,19 +4,25 @@ import { prisma } from '@documenso/prisma';
import { AppError } from '../../errors/app-error';
import { AppErrorCode } from '../../errors/app-error';
import { buildTeamWhereQuery } from '../../utils/teams';
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 }),
},
});
@ -46,34 +52,22 @@ export const setTemplateAttachments = async ({
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,
},
});
upsertedAttachments.push(updated);
} else {
const created = await prisma.attachment.create({
data: {
label: attachment.label,
url: attachment.url,
type: attachment.type,
templateId,
},
});
upsertedAttachments.push(created);
}
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;

View File

@ -47,6 +47,7 @@ export const attachmentRouter = router({
documentId,
attachments,
user: ctx.user,
teamId: ctx.teamId,
requestMetadata: ctx.metadata.requestMetadata,
});
@ -81,6 +82,8 @@ export const attachmentRouter = router({
const updatedAttachments = await setTemplateAttachments({
templateId,
userId: ctx.user.id,
teamId: ctx.teamId,
attachments,
});