@dynamatix/cat-shared 0.0.90 → 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);
@@ -219,6 +219,20 @@ function applyAuditMiddleware(schema, collectionName) {
219
219
  });
220
220
 
221
221
  // Handle delete audits
222
+ schema.pre(['findOneAndDelete', 'findByIdAndDelete', 'deleteOne', 'deleteMany'], async function (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
+ }
234
+ });
235
+
222
236
  schema.post(['findOneAndDelete', 'findByIdAndDelete', 'deleteOne', 'deleteMany'], async function (result) {
223
237
  if (!result) return;
224
238
 
@@ -229,22 +243,24 @@ function applyAuditMiddleware(schema, collectionName) {
229
243
  const userId = context?.userId || 'anonymous';
230
244
  const contextId = context?.contextId;
231
245
 
232
- const entityDescription = await resolveEntityDescription(result, auditConfig);
233
- const deletionDescription = await resolveDeletionDescription(result, auditConfig);
246
+ const entityDescription = await resolveEntityDescription(this._docToDelete, auditConfig);
247
+ const deletionDescription = await resolveDeletionDescription(this._docToDelete, auditConfig);
234
248
 
235
249
  const log = {
236
250
  name: deletionDescription || 'Entity Deletion',
237
251
  entity: entityDescription,
238
- recordId: contextId || result._id,
252
+ recordId: contextId || deletedDoc._id,
239
253
  oldValue: '',
240
254
  newValue: 'Deleted',
241
255
  createdBy: userId,
242
256
  externalData: {
243
257
  description: entityDescription,
244
- contextId: contextId || result._id
258
+ contextId: contextId || deletedDoc._id
245
259
  }
246
260
  };
247
-
261
+ if (log.newValue === log.oldValue) {
262
+ return;
263
+ }
248
264
  await AuditLog.create(log);
249
265
  await updateContextAuditCount(contextId, 1);
250
266
  if (onAuditLogCreated) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamatix/cat-shared",
3
- "version": "0.0.90",
3
+ "version": "0.0.92",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"