Automate revision creation
This commit is contained in:
@ -5,7 +5,7 @@ import {
|
|||||||
} from '../sequelize';
|
} from '../sequelize';
|
||||||
|
|
||||||
import auth from './authentication';
|
import auth from './authentication';
|
||||||
import pagination from './middlewares/pagination';
|
// import pagination from './middlewares/pagination';
|
||||||
import { presentDocument } from '../presenters';
|
import { presentDocument } from '../presenters';
|
||||||
import { Document, Atlas } from '../models';
|
import { Document, Atlas } from '../models';
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ const router = new Router();
|
|||||||
|
|
||||||
// FIXME: This really needs specs :/
|
// FIXME: This really needs specs :/
|
||||||
router.post('documents.info', auth({ require: false }), async (ctx) => {
|
router.post('documents.info', auth({ require: false }), async (ctx) => {
|
||||||
let { id } = ctx.body;
|
const { id } = ctx.body;
|
||||||
ctx.assertPresent(id, 'id is required');
|
ctx.assertPresent(id, 'id is required');
|
||||||
|
|
||||||
const document = await Document.findOne({
|
const document = await Document.findOne({
|
||||||
@ -62,13 +62,13 @@ router.post('documents.search', auth(), async (ctx) => {
|
|||||||
sql,
|
sql,
|
||||||
{
|
{
|
||||||
replacements: {
|
replacements: {
|
||||||
query: query,
|
query,
|
||||||
},
|
},
|
||||||
model: Document,
|
model: Document,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
let data = [];
|
const data = [];
|
||||||
await Promise.all(documents.map(async (document) => {
|
await Promise.all(documents.map(async (document) => {
|
||||||
data.push(await presentDocument(document));
|
data.push(await presentDocument(document));
|
||||||
}));
|
}));
|
||||||
@ -92,40 +92,39 @@ router.post('documents.create', auth(), async (ctx) => {
|
|||||||
ctx.assertPresent(text, 'text is required');
|
ctx.assertPresent(text, 'text is required');
|
||||||
|
|
||||||
const user = ctx.state.user;
|
const user = ctx.state.user;
|
||||||
const ownerAtlas = await Atlas.findOne({
|
const ownerCollection = await Atlas.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: collection,
|
id: collection,
|
||||||
teamId: user.teamId,
|
teamId: user.teamId,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!ownerAtlas) throw httpErrors.BadRequest();
|
if (!ownerCollection) throw httpErrors.BadRequest();
|
||||||
|
|
||||||
let parentDocumentObj = {};
|
let parentDocumentObj = {};
|
||||||
if (parentDocument && ownerAtlas.type === 'atlas') {
|
if (parentDocument && ownerCollection.type === 'atlas') {
|
||||||
parentDocumentObj = await Document.findOne({
|
parentDocumentObj = await Document.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: parentDocument,
|
id: parentDocument,
|
||||||
atlasId: ownerAtlas.id,
|
atlasId: ownerCollection.id,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const document = await Document.create({
|
const document = await Document.create({
|
||||||
parentDocumentId: parentDocumentObj.id,
|
parentDocumentId: parentDocumentObj.id,
|
||||||
atlasId: ownerAtlas.id,
|
atlasId: ownerCollection.id,
|
||||||
teamId: user.teamId,
|
teamId: user.teamId,
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
lastModifiedById: user.id,
|
lastModifiedById: user.id,
|
||||||
title,
|
title,
|
||||||
text,
|
text,
|
||||||
});
|
});
|
||||||
await document.createRevision();
|
|
||||||
|
|
||||||
// TODO: Move to afterSave hook if possible with imports
|
// TODO: Move to afterSave hook if possible with imports
|
||||||
if (parentDocument && ownerAtlas.type === 'atlas') {
|
if (parentDocument && ownerCollection.type === 'atlas') {
|
||||||
ownerAtlas.addNodeToNavigationTree(document);
|
ownerCollection.addNodeToNavigationTree(document);
|
||||||
await ownerAtlas.save();
|
await ownerCollection.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
@ -158,7 +157,6 @@ router.post('documents.update', auth(), async (ctx) => {
|
|||||||
document.text = text;
|
document.text = text;
|
||||||
document.lastModifiedById = user.id;
|
document.lastModifiedById = user.id;
|
||||||
await document.save();
|
await document.save();
|
||||||
await document.createRevision();
|
|
||||||
|
|
||||||
// Update
|
// Update
|
||||||
const collection = await Atlas.findById(document.atlasId);
|
const collection = await Atlas.findById(document.atlasId);
|
||||||
|
@ -20,6 +20,18 @@ const generateSlug = (title, urlId) => {
|
|||||||
return `${slugifiedTitle}-${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) => {
|
const documentBeforeSave = (doc) => {
|
||||||
doc.html = convertToMarkdown(doc.text);
|
doc.html = convertToMarkdown(doc.text);
|
||||||
doc.preview = truncateMarkdown(doc.text, 160);
|
doc.preview = truncateMarkdown(doc.text, 160);
|
||||||
@ -52,6 +64,8 @@ const Document = sequelize.define('document', {
|
|||||||
},
|
},
|
||||||
beforeCreate: documentBeforeSave,
|
beforeCreate: documentBeforeSave,
|
||||||
beforeUpdate: documentBeforeSave,
|
beforeUpdate: documentBeforeSave,
|
||||||
|
afterCreate: async (doc) => await createRevision(doc),
|
||||||
|
afterUpdate: async (doc) => await createRevision(doc),
|
||||||
},
|
},
|
||||||
instanceMethods: {
|
instanceMethods: {
|
||||||
buildUrl() {
|
buildUrl() {
|
||||||
@ -61,17 +75,6 @@ const Document = sequelize.define('document', {
|
|||||||
getUrl() {
|
getUrl() {
|
||||||
return `/documents/${this.id}`;
|
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,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user