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 => {
|
||||
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,
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
@ -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;
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
@ -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: [],
|
||||
};
|
||||
},
|
||||
},
|
||||
|
@ -39,14 +39,16 @@ export async function presentDocument(ctx, document, options) {
|
||||
};
|
||||
|
||||
if (options.includeCollection) {
|
||||
data.collection = await ctx.cache.get(document.atlasId, async () => {
|
||||
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();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user