From 56beb8b55f5a4e8273b6b1e79b076fa78aa6b6fd Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Wed, 28 Jun 2017 23:18:46 -0700 Subject: [PATCH 1/2] Fixed document.update API --- server/api/documents.test.js | 59 +++++++++++++++++++++++++++++++++++ server/models/Collection.js | 40 +++++++++--------------- server/presenters/document.js | 17 +++++----- server/test/support.js | 9 +++--- 4 files changed, 86 insertions(+), 39 deletions(-) diff --git a/server/api/documents.test.js b/server/api/documents.test.js index e6dd8e6a..9a2f1e63 100644 --- a/server/api/documents.test.js +++ b/server/api/documents.test.js @@ -156,3 +156,62 @@ describe('#documents.unstar', async () => { expect(body).toMatchSnapshot(); }); }); + +describe('#documents.update', async () => { + it('should update document details in the root', async () => { + const { user, document } = await seed(); + + const res = await server.post('/api/documents.update', { + body: { + token: user.getJwtToken(), + id: document.id, + title: 'Updated title', + text: 'Updated text', + }, + }); + 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.collection.documentStructure[1].title).toBe( + 'Updated title' + ); + }); + + it('should update document details for children', async () => { + const { user, document, collection } = await seed(); + collection.documentStructure = [ + { + id: 'af1da94b-9591-4bab-897c-11774b804b77', + url: '/d/some-beef-RSZwQDsfpc', + title: 'some beef', + children: [ + { + id: 'ab1da94b-9591-4bab-897c-11774b804b66', + url: '/d/another-doc-RSZwQDsfpc', + title: 'Another doc', + children: [], + }, + { ...document.toJSON(), children: [] }, + ], + }, + ]; + await collection.save(); + + const res = await server.post('/api/documents.update', { + body: { + token: user.getJwtToken(), + id: document.id, + title: 'Updated title', + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.title).toBe('Updated title'); + expect(body.data.collection.documentStructure[0].children[1].title).toBe( + 'Updated title' + ); + }); +}); diff --git a/server/models/Collection.js b/server/models/Collection.js index e19befe4..953a57ae 100644 --- a/server/models/Collection.js +++ b/server/models/Collection.js @@ -119,35 +119,25 @@ const Collection = sequelize.define( return this; }, - async updateDocument(document) { + async updateDocument(updatedDocument) { if (!this.documentStructure) return; + const { id } = updatedDocument; - const updateChildren = (children, document) => { - const id = document.id; - - if (_.find(children, { id })) { - children = children.map(childDocument => { - if (childDocument.id === id) { - childDocument = { - ...document.toJSON(), - children: childDocument.children, - }; - } - return childDocument; - }); - } else { - children = children.map(childDocument => { - return updateChildren(childDocument.children, id); - }); - } - return children; + const updateChildren = documents => { + return documents.map(document => { + if (document.id === id) { + document = { + ...updatedDocument.toJSON(), + children: document.children, + }; + } else { + document.children = updateChildren(document.children); + } + return document; + }); }; - this.documentStructure = updateChildren( - this.documentStructure, - document - ); - + this.documentStructure = updateChildren(this.documentStructure); await this.save(); return this; }, diff --git a/server/presenters/document.js b/server/presenters/document.js index 8757ed7e..54d1f467 100644 --- a/server/presenters/document.js +++ b/server/presenters/document.js @@ -39,16 +39,13 @@ async function present(ctx, document, options) { } if (options.includeCollection) { - data.collection = await ctx.cache.get(document.atlasId, async () => { - const collection = - options.collection || - (await Collection.findOne({ - where: { - id: document.atlasId, - }, - })); - return presentCollection(ctx, collection); - }); + data.collection = + options.collection || + (await Collection.findOne({ + where: { + id: document.atlasId, + }, + })); } if (options.includeCollaborators) { diff --git a/server/test/support.js b/server/test/support.js index 0b26003d..34105b01 100644 --- a/server/test/support.js +++ b/server/test/support.js @@ -36,7 +36,7 @@ const seed = async () => { }, }); - const collection = await Collection.create({ + let collection = await Collection.create({ id: '86fde1d4-0050-428f-9f0b-0bf77f8bdf61', name: 'Collection', urlId: 'collection', @@ -45,16 +45,17 @@ const seed = async () => { type: 'atlas', }); - const document = await Document.create({ + let document = await Document.create({ parentDocumentId: null, atlasId: collection.id, teamId: collection.teamId, userId: collection.creatorId, lastModifiedById: collection.creatorId, createdById: collection.creatorId, - title: 'Introduction', - text: '# Introduction\n\nLets get started...', + title: 'Second document', + text: '# Much guidance', }); + collection = await collection.addDocumentToStructure(document); return { user, From 22153441fa2f3ce582239499fa56965791fef8fc Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Wed, 28 Jun 2017 23:24:19 -0700 Subject: [PATCH 2/2] Updated IDs and fixed tests --- server/api/__snapshots__/auth.test.js.snap | 4 ++-- server/api/__snapshots__/user.test.js.snap | 2 +- server/api/documents.test.js | 6 ++---- server/presenters/document.js | 17 ++++++++++------- server/test/support.js | 4 ++-- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/server/api/__snapshots__/auth.test.js.snap b/server/api/__snapshots__/auth.test.js.snap index c7e602ea..2be2eba7 100644 --- a/server/api/__snapshots__/auth.test.js.snap +++ b/server/api/__snapshots__/auth.test.js.snap @@ -3,7 +3,7 @@ exports[`#auth.login should login with email 1`] = ` Object { "avatarUrl": "http://example.com/avatar.png", - "id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61", + "id": "46fde1d4-0050-428f-9f0b-0bf77f4bdf61", "name": "User 1", "username": "user1", } @@ -12,7 +12,7 @@ Object { exports[`#auth.login should login with username 1`] = ` Object { "avatarUrl": "http://example.com/avatar.png", - "id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61", + "id": "46fde1d4-0050-428f-9f0b-0bf77f4bdf61", "name": "User 1", "username": "user1", } diff --git a/server/api/__snapshots__/user.test.js.snap b/server/api/__snapshots__/user.test.js.snap index 6dc4de62..2f6503ac 100644 --- a/server/api/__snapshots__/user.test.js.snap +++ b/server/api/__snapshots__/user.test.js.snap @@ -13,7 +13,7 @@ exports[`#user.info should return known user 1`] = ` Object { "data": Object { "avatarUrl": "http://example.com/avatar.png", - "id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61", + "id": "46fde1d4-0050-428f-9f0b-0bf77f4bdf61", "name": "User 1", "username": "user1", }, diff --git a/server/api/documents.test.js b/server/api/documents.test.js index 9a2f1e63..ec477e6c 100644 --- a/server/api/documents.test.js +++ b/server/api/documents.test.js @@ -174,9 +174,7 @@ describe('#documents.update', async () => { expect(res.status).toEqual(200); expect(body.data.title).toBe('Updated title'); expect(body.data.text).toBe('Updated text'); - expect(body.data.collection.documentStructure[1].title).toBe( - 'Updated title' - ); + expect(body.data.collection.documents[1].title).toBe('Updated title'); }); it('should update document details for children', async () => { @@ -210,7 +208,7 @@ describe('#documents.update', async () => { expect(res.status).toEqual(200); expect(body.data.title).toBe('Updated title'); - expect(body.data.collection.documentStructure[0].children[1].title).toBe( + expect(body.data.collection.documents[0].children[1].title).toBe( 'Updated title' ); }); diff --git a/server/presenters/document.js b/server/presenters/document.js index 54d1f467..8757ed7e 100644 --- a/server/presenters/document.js +++ b/server/presenters/document.js @@ -39,13 +39,16 @@ async function present(ctx, document, options) { } if (options.includeCollection) { - data.collection = - options.collection || - (await Collection.findOne({ - where: { - id: document.atlasId, - }, - })); + data.collection = await ctx.cache.get(document.atlasId, async () => { + const collection = + options.collection || + (await Collection.findOne({ + where: { + id: document.atlasId, + }, + })); + return presentCollection(ctx, collection); + }); } if (options.includeCollaborators) { diff --git a/server/test/support.js b/server/test/support.js index 34105b01..128ede92 100644 --- a/server/test/support.js +++ b/server/test/support.js @@ -23,7 +23,7 @@ const seed = async () => { }); const user = await User.create({ - id: '86fde1d4-0050-428f-9f0b-0bf77f8bdf61', + id: '46fde1d4-0050-428f-9f0b-0bf77f4bdf61', email: 'user1@example.com', username: 'user1', name: 'User 1', @@ -37,7 +37,7 @@ const seed = async () => { }); let collection = await Collection.create({ - id: '86fde1d4-0050-428f-9f0b-0bf77f8bdf61', + id: '26fde1d4-0050-428f-9f0b-0bf77f8bdf62', name: 'Collection', urlId: 'collection', teamId: team.id,