Browse Source

refactor: Include oldDoc in updated events

Owen Diffey 1 year ago
parent
commit
89e9dd5166

+ 8 - 3
backend/src/modules/DataModule.ts

@@ -318,10 +318,15 @@ export default class DataModule extends BaseModule {
 		})
 			.filter(([, event]) => !!event)
 			.forEach(([action, event]) => {
-				patchEventEmitter.on(event, async ({ doc }) => {
+				patchEventEmitter.on(event, async ({ doc, oldDoc }) => {
+					const modelId = doc?._id ?? oldDoc?._id;
+
+					if (!modelId)
+						throw new Error(`Model Id not found for "${event}"`);
+
 					await this._jobQueue.runJob("events", "publish", {
-						channel: `model.${modelName}.${doc._id}.${action}`,
-						value: doc
+						channel: `model.${modelName}.${modelId}.${action}`,
+						value: { doc, oldDoc }
 					});
 				});
 			});

+ 1 - 1
backend/src/schemas/news.ts

@@ -108,7 +108,7 @@ export const schema = new Schema<NewsSchema, NewsModel, {}, NewsQueryHelpers>(
 				hasPermission: true
 			},
 			newest: {
-				async method() {
+				async method(payload?: { showToNewUsers: boolean }) {
 					return this.find().newest(payload?.showToNewUsers);
 				},
 				hasPermission: true

+ 0 - 4
backend/src/schemas/plugins/getData.ts

@@ -248,10 +248,6 @@ export default function getDataPlugin(schema: Schema) {
 			context: JobContext,
 			payload: Parameters<GetData["getData"]>[0]
 		) {
-			await context.assertPermission(
-				`data.${this.collection.collectionName}.getData`
-			);
-
 			return this.getData(payload);
 		},
 		...(schema.get("jobConfig") ?? {})