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

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