From ac1120914a81d06873488c87aad4a718b3512529 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 24 Dec 2020 13:28:08 -0800 Subject: [PATCH] fix: Unable to delete archived and templated documents (#1749) closes #1746 --- server/models/Document.js | 13 +++++++++---- server/models/Document.test.js | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/server/models/Document.js b/server/models/Document.js index 6d5700cf1..71900e769 100644 --- a/server/models/Document.js +++ b/server/models/Document.js @@ -650,8 +650,10 @@ Document.prototype.delete = function (userId: string) { async (transaction: Transaction): Promise => { if (!this.archivedAt && !this.template) { // delete any children and remove from the document structure - const collection = await this.getCollection(); + const collection = await this.getCollection({ transaction }); if (collection) await collection.deleteDocument(this, { transaction }); + } else { + await this.destroy({ transaction }); } await Revision.destroy({ @@ -659,10 +661,13 @@ Document.prototype.delete = function (userId: string) { transaction, }); - this.lastModifiedById = userId; - this.deletedAt = new Date(); + await this.update( + { lastModifiedById: userId }, + { + transaction, + } + ); - await this.save({ transaction }); return this; } ); diff --git a/server/models/Document.test.js b/server/models/Document.test.js index 6ecdb7747..85527316b 100644 --- a/server/models/Document.test.js +++ b/server/models/Document.test.js @@ -279,4 +279,25 @@ describe("#delete", () => { expect(document.lastModifiedById).toBe(user.id); expect(document.deletedAt).toBeTruthy(); }); + + test("should soft delete templates", async () => { + let document = await buildDocument({ template: true }); + let user = await buildUser(); + + await document.delete(user.id); + + document = await Document.findByPk(document.id, { paranoid: false }); + expect(document.lastModifiedById).toBe(user.id); + expect(document.deletedAt).toBeTruthy(); + }); + test("should soft delete archived", async () => { + let document = await buildDocument({ archivedAt: new Date() }); + let user = await buildUser(); + + await document.delete(user.id); + + document = await Document.findByPk(document.id, { paranoid: false }); + expect(document.lastModifiedById).toBe(user.id); + expect(document.deletedAt).toBeTruthy(); + }); });