Added deletion of documents
This commit is contained in:
@ -136,20 +136,15 @@ router.post('documents.create', auth(), async ctx => {
|
|||||||
text,
|
text,
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: Move to afterSave hook if possible with imports
|
if (ownerCollection.type === 'atlas') {
|
||||||
if (parentDocument && ownerCollection.type === 'atlas') {
|
await ownerCollection.addDocumentToStructure(newDocument, index);
|
||||||
ownerCollection.addDocument(
|
|
||||||
newDocument,
|
|
||||||
newDocument.parentDocumentId,
|
|
||||||
index || -1
|
|
||||||
);
|
|
||||||
await ownerCollection.save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
data: await presentDocument(ctx, newDocument, {
|
data: await presentDocument(ctx, newDocument, {
|
||||||
includeCollection: true,
|
includeCollection: true,
|
||||||
includeCollaborators: true,
|
includeCollaborators: true,
|
||||||
|
collection: ownerCollection,
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@ -197,14 +192,15 @@ router.post('documents.delete', auth(), async ctx => {
|
|||||||
|
|
||||||
if (collection.type === 'atlas') {
|
if (collection.type === 'atlas') {
|
||||||
// Don't allow deletion of root docs
|
// Don't allow deletion of root docs
|
||||||
if (!document.parentDocumentId) {
|
if (collection.documentStructure.length === 1) {
|
||||||
throw httpErrors.BadRequest("Unable to delete atlas's root document");
|
throw httpErrors.BadRequest(
|
||||||
|
"Unable to delete collection's only document"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete all chilren
|
// Delete all chilren
|
||||||
try {
|
try {
|
||||||
await collection.deleteDocument(document);
|
await collection.deleteDocument(document);
|
||||||
await collection.save();
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw httpErrors.BadRequest('Error while deleting');
|
throw httpErrors.BadRequest('Error while deleting');
|
||||||
}
|
}
|
||||||
|
@ -83,16 +83,22 @@ const Collection = sequelize.define(
|
|||||||
return this.documentStructure;
|
return this.documentStructure;
|
||||||
},
|
},
|
||||||
|
|
||||||
async addDocument(document, parentDocumentId, index = -1) {
|
async addDocumentToStructure(document, index) {
|
||||||
if (!this.documentStructure) return;
|
if (!this.documentStructure) return;
|
||||||
|
|
||||||
if (!parentDocumentId) {
|
if (!document.parentDocumentId) {
|
||||||
this.documentStructure.splice(index, 0, document.toJSON());
|
this.documentStructure.splice(
|
||||||
|
index || this.documentStructure.length,
|
||||||
|
0,
|
||||||
|
document.toJSON()
|
||||||
|
);
|
||||||
|
// Sequelize doesn't seem to set the value with splice on JSONB field
|
||||||
|
this.documentStructure = this.documentStructure;
|
||||||
} else {
|
} else {
|
||||||
this.documentStructure = this.documentStructure.map(childDocument => {
|
this.documentStructure = this.documentStructure.map(childDocument => {
|
||||||
if (parentDocumentId === childDocument.id) {
|
if (document.parentDocumentId === childDocument.id) {
|
||||||
childDocument.children = childDocument.children.splice(
|
childDocument.children.splice(
|
||||||
index,
|
index || childDocument.children.length,
|
||||||
0,
|
0,
|
||||||
document.toJSON()
|
document.toJSON()
|
||||||
);
|
);
|
||||||
@ -101,11 +107,12 @@ const Collection = sequelize.define(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await this.save();
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
async updateDocument(document) {
|
async updateDocument(document) {
|
||||||
// if (!this.documentStructure) return;
|
if (!this.documentStructure) return;
|
||||||
|
|
||||||
const updateChildren = (children, document) => {
|
const updateChildren = (children, document) => {
|
||||||
const id = document.id;
|
const id = document.id;
|
||||||
@ -151,7 +158,10 @@ const Collection = sequelize.define(
|
|||||||
_.remove(children, { id });
|
_.remove(children, { id });
|
||||||
} else {
|
} else {
|
||||||
children = children.map(childDocument => {
|
children = children.map(childDocument => {
|
||||||
return deleteFromChildren(childDocument.children, id);
|
return {
|
||||||
|
...childDocument,
|
||||||
|
children: deleteFromChildren(childDocument.children, id),
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return children;
|
return children;
|
||||||
@ -161,6 +171,8 @@ const Collection = sequelize.define(
|
|||||||
this.documentStructure,
|
this.documentStructure,
|
||||||
document.id
|
document.id
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await this.save();
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -39,16 +39,16 @@ export async function presentDocument(ctx, document, options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (options.includeCollection) {
|
if (options.includeCollection) {
|
||||||
data.collection =
|
data.collection = await ctx.cache.get(document.atlasId, async () => {
|
||||||
options.collection ||
|
const collection =
|
||||||
(await ctx.cache.get(document.atlasId, async () => {
|
options.collection ||
|
||||||
const collection = await Collection.findOne({
|
(await Collection.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: document.atlasId,
|
id: document.atlasId,
|
||||||
},
|
},
|
||||||
});
|
}));
|
||||||
return await presentCollection(ctx, collection);
|
return await presentCollection(ctx, collection);
|
||||||
}));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.includeCollaborators) {
|
if (options.includeCollaborators) {
|
||||||
|
Reference in New Issue
Block a user