Fixed Collection#updateDocument
This commit is contained in:
@ -157,18 +157,16 @@ router.post('documents.create', auth(), async ctx => {
|
|||||||
router.post('documents.update', auth(), async ctx => {
|
router.post('documents.update', auth(), async ctx => {
|
||||||
const { id, title, text } = ctx.body;
|
const { id, title, text } = ctx.body;
|
||||||
ctx.assertPresent(id, 'id is required');
|
ctx.assertPresent(id, 'id is required');
|
||||||
ctx.assertPresent(title, 'title is required');
|
ctx.assertPresent(title || text, 'title or text is required');
|
||||||
ctx.assertPresent(text, 'text is required');
|
|
||||||
|
|
||||||
const user = ctx.state.user;
|
const user = ctx.state.user;
|
||||||
const document = await getDocumentForId(id);
|
const document = await getDocumentForId(id);
|
||||||
|
|
||||||
if (!document || document.teamId !== user.teamId)
|
if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound();
|
||||||
throw httpErrors.BadRequest();
|
|
||||||
|
|
||||||
// Update document
|
// Update document
|
||||||
document.title = title;
|
if (title) document.title = title;
|
||||||
document.text = text;
|
if (text) document.text = text;
|
||||||
document.lastModifiedById = user.id;
|
document.lastModifiedById = user.id;
|
||||||
await document.save();
|
await document.save();
|
||||||
|
|
||||||
@ -181,6 +179,7 @@ router.post('documents.update', auth(), async ctx => {
|
|||||||
data: await presentDocument(ctx, document, {
|
data: await presentDocument(ctx, document, {
|
||||||
includeCollection: true,
|
includeCollection: true,
|
||||||
includeCollaborators: true,
|
includeCollaborators: true,
|
||||||
|
collection: collection,
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
@ -50,12 +50,7 @@ const Collection = sequelize.define(
|
|||||||
title: 'Introduction',
|
title: 'Introduction',
|
||||||
text: '# Introduction\n\nLets get started...',
|
text: '# Introduction\n\nLets get started...',
|
||||||
});
|
});
|
||||||
collection.documentStructure = [
|
collection.documentStructure = [document.toJSON()];
|
||||||
{
|
|
||||||
...document.toJSON(),
|
|
||||||
children: [],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
await collection.save();
|
await collection.save();
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -88,56 +83,86 @@ const Collection = sequelize.define(
|
|||||||
return this.documentStructure;
|
return this.documentStructure;
|
||||||
},
|
},
|
||||||
|
|
||||||
async addDocument(document, parentDocumentId, index) {
|
async addDocument(document, parentDocumentId, index = -1) {
|
||||||
|
if (!this.documentStructure) return;
|
||||||
|
|
||||||
if (!parentDocumentId) {
|
if (!parentDocumentId) {
|
||||||
this.documentStructure.splice(index, 0, document.toJSON());
|
this.documentStructure.splice(index, 0, document.toJSON());
|
||||||
} else {
|
} else {
|
||||||
this.documentStructure = this.documentStructure.forEach(doc => {
|
this.documentStructure = this.documentStructure.map(childDocument => {
|
||||||
if (parentDocumentId === document) {
|
if (parentDocumentId === childDocument.id) {
|
||||||
return doc.children.splice(index, 0, document.toJSON());
|
childDocument.children = childDocument.children.splice(
|
||||||
|
index,
|
||||||
|
0,
|
||||||
|
document.toJSON()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
return childDocument;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.documentStructure;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
async updateDocument(document) {
|
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 = [];
|
async deleteDocument(document) {
|
||||||
// node.children.forEach(async childNode => {
|
if (!this.documentStructure) return;
|
||||||
// const child = await deleteNodeAndDocument(
|
|
||||||
// childNode,
|
|
||||||
// documentId,
|
|
||||||
// shouldDelete
|
|
||||||
// );
|
|
||||||
// if (child) newChildren.push(child);
|
|
||||||
// });
|
|
||||||
// node.children = newChildren;
|
|
||||||
|
|
||||||
// if (shouldDelete) {
|
const deleteFromChildren = (children, id) => {
|
||||||
// const doc = await Document.findById(node.id);
|
if (_.find(children, { id })) {
|
||||||
// await doc.destroy();
|
_.remove(children, { id });
|
||||||
// }
|
} else {
|
||||||
|
children = children.map(childDocument => {
|
||||||
|
return deleteFromChildren(childDocument.children, id);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return children;
|
||||||
|
};
|
||||||
|
|
||||||
// return shouldDelete ? null : node;
|
this.documentStructure = deleteFromChildren(
|
||||||
// };
|
this.documentStructure,
|
||||||
|
document.id
|
||||||
// this.navigationTree = await deleteNodeAndDocument(
|
);
|
||||||
// this.navigationTree,
|
return this;
|
||||||
// document.id
|
},
|
||||||
// );
|
|
||||||
// },
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -100,10 +100,13 @@ const Document = sequelize.define(
|
|||||||
return `/d/${slugifiedTitle}-${this.urlId}`;
|
return `/d/${slugifiedTitle}-${this.urlId}`;
|
||||||
},
|
},
|
||||||
toJSON() {
|
toJSON() {
|
||||||
|
// Warning: only use for new documents as order of children is
|
||||||
|
// handled in the collection's documentStructure
|
||||||
return {
|
return {
|
||||||
id: this.id,
|
id: this.id,
|
||||||
title: this.title,
|
title: this.title,
|
||||||
url: this.getUrl(),
|
url: this.getUrl(),
|
||||||
|
children: [],
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -39,14 +39,16 @@ export async function presentDocument(ctx, document, options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (options.includeCollection) {
|
if (options.includeCollection) {
|
||||||
data.collection = await ctx.cache.get(document.atlasId, async () => {
|
data.collection =
|
||||||
const collection = await Collection.findOne({
|
options.collection ||
|
||||||
where: {
|
(await ctx.cache.get(document.atlasId, async () => {
|
||||||
id: document.atlasId,
|
const collection = await Collection.findOne({
|
||||||
},
|
where: {
|
||||||
});
|
id: document.atlasId,
|
||||||
return await presentCollection(ctx, collection);
|
},
|
||||||
});
|
});
|
||||||
|
return await presentCollection(ctx, collection);
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.includeCollaborators) {
|
if (options.includeCollaborators) {
|
||||||
@ -93,7 +95,6 @@ export async function presentCollection(
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (collection.type === 'atlas') {
|
if (collection.type === 'atlas') {
|
||||||
data.navigationTree = collection.navigationTree;
|
|
||||||
data.documents = await collection.getDocumentsStructure();
|
data.documents = await collection.getDocumentsStructure();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user