瀏覽代碼

feat: add news published/unpublished events

Kristian Vos 7 月之前
父節點
當前提交
c39d9e688d

+ 52 - 1
backend/src/modules/DataModule/models/news/config.ts

@@ -1,6 +1,11 @@
+import { HydratedDocument } from "mongoose";
+import ModelCreatedEvent from "../../ModelCreatedEvent";
+import ModelDeletedEvent from "../../ModelDeletedEvent";
+import ModelUpdatedEvent from "../../ModelUpdatedEvent";
 import { NewsStatus } from "./NewsStatus";
 import getData from "./getData";
-import { NewsSchemaOptions } from "./schema";
+import { NewsSchema, NewsSchemaOptions } from "./schema";
+import EventsModule from "@/modules/EventsModule";
 
 export default {
 	documentVersion: 3,
@@ -15,5 +20,51 @@ export default {
 			return query;
 		}
 	},
+	eventListeners: {
+		"data.news.created.*": async (event: ModelCreatedEvent) => {
+			const { doc }: { doc: HydratedDocument<NewsSchema> } =
+				event.getData();
+
+			if (doc.status === NewsStatus.PUBLISHED) {
+				const EventClass = EventsModule.getEvent(`data.news.published`);
+				await EventsModule.publish(new EventClass({ doc }));
+			}
+		},
+		"data.news.updated.*": async (event: ModelUpdatedEvent) => {
+			const {
+				doc,
+				oldDoc
+			}: {
+				doc: HydratedDocument<NewsSchema>;
+				oldDoc: HydratedDocument<NewsSchema>;
+			} = event.getData();
+
+			if (doc.status === oldDoc.status) return;
+			if (doc.status === NewsStatus.PUBLISHED) {
+				const EventClass = EventsModule.getEvent(`data.news.published`);
+				await EventsModule.publish(new EventClass({ doc }));
+			} else if (oldDoc.status === NewsStatus.PUBLISHED) {
+				const EventClass = EventsModule.getEvent(
+					`data.news.unpublished`
+				);
+				await EventsModule.publish(
+					new EventClass({ oldDoc }, doc._id.toString()) // TODO maybe only pass modelId to unpublished event here, or when sending to clients
+				);
+			}
+		},
+		"data.news.deleted.*": async (event: ModelDeletedEvent) => {
+			const { oldDoc }: { oldDoc: HydratedDocument<NewsSchema> } =
+				event.getData();
+
+			if (oldDoc.status === NewsStatus.PUBLISHED) {
+				const EventClass = EventsModule.getEvent(
+					`data.news.unpublished`
+				);
+				await EventsModule.publish(
+					new EventClass({ oldDoc }, oldDoc._id.toString()) // TODO maybe only pass modelId to unpublished event here, or when sending to clients
+				);
+			}
+		}
+	},
 	getData
 } as NewsSchemaOptions;

+ 9 - 0
backend/src/modules/DataModule/models/news/events/NewsPublishedEvent.ts

@@ -0,0 +1,9 @@
+import DataModuleEvent from "@/modules/DataModule/DataModuleEvent";
+
+export default abstract class NewsPublishedEvent extends DataModuleEvent {
+	protected static _modelName = "news";
+
+	protected static _name = "published";
+
+	protected static _hasPermission = true;
+}

+ 10 - 0
backend/src/modules/DataModule/models/news/events/NewsUnpublishedEvent.ts

@@ -0,0 +1,10 @@
+import DataModuleEvent from "@/modules/DataModule/DataModuleEvent";
+import isNewsPublished from "@/modules/DataModule/permissions/modelPermissions/isNewsPublished";
+
+export default abstract class NewsUnpublishedEvent extends DataModuleEvent {
+	protected static _modelName = "news";
+
+	protected static _name = "unpublished";
+
+	protected static _hasModelPermission = isNewsPublished;
+}