Merge pull request #89 from jorilallo/jori/document.update

Fixed document.update API
This commit is contained in:
Jori Lallo
2017-06-29 19:00:45 -07:00
committed by GitHub
5 changed files with 82 additions and 34 deletions

View File

@ -3,7 +3,7 @@
exports[`#auth.login should login with email 1`] = ` exports[`#auth.login should login with email 1`] = `
Object { Object {
"avatarUrl": "http://example.com/avatar.png", "avatarUrl": "http://example.com/avatar.png",
"id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61", "id": "46fde1d4-0050-428f-9f0b-0bf77f4bdf61",
"name": "User 1", "name": "User 1",
"username": "user1", "username": "user1",
} }
@ -12,7 +12,7 @@ Object {
exports[`#auth.login should login with username 1`] = ` exports[`#auth.login should login with username 1`] = `
Object { Object {
"avatarUrl": "http://example.com/avatar.png", "avatarUrl": "http://example.com/avatar.png",
"id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61", "id": "46fde1d4-0050-428f-9f0b-0bf77f4bdf61",
"name": "User 1", "name": "User 1",
"username": "user1", "username": "user1",
} }

View File

@ -13,7 +13,7 @@ exports[`#user.info should return known user 1`] = `
Object { Object {
"data": Object { "data": Object {
"avatarUrl": "http://example.com/avatar.png", "avatarUrl": "http://example.com/avatar.png",
"id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61", "id": "46fde1d4-0050-428f-9f0b-0bf77f4bdf61",
"name": "User 1", "name": "User 1",
"username": "user1", "username": "user1",
}, },

View File

@ -156,3 +156,60 @@ describe('#documents.unstar', async () => {
expect(body).toMatchSnapshot(); 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.documents[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.documents[0].children[1].title).toBe(
'Updated title'
);
});
});

View File

@ -119,35 +119,25 @@ const Collection = sequelize.define(
return this; return this;
}, },
async updateDocument(document) { async updateDocument(updatedDocument) {
if (!this.documentStructure) return; if (!this.documentStructure) return;
const { id } = updatedDocument;
const updateChildren = (children, document) => { const updateChildren = documents => {
const id = document.id; return documents.map(document => {
if (document.id === id) {
if (_.find(children, { id })) { document = {
children = children.map(childDocument => { ...updatedDocument.toJSON(),
if (childDocument.id === id) { children: document.children,
childDocument = { };
...document.toJSON(), } else {
children: childDocument.children, document.children = updateChildren(document.children);
}; }
} return document;
return childDocument; });
});
} else {
children = children.map(childDocument => {
return updateChildren(childDocument.children, id);
});
}
return children;
}; };
this.documentStructure = updateChildren( this.documentStructure = updateChildren(this.documentStructure);
this.documentStructure,
document
);
await this.save(); await this.save();
return this; return this;
}, },

View File

@ -23,7 +23,7 @@ const seed = async () => {
}); });
const user = await User.create({ const user = await User.create({
id: '86fde1d4-0050-428f-9f0b-0bf77f8bdf61', id: '46fde1d4-0050-428f-9f0b-0bf77f4bdf61',
email: 'user1@example.com', email: 'user1@example.com',
username: 'user1', username: 'user1',
name: 'User 1', name: 'User 1',
@ -36,8 +36,8 @@ const seed = async () => {
}, },
}); });
const collection = await Collection.create({ let collection = await Collection.create({
id: '86fde1d4-0050-428f-9f0b-0bf77f8bdf61', id: '26fde1d4-0050-428f-9f0b-0bf77f8bdf62',
name: 'Collection', name: 'Collection',
urlId: 'collection', urlId: 'collection',
teamId: team.id, teamId: team.id,
@ -45,16 +45,17 @@ const seed = async () => {
type: 'atlas', type: 'atlas',
}); });
const document = await Document.create({ let document = await Document.create({
parentDocumentId: null, parentDocumentId: null,
atlasId: collection.id, atlasId: collection.id,
teamId: collection.teamId, teamId: collection.teamId,
userId: collection.creatorId, userId: collection.creatorId,
lastModifiedById: collection.creatorId, lastModifiedById: collection.creatorId,
createdById: collection.creatorId, createdById: collection.creatorId,
title: 'Introduction', title: 'Second document',
text: '# Introduction\n\nLets get started...', text: '# Much guidance',
}); });
collection = await collection.addDocumentToStructure(document);
return { return {
user, user,