From 0de27e55d6ec98c55c701de48e258500e2bbdaf1 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Wed, 6 Jul 2016 21:36:50 -0700 Subject: [PATCH] Backend fixes --- server/api/documents.js | 13 +++++++++++-- .../20160622043741-add-parent-document.js | 4 ---- server/models/Atlas.js | 18 +++++++++--------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/server/api/documents.js b/server/api/documents.js index efc0ef0a..36770588 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -139,20 +139,29 @@ router.post('documents.delete', auth(), async (ctx) => { ctx.assertPresent(id, 'id is required'); const user = ctx.state.user; - let document = await Document.findOne({ + const document = await Document.findOne({ where: { id: id, teamId: user.teamId, }, }); + const atlas = await Atlas.findById(document.atlasId); if (!document) throw httpErrors.BadRequest(); - // TODO: Don't allow to destroy root docs // TODO: handle sub documents + // Don't allow deletion of root docs + if (atlas.type === 'atlas' && !document.parentDocumentId) { + throw httpErrors.BadRequest('Unable to delete atlas\'s root document'); + } + try { await document.destroy(); + + if (atlas.type === 'atlas') { + await atlas.updateNavigationTree(); + } } catch (e) { throw httpErrors.BadRequest('Error while deleting'); }; diff --git a/server/migrations/20160622043741-add-parent-document.js b/server/migrations/20160622043741-add-parent-document.js index eda6ae28..f8ecdbb7 100644 --- a/server/migrations/20160622043741-add-parent-document.js +++ b/server/migrations/20160622043741-add-parent-document.js @@ -8,10 +8,6 @@ module.exports = { { type: Sequelize.UUID, allowNull: true, - references: { - model: "documents", - key: "id", - } } ); }, diff --git a/server/models/Atlas.js b/server/models/Atlas.js index 7fc61cf0..98405fa5 100644 --- a/server/models/Atlas.js +++ b/server/models/Atlas.js @@ -96,15 +96,15 @@ const Atlas = sequelize.define('atlas', { atlasId: this.id, }, }); - if (!childDocument) throw new Error; - - childNodes.push({ - id: childDocument.id, - title: childDocument.title, - url: childDocument.getUrl(), - children: await getIdsForChildren(child.children), - }) - nodeIds.push(child.id); + if (childDocument) { + childNodes.push({ + id: childDocument.id, + title: childDocument.title, + url: childDocument.getUrl(), + children: await getIdsForChildren(child.children), + }) + nodeIds.push(child.id); + } } return childNodes; };