Compare commits

...

3 Commits

Author SHA1 Message Date
codegen-sh[bot] 000fb56241 Fix event timestamp issue: remove createdAt from revision creation
Events should reflect the time they are recorded in the database, not the model's creation time.
The revision model itself can still have its createdAt set to document.updatedAt, but the event
should use the current database time when it's created.
2025-07-11 21:50:55 +00:00
codegen-sh[bot] cfad218a82 Fix event createdAt timestamp to match model createdAt
When creating events through withCtx methods, the event's createdAt timestamp
should match the model's createdAt timestamp to maintain consistency.
This fixes the failing revisionCreator test that expects both the revision
and its associated event to have the same createdAt timestamp.
2025-07-11 14:46:17 +00:00
codegen-sh[bot] 0f73400ada Convert Revision model to use auto event insertion with withCtx methods
- Add static eventNamespace = 'revisions' to Revision model for proper event naming
- Add createFromDocumentWithCtx method that uses createWithCtx for auto event creation
- Update revisionCreator command to use new withCtx method instead of manual Event.create
- Update revision routes to use saveWithCtx instead of save for auto event creation
- Remove manual Event imports and creation in favor of auto event system

Follows the pattern established in PR #8181 for Comment and Reaction models.
2025-07-11 14:30:26 +00:00
4 changed files with 32 additions and 20 deletions
+8 -19
View File
@@ -1,4 +1,5 @@
import { Document, User, Event, Revision } from "@server/models";
import { createContext } from "@server/context";
import { Document, User, Revision } from "@server/models";
import { sequelize } from "@server/storage/database";
import { DocumentEvent, RevisionEvent } from "@server/types";
@@ -12,25 +13,13 @@ export default async function revisionCreator({
user: User;
}) {
return sequelize.transaction(async (transaction) => {
const revision = await Revision.createFromDocument(document, {
const ctx = createContext({
user,
authType: event.authType,
ip: event.ip ?? user.lastActiveIp,
transaction,
});
await Event.create(
{
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
modelId: revision.id,
teamId: document.teamId,
actorId: user.id,
createdAt: document.updatedAt,
ip: event.ip ?? user.lastActiveIp,
authType: event.authType,
},
{
transaction,
}
);
return revision;
return Revision.createFromDocumentWithCtx(ctx, document);
});
}
+22
View File
@@ -17,6 +17,7 @@ import {
} from "sequelize-typescript";
import type { ProsemirrorData } from "@shared/types";
import { DocumentValidation, RevisionValidation } from "@shared/validations";
import { APIContext } from "@server/types";
import Document from "./Document";
import User from "./User";
import ParanoidModel from "./base/ParanoidModel";
@@ -39,6 +40,7 @@ class Revision extends ParanoidModel<
InferAttributes<Revision>,
Partial<InferCreationAttributes<Revision>>
> {
static eventNamespace = "revisions";
@IsNumeric
@Column(DataType.SMALLINT)
version?: number | null;
@@ -173,6 +175,26 @@ class Revision extends ParanoidModel<
return revision.save(options);
}
/**
* Create a Revision model from a Document model with context for auto event creation
*
* @param ctx The API context
* @param document The document to create from
* @returns A Promise that resolves to the created revision
*/
static createFromDocumentWithCtx(ctx: APIContext, document: Document) {
return this.createWithCtx(ctx, {
title: document.title,
icon: document.icon,
color: document.color,
content: document.content,
userId: document.lastModifiedById,
editorVersion: document.editorVersion,
version: document.version,
documentId: document.id,
});
}
// instance methods
/**
+1
View File
@@ -270,6 +270,7 @@ class Model<
ip: context.ip,
changes: model.previousChangeset,
data: context.event.data,
createdAt: "createdAt" in model ? model.createdAt : undefined,
};
if (context.event?.persist !== false) {
+1 -1
View File
@@ -84,7 +84,7 @@ router.post(
authorize(user, "update", revision);
revision.name = name;
await revision.save({ transaction });
await revision.saveWithCtx(ctx, { transaction });
ctx.body = {
data: await presentRevision(revision),