Notifications refactor (#5151

* Ongoing

* refactor

* test

* Add cleanup task

* refactor
This commit is contained in:
Tom Moor
2023-04-08 09:22:49 -04:00
committed by GitHub
parent c97110e72b
commit 9c9ceef8ee
28 changed files with 1122 additions and 901 deletions

View File

@@ -1,3 +1,4 @@
import type { SaveOptions } from "sequelize";
import {
Table,
ForeignKey,
@@ -10,12 +11,42 @@ import {
DataType,
Default,
AllowNull,
AfterSave,
Scopes,
} from "sequelize-typescript";
import { NotificationEventType } from "@shared/types";
import Collection from "./Collection";
import Comment from "./Comment";
import Document from "./Document";
import Event from "./Event";
import Revision from "./Revision";
import Team from "./Team";
import User from "./User";
import Fix from "./decorators/Fix";
@Scopes(() => ({
withTeam: {
include: [
{
association: "team",
},
],
},
withUser: {
include: [
{
association: "user",
},
],
},
withActor: {
include: [
{
association: "actor",
},
],
},
}))
@Table({
tableName: "notifications",
modelName: "notification",
@@ -40,8 +71,8 @@ class Notification extends Model {
@CreatedAt
createdAt: Date;
@Column
event: string;
@Column(DataType.STRING)
event: NotificationEventType;
// associations
@@ -60,6 +91,14 @@ class Notification extends Model {
@Column(DataType.UUID)
actorId: string;
@BelongsTo(() => Comment, "commentId")
comment: Comment;
@AllowNull
@ForeignKey(() => Comment)
@Column(DataType.UUID)
commentId: string;
@BelongsTo(() => Document, "documentId")
document: Document;
@@ -68,12 +107,49 @@ class Notification extends Model {
@Column(DataType.UUID)
documentId: string;
@BelongsTo(() => Revision, "revisionId")
revision: Revision;
@AllowNull
@ForeignKey(() => Revision)
@Column(DataType.UUID)
revisionId: string;
@BelongsTo(() => Collection, "collectionId")
collection: Collection;
@AllowNull
@ForeignKey(() => Collection)
@Column(DataType.UUID)
collectionId: string;
@BelongsTo(() => Team, "teamId")
team: Team;
@ForeignKey(() => Team)
@Column(DataType.UUID)
teamId: string;
@AfterSave
static async createEvent(
model: Notification,
options: SaveOptions<Notification>
) {
const params = {
name: "notifications.create",
userId: model.userId,
modelId: model.id,
teamId: model.teamId,
documentId: model.documentId,
actorId: model.actorId,
};
if (options.transaction) {
options.transaction.afterCommit(() => void Event.schedule(params));
return;
}
await Event.schedule(params);
}
}
export default Notification;