From 4e29b14426331f0ab58a8ff07cbb18beed30d0f7 Mon Sep 17 00:00:00 2001 From: Saumya Pandey Date: Fri, 6 Aug 2021 23:18:53 +0530 Subject: [PATCH] Updates on server to support drafts with no collection --- server/api/documents.js | 60 ++++++++++++++++++++----------------- server/models/Collection.js | 35 ++++++++++++++++++++-- server/policies/document.js | 18 +++++++---- 3 files changed, 77 insertions(+), 36 deletions(-) diff --git a/server/api/documents.js b/server/api/documents.js index faa9bf25f1..187eab232a 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -431,13 +431,8 @@ router.post("documents.drafts", auth(), pagination(), async (ctx) => { authorize(user, "read", collection); } - const collectionIds = !!collectionId - ? [collectionId] - : await user.collectionIds(); - const whereConditions = { userId: user.id, - collectionId: collectionIds, publishedAt: { [Op.eq]: null }, updatedAt: undefined, }; @@ -1039,6 +1034,7 @@ router.post("documents.update", auth(), async (ctx) => { transaction = await sequelize.transaction(); if (publish) { + ctx.assertPresent(collection, "Collection should be present"); await document.publish(user.id, { transaction }); } else { await document.save({ autosave, transaction }); @@ -1194,7 +1190,8 @@ router.post("documents.delete", auth(), async (ctx) => { }); authorize(user, "permanentDelete", document); - await Document.update( + //unscoping to apply on drafts documents + await Document.unscoped().update( { parentDocumentId: null }, { where: { @@ -1346,7 +1343,9 @@ router.post("documents.create", auth(), async (ctx) => { } = ctx.body; const editorVersion = ctx.headers["x-editor-version"]; - ctx.assertUuid(collectionId, "collectionId must be an uuid"); + if (collectionId) { + ctx.assertUuid(collectionId, "collectionId must be an uuid"); + } if (parentDocumentId) { ctx.assertUuid(parentDocumentId, "parentDocumentId must be an uuid"); } @@ -1356,31 +1355,38 @@ router.post("documents.create", auth(), async (ctx) => { const user = ctx.state.user; authorize(user, "createDocument", user.team); - const collection = await Collection.scope({ - method: ["withMembership", user.id], - }).findOne({ - where: { - id: collectionId, - teamId: user.teamId, - }, - }); - authorize(user, "publish", collection); - + let collection; let parentDocument; - if (parentDocumentId) { - parentDocument = await Document.findOne({ + let templateDocument; + + if (collectionId) { + collection = await Collection.scope({ + method: ["withMembership", user.id], + }).findOne({ where: { - id: parentDocumentId, - collectionId: collection.id, + id: collectionId, + teamId: user.teamId, }, }); - authorize(user, "read", parentDocument, { collection }); - } - let templateDocument; - if (templateId) { - templateDocument = await Document.findByPk(templateId, { userId: user.id }); - authorize(user, "read", templateDocument); + authorize(user, "publish", collection); + + if (parentDocumentId) { + parentDocument = await Document.findOne({ + where: { + id: parentDocumentId, + collectionId: collection.id, + }, + }); + authorize(user, "read", parentDocument, { collection }); + } + + if (templateId) { + templateDocument = await Document.findByPk(templateId, { + userId: user.id, + }); + authorize(user, "read", templateDocument); + } } const document = await documentCreator({ diff --git a/server/models/Collection.js b/server/models/Collection.js index 039420c509..6b1b3d75bb 100644 --- a/server/models/Collection.js +++ b/server/models/Collection.js @@ -197,14 +197,43 @@ Collection.associate = (models) => { }; Collection.addHook("afterDestroy", async (model: Collection) => { + // sets deletedAt attribute of archived, templates and normal documents await Document.destroy({ where: { collectionId: model.id, - archivedAt: { - [Op.eq]: null, - }, }, }); + + // updating the documents that moved to deleted state from archived state + await Document.update( + { + archivedAt: null, + }, + { + where: { + deletedAt: { + [Op.ne]: null, + }, + }, + paranoid: false, + } + ); + + // updating the drafts to have no collection + await Document.unscoped().update( + { + collectionId: null, + parentDocumentId: null, + }, + { + where: { + publishedAt: { + [Op.eq]: null, + }, + collectionId: model.id, + }, + } + ); }); Collection.addHook("afterCreate", (model: Collection, options) => { diff --git a/server/policies/document.js b/server/policies/document.js index 519fdbb3c5..4a9850b051 100644 --- a/server/policies/document.js +++ b/server/policies/document.js @@ -24,11 +24,8 @@ allow(User, ["star", "unstar"], Document, (user, document) => { if (document.deletedAt) return false; if (document.template) return false; - invariant( - document.collection, - "collection is missing, did you forget to include in the query scope?" - ); - if (cannot(user, "read", document.collection)) return false; + if (document.collection && cannot(user, "read", document.collection)) + return false; return user.teamId === document.teamId; }); @@ -37,6 +34,10 @@ allow(User, "share", Document, (user, document) => { if (document.archivedAt) return false; if (document.deletedAt) return false; + invariant( + document.collection, + "collection is missing, did you forget to include in the query scope?" + ); if (cannot(user, "share", document.collection)) { return false; } @@ -48,7 +49,12 @@ allow(User, "update", Document, (user, document) => { if (document.archivedAt) return false; if (document.deletedAt) return false; - if (cannot(user, "update", document.collection)) { + // if a document has a collection and it is published, it is everything except the draft. + if ( + document.collection && + document.publishedAt && + cannot(user, "update", document.collection) + ) { return false; }