feat: enhance document attachment handling and audit logging

- Added support for attachment updates in the updateDocument functionc.
- Introduced new audit log type for document attachments updates.
- Updated ZDocumentAuditLog schemas to include attachment-related events.
- Modified AddSettingsFormPartial to handle attachment IDs and types correctly.
- Set default value for attachment type in the Prisma schema.
This commit is contained in:
Catalin Documenso
2025-04-30 15:53:58 +03:00
parent e3f8e76e6a
commit 6980db57d3
8 changed files with 110 additions and 15 deletions

View File

@ -69,6 +69,7 @@ export const updateDocument = async ({
},
},
},
attachments: true,
},
});
@ -160,6 +161,8 @@ 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[] = [];
@ -239,6 +242,20 @@ export const updateDocument = async ({
);
}
if (data.attachments) {
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;
@ -260,18 +277,42 @@ export const updateDocument = async ({
visibility: data.visibility as DocumentVisibility,
useLegacyFieldInsertion: data.useLegacyFieldInsertion,
authOptions,
attachments: {
deleteMany: {},
create:
data.attachments?.map((attachment) => ({
type: 'LINK',
label: attachment.label,
url: attachment.url,
})) || [],
},
},
});
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,
});