Fixed document.update API

This commit is contained in:
Jori Lallo
2017-06-28 23:18:46 -07:00
parent b6fa0a4a4e
commit 56beb8b55f
4 changed files with 86 additions and 39 deletions

View File

@ -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'
);
});
});

View File

@ -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;
},

View File

@ -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) {

View File

@ -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,