Automate revision creation

This commit is contained in:
Jori Lallo
2016-08-12 15:36:48 +02:00
parent f301c305ab
commit bcb39d823f
2 changed files with 26 additions and 25 deletions

View File

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