From e76316f18b44351573e46733bbe0cb1b9ffcc874 Mon Sep 17 00:00:00 2001 From: Vyacheslav Tumanov Date: Wed, 19 Jun 2024 18:48:07 +0500 Subject: [PATCH] Prevent saving the same attachment twice (#5850) Signed-off-by: Vyacheslav Tumanov --- .../src/components/AttachmentRefInput.svelte | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/plugins/attachment-resources/src/components/AttachmentRefInput.svelte b/plugins/attachment-resources/src/components/AttachmentRefInput.svelte index 75fbce37c5..9bf6ed6677 100644 --- a/plugins/attachment-resources/src/components/AttachmentRefInput.svelte +++ b/plugins/attachment-resources/src/components/AttachmentRefInput.svelte @@ -143,8 +143,25 @@ } } + const existingAttachmentsQuery = createQuery() + let existingAttachments: Ref[] = [] + $: existingAttachmentsQuery.query( + attachment.class.Attachment, + { + space, + attachedTo: objectId, + attachedToClass: _class, + _id: { $in: Array.from(attachments.keys()) } + }, + (res) => { + existingAttachments = res.map((p) => p._id) + } + ) + async function saveAttachment (doc: Attachment): Promise { - await client.addCollection(attachment.class.Attachment, space, objectId, _class, 'attachments', doc, doc._id) + if (!existingAttachments.includes(doc._id)) { + await client.addCollection(attachment.class.Attachment, space, objectId, _class, 'attachments', doc, doc._id) + } } async function fileSelected (): Promise { @@ -209,6 +226,9 @@ } }) } + if (!saved && shouldSaveDraft) { + void createAttachments() + } }) export function removeDraft (removeFiles: boolean) {