diff --git a/server/api/documents.js b/server/api/documents.js index 41540d1a..e1308595 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -136,20 +136,15 @@ router.post('documents.create', auth(), async ctx => { text, }); - // TODO: Move to afterSave hook if possible with imports - if (parentDocument && ownerCollection.type === 'atlas') { - ownerCollection.addDocument( - newDocument, - newDocument.parentDocumentId, - index || -1 - ); - await ownerCollection.save(); + if (ownerCollection.type === 'atlas') { + await ownerCollection.addDocumentToStructure(newDocument, index); } ctx.body = { data: await presentDocument(ctx, newDocument, { includeCollection: true, includeCollaborators: true, + collection: ownerCollection, }), }; }); @@ -197,14 +192,15 @@ router.post('documents.delete', auth(), async ctx => { if (collection.type === 'atlas') { // Don't allow deletion of root docs - if (!document.parentDocumentId) { - throw httpErrors.BadRequest("Unable to delete atlas's root document"); + if (collection.documentStructure.length === 1) { + throw httpErrors.BadRequest( + "Unable to delete collection's only document" + ); } // Delete all chilren try { await collection.deleteDocument(document); - await collection.save(); } catch (e) { throw httpErrors.BadRequest('Error while deleting'); } diff --git a/server/models/Collection.js b/server/models/Collection.js index 8d69f88c..c582cda3 100644 --- a/server/models/Collection.js +++ b/server/models/Collection.js @@ -83,16 +83,22 @@ const Collection = sequelize.define( return this.documentStructure; }, - async addDocument(document, parentDocumentId, index = -1) { + async addDocumentToStructure(document, index) { if (!this.documentStructure) return; - if (!parentDocumentId) { - this.documentStructure.splice(index, 0, document.toJSON()); + if (!document.parentDocumentId) { + this.documentStructure.splice( + index || this.documentStructure.length, + 0, + document.toJSON() + ); + // Sequelize doesn't seem to set the value with splice on JSONB field + this.documentStructure = this.documentStructure; } else { this.documentStructure = this.documentStructure.map(childDocument => { - if (parentDocumentId === childDocument.id) { - childDocument.children = childDocument.children.splice( - index, + if (document.parentDocumentId === childDocument.id) { + childDocument.children.splice( + index || childDocument.children.length, 0, document.toJSON() ); @@ -101,11 +107,12 @@ const Collection = sequelize.define( }); } + await this.save(); return this; }, async updateDocument(document) { - // if (!this.documentStructure) return; + if (!this.documentStructure) return; const updateChildren = (children, document) => { const id = document.id; @@ -151,7 +158,10 @@ const Collection = sequelize.define( _.remove(children, { id }); } else { children = children.map(childDocument => { - return deleteFromChildren(childDocument.children, id); + return { + ...childDocument, + children: deleteFromChildren(childDocument.children, id), + }; }); } return children; @@ -161,6 +171,8 @@ const Collection = sequelize.define( this.documentStructure, document.id ); + + await this.save(); return this; }, }, diff --git a/server/presenters.js b/server/presenters.js index 3e95840e..10dc2080 100644 --- a/server/presenters.js +++ b/server/presenters.js @@ -39,16 +39,16 @@ export async function presentDocument(ctx, document, options) { }; if (options.includeCollection) { - data.collection = - options.collection || - (await ctx.cache.get(document.atlasId, async () => { - const collection = await Collection.findOne({ + data.collection = await ctx.cache.get(document.atlasId, async () => { + const collection = + options.collection || + (await Collection.findOne({ where: { id: document.atlasId, }, - }); - return await presentCollection(ctx, collection); - })); + })); + return await presentCollection(ctx, collection); + }); } if (options.includeCollaborators) {