fix: User updates are not synced between clients (#6490)
* Add Model.changeset method to get minified changes since last update * fix: Handle arrays * Add changes column, types * test
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type {
|
||||
CreateOptions,
|
||||
InferAttributes,
|
||||
InferCreationAttributes,
|
||||
SaveOptions,
|
||||
@@ -17,7 +18,7 @@ import {
|
||||
Length,
|
||||
} from "sequelize-typescript";
|
||||
import { globalEventQueue } from "../queues";
|
||||
import { Event as TEvent } from "../types";
|
||||
import { APIContext, Event as TEvent } from "../types";
|
||||
import Collection from "./Collection";
|
||||
import Document from "./Document";
|
||||
import Team from "./Team";
|
||||
@@ -35,20 +36,36 @@ class Event extends IdModel<
|
||||
@Column(DataType.UUID)
|
||||
modelId: string | null;
|
||||
|
||||
/**
|
||||
* The name of the event.
|
||||
*/
|
||||
@Length({
|
||||
max: 255,
|
||||
msg: "name must be 255 characters or less",
|
||||
})
|
||||
@Column
|
||||
@Column(DataType.STRING)
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* The originating IP address of the event.
|
||||
*/
|
||||
@IsIP
|
||||
@Column
|
||||
ip: string | null;
|
||||
|
||||
/**
|
||||
* Metadata associated with the event, previously used for storing some changed attributes.
|
||||
*/
|
||||
@Column(DataType.JSONB)
|
||||
data: Record<string, any> | null;
|
||||
|
||||
/**
|
||||
* The changes made to the model – gradually moving to this column away from `data` which can be
|
||||
* used for arbitrary data associated with the event.
|
||||
*/
|
||||
@Column(DataType.JSONB)
|
||||
changes?: Record<string, any> | null;
|
||||
|
||||
// hooks
|
||||
|
||||
@BeforeCreate
|
||||
@@ -132,6 +149,30 @@ class Event extends IdModel<
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and persist new event from request context
|
||||
*
|
||||
* @param ctx The request context to use
|
||||
* @param attributes The event attributes
|
||||
* @returns A promise resolving to the new event
|
||||
*/
|
||||
static createFromContext(
|
||||
ctx: APIContext,
|
||||
attributes: Omit<Partial<Event>, "ip" | "teamId" | "actorId"> = {},
|
||||
options?: CreateOptions<InferAttributes<Event>>
|
||||
) {
|
||||
const { user } = ctx.state.auth;
|
||||
return this.create(
|
||||
{
|
||||
...attributes,
|
||||
actorId: user.id,
|
||||
teamId: user.teamId,
|
||||
ip: ctx.request.ip,
|
||||
},
|
||||
options
|
||||
);
|
||||
}
|
||||
|
||||
static ACTIVITY_EVENTS: TEvent["name"][] = [
|
||||
"collections.create",
|
||||
"collections.delete",
|
||||
|
||||
Reference in New Issue
Block a user