@dynamatix/cat-shared 0.0.91 → 0.0.92

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -135,7 +135,7 @@ function applyAuditMiddleware(schema, collectionName) {
135
135
  });
136
136
  }
137
137
  }
138
-
138
+ logs = logs.filter(log => log.newValue !== log.oldValue);
139
139
  if (logs.length) {
140
140
  await AuditLog.insertMany(logs);
141
141
  await updateContextAuditCount(contextId, logs.length);
@@ -208,7 +208,7 @@ function applyAuditMiddleware(schema, collectionName) {
208
208
  }
209
209
  }
210
210
  }
211
-
211
+ logs = logs.filter(log => log.newValue !== log.oldValue);
212
212
  if (logs.length) {
213
213
  await AuditLog.insertMany(logs);
214
214
  await updateContextAuditCount(contextId, logs.length);
@@ -220,9 +220,17 @@ function applyAuditMiddleware(schema, collectionName) {
220
220
 
221
221
  // Handle delete audits
222
222
  schema.pre(['findOneAndDelete', 'findByIdAndDelete', 'deleteOne', 'deleteMany'], async function (next) {
223
- // Store the query to fetch document before deletion
224
- this._deleteQuery = this.getQuery();
225
- next();
223
+ try {
224
+ // Fetch the document before deletion
225
+ const docToDelete = await this.model.findOne(this.getQuery()).lean();
226
+ if (docToDelete) {
227
+ // Store the document for use in post middleware
228
+ this._docToDelete = docToDelete;
229
+ }
230
+ next();
231
+ } catch (error) {
232
+ next(error);
233
+ }
226
234
  });
227
235
 
228
236
  schema.post(['findOneAndDelete', 'findByIdAndDelete', 'deleteOne', 'deleteMany'], async function (result) {
@@ -231,16 +239,12 @@ function applyAuditMiddleware(schema, collectionName) {
231
239
  const auditConfig = await AuditConfigModel.findOne({ collectionName });
232
240
  if (!auditConfig?.trackDeletion) return;
233
241
 
234
- // Fetch the document before it was deleted
235
- const deletedDoc = await this.model.findOne(this._deleteQuery).lean();
236
- if (!deletedDoc) return;
237
-
238
242
  const context = getContext();
239
243
  const userId = context?.userId || 'anonymous';
240
244
  const contextId = context?.contextId;
241
245
 
242
- const entityDescription = await resolveEntityDescription(deletedDoc, auditConfig);
243
- const deletionDescription = await resolveDeletionDescription(deletedDoc, auditConfig);
246
+ const entityDescription = await resolveEntityDescription(this._docToDelete, auditConfig);
247
+ const deletionDescription = await resolveDeletionDescription(this._docToDelete, auditConfig);
244
248
 
245
249
  const log = {
246
250
  name: deletionDescription || 'Entity Deletion',
@@ -254,7 +258,9 @@ function applyAuditMiddleware(schema, collectionName) {
254
258
  contextId: contextId || deletedDoc._id
255
259
  }
256
260
  };
257
-
261
+ if (log.newValue === log.oldValue) {
262
+ return;
263
+ }
258
264
  await AuditLog.create(log);
259
265
  await updateContextAuditCount(contextId, 1);
260
266
  if (onAuditLogCreated) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamatix/cat-shared",
3
- "version": "0.0.91",
3
+ "version": "0.0.92",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"