fix: documentUpdater called without change can result in lastModifiedById being updated

This commit is contained in:
Tom Moor
2022-05-22 22:39:54 +01:00
parent a78ad8dec2
commit 73de15fd5d
3 changed files with 59 additions and 5 deletions

View File

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

View File

@@ -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",

View File

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