fix: Templates should not be visible in collection structure

This commit is contained in:
Tom Moor 2021-03-10 09:39:42 -08:00
parent f50b88716b
commit 0aa72036d7
2 changed files with 61 additions and 4 deletions

View File

@ -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;

View File

@ -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();