diff --git a/server/commands/documentUpdater.test.ts b/server/commands/documentUpdater.test.ts new file mode 100644 index 000000000..177819bbb --- /dev/null +++ b/server/commands/documentUpdater.test.ts @@ -0,0 +1,54 @@ +import { sequelize } from "@server/database/sequelize"; +import { Event } from "@server/models"; +import { buildDocument, buildUser } from "@server/test/factories"; +import { flushdb } from "@server/test/support"; +import documentUpdater from "./documentUpdater"; + +beforeEach(() => flushdb()); + +describe("documentUpdater", () => { + const ip = "127.0.0.1"; + + it("should change lastModifiedById", async () => { + const user = await buildUser(); + let document = await buildDocument({ + teamId: user.teamId, + }); + + document = await sequelize.transaction(async (transaction) => + documentUpdater({ + text: "Changed", + document, + user, + ip, + transaction, + }) + ); + + const event = await Event.findOne(); + expect(document.lastModifiedById).toEqual(user.id); + expect(event!.name).toEqual("documents.update"); + expect(event!.documentId).toEqual(document.id); + }); + + it("should not change lastModifiedById or generate event if nothing changed", async () => { + const user = await buildUser(); + let document = await buildDocument({ + teamId: user.teamId, + }); + + document = await sequelize.transaction(async (transaction) => + documentUpdater({ + title: document.title, + document, + user, + ip, + transaction, + }) + ); + + const event = await Event.findOne(); + expect(document.lastModifiedById).not.toEqual(user.id); + expect(event).toEqual(null); + }); +}); diff --git a/server/commands/documentUpdater.ts b/server/commands/documentUpdater.ts index f8c554961..80e3ff1bb 100644 --- a/server/commands/documentUpdater.ts +++ b/server/commands/documentUpdater.ts @@ -68,16 +68,12 @@ export default async function documentUpdater({ } } - document.lastModifiedById = user.id; const changed = document.changed(); if (publish) { + document.lastModifiedById = user.id; await document.publish(user.id, { transaction }); - } else { - await document.save({ transaction }); - } - if (publish) { await Event.create( { name: "documents.publish", @@ -93,6 +89,9 @@ export default async function documentUpdater({ { transaction } ); } else if (changed) { + document.lastModifiedById = user.id; + await document.save({ transaction }); + await Event.create( { name: "documents.update", diff --git a/server/commands/revisionCreator.test.ts b/server/commands/revisionCreator.test.ts index 3da30ba33..62ea6bcb8 100644 --- a/server/commands/revisionCreator.test.ts +++ b/server/commands/revisionCreator.test.ts @@ -22,6 +22,7 @@ describe("revisionCreator", () => { const event = await Event.findOne(); expect(revision.documentId).toEqual(document.id); expect(revision.userId).toEqual(user.id); + expect(revision.createdAt).toEqual(document.updatedAt); expect(event!.name).toEqual("revisions.create"); expect(event!.modelId).toEqual(revision.id); expect(event!.createdAt).toEqual(document.updatedAt);