From d98ec8630755da2ef7560c78146cb1941bc59d37 Mon Sep 17 00:00:00 2001 From: Saumya Pandey Date: Wed, 11 Aug 2021 17:06:27 +0530 Subject: [PATCH] Create documentUpdater command --- server/api/documents.js | 86 +++++++++++++----------------- server/commands/documentUpdater.js | 55 +++++++++++++++++++ 2 files changed, 91 insertions(+), 50 deletions(-) create mode 100644 server/commands/documentUpdater.js diff --git a/server/api/documents.js b/server/api/documents.js index e6ab6c9c30..eb3b3799be 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -6,6 +6,7 @@ import documentCreator from "../commands/documentCreator"; import documentImporter from "../commands/documentImporter"; import documentMover from "../commands/documentMover"; import { documentPermanentDeleter } from "../commands/documentPermanentDeleter"; +import { documentUpdater } from "../commands/documentUpdater"; import env from "../env"; import { NotFoundError, @@ -32,7 +33,6 @@ import { presentDocument, presentPolicies, } from "../presenters"; -import { sequelize } from "../sequelize"; import pagination from "./middlewares/pagination"; const Op = Sequelize.Op; @@ -1017,95 +1017,81 @@ router.post("documents.update", auth(), async (ctx) => { } const previousTitle = document.title; - - // Update document - if (title) document.title = title; - if (editorVersion) document.editorVersion = editorVersion; - if (templateId) document.templateId = templateId; - - if (append) { - document.text += text; - } else if (text !== undefined) { - document.text = text; - } - document.lastModifiedById = user.id; let collection; - if (!document.publishedAt && document.collection === null && collectionId) { + //draft with no collection and want to be published + if (!document.publishedAt && !document.collection && publish) { + ctx.assertPresent(collectionId, "collectionId should be present"); collection = await Collection.scope({ method: ["withMembership", user.id], }).findByPk(collectionId); - document.collectionId = collectionId; - if (parentDocumentId) document.parentDocumentId = parentDocumentId; + ctx.assertPresent(collection, "Collection should be present"); } else { collection = document.collection; } - let transaction; - try { - transaction = await sequelize.transaction(); - - if (publish) { - ctx.assertPresent(collection, "Collection should be present"); - await document.publish(user.id, { transaction }); - } else { - await document.save({ autosave, transaction }); - } - await transaction.commit(); - } catch (err) { - if (transaction) { - await transaction.rollback(); - } - throw err; + if (publish) { + ctx.assertPresent(collection, "Collection should be present"); } + const updatedDocument = await documentUpdater(document, user, collection, { + id, + title, + text, + publish, + autosave, + done, + templateId, + append, + collectionId, + editorVersion, + parentDocumentId, + }); + if (publish) { await Event.create({ name: "documents.publish", - documentId: document.id, - collectionId: document.collectionId, - teamId: document.teamId, + documentId: updatedDocument.id, + collectionId: updatedDocument.collectionId, + teamId: updatedDocument.teamId, actorId: user.id, - data: { title: document.title }, + data: { title: updatedDocument.title }, ip: ctx.request.ip, }); } else { await Event.create({ name: "documents.update", - documentId: document.id, - collectionId: document.collectionId, - teamId: document.teamId, + documentId: updatedDocument.id, + collectionId: updatedDocument.collectionId, + teamId: updatedDocument.teamId, actorId: user.id, data: { autosave, done, - title: document.title, + title: updatedDocument.title, }, ip: ctx.request.ip, }); } - if (document.title !== previousTitle) { + if (updatedDocument.title !== previousTitle) { Event.add({ name: "documents.title_change", - documentId: document.id, - collectionId: document.collectionId, - teamId: document.teamId, + documentId: updatedDocument.id, + collectionId: updatedDocument.collectionId, + teamId: updatedDocument.teamId, actorId: user.id, data: { previousTitle, - title: document.title, + title: updatedDocument.title, }, ip: ctx.request.ip, }); } - document.updatedBy = user; - document.collection = collection; - ctx.body = { - data: await presentDocument(document), - policies: presentPolicies(user, [document]), + data: await presentDocument(updatedDocument), + policies: presentPolicies(user, [updatedDocument]), }; }); diff --git a/server/commands/documentUpdater.js b/server/commands/documentUpdater.js new file mode 100644 index 0000000000..ffdff757f3 --- /dev/null +++ b/server/commands/documentUpdater.js @@ -0,0 +1,55 @@ +// @flow +import { Document, User, Collection } from "../models"; +import { sequelize } from "../sequelize"; + +export async function documentUpdater( + document: Document, + user: User, + collection?: Collection, + { + title, + editorVersion, + templateId, + text, + append, + collectionId, + parentDocumentId, + publish, + autosave, + }: Object +): Document { + // Update document + if (title) document.title = title; + if (editorVersion) document.editorVersion = editorVersion; + if (templateId) document.templateId = templateId; + document.text = append ? document.text + text : text; + + document.lastModifiedById = user.id; + + let transaction; + let updatedDocument; + try { + transaction = await sequelize.transaction(); + + if (publish) { + if (!document.publishedAt && !document.collection && collectionId) { + document.collectionId = collectionId; + if (parentDocumentId) document.parentDocumentId = parentDocumentId; + } + updatedDocument = await document.publish(user.id, { transaction }); + } else { + updatedDocument = await document.save({ autosave, transaction }); + } + await transaction.commit(); + } catch (err) { + if (transaction) { + await transaction.rollback(); + } + throw err; + } + + updatedDocument.updatedBy = user; + updatedDocument.collection = collection; + + return updatedDocument; +}