From bcb39d823f5ef2821d06e2b7f5e515b0b877eaf7 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Fri, 12 Aug 2016 15:36:48 +0200 Subject: [PATCH] Automate revision creation --- server/api/documents.js | 26 ++++++++++++-------------- server/models/Document.js | 25 ++++++++++++++----------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/server/api/documents.js b/server/api/documents.js index 9d39286c..f0366477 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -5,7 +5,7 @@ import { } from '../sequelize'; import auth from './authentication'; -import pagination from './middlewares/pagination'; +// import pagination from './middlewares/pagination'; import { presentDocument } from '../presenters'; import { Document, Atlas } from '../models'; @@ -13,7 +13,7 @@ const router = new Router(); // FIXME: This really needs specs :/ router.post('documents.info', auth({ require: false }), async (ctx) => { - let { id } = ctx.body; + const { id } = ctx.body; ctx.assertPresent(id, 'id is required'); const document = await Document.findOne({ @@ -62,13 +62,13 @@ router.post('documents.search', auth(), async (ctx) => { sql, { replacements: { - query: query, + query, }, model: Document, } ); - let data = []; + const data = []; await Promise.all(documents.map(async (document) => { data.push(await presentDocument(document)); })); @@ -92,40 +92,39 @@ router.post('documents.create', auth(), async (ctx) => { ctx.assertPresent(text, 'text is required'); const user = ctx.state.user; - const ownerAtlas = await Atlas.findOne({ + const ownerCollection = await Atlas.findOne({ where: { id: collection, teamId: user.teamId, }, }); - if (!ownerAtlas) throw httpErrors.BadRequest(); + if (!ownerCollection) throw httpErrors.BadRequest(); let parentDocumentObj = {}; - if (parentDocument && ownerAtlas.type === 'atlas') { + if (parentDocument && ownerCollection.type === 'atlas') { parentDocumentObj = await Document.findOne({ where: { id: parentDocument, - atlasId: ownerAtlas.id, + atlasId: ownerCollection.id, }, }); } const document = await Document.create({ parentDocumentId: parentDocumentObj.id, - atlasId: ownerAtlas.id, + atlasId: ownerCollection.id, teamId: user.teamId, userId: user.id, lastModifiedById: user.id, title, text, }); - await document.createRevision(); // TODO: Move to afterSave hook if possible with imports - if (parentDocument && ownerAtlas.type === 'atlas') { - ownerAtlas.addNodeToNavigationTree(document); - await ownerAtlas.save(); + if (parentDocument && ownerCollection.type === 'atlas') { + ownerCollection.addNodeToNavigationTree(document); + await ownerCollection.save(); } ctx.body = { @@ -158,7 +157,6 @@ router.post('documents.update', auth(), async (ctx) => { document.text = text; document.lastModifiedById = user.id; await document.save(); - await document.createRevision(); // Update const collection = await Atlas.findById(document.atlasId); diff --git a/server/models/Document.js b/server/models/Document.js index 71274429..8570dde9 100644 --- a/server/models/Document.js +++ b/server/models/Document.js @@ -20,6 +20,18 @@ const generateSlug = (title, urlId) => { return `${slugifiedTitle}-${urlId}`; }; +const createRevision = async (doc) => { + // Create revision of the current (latest) + await Revision.create({ + title: doc.title, + text: doc.text, + html: doc.html, + preview: doc.preview, + userId: doc.lastModifiedById, + documentId: doc.id, + }); +}; + const documentBeforeSave = (doc) => { doc.html = convertToMarkdown(doc.text); doc.preview = truncateMarkdown(doc.text, 160); @@ -52,6 +64,8 @@ const Document = sequelize.define('document', { }, beforeCreate: documentBeforeSave, beforeUpdate: documentBeforeSave, + afterCreate: async (doc) => await createRevision(doc), + afterUpdate: async (doc) => await createRevision(doc), }, instanceMethods: { buildUrl() { @@ -61,17 +75,6 @@ const Document = sequelize.define('document', { getUrl() { return `/documents/${this.id}`; }, - async createRevision() { - // Create revision of the current (latest) - await Revision.create({ - title: this.title, - text: this.text, - html: this.html, - preview: this.preview, - userId: this.lastModifiedById, - documentId: this.id, - }); - }, }, });