fix: Add ability to permanently delete documents in trash (#2192)
* Align false conditions before true * Update documents.delete endpoint for permanent delete * Add permanent delete to events table and integrate with socket.io * Add permanent delete to document menu * Update parentDocumentId of direct child to null * Add translation * Add test for permanent delete * Add space * Update app/scenes/DocumentPermanentDelete.js Co-authored-by: Tom Moor <tom.moor@gmail.com> * Update app/stores/DocumentsStore.js Co-authored-by: Tom Moor <tom.moor@gmail.com> * Update server/commands/documentPermanentDeleter.js Co-authored-by: Tom Moor <tom.moor@gmail.com> * Update app/scenes/DocumentPermanentDelete.js Co-authored-by: Tom Moor <tom.moor@gmail.com> * Change socket room from team to collection * Add translation * Create log func for commands * Move tests from utils to permanentDeleter command * Add additional tests * Set redirect to trash * Return promise from beforeEach * Add undeleted documents validation * Include deleteAt attribute in db query * Update server/commands/documentPermanentDeleter.js Co-authored-by: Tom Moor <tom.moor@gmail.com> * tweak language Co-authored-by: Tom Moor <tom.moor@gmail.com>
This commit is contained in:
64
server/commands/documentPermanentDeleter.js
Normal file
64
server/commands/documentPermanentDeleter.js
Normal file
@ -0,0 +1,64 @@
|
||||
// @flow
|
||||
import debug from "debug";
|
||||
import { Document, Attachment } from "../models";
|
||||
import { sequelize } from "../sequelize";
|
||||
import parseAttachmentIds from "../utils/parseAttachmentIds";
|
||||
|
||||
const log = debug("commands");
|
||||
|
||||
export async function documentPermanentDeleter(documents: Document[]) {
|
||||
const activeDocument = documents.find((doc) => !doc.deletedAt);
|
||||
|
||||
if (activeDocument) {
|
||||
throw new Error(
|
||||
`Cannot permanently delete ${activeDocument.id} document. Please delete it and try again.`
|
||||
);
|
||||
}
|
||||
|
||||
const query = `
|
||||
SELECT COUNT(id)
|
||||
FROM documents
|
||||
WHERE "searchVector" @@ to_tsquery('english', :query) AND
|
||||
"teamId" = :teamId AND
|
||||
"id" != :documentId
|
||||
`;
|
||||
|
||||
for (const document of documents) {
|
||||
const attachmentIds = parseAttachmentIds(document.text);
|
||||
|
||||
for (const attachmentId of attachmentIds) {
|
||||
const [{ count }] = await sequelize.query(query, {
|
||||
type: sequelize.QueryTypes.SELECT,
|
||||
replacements: {
|
||||
documentId: document.id,
|
||||
teamId: document.teamId,
|
||||
query: attachmentId,
|
||||
},
|
||||
});
|
||||
|
||||
if (parseInt(count) === 0) {
|
||||
const attachment = await Attachment.findOne({
|
||||
where: {
|
||||
teamId: document.teamId,
|
||||
id: attachmentId,
|
||||
},
|
||||
});
|
||||
|
||||
if (attachment) {
|
||||
await attachment.destroy();
|
||||
|
||||
log(`Attachment ${attachmentId} deleted`);
|
||||
} else {
|
||||
log(`Unknown attachment ${attachmentId} ignored`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Document.scope("withUnpublished").destroy({
|
||||
where: {
|
||||
id: documents.map((document) => document.id),
|
||||
},
|
||||
force: true,
|
||||
});
|
||||
}
|
123
server/commands/documentPermanentDeleter.test.js
Normal file
123
server/commands/documentPermanentDeleter.test.js
Normal file
@ -0,0 +1,123 @@
|
||||
// @flow
|
||||
import { subDays } from "date-fns";
|
||||
import { Attachment, Document } from "../models";
|
||||
import { buildAttachment, buildDocument } from "../test/factories";
|
||||
import { flushdb } from "../test/support";
|
||||
import { documentPermanentDeleter } from "./documentPermanentDeleter";
|
||||
|
||||
jest.mock("aws-sdk", () => {
|
||||
const mS3 = { deleteObject: jest.fn().mockReturnThis(), promise: jest.fn() };
|
||||
return {
|
||||
S3: jest.fn(() => mS3),
|
||||
Endpoint: jest.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
beforeEach(() => flushdb());
|
||||
|
||||
describe("documentPermanentDeleter", () => {
|
||||
it("should destroy documents", async () => {
|
||||
const document = await buildDocument({
|
||||
publishedAt: subDays(new Date(), 90),
|
||||
deletedAt: new Date(),
|
||||
});
|
||||
|
||||
const countDeletedDoc = await documentPermanentDeleter([document]);
|
||||
|
||||
expect(countDeletedDoc).toEqual(1);
|
||||
expect(await Document.unscoped().count({ paranoid: false })).toEqual(0);
|
||||
});
|
||||
|
||||
it("should error when trying to destroy undeleted documents", async () => {
|
||||
const document = await buildDocument({
|
||||
publishedAt: new Date(),
|
||||
});
|
||||
|
||||
let error;
|
||||
try {
|
||||
await documentPermanentDeleter([document]);
|
||||
} catch (err) {
|
||||
error = err.message;
|
||||
}
|
||||
|
||||
expect(error).toEqual(
|
||||
`Cannot permanently delete ${document.id} document. Please delete it and try again.`
|
||||
);
|
||||
});
|
||||
|
||||
it("should destroy attachments no longer referenced", async () => {
|
||||
const document = await buildDocument({
|
||||
publishedAt: subDays(new Date(), 90),
|
||||
deletedAt: new Date(),
|
||||
});
|
||||
|
||||
const attachment = await buildAttachment({
|
||||
teamId: document.teamId,
|
||||
documentId: document.id,
|
||||
});
|
||||
|
||||
document.text = ``;
|
||||
await document.save();
|
||||
|
||||
const countDeletedDoc = await documentPermanentDeleter([document]);
|
||||
|
||||
expect(countDeletedDoc).toEqual(1);
|
||||
expect(await Attachment.count()).toEqual(0);
|
||||
expect(await Document.unscoped().count({ paranoid: false })).toEqual(0);
|
||||
});
|
||||
|
||||
it("should handle unknown attachment ids", async () => {
|
||||
const document = await buildDocument({
|
||||
publishedAt: subDays(new Date(), 90),
|
||||
deletedAt: new Date(),
|
||||
});
|
||||
|
||||
const attachment = await buildAttachment({
|
||||
teamId: document.teamId,
|
||||
documentId: document.id,
|
||||
});
|
||||
|
||||
document.text = ``;
|
||||
await document.save();
|
||||
|
||||
// remove attachment so it no longer exists in the database, this is also
|
||||
// representative of a corrupt attachment id in the doc or the regex returning
|
||||
// an incorrect string
|
||||
await attachment.destroy({ force: true });
|
||||
|
||||
const countDeletedDoc = await documentPermanentDeleter([document]);
|
||||
|
||||
expect(countDeletedDoc).toEqual(1);
|
||||
expect(await Attachment.count()).toEqual(0);
|
||||
expect(await Document.unscoped().count({ paranoid: false })).toEqual(0);
|
||||
});
|
||||
|
||||
it("should not destroy attachments referenced in other documents", async () => {
|
||||
const document1 = await buildDocument();
|
||||
|
||||
const document = await buildDocument({
|
||||
teamId: document1.teamId,
|
||||
publishedAt: subDays(new Date(), 90),
|
||||
deletedAt: subDays(new Date(), 60),
|
||||
});
|
||||
|
||||
const attachment = await buildAttachment({
|
||||
teamId: document1.teamId,
|
||||
documentId: document.id,
|
||||
});
|
||||
|
||||
document1.text = ``;
|
||||
await document1.save();
|
||||
|
||||
document.text = ``;
|
||||
await document.save();
|
||||
|
||||
expect(await Attachment.count()).toEqual(1);
|
||||
|
||||
const countDeletedDoc = await documentPermanentDeleter([document]);
|
||||
|
||||
expect(countDeletedDoc).toEqual(1);
|
||||
expect(await Attachment.count()).toEqual(1);
|
||||
expect(await Document.unscoped().count({ paranoid: false })).toEqual(1);
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user