Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Unable to delete archived and templated documents #1749

Merged
merged 1 commit into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions server/models/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,19 +650,24 @@ Document.prototype.delete = function (userId: string) {
async (transaction: Transaction): Promise<Document> => {
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({
where: { documentId: this.id },
transaction,
});

this.lastModifiedById = userId;
this.deletedAt = new Date();
await this.update(
{ lastModifiedById: userId },
{
transaction,
}
);

await this.save({ transaction });
return this;
}
);
Expand Down
21 changes: 21 additions & 0 deletions server/models/Document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});