From a4dca58ae7ef4d9d35624d0cae1e741608026b16 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Sun, 4 Jun 2017 22:12:36 -0700 Subject: [PATCH] Fixed Collection#updateDocument --- server/api/documents.js | 11 ++-- server/models/Collection.js | 109 ++++++++++++++++++++++-------------- server/models/Document.js | 3 + server/presenters.js | 19 ++++--- 4 files changed, 85 insertions(+), 57 deletions(-) diff --git a/server/api/documents.js b/server/api/documents.js index adcfaf153..41540d1ad 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -157,18 +157,16 @@ router.post('documents.create', auth(), async ctx => { router.post('documents.update', auth(), async ctx => { const { id, title, text } = ctx.body; ctx.assertPresent(id, 'id is required'); - ctx.assertPresent(title, 'title is required'); - ctx.assertPresent(text, 'text is required'); + ctx.assertPresent(title || text, 'title or text is required'); const user = ctx.state.user; const document = await getDocumentForId(id); - if (!document || document.teamId !== user.teamId) - throw httpErrors.BadRequest(); + if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound(); // Update document - document.title = title; - document.text = text; + if (title) document.title = title; + if (text) document.text = text; document.lastModifiedById = user.id; await document.save(); @@ -181,6 +179,7 @@ router.post('documents.update', auth(), async ctx => { data: await presentDocument(ctx, document, { includeCollection: true, includeCollaborators: true, + collection: collection, }), }; }); diff --git a/server/models/Collection.js b/server/models/Collection.js index bd19d14b7..8d69f88c3 100644 --- a/server/models/Collection.js +++ b/server/models/Collection.js @@ -50,12 +50,7 @@ const Collection = sequelize.define( title: 'Introduction', text: '# Introduction\n\nLets get started...', }); - collection.documentStructure = [ - { - ...document.toJSON(), - children: [], - }, - ]; + collection.documentStructure = [document.toJSON()]; await collection.save(); }, }, @@ -88,56 +83,86 @@ const Collection = sequelize.define( return this.documentStructure; }, - async addDocument(document, parentDocumentId, index) { + async addDocument(document, parentDocumentId, index = -1) { + if (!this.documentStructure) return; + if (!parentDocumentId) { this.documentStructure.splice(index, 0, document.toJSON()); } else { - this.documentStructure = this.documentStructure.forEach(doc => { - if (parentDocumentId === document) { - return doc.children.splice(index, 0, document.toJSON()); + this.documentStructure = this.documentStructure.map(childDocument => { + if (parentDocumentId === childDocument.id) { + childDocument.children = childDocument.children.splice( + index, + 0, + document.toJSON() + ); } + return childDocument; }); } - return this.documentStructure; + return this; }, async updateDocument(document) { - // Update document info in this.documents + // if (!this.documentStructure) return; + + const updateChildren = (children, document) => { + const id = document.id; + console.log(id); + if (_.find(children, { id })) { + console.log(1); + children = children.map(childDocument => { + console.log( + childDocument.id, + childDocument.title, + childDocument.id === id + ); + if (childDocument.id === id) { + childDocument = { + ...document.toJSON(), + children: childDocument.children, + }; + } + return childDocument; + }); + } else { + console.log(2); + children = children.map(childDocument => { + return updateChildren(childDocument.children, id); + }); + } + return children; + }; + + this.documentStructure = updateChildren( + this.documentStructure, + document + ); + this.save(); + return this; }, - // async deleteDocument(document) { - // const deleteNodeAndDocument = async ( - // node, - // documentId, - // shouldDelete = false - // ) => { - // // Delete node if id matches - // if (document.id === node.id) shouldDelete = true; - // const newChildren = []; - // node.children.forEach(async childNode => { - // const child = await deleteNodeAndDocument( - // childNode, - // documentId, - // shouldDelete - // ); - // if (child) newChildren.push(child); - // }); - // node.children = newChildren; + async deleteDocument(document) { + if (!this.documentStructure) return; - // if (shouldDelete) { - // const doc = await Document.findById(node.id); - // await doc.destroy(); - // } + const deleteFromChildren = (children, id) => { + if (_.find(children, { id })) { + _.remove(children, { id }); + } else { + children = children.map(childDocument => { + return deleteFromChildren(childDocument.children, id); + }); + } + return children; + }; - // return shouldDelete ? null : node; - // }; - - // this.navigationTree = await deleteNodeAndDocument( - // this.navigationTree, - // document.id - // ); - // }, + this.documentStructure = deleteFromChildren( + this.documentStructure, + document.id + ); + return this; + }, }, } ); diff --git a/server/models/Document.js b/server/models/Document.js index c041eaa82..9e70cef35 100644 --- a/server/models/Document.js +++ b/server/models/Document.js @@ -100,10 +100,13 @@ const Document = sequelize.define( return `/d/${slugifiedTitle}-${this.urlId}`; }, toJSON() { + // Warning: only use for new documents as order of children is + // handled in the collection's documentStructure return { id: this.id, title: this.title, url: this.getUrl(), + children: [], }; }, }, diff --git a/server/presenters.js b/server/presenters.js index d70e5fb99..3e95840e5 100644 --- a/server/presenters.js +++ b/server/presenters.js @@ -39,14 +39,16 @@ export async function presentDocument(ctx, document, options) { }; if (options.includeCollection) { - data.collection = await ctx.cache.get(document.atlasId, async () => { - const collection = await Collection.findOne({ - where: { - id: document.atlasId, - }, - }); - return await presentCollection(ctx, collection); - }); + data.collection = + options.collection || + (await ctx.cache.get(document.atlasId, async () => { + const collection = await Collection.findOne({ + where: { + id: document.atlasId, + }, + }); + return await presentCollection(ctx, collection); + })); } if (options.includeCollaborators) { @@ -93,7 +95,6 @@ export async function presentCollection( }; if (collection.type === 'atlas') { - data.navigationTree = collection.navigationTree; data.documents = await collection.getDocumentsStructure(); }