diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index d53b72e155..48e3f85ee7 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -3036,6 +3036,92 @@ describe("#documents.restore", () => { expect(body.data.collectionId).toEqual(anotherCollection.id); }); + it("should allow restore of collection templates", async () => { + const team = await buildTeam(); + const user = await buildUser({ teamId: team.id }); + const collection = await buildCollection({ + userId: user.id, + teamId: team.id, + }); + const template = await buildDocument({ + template: true, + userId: user.id, + collectionId: collection.id, + teamId: team.id, + }); + await template.delete(user); + + const res = await server.post("/api/documents.restore", { + body: { + token: user.getJwtToken(), + id: template.id, + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.deletedAt).toEqual(null); + expect(body.data.collectionId).toEqual(collection.id); + }); + + it("should allow restore of templates from a deleted collection", async () => { + const team = await buildTeam(); + const user = await buildUser({ teamId: team.id }); + const collection = await buildCollection({ + userId: user.id, + teamId: team.id, + }); + const anotherCollection = await buildCollection({ + userId: user.id, + teamId: team.id, + }); + const template = await buildDocument({ + template: true, + userId: user.id, + collectionId: collection.id, + teamId: team.id, + }); + await template.delete(user); + await collection.destroy({ hooks: false }); + + const res = await server.post("/api/documents.restore", { + body: { + token: user.getJwtToken(), + id: template.id, + collectionId: anotherCollection.id, + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.deletedAt).toEqual(null); + expect(body.data.collectionId).toEqual(anotherCollection.id); + }); + + it("should allow restore of workspace templates", async () => { + const team = await buildTeam(); + const user = await buildUser({ teamId: team.id }); + const template = await buildDocument({ + template: true, + userId: user.id, + teamId: team.id, + collectionId: null, + }); + await template.delete(user); + + const res = await server.post("/api/documents.restore", { + body: { + token: user.getJwtToken(), + id: template.id, + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.deletedAt).toEqual(null); + expect(body.data.collectionId).toEqual(null); + }); + it("should not allow restore of documents to a deleted collection", async () => { const team = await buildTeam(); const user = await buildUser({ teamId: team.id }); diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index aba5e373d0..2e9b06f3d6 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -828,13 +828,14 @@ router.post( }).findByPk(destCollectionId) : undefined; - if (!destCollection?.isActive) { + // In case of workspace templates, both source and destination collections are undefined. + if (!document.isWorkspaceTemplate && !destCollection?.isActive) { throw ValidationError( "Unable to restore, the collection may have been deleted or archived" ); } - // Skip this for drafts of a deleted collection as they won't have sourceCollectionId + // Skip this for workspace templates and drafts of a deleted collection as they won't have sourceCollectionId. if (sourceCollectionId && sourceCollectionId !== destCollectionId) { authorize(user, "updateDocument", srcCollection); await srcCollection?.removeDocumentInStructure(document, { @@ -843,7 +844,19 @@ router.post( }); } - if (document.deletedAt) { + if (document.deletedAt && document.isWorkspaceTemplate) { + authorize(user, "restore", document); + + await document.restore({ transaction }); + await Event.createFromContext(ctx, { + name: "documents.restore", + documentId: document.id, + collectionId: document.collectionId, + data: { + title: document.title, + }, + }); + } else if (document.deletedAt) { authorize(user, "restore", document); authorize(user, "updateDocument", destCollection);