diff --git a/server/api/documents.test.js b/server/api/documents.test.js index 9b8dacf6..9c1c9cfb 100644 --- a/server/api/documents.test.js +++ b/server/api/documents.test.js @@ -1441,6 +1441,28 @@ describe("#documents.restore", () => { expect(body.data.archivedAt).toEqual(null); }); + it("should not add restored templates to collection structure", async () => { + const user = await buildUser(); + const collection = await buildCollection({ teamId: user.teamId }); + const template = await buildDocument({ + teamId: user.teamId, + collectionId: collection.id, + template: true, + }); + await template.archive(user.id); + + 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.archivedAt).toEqual(null); + + await collection.reload(); + expect(collection.documentStructure).toEqual(null); + }); + it("should restore archived when previous parent is archived", async () => { const { user, document } = await seed(); const childDocument = await buildDocument({ @@ -1748,6 +1770,37 @@ describe("#documents.update", () => { expect(body.data.text).toBe("Updated text"); }); + it("should not add template to collection structure when publishing", async () => { + const user = await buildUser(); + const collection = await buildCollection({ teamId: user.teamId }); + const template = await buildDocument({ + teamId: user.teamId, + collectionId: collection.id, + template: true, + publishedAt: null, + }); + + const res = await server.post("/api/documents.update", { + body: { + token: user.getJwtToken(), + id: template.id, + title: "Updated title", + text: "Updated text", + lastRevision: template.revision, + publish: true, + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.title).toBe("Updated title"); + expect(body.data.text).toBe("Updated text"); + expect(body.data.publishedAt).toBeTruthy(); + + await collection.reload(); + expect(collection.documentStructure).toBe(null); + }); + it("should allow publishing document in private collection", async () => { const { user, collection, document } = await seed(); document.publishedAt = null; diff --git a/server/models/Document.js b/server/models/Document.js index bc31eca0..0ec33633 100644 --- a/server/models/Document.js +++ b/server/models/Document.js @@ -578,8 +578,10 @@ Document.prototype.archiveWithChildren = async function (userId, options) { Document.prototype.publish = async function (userId: string, options) { if (this.publishedAt) return this.save(options); - const collection = await Collection.findByPk(this.collectionId); - await collection.addDocumentToStructure(this, 0); + if (!this.template) { + const collection = await Collection.findByPk(this.collectionId); + await collection.addDocumentToStructure(this, 0); + } this.lastModifiedById = userId; this.publishedAt = new Date(); @@ -636,8 +638,10 @@ Document.prototype.unarchive = async function (userId: string) { if (!parent) this.parentDocumentId = undefined; } - await collection.addDocumentToStructure(this); - this.collection = collection; + if (!this.template) { + await collection.addDocumentToStructure(this); + this.collection = collection; + } if (this.deletedAt) { await this.restore();